如何在PINE策略脚本中每个挥杆低/高下的每个秋千下的动态停止损失订单编码?
我正在尝试通过停止损失订单对策略进行编程,这些策略会自动放置在每个挥杆低或高的下方,因为我相信具有百分比或tick的静态停止损失订单更有可能在市场上失败。
在网上搜索很长时间后,我找不到任何与动态止损一起使用的脚本,但是大多数脚本都可以使用固定的停止损失订单。然后,我遇到了
ta.lowest(低,14) /最高(高,14)函数< / p>
,并认为它可以用来识别一定范围内最低蜡烛的值,然后可以用作添加的初始点一个百分比,瞧,这将是基于摇摆低/高的动态停止损失。现在问题是它没有像我想象的那样奏效,我不知道为什么,这就是为什么我要发布这个问题并希望有人可以指出我的错误。
在这里证明它不起作用: 在达到最低蜡烛的价格(-0.1%)之后,应停止交易最后5支蜡烛
这是脚本不起作用的部分:
// *************Entry orders*************
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
//*************Stop Loss Orders*************
long_pos = strategy.position_size > 0
short_pos = strategy.position_size < 0
sl_perc = input.float(0.1, "Stop Loss Percentage under swing low/high")
sl_long = ta.lowest(low, 5) - ta.lowest(low, 14)/100*sl_perc
sl_short = ta.highest(high,5) + ta.lowest(low, 14)/100*sl_perc
strategy.exit("LongExit", "SL Long", stop = sl_long )
strategy.exit("ShortExit", "SL Short",stop = sl_short )
感谢您的建议和帮助!
I am trying to program a strategy with stop loss orders that would automatically be placed under each swing low or high, because I believe that static stop loss orders with percentages or ticks are more likely to fail in the market.
After searching online for a long time I could not find any script that worked with dynamic stop loss, but most of them rather worked with fixed stop loss orders. Then, I came across the
ta.lowest(low,14) / ta.highest(high,14) function
and thought that it could be used to identify the value of the lowest candle in a certain range which could then be used as the initial point on which to add a percentage, and voila, that would be a dynamic stop loss based on swing low/high. Now the problem is it did not work out as I imagined and I have no clue why, which is why I am posting this question and hope that someone can point out my mistake.
Here the proof that it is not working:
The trade should be stopped out after reaching the price of the lowest candle (-0.1%) within the range of the last 5 candles
This is the part of the script that does not work:
// *************Entry orders*************
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
//*************Stop Loss Orders*************
long_pos = strategy.position_size > 0
short_pos = strategy.position_size < 0
sl_perc = input.float(0.1, "Stop Loss Percentage under swing low/high")
sl_long = ta.lowest(low, 5) - ta.lowest(low, 14)/100*sl_perc
sl_short = ta.highest(high,5) + ta.lowest(low, 14)/100*sl_perc
strategy.exit("LongExit", "SL Long", stop = sl_long )
strategy.exit("ShortExit", "SL Short",stop = sl_short )
Thanks for any suggestions and help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我自己找到了答案:
我选择了出口的错误ID。真的很愚蠢的错误:(
(希望这对任何想尝试同样的人都有用)
I think I found the answer myself:
I chose the wrong ID for the Exits. Really stupid mistake :(
(Hope that can be useful for anyone looking to try to do the same)