我希望我的 SL 能够准确地执行。就像 TP 的情况一样。 这里有一个例子:
不幸的是我只能管理两者之一。 如果我这样做...只有 SL 是正确的:
if strategy.position_size > 0
strategy.exit("SL", from_entry = "Long", stop = SL_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)
strategy.exit("TP1/SL", from_entry = "Long", limit = TP1_L, qty_percent = 80)//, alert_message = i_alert_txt_TP_long)
strategy.exit("TP2/SL", from_entry = "Long", limit = TP2_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_long)
我能做什么?
//@version=5
strategy('EMA Cross Strategy', overlay=true, calc_on_every_tick=true, currency=currency.USD)
emaFast = input.int(title='Fast EMA', defval=5, minval=1, maxval=9999)
emaSlow = input.int(title='Slow EMA', defval=10, minval=1, maxval=9999)
fastEMA = ta.ema(close, emaFast)
slowEMA = ta.ema(close, emaSlow)
plot(series=fastEMA, color=color.new(color.orange, 0), linewidth=2)
plot(series=slowEMA, color=color.new(color.blue, 0), linewidth=2)
bull = ta.crossover(fastEMA, slowEMA)
bear = ta.crossunder(fastEMA, slowEMA)
showBuySell = input(true, "BUY/SELL SIGNALS ON/OFF", group="BUY & SELL SIGNALS")
sensitivity = input.float(6.0, "SENSITIVITY (1-6)", 0.1, 6, group="BUY & SELL SIGNALS")
percentStop = input.float(0.25, "STOP LOSS %", 0, group="BUY & SELL SIGNALS")
percentTake = input.float(0.15, "TAKE PROFIT %", 0, group="BUY & SELL SIGNALS")
percentTake2 = input.float(0.35, "TAKE PROFIT2 %", 0, group="BUY & SELL SIGNALS")
entry_price = math.round_to_mintick(strategy.position_avg_price)
atrStopL = math.round_to_mintick(strategy.position_avg_price * (1 - percentStop/100))
atrTake1L = math.round_to_mintick(strategy.position_avg_price * (1 + percentTake/100))
atrTake2L = math.round_to_mintick(strategy.position_avg_price * (1 + percentTake2/100))
SL_L = ta.valuewhen(entry_price, atrStopL, 0)
TP1_L = ta.valuewhen(entry_price, atrTake1L, 0)
TP2_L = ta.valuewhen(entry_price, atrTake2L, 0)
// plot(SL_L, color = color.red)
// plot(TP1_L, color = color.green)
// plot(TP2_L, color = color.green)
atrStopS = math.round_to_mintick(strategy.position_avg_price * (1 + percentStop/100))
atrTake1S = math.round_to_mintick(strategy.position_avg_price * (1 - percentTake/100))
atrTake2S = math.round_to_mintick(strategy.position_avg_price * (1 - percentTake2/100))
SL_S = ta.valuewhen(entry_price, atrStopS, 0)
TP1_S = ta.valuewhen(entry_price, atrTake1S, 0)
TP2_S = ta.valuewhen(entry_price, atrTake2S, 0)
plot(SL_S, color = color.red)
plot(TP1_S, color = color.green)
plot(TP2_S, color = color.green)
// Entries and Exits with TP/SL
if bull and strategy.position_size == 0
strategy.entry("Long" , strategy.long)// , alert_message = i_alert_txt_entry_long)
// if bear and strategy.position_size > 0
// strategy.close("Long")// , alert_message = i_alert_txt_exit_short, comment = "Close Short")
// strategy.exit("TP1/SL", from_entry = "Long", limit = TP1_L, stop = SL_L, qty_percent = 80)//, alert_message = i_alert_txt_TP_long)
// strategy.exit("TP2/SL", from_entry = "Long", limit = TP2_L, stop = SL_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_long)
if strategy.position_size > 0
strategy.exit("TP1/SL", from_entry = "Long", limit = TP1_L, qty_percent = 80)//, alert_message = i_alert_txt_TP_long)
strategy.exit("TP2/SL", from_entry = "Long", limit = TP2_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_long)
strategy.exit("SL", from_entry = "Long", stop = SL_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)
if bear and strategy.position_size == 0
strategy.entry("Short" , strategy.short)//, alert_message = i_alert_txt_entry_short)
// if bull and strategy.position_size < 0
// strategy.close("Short")// , alert_message = i_alert_txt_exit_long , comment = "Close Long")
if strategy.position_size < 0
strategy.exit("TP1/SL", from_entry = "Short", limit = TP1_S, qty_percent = 80)//, alert_message = i_alert_txt_TP_short)
strategy.exit("TP2/SL", from_entry = "Short", limit = TP2_S)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)
strategy.exit("SL", from_entry = "Short", stop = SL_S)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)
您应该在每个
stop
调用中包含 strategy.exit()
参数。
// Entries and Exits with TP/SL
if bull and strategy.position_size == 0
strategy.entry("Long" , strategy.long)// , alert_message = i_alert_txt_entry_long)
// if bear and strategy.position_size > 0
// strategy.close("Long")// , alert_message = i_alert_txt_exit_short, comment = "Close Short")
// strategy.exit("TP1/SL", from_entry = "Long", limit = TP1_L, stop = SL_L, qty_percent = 80)//, alert_message = i_alert_txt_TP_long)
// strategy.exit("TP2/SL", from_entry = "Long", limit = TP2_L, stop = SL_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_long)
if strategy.position_size > 0
strategy.exit("TP1/SL", from_entry = "Long", limit = TP1_L, stop = SL_L, qty_percent = 80)//, alert_message = i_alert_txt_TP_long)
strategy.exit("TP2/SL", from_entry = "Long", limit = TP2_L, stop = SL_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_long)
strategy.exit("SL", from_entry = "Long", stop = SL_L)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)
if bear and strategy.position_size == 0
strategy.entry("Short" , strategy.short)//, alert_message = i_alert_txt_entry_short)
// if bull and strategy.position_size < 0
// strategy.close("Short")// , alert_message = i_alert_txt_exit_long , comment = "Close Long")
if strategy.position_size < 0
strategy.exit("TP1/SL", from_entry = "Short", limit = TP1_S, stop = SL_S, qty_percent = 80)//, alert_message = i_alert_txt_TP_short)
strategy.exit("TP2/SL", from_entry = "Short", limit = TP2_S, stop = SL_S)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)
strategy.exit("SL", from_entry = "Short", stop = SL_S)//, qty_percent = 20)//, alert_message = i_alert_txt_TP_short)