Tradingview Pinescript Strategy.close() 不工作

时间:2021-06-03 02:58:37

标签: pine-script

Tradingview Pinescript Strategy.close() 不起作用

策略是:

多头条件:收盘价必须高于超趋势(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 个答案:

答案 0 :(得分:0)

您应该在 id 函数中使用与在 strategy.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)