即使到达TP/SL
我不明白为什么当我设置 sl/tp 时, pine脚本正在退出我在下一个bar上的位置。
这是我简单的示例代码:
if entryConditionsAreMet
strategy.entry("short", strategy.short)
// let's assume 5% TP and 2% SL
takeProfitShort = strategy.position_avg_price * (1 - (5 / 100))
stopLossShort = strategy.position_avg_price * (1 + (2 / 100))
strategy.exit("exitShort", "short", limit=takeProfitShort, stop=stopLossShort)
它可以正确打开并关闭位置,但是TP/SL 总是出现在下一个bar 上。
查看下面了解的图片(绿线是TP,橙色是SL):
如您所见,该位置不是在达到TP时关闭的位置,而是在下一个开放栏上关闭。
为什么会发生这种情况?如何避免它?
I cannot understand why Pine Script is exiting my position on next bar when I set SL/TP.
This is my simple example code:
if entryConditionsAreMet
strategy.entry("short", strategy.short)
// let's assume 5% TP and 2% SL
takeProfitShort = strategy.position_avg_price * (1 - (5 / 100))
stopLossShort = strategy.position_avg_price * (1 + (2 / 100))
strategy.exit("exitShort", "short", limit=takeProfitShort, stop=stopLossShort)
It opens and closes positions correctly, however the TP/SL always occur on the next bar.
Look at the picture below to understand (the green line is the TP and the orange one is the SL):
I'd expect the position closes on the same bar, not in the next one!
As you can see, the position closes not when the TP is reached but on the next opening bar.
Why does this happen? How to avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下操作:
Try this:
这是当前在Pinescript中不可能的(据我所知
) strategy.close_all() 允许在同一栏上关闭条目,通过将
立即设置为true来打开它们。
这两个
strategy.exit()
和stragity.entry()
将忽略限制(获利),并在入口栏上停止(Stoploss),并且将在以下条的开放式。This is not currently possible in pinescript (as far as I am aware) but you can trigger an exit on the close of the entry bar:
Using
strategy.close_all()
allows for closing entries on the same bar they are opened by setting theimmediately
parameter to true.Both
strategy.exit()
andstrategy.entry()
will ignore the limit (take profit) and stop (stoploss) on the entry bar, and will be active criteria on the open of the following bar.