0

我正在尝试同时使用特定价格(或首选百分比损失率)的止损单和追踪止损单,但是如果我使用两种订单类型运行代码,则只有其中一种(取决于我使用的代码有时我只得到止损单的结果,或者有时我只得到追踪止损的结果)给出了正确的结果。

我找到了一个关于同时运行代码的解决方案,但是这次止损订单等待蜡烛关闭(如果我选择蜡烛代码的低值不会给出任何关于止损的结果),所以它可能会造成比我指定了。

我用于止损和追踪止损的当前代码如下。

stop_loss_ratio = input.float(title = "Stop loss Ratio", minval = 0.0, step = 0.1, defval = 1) * 0.01
strategy.entry('Long1', strategy.long, when=bull1 and (dayofmonth > 7), alert_message='Open Long1 (Monthly) Position')
entry_price_long = strategy.opentrades > 0 ? (strategy.opentrades.entry_price(strategy.opentrades - 1)) : na
stop_loss_price_long = entry_price_long * (1 - stop_loss_ratio)
entry_price_short = strategy.opentrades < 0 ? (strategy.opentrades.entry_price(strategy.opentrades - 1)) : na
stop_loss_price_short = entry_price_short * (1 + stop_loss_ratio)
strategy.close('Long1', when= close < stop_loss_price_long, comment = 'Stop Loss Long1')
trailStop = input.float(title='Trailing Stop (%)', minval=0.0, step=0.1, defval=1.4) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - trailStop)
    math.max(stopValue, longStopPrice[1])
else
    0
shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + trailStop)
    math.min(stopValue, shortStopPrice[1])
else
    99999
if strategy.position_size > 0
    strategy.exit(id='Trailing Stop', stop=longStopPrice, alert_message='Close by Trailing Stop (Weekly) Position')
4

0 回答 0