简体   繁体   English

Pine Script 策略 SL 问题

[英]Pine Script Strategy SL Issue

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jpeach

//@version=3
strategy("MTF STOCH STRATEGY 2020", overlay=true)

//MULTI-TIMEFRAME STOCHASTIC BUILD.20200303

// STOCH 1 MAIN
resCustom = input(title="Stochastic 1 Resolution", type=resolution, defval="60")
len = input(14, minval=1, title="Stoch 1 Lenth") 
smoothK = input(3, minval=1, title="SmoothK for Stoch 1")
smoothD = input(3, minval=1, title="SmoothD for Stoch 1")

// STOCH 2
ssStoch = input(true, title="Stochastic 2")
resCustom2 = input(title="Stochastic 2 Resolution", type=resolution, defval="15")
len2 = input(14, minval=1, title="Stoch 2 Length")
smoothK2 = input(3, minval=1, title="SmoothK for Stoch 2")
smoothD2 = input(3, minval=1, title="SmoothD for Stoch 2")

// STOCH 3
stStoch = input(true, title="Stochastic 3")
resCustom3 = input(title="Stochastic 3 Resolution", type=resolution, defval="180")
len3 = input(14, minval=1, title="Stoch 3 Length")
smoothK3 = input(3, minval=1, title="SmoothK for Stoch 3")
smoothD3 = input(3, minval=1, title="SmoothD for Stoch 3")

//STOCH 4
sfStoch = input(true, title="Stochastic 4")
resCustom4 = input(title="Stochastic 4 Resolution", type=resolution, defval="D")
len4 = input(14, minval=1, title="Stoch 4 Length")
smoothK4 = input(3, minval=1, title="SmoothK for Stoch 4")
smoothD4 = input(3, minval=1, title="SmoothD for Stoch 4")

upLine = input(80, minval=50, maxval=90, title="Upper Line Value?")
lowLine = input(20, minval=10, maxval=50, title="Lower Line Value?")
sml = input(true, title="Show Mid Line?")
sal = input(true, title="Show BG Highlight When All Oversold/Overbought")

// 1ST STOCH FORMULA
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)
outK = security(tickerid, resCustom, k[1], lookahead=barmerge.lookahead_on)
outD = security(tickerid, resCustom, d[1], lookahead=barmerge.lookahead_on)

// 2ND STOCH FORMULA [CHART RES]
k2 = sma(stoch(close, high, low, len2), smoothK2)
d2 = sma(k2, smoothD2)
outK2 = security(tickerid, resCustom2, k2, lookahead=barmerge.lookahead_off)
outD2 = security(tickerid, resCustom2, d2, lookahead=barmerge.lookahead_off)

// 3RD STOCH FORMULA
k3 = sma(stoch(close, high, low, len3), smoothK3)
d3 = sma(k3, smoothD3)
outK3 = security(tickerid, resCustom3, k3[1], lookahead=barmerge.lookahead_on)
outD3 = security(tickerid, resCustom3, d3[1], lookahead=barmerge.lookahead_on)

// 4TH STOCH FORMULA
k4 = sma(stoch(close, high, low, len4), smoothK4)
d4 = sma(k4, smoothD4)
outK4 = security(tickerid, resCustom4, k4[1], lookahead=barmerge.lookahead_on)
outD4 = security(tickerid, resCustom4, d4[1], lookahead=barmerge.lookahead_on)

// DEFINITIONS FOR CROSS
aboveLine = outK > upLine ? 1 : 0
belowLine = outK < lowLine ? 1 : 0
crossUp = (outK[1] < outD[1] and outK[1] < lowLine[1]) and (outK > outD)  ? 1 : 0
crossDn = (outK[1] > outD[1] and outK[1] > upLine[1]) and (outK < outD) ? 1 : 0

// NON-STRICT CROSS DEFINITIONS
crossUpAll = (outK[1] < outD[1] and outK > outD) ? 1 : 0
crossDownAll = (outK[1] > outD[1] and outK < outD) ? 1 : 0

// BG HIGHLIGHTS
bgcolor(sal and (outK>upLine) and (outK2>upLine) and (outK3>upLine) and (outK4>upLine) ? purple : na, transp=70)
bgcolor(sal and (outK<lowLine) and (outK2<lowLine) and (outK3<lowLine) and (outK4<lowLine) ? purple : na, transp=70)

// PLOT 1ST STOCH
plot(outK, title="Stoch K 1", style=line, linewidth=1, color=white, transp=0)
plot(outD, title="Stoch D 1", style=line, linewidth=1, color=red, transp=0)

// PLOT 2ND STOCH
plot(ssStoch and outK2 ? outK2 : na, title="Stoch K 2", style=line, linewidth=1, color=white, transp=60)
plot(ssStoch and outD2 ? outD2 : na, title="Stoch D 2", style=line, linewidth=1, color=red, transp=100)

// PLOT 3RD STOCH
plot(stStoch and outK3 ? outK3 : na, title="Stoch K 3", style=line, linewidth=2, color=white, transp=60)
plot(stStoch and outD3 ? outD3 : na, title="Stoch D 3", style=line, linewidth=2, color=red, transp=100)

// PLOT 4TH STOCH
plot(sfStoch and outK4 ? outK4 : na, title="Stoch K 4", style=line, linewidth=4, color=white, transp=90)
plot(sfStoch and outD4 ? outD4 : na, title="Stoch D 4", style=line, linewidth=4, color=red, transp=100)

p1 = plot(upLine, title= "Upper Line", style=solid, linewidth=1, color=gray)
p2 = plot(lowLine, title= "Lower Line", style=solid, linewidth=1, color=gray)
plot(sml and 50 ? 50 : na, title="Mid Line", style=linebr, linewidth=1, color=gray)
fill(p1, p2, color=silver, transp=90)

// *ALERTS SCRIPT*

// SHORT TIME FRAME STOCH CHECK [STOCH 2] DEFINITIONS
rev = input(20, minval=1, title="Review Bars (Chart Resolution)") 
stfcheckS = highest(outK2,rev)
stfcheckB = lowest(outK2,rev)

// ALERTS
buy_alert = crossUpAll and (outK3>upLine) and (stfcheckB<20) ? 1 : 0
sell_alert = crossDownAll and (outK3<lowLine) and (stfcheckS>80) ? 1 : 0

// ALERT PLOTS AND HIGHLIGHTS
plot((buy_alert)or (sell_alert) ? 1 : 0, title="Alert", style=line, linewidth=1, color=black, transp=100)
bgcolor(buy_alert ? lime : na, transp=50)
bgcolor(sell_alert ? red : na, transp=50)


// STF HIGHLIGHTS
plotchar((outK>outD) and (outK2[1]>outD2[1] or crossover(outK2[1],outD2[1])) and outK2[1]<upLine, char='•', location=location.bottom, color=lime, transp=0, offset=0)
plotchar(outK2[1]>upLine and outK>upLine and outK>outD , char='•', location=location.bottom, color=lime, transp=0, offset=0)
plotchar((outK<outD) and (outK2[1]<outD2[1] or crossunder(outK2[1],outD2[1])) and outK2[1]>lowLine, char='•', location=location.bottom, color=red, transp=0, offset=0)
plotchar(outK2[1]<lowLine and outK<lowLine and outK<outD , char='•', location=location.bottom, color=red, transp=0, offset=0)


// ATR SL/TP CODE

length = input(14, minval=1)
atrres = input(title="Custom Resolution", type=resolution, defval="15")
useatrres = input(false, title="Use Current Resolution")
res = useatrres ? period : atrres

atrout = security(tickerid, atrres, atr(length)[1], lookahead=barmerge.lookahead_on)

stopMult = input(title="ATR S/L Multiplier", defval="3")
profitMult = input(title="ATR T/P Multiplier", defval="3")

// ENTRY/EXIT FUNCTIONS

longCondition = buy_alert
if (longCondition)
    strategy.entry("Long", strategy.long)
//    strategy.close("Long", when=longClose)
//    strategy.exit("XL","Long", limit=tp,  when=buy_trend, stop=sl)

shortCondition = sell_alert
if (shortCondition)
    strategy.entry("Short", strategy.short)
//    strategy.close("Short", when=shortClose)
//    strategy.exit("XS","Short", when=sell_trend, limit=tp, stop=sl)

// S/L Line Plots

longStop = na
longStop :=  shortCondition ? na : longCondition and strategy.position_size <=0 ? close - (atrout * stopMult) : longStop[1] 
shortStop = na
shortStop := longCondition ? na : shortCondition and strategy.position_size >=0 ? close + (atrout * stopMult) : shortStop[1]

strategy.exit("Long ATR Stop", "Long", stop=longStop)
strategy.exit("Short ATR Stop", "Short", stop=shortStop)

s1 = plot(longStop, style=linebr, color=red, linewidth=2, title='Long ATR Stop')
s2 = plot(shortStop, style=linebr, color=red, linewidth=2, title='Short ATR Stop')

line 153: Cannot call operator * with arguments (series, string);第 153 行:无法使用参数(系列、字符串)调用operator * available overloads: *(integer, integer) => integer;可用的重载:*(integer, integer) => integer; *(float, float) => float; *(浮动,浮动)=>浮动; *(const integer, const integer) => const integer; *(const integer, const integer) => const integer; *(const float, const float) => const float; *(const float, const float) => const float; *(integer, series[integer]) => series[integer]; *(整数, 系列[整数]) => 系列[整数]; *(series[integer], integer) => series[integer]; *(系列[整数], 整数) => 系列[整数]; *(series[integer], series[integer]) => series[integer]; *(series[integer], series[integer]) => series[integer]; *(float, series) => series; *(浮动,系列)=> 系列; *(series, float) => series; *(系列,浮点数)=> 系列; *(series, series) => series; *(系列,系列)=> 系列; line 155: Cannot call operator * with arguments (series, string);第 155 行:不能使用参数(系列、字符串)调用operator * available overloads: *(integer, integer) => integer;可用的重载:*(integer, integer) => integer; *(float, float) => float; *(浮动,浮动)=>浮动; *(const integer, const integer) => const integer; *(const integer, const integer) => const integer; *(const float, const float) => const float; *(const float, const float) => const float; *(integer, series[integer]) => series[integer]; *(整数, 系列[整数]) => 系列[整数]; *(series[integer], integer) => series[integer]; *(系列[整数], 整数) => 系列[整数]; *(series[integer], series[integer]) => series[integer]; *(series[integer], series[integer]) => series[integer]; *(float, series) => series; *(浮动,系列)=> 系列; *(series, float) => series; *(系列,浮点数)=> 系列; *(series, series) => series *(系列,系列)=> 系列

Hi, is anyone able to review the code above and explain the issue above?嗨,有人能够查看上面的代码并解释上面的问题吗? Thank you, JP PS.谢谢你,JP PS。 The issue specifically refers to the two lines below问题特指下面两行

longStop = na
longStop :=  shortCondition ? na : longCondition and strategy.position_size <=0 ? close - (atrout * stopMult) : longStop[1] 
shortStop = na
shortStop := longCondition ? na : shortCondition and strategy.position_size >=0 ? close + (atrout * stopMult) : shortStop[1]

Fix defval of this lines:修复此行的 defval:

stopMult = input(title="ATR S/L Multiplier", defval=3)
profitMult = input(title="ATR T/P Multiplier", defval=3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM