Pine script Tradingview脚本策略。关闭()不起作用

Pine script Tradingview脚本策略。关闭()不起作用,pine-script,Pine Script,Tradingview脚本策略。关闭()不起作用 策略是: 长期条件:收盘价必须高于超趋势(100,3),随机(%d)必须交叉超卖 退出条件:RSI高于70时 短期条件: 收盘价必须低于超趋势(100,3),随机(%d)必须超过超买 退出条件:RSI低于30时 代码: strategy("Momentum Stochastic+Supertrend", overlay=true, default_qty_value=1) // stochastic length = in

Tradingview脚本策略。关闭()不起作用

策略是:

长期条件:收盘价必须高于超趋势(100,3),随机(%d)必须交叉超卖 退出条件:RSI高于70时

短期条件: 收盘价必须低于超趋势(100,3),随机(%d)必须超过超买 退出条件:RSI低于30时

代码:


strategy("Momentum Stochastic+Supertrend", overlay=true, default_qty_value=1)

// stochastic
length = input(14, minval=1)
OverBought = input(80)
OverSold = input(20)
smoothK = 3
smoothD = 3
k = sma(stoch(close, high, low, length), smoothK)
d = sma(k, smoothD)
co = crossover(d, OverSold)
cu = crossunder(d,OverBought)

///////////// RSI
RSIlength = input(14,title="RSI Period Length") 
RSIoverSold = 70
RSIoverBought = 30
price = close
vrsi = rsi(price, RSIlength)
RSICO = crossover(vrsi, RSIoverSold)
RSICU = crossunder(vrsi, RSIoverBought)

res = input(title="Main SuperTrend Time Frame", type=resolution, defval="3")
Factor=input(3, minval=1,maxval = 100)
Pd=input(100, minval=1,maxval = 100)

tp = input(5000,title="Take Profit")
sl = input(3500,title="Stop Loss")


//Up=hl2-(Factor*atr(Pd))
//Dn=hl2+(Factor*atr(Pd))
MUp=security(tickerid,res,hl2-(Factor*atr(Pd)))
MDn=security(tickerid,res,hl2+(Factor*atr(Pd)))

Mclose=security(tickerid,res,close)

//TrendUp=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
//TrendDown=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn

MTrendUp=Mclose[1]>MTrendUp[1]? max(MUp,MTrendUp[1]) : MUp
MTrendDown=Mclose[1]<MTrendDown[1]? min(MDn,MTrendDown[1]) : MDn

//Trend = close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
//Tsl = Trend==1? TrendUp: TrendDown

MTrend = Mclose > MTrendDown[1] ? 1: Mclose< MTrendUp[1]? -1: nz(MTrend[1],1)
MTsl = MTrend==1? MTrendUp: MTrendDown

//linecolor = Trend == 1 ? green : red
//plot(Tsl, color = linecolor , style = line , linewidth = 2,title = "SuperTrend")

Mlinecolor = MTrend == 1 ? green : red
plot(MTsl, color = Mlinecolor , style = line , linewidth = 2,title = "Main SuperTrend")

//plotshape(cross(close,Tsl) and close>Tsl , "Up Arrow", shape.triangleup,location.belowbar,green,0,0)
//plotshape(cross(Tsl,close) and close<Tsl , "Down Arrow", shape.triangledown , location.abovebar, red,0,0)

//up = Trend == 1 and Trend[1] == -1 and MTrend == 1 
//down = Trend == -1 and Trend[1] == 1 and MTrend == -1 
//plotarrow(up ? Trend : na, title="Up Entry Arrow", colorup=lime, maxheight=60, minheight=50, transp=0)
//plotarrow(down ? Trend : na, title="Down Entry Arrow", colordown=red, maxheight=60, minheight=50, transp=0)

golong = MTrend == 1 and crossover(d, OverSold)
//longexit = MTrend == 1 and crossunder(d,OverBought)
longX = crossunder(vrsi, RSIoverBought)

goshort = MTrend == -1 and crossunder(d,OverBought)
//shortexit = MTrend == -1 and crossover(d, OverSold)
shortX = crossover(vrsi, RSIoverSold)

strategy.entry("Buy", strategy.long,when=golong)
//strategy.exit("Close Buy","Buy",profit=tp,loss=sl)
strategy.close("Exit", when=longX)  

strategy.entry("Sell", strategy.short,when=goshort)
//strategy.exit("Close Sell","Sell",profit=tp,loss=sl)
strategy.close("Exit", when=shortX)  


策略(“动量随机+超趋势”,叠加=真,默认数量值=1)
//随机的
长度=输入(14,最小值=1)
超买=投入(80)
超卖=输入(20)
平滑k=3
smoothD=3
k=sma(stoch(闭合、高、低、长度)、smoothK)
d=sma(k,平滑d)
co=交叉(d,超卖)
cu=交叉低于(d,超买)
/////////////RSI
RSI长度=输入(14,title=“RSI期间长度”)
RSI超卖=70
超买=30
价格=收盘价
vrsi=rsi(价格,rsi长度)
RSICO=交叉(vrsi,RSI超卖)
RSICU=交叉低于(vrsi,RSICU超买)
res=输入(title=“主超趋势时间范围”,type=分辨率,deffal=“3”)
系数=输入(3,最小值=1,最大值=100)
Pd=输入(100,最小值=1,最大值=100)
tp=输入(5000,标题=“获取利润”)
sl=输入(3500,标题=“停止损失”)
//Up=hl2-(系数*atr(Pd))
//Dn=hl2+(系数*atr(Pd))
MUp=安全性(TICKRID、res、hl2-(系数*atr(Pd)))
MDn=安全性(tickerid、res、hl2+(系数*atr(Pd)))
Mclose=安全性(tickerid、res、close)
//趋势上升=关闭[1]>趋势上升[1]?最大值(上升,趋势上升[1]):上升
//趋势下降=关闭[1]MTrendUp[1]?最大值(MUp,MTrendUp[1]):MUp
MTrendDown=Mclose[1]趋势下降[1]?1:关闭<趋势上升[1]-1:nz(趋势[1],1)
//Tsl=趋势==1?趋势上升:趋势下降
MTrend=Mclose>MTrendDown[1]?1:McloseTsl,“向上箭头”,形状。三角形,位置。下方,绿色,0,0)

//plotshape(cross(Tsl,close)和close您应该在
策略中使用相同的
id
。close()
函数与
entry()
调用关闭相应位置时的函数相同

strategy.entry("Buy", strategy.long,when=golong)
strategy.close("Buy", when=longX)  

strategy.entry("Sell", strategy.short,when=goshort)
strategy.close("Sell", when=shortX)