这种策略本身可以很好地工作,但是当我添加止损逻辑时却不能。 多头头寸立即被退出(止损),但我的短裤完美地达到了止损。但是,空头并不总是应有的位置。我不确定自己在做什么错。我认为有一些未取消的订单或未正确触发的订单。这是蜡烛内的止损。
//@version=4
strategy("My TEMA Strategy+SL,TP", overlay=true, pyramiding=0)
//TEMA
xPrice = ohlc4
Length = input(14, minval=1)
xEMA1 = ema(xPrice, Length)
xEMA2 = ema(xEMA1, Length)
xEMA3 = ema(xEMA2, Length)
Length2=input(28, minval=1)
xxEMA1 = ema(xPrice, Length2)
xxEMA2 = ema(xxEMA1, Length2)
xxEMA3 = ema(xxEMA2, Length2)
FastMA = 3 * xEMA1 - 3 * xEMA2 + xEMA3
SlowMA = 3 * xxEMA1 - 3 * xxEMA2 + xxEMA3
longcond = crossover (FastMA,SlowMA)
shortcond = crossunder (FastMA,SlowMA)
plot(FastMA, title="TEMA1",color=color.green,linewidth=2)
plot(SlowMA, title="TEMA2",color=color.red,linewidth=2)
if (longcond)
strategy.entry("Long", strategy.long)
if (shortcond)
strategy.entry("Short", strategy.short)
//Long Short itra candle stop loss. Shorts working Longs not working
intra_stop_level = input(1, title="Intra Candle Stop %")
intra_stop_long = strategy.position_avg_price*(1-intra_stop_level/100)
intra_stop_short = strategy.position_avg_price*(1+intra_stop_level/100)
strategy.exit("Stop LossL","Long",stop=intra_stop_long,limit=1)
strategy.exit("Stop LossS","Short",stop=intra_stop_short,limit=1)
答案 0 :(得分:0)
使用刻度数退出头寸
//@version=4
strategy("My Strategy", overlay=true)
TICKS_TO_EXIT = 10
MAX_LOSS_IN_TICKS = 15
strategy.entry("Id", strategy.long, when=bar_index % 2 == 1)
strategy.exit("exitId", "Id", profit=TICKS_TO_EXIT, loss=MAX_LOSS_IN_TICKS)
从入场价中获利退出
//@version=4
strategy("My Strategy", overlay=true)
long_condition = bar_index % 5 == 4
entryPrice = 0.0
entryPrice := entryPrice[1]
if long_condition and na(strategy.position_avg_price)
strategy.entry("longId", strategy.long)
entryPrice := close
limitPrice = entryPrice * 1.05
strategy.exit("exitId", "longId", limit=limitPrice)