问题描述
我在下面创建了策略。但是当我查看 Strategy Tester 的交易列表时,我发现它们并没有在我的 limit=TP_long 或 stop=SL_long 处退出。 Here is one of them 它以 108.726 而不是 TP 109.067 或 SL 108.355 退出。任何帮助都可能有用。谢谢
嗨,我在下面创建了策略。但是当我查看 Strategy Tester 的交易列表时,我发现它们并没有在我的 limit=TP_long 或 stop=SL_long 处退出。 Here is one of them 它以 108.726 而不是 TP 109.067 或 SL 108.355 退出。任何帮助都可能有用。谢谢
//@version=4
strategy(title="EMA3489 + Doji + RSI Strategy",shorttitle="EMA3489 + Doji + RSI Strategy",overlay=true)
//strategy start date
startDate = input(title="Start Date",type=input.integer,defval=1,minval=1,maxval=31)
startMonth = input(title="Start Month",maxval=12)
startYear = input(title="Start Year",defval=2020,minval=2018,maxval=2100)
endDate = input(title="End Date",maxval=31)
endMonth = input(title="End Month",defval=12,minval=12,maxval=12)
endYear = input(title="End Year",defval=2021,minval=2020,maxval=2100)
// Look if the close time of the current bar
// falls inside the date range
inDaterange = (time >= timestamp(syminfo.timezone,startYear,startMonth,startDate,0)) and (time < timestamp(syminfo.timezone,endYear,endMonth,endDate,0))
//Moving Averages
maFastSource = input(defval = close,title = "Fast EMA Source")
maFastLength = input(defval = 34,title = "Fast EMA Period",minval = 1)
// long ma
maSlowSource = input(defval = close,title = "Slow EMA Source")
maSlowLength = input(defval = 89,title = "Slow EMA Period",minval = 1)
maFast = ema(maFastSource,maFastLength)
maSlow = ema(maSlowSource,maSlowLength)
//rsi
rsiSource = input(title="RSI Source",type=input.source,defval=close)
rsiLength = input(title="RSI Length",defval=14)
rsiValue = round(rsi(rsiSource,rsiLength),2)
rsiOverbought = input(title="RSI Overbought Level",defval=70)
rsiOversold = input(title="RSI Oversold Level",defval=30)
// On the last price bar,make a new trend line
HOO=input(defval=0.12,type=input.float,title="(H-L)/O")
p1v=input(defval=3.5,title="(H-L)/(C-O)")
p3v=input(defval=3.5,title="(H-L)/(H-O) or (H-L)/(O-L)")
ema1=input(defval=0.1,title="C - FastemA")
ema2=input(defval=0.1,title="C - SlowEMA")
p2=(high-low)/open>HOO/100
p1_buy=abs((high-low)/(close-open))>p1v
p1_sell=abs((high-low)/(close-open))>p1v
p3_buy=(high-low)/(high-open)>p3v
p3_sell=(high-low)/(open-low)>p3v
emafar_buy = (maFast-close)/open>ema1/100 or (maSlow-close)/open>ema2/100
emafar_sell = (close-maFast)/open>ema1/100 or (close-maSlow)/open>ema2/100
rsiob=rsiValue>rsiOverbought
rsios=rsiValue<rsiOversold
//Conditions
openlong = p1_buy and p3_buy and p2 and emafar_buy and rsios
openshort= p1_sell and p3_sell and p2 and emafar_sell and rsiob
//follow EMA
SL_long= abs(low)
TP_long= close+2*abs(close-low)
SL_short= abs(high)
TP_short= close-2*abs(high-close)
//Entry and Exit
if (inDaterange)
strategy.entry("Long",strategy.long,when = openlong)
strategy.exit("Long",limit=TP_long,stop=SL_long,comment="SLTP")
strategy.entry("Short",strategy.short,when = openshort)
strategy.exit("Short",limit=TP_short,stop=SL_short,comment="SLTP")
if (not inDaterange)
strategy.close_all()
//plot
plot(maFast,title="maFast",color=color.yellow,linewidth=2)
plot(maSlow,title="maSlow",color=color.green,linewidth=3)
if (openlong)
label.new(x=bar_index,y=close-150*syminfo.mintick,textcolor=color.white,style=label.style_label_up,text="BUY: " + tostring(close) + "\nRSI: "+tostring(rsiValue) + "\nTP: " +tostring(TP_long)+" - SL: "+tostring(SL_long))
if (openshort)
label.new(x=bar_index,y=close+150*syminfo.mintick,color=color.red,text="SELL: " + tostring(close) + "\nRSI: "+tostring(rsiValue) + "\nTP: " +tostring(TP_short)+" - SL: "+tostring(SL_short))
strategy.entry("Long",when = openlong)
cl=valuewhen(openlong,close,0)
ll=valuewhen(openlong,low,0)
strategy.exit("long exit",from_entry="Long",profit=2*abs(cl-ll)/syminfo.mintick,loss=abs(cl-ll)/syminfo.mintick,comment="SLTP LONG")
strategy.entry("Short",when = openshort)
cl2=valuewhen(openshort,0)
hl=valuewhen(openshort,high,0)
strategy.exit("short exit",from_entry="Short",profit=2*abs(cl2-hl)/syminfo.mintick,loss=abs(cl2-hl)/syminfo.mintick,comment="SLTP SHORT")
解决方法
如果每个 strategy.exit
必须只关闭它自己的 strategy.entry
,试试这个:
...
//Entry and Exit
if (inDateRange)
if openlong
strategy.entry("Long",strategy.long)
strategy.exit("long exit",from_entry="Long",limit=TP_long,stop=SL_long,comment="SLTP")
if openshort
strategy.entry("Short",strategy.short)
strategy.exit("short exit",from_entry="Short",limit=TP_short,stop=SL_short,comment="SLTP")
if (not inDateRange)
strategy.close_all()
...