输入后如何将值存储在Pinescript上?
我将利润值1:1设置为风险。 它基于ATR。价格一旦上方/低于ATR,它就会给我入口点,之后,我计算了入口价格和ATR之间的距离。因此,这个距离将与我的TP一样。但是发生的是,该机器人不断从最后一个栏中获得ATR级别,而不是从入口栏中获得。
这是代码的示例和交易的屏幕截图:
//@version=4
buy = crossover(close,atr)
sell = crossunder(close,atr)
strategy.entry("long", true, when = buy)
strategy.entry("short", false, when = sell)
//calculate difference between entry and atr
differenceLong = strategy.position_avg_price - atr
differenceShort = atr - strategy.position_avg_price
//Calculate the TP price
longExitPrice = strategy.position_avg_price + differenceLong
shortExitPrice = strategy.position_avg_price - differenceShort
if (strategy.position_size > 0)
strategy.exit(id="TP", limit = longExitPrice)
if (strategy.position_size < 0)
strategy.exit(id="TP", limit = shortExitPrice)
I'm setting a take profit value 1:1 to the risk.
It's based on ATR. Once the price closes above/below atr it gives me the entry point and after that, I calculate the distance between the entry price and the ATR. So this distance will be as same as my TP. But what happens is that the bot keeps getting the ATR level from the last bar, and not from the entry bar.
Here's the sample of the code and a screenshot of a trade:
//@version=4
buy = crossover(close,atr)
sell = crossunder(close,atr)
strategy.entry("long", true, when = buy)
strategy.entry("short", false, when = sell)
//calculate difference between entry and atr
differenceLong = strategy.position_avg_price - atr
differenceShort = atr - strategy.position_avg_price
//Calculate the TP price
longExitPrice = strategy.position_avg_price + differenceLong
shortExitPrice = strategy.position_avg_price - differenceShort
if (strategy.position_size > 0)
strategy.exit(id="TP", limit = longExitPrice)
if (strategy.position_size < 0)
strategy.exit(id="TP", limit = shortExitPrice)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用内置变量
stragity.position_size
弄清楚是否输入新位置。然后使用var
在条目时存储ATR值。通过使用VAR,您可以确保除非您具体这样做,否则不会更新其值。在您的退出计算中使用
ATR_AT_ENTRY
。注意:如果您使用的是
process_orders_on_close = true
,则需要获得以前的atr值,例如:atr_at_entry:= is_new_entry? ATR [1]:ATR_AT_ENTRY
Use the built-in variable
strategy.position_size
to figure out if you enter a new position. Then use avar
to store the ATR value at the time of the entry. By using a var, you are making sure that its value will not be updated unless you specifically do so.Use
atr_at_entry
in your exit calculations.Note: If you are using
process_orders_on_close=true
, you need to get the previous atr value like so:atr_at_entry := is_new_entry ? atr[1] : atr_at_entry