为单次入场设置多个止损离场订单

Place multiple stop exit orders for single entry

在 tradingview 的一个策略中,我输入了一个入场点并设置了追踪止损的条件。同时我想要一个固定价格的止损订单,但是当我下两个strategy.exit()命令时,实际只使用了一个,因为都是"stop"类型。但是可以通过调用 strategy.exit() 并给它相同的 id 来用另一个替换退出命令。所以我的想法是在适合我的时候用另一个出口替换另一个出口,这样同时只有一个出口订单处于活动状态。

假设我有一个策略,条件是在一定价格下入场,例如conditionEnterenterPrice,入场时我也知道止损stopLossPrice,所以我可以设置:

if conditionEnter
    strategy.entry(id="longEnter", long=true, qty=1, limit=enterPrice
strategy.exit(id="longExit", from_entry="longEnter", qty_percent=100, stop=stopLossPrice)

现在,如果满足conditionTrailingStopLoss,我相应地替换它:

if conditionTrailingStopLoss
    strategy.exit(id="longExit", from_entry="longEnter", qty=strategy.position_size * 0.2, trail_price=priceToTriggerTrailingStopLoss, trail_offset=100)

其中 priceToTriggerTrailingStopLoss 等于或低于收盘价,以便立即设置追踪止损并追踪价格 100 个跳动点。注意,即使执行,也只有当前仓位的五分之一退出。

正如预期的那样,追踪止损退出订单的放置删除了原始止损退出订单 - 即使使用不同的 ID 也是如此。我想等到追踪止损已执行(已执行),然后再次下达止损退出订单。但是如何确定执行时间呢?

我尝试了一些东西

if strategy.position_size < strategy.position_size[1]
    strategy.exit(id="longEnter", long=true, qty=1, limit=enterPrice
strategy.exit(id="longExit", from_entry="longEnter", qty_percent=100, stop=stopLossPrice)

看到已经执行了一些退出,但是我从来没有看到两个命令都执行过。更改命令的顺序确实会影响忽略两者中的哪一个。如果在上面的代码中我将 id 更改为完全不同的东西,那么三个退出订单中的 none 将被执行!

有人可以帮忙吗?我的意思是想要有一个固定止损和一个追踪止损并不是很奇特的愿望,它必须以某种方式起作用,对吗?

编辑: 如果我在图表中向后滚动,我确实有时会看到执行了一个,有时会看到另一个退出订单,但对于同一个入场订单仍然不会同时执行。

我找到了解决办法,也可以用strategy.order()命令设置止损单,像这样:

if conditionEnter
    strategy.entry(id="longEnter", long=true, qty=1, limit=enterPrice
    strategy.order(id="stopLossLong", long=strategy.short,qty=strategy.position_size, limit=syminfo.mintick, stop=stopLossPrice)