繁体   English   中英

做空的追踪止损和获利

[英]Trailing Stop Loss and Take Profit for Shorting

我有一个追踪止损和止盈,在做多 position 时有效,但是我不确定当策略做空时如何调整它。 到目前为止,这是我的代码:

bullish = tk_cross_bull and cs_cross_bull and price_above_kumo and ta.crossover(lower, close)
bearish = tk_cross_bear and cs_cross_bear and price_below_kumo and ta.crossover(close,lower)

// Configure trail stop level with input options
longTrailPerc = input.float(title='Trail Long Loss (%)', minval=0.0, step=0.1, defval=5) * 0.01
shortTrailPerc = input.float(title='Trail Short Loss (%)', minval=0.0, step=0.1, defval=5) * 0.01

// Determine trail stop loss prices
longStopPrice = 0.0
shortStopPrice = 0.0

longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - longTrailPerc)
    math.max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + shortTrailPerc)
    math.min(stopValue, shortStopPrice[1])
else
    999999

strategy.entry('Short', strategy.short, when=bearish and short_entry and timePeriod)
//strategy.exit('Exit', stop = longStopPrice, limit = shortStopPrice)
//strategy.close('Short', when=bullish and not long_entry)
if (bullish and timePeriod)
    strategy.exit(id='Exit', limit = shortStopPrice)

您已经计算出您的空头止损价格,这似乎是正确的。 然后在你的strategy.exit()调用中使用这个值。

strategy.entry('Short', strategy.short, when=bearish and short_entry and timePeriod)

if (strategy.position_size < 0)
    strategy.exit("Short Exit", "Short", stop=shortStopPrice

暂无
暂无

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

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