pine-script - Tradingview Pinescript Strategy.close() 不工作
问题描述
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)
解决方案
您应该在函数中使用与关闭相应头寸的调用相同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)
推荐阅读
- c++ - 如何挂钩 windows7 explorer.exe 以隐藏文件
- symfony - 扩展 EntityType 以访问实体
- c# - 调整图像文件大小时,GDI+ 中发生一般错误
- r - 将 R 中的 group_by 输出导出到 talex
- c# - 关键字“null”附近的语法不正确
- python - 如何制作带有相关子项的 Pandas 计数数据透视表?
- javascript - 根据 Object 值对 Observable 数组进行排序
- java - 期间发生内部错误:“正在启动
"。ATTR_EXCLUDE_TEST_CODE - docker - WixToolset 不能安装在具有 windows/servercore 基础映像的 windows 容器中
- oracle-data-integrator - 有没有一种方法可以在 ODI 元数据数据库中的某处找到数据存储对象上的强制执行上下文?