首先,对不起我的英语。我不是母语人士。
我发送了用松树脚本编写的策略框架。例如,您在策略中看到的;当长信号进入时,L1和L2位置打开。
我想更改strategy.exit部分。
当L1达到long_tp(目标点)时,L2的止损应该进入入场价(收支平衡),但是脚本中的止损就像尾随的止损会逐步变为价格和ATR。
ATR每步变化,因此我的初始止损水平(entryprice-1.5xATR)始终在变化。我不想改变。它应该停留在初始水平(止损=初始价格-1.5倍初始ATR)。
总之:当我获得多头头寸L1和L2时,止损水平应该为(entryprice-1.5xinitialATR),并且如果L1达到止盈水平,则L2的止损会变为进入价格,而进入价格应该保持不变使用ATR。
//@version=3
strategy(title="MA Crossover", overlay = true, pyramiding=0, initial_capital=10000, currency=currency.USD, calc_on_order_fills=1,default_qty_type=strategy.fixed, default_qty_value=10000)
price = close
fastlength = input(5,"fast length", minval=1, maxval=300)
slowlength = input(13,"slow length", minval=1, maxval=300)
sl_coefficent = input(1.5, "SL")
tp_coefficient = input(1, "TP")
///ATR alculation
atrlength = input(title="ATR Length", defval=14, minval=1)
atrsmoothing = input(title="ATR Smoothing", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, atrlength) =>
if atrsmoothing == "RMA"
rma(source, atrlength)
else
if atrsmoothing == "SMA"
sma(source, atrlength)
else
if atrsmoothing == "EMA"
ema(source, atrlength)
else
wma(source, atrlength)
atr = ma_function(tr(true), atrlength)
//Moving Averagers
fastMA = sma(close,fastlength)
slowMA = sma(close, slowlength)
plot(fastMA, title = "fast", color = blue, linewidth=2, transp=0)
plot(slowMA, title = "slow", color = red, linewidth=2, transp=0)
//Signals
short_signal = slowMA > fastMA and price < slowMA
long_signal = slowMA < fastMA and price > fastMA
//Entry and Exit Conditions
enterLong = (long_signal)
enterShort = (short_signal)
exitLong = (short_signal)
exitShort = (long_signal)
//STRATEGY
if (year>2018)
//Long entries with standard 1.5 ATR for SL, 1 ATR for TP
long_sl = price - atr * sl_coefficent
long_tp = price + atr * tp_coefficient
strategy.entry("L1", strategy.long, when = enterLong)
strategy.exit("L1 Limit Exit", "L1", stop = long_sl, limit = long_tp)
strategy.close("L1", when = exitLong)
//Long entries with no TP
strategy.entry("L2", strategy.long, when = enterLong)
strategy.exit("L2 Limit Exit", "L2", stop = long_sl)
strategy.close("L2", when = exitLong)
//Short entries with standard 1.5 ATR for SL, 1 ATR for TP
short_sl = price + atr * sl_coefficent
short_tp = price - atr * tp_coefficient
strategy.entry("S1", strategy.short, when = enterShort)
strategy.exit("S1 Limit Exit", "S1", stop = short_sl, limit = short_tp)
strategy.close("S1", when = exitShort)
//Short entries with no TP
strategy.entry("S2", strategy.short, when = enterShort)
strategy.exit("S2 Limit Exit", "S2", stop = short_sl)
strategy.close("S2", when = exitShort)
答案 0 :(得分:1)
根据您的评论价值,当您进入交易时什么时候将有助于保持ATR的价值:
ATR_Long := (strategy.position_size == 0 and strategy.long == 1 and strategy.position_entry_name == "Long" )? na : valuewhen(GoLong, one_atr, 0)
ATR_Short := (strategy.position_size == 0 and strategy.short == 1 and strategy.position_entry_name == "Short")? na : valuewhen(GoShort, one_atr, 0)
答案 1 :(得分:0)
如果这正是您要寻找的,我不确定我是否对您了解:
//Long entries with standard 1.5 ATR for SL, 1 ATR for TP
long_sl = strategy.position_avg_price - atr * sl_coefficent
long_tp = strategy.position_avg_price + atr * tp_coefficient