繁体   English   中英

努力使用 strategy.exit 添加止损,它不断添加太多订单

[英]Struggling to add a stop loss using strategy.exit, it keeps adding too many orders

我尝试添加止损,但无论我尝试什么,它要么不添加止损,要么添加超过 3000 个订单并且无法正常工作。 这是我到目前为止的代码(所有策略位),任何帮助或建议将不胜感激。

var int long_active = 0
var int short_active = 0

if(hour == 23)
    short_active := 0
    long_active := 0
    strategy.cancel_all()
    strategy.close_all()

if (current > prev and 2<hour and hour<23)
    short_active := 0
    strategy.cancel("short_exit")
    strategy.close("short")
    strategy.entry("long", strategy.long, comment="long")
    strategy.exit("long_exit", "long",stop = stop_loss)

if (current < prev and 2<hour and hour<23)
    long_active := 0
    strategy.cancel("long_exit")
    strategy.close("long")
    strategy.entry("short", strategy.short, comment="short")
    strategy.exit("short_exit", "short", stop = stop_loss)

您应该将您的strategy.exit()调用放在全局 scope 中。 实际上,只有当您的 if 条件为true时才会调用它们。

您可以添加方向检查以防止不必要的呼叫。

if (current > prev and 2<hour and hour<23)
    short_active := 0
    strategy.cancel("short_exit")
    strategy.close("short")
    strategy.entry("long", strategy.long, comment="long")

if (current < prev and 2<hour and hour<23)
    long_active := 0
    strategy.cancel("long_exit")
    strategy.close("long")
    strategy.entry("short", strategy.short, comment="short")

if (strategy.position_size > 0) // Long
    strategy.exit("long_exit", "long",stop = stop_loss)

if (strategy.position_size < 0) // Short
    strategy.exit("short_exit", "short", stop = stop_loss)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM