我是 pine 脚本的新手(以及一般的编码)。首先,我设法编写了一些指标,现在我在编写策略时遇到了问题。基本上,问题在于该策略在成功触及 SL 之前不断打开交易并关闭之前的交易。我试过在网上寻找答案,但我能找到的所有建议都没有奏效。这是代码,我真的很想了解有关 pine 脚本的更多信息(现在我已经学习了手册、youtube 以及我在这里喜欢的所有内容......)。
(我也计划为这个策略编写追踪止损,但现在这似乎是遥远的未来::()
//@version=5
strategy("My Strategy2", pyramiding = 1, overlay=true, calc_on_every_tick=true)
//supertrend1 - supertrend slow
GRP1 = "ST1"
atrPeriod1 = input(12, "ATR Length1", group = GRP1)
srcInput1 = input.source(hl2, "Source1")
factor1 = input.float(3.0, "Factor1", step = 0.01, group = GRP1)
[supertrend1, direction1] = ta.supertrend(factor1, atrPeriod1)
bodyMiddle1 = plot((open + close) / 2, display=display.none)
upTrend1 = plot(direction1 < 0 ? supertrend1 : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend1 = plot(direction1 < 0? na : supertrend1, "Down Trend", color = color.red, style=plot.style_linebr)
//supertrend - medium
GRP2 = "ST2"
atrPeriod2 = input(11, "ATR Length2", group = GRP2)
srcInput2= input.source(hl2, "Source2")
factor2 = input.float(2.0, "Factor2", step = 0.01, group = GRP2)
[supertrend2, direction2] = ta.supertrend(factor2, atrPeriod2)
bodyMiddle2 = plot((open + close) / 2, display=display.none)
upTrend2 = plot(direction2 < 0 ? supertrend2 : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend2 = plot(direction2 < 0? na : supertrend2, "Down Trend", color = color.red, style=plot.style_linebr)
// supertrend - fast
GRP3 = "ST3"
atrPeriod3 = input(10, "ATR Length3", group = GRP3)
srcInput3 = input.source(hl2, "Source3")
factor3 = input.float(1.0, "Factor3", step = 0.01, group = GRP3)
[supertrend3, direction3] = ta.supertrend(factor3, atrPeriod3)
bodyMiddle3 = plot((open + close) / 2, display=display.none)
upTrend3 = plot(direction3 < 0 ? supertrend3 : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend3 = plot(direction3 < 0? na : supertrend3, "Down Trend", color = color.red, style=plot.style_linebr)
GRP4 = "EMA1"
len1 = input.int(50, minval=1, title="Length", group=GRP4)
src1 = input(close, title="Source", group=GRP4)
offset1 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group=GRP4)
out1 = ta.ema(src1, len1)
plot(out1, linewidth=2, title="EMA", color=color.silver, offset=offset1)
GRP8 = "StochRSI"
smoothK = input.int(3, "K", minval=1, group=GRP8)
smoothD = input.int(3, "D", minval=1, group=GRP8)
lengthRSI = input.int(14, "RSI Length", minval=1, group=GRP8)
lengthStoch = input.int(14, "Stochastic Length", minval=1, group=GRP8)
src = input(close, title="RSI Source", group=GRP8)
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
plInput = input.float(1.5, title="P/L Ratio", step=0.1, minval=0.1)
// long entery conditions
longConditionsStoRSI = (k > d)
longSupertrend = close > (direction2 < 0 ? supertrend2 : na)
longConditionEMA = close > ta.ema(src1, len1)
longconditions = longConditionsStoRSI and longSupertrend and longConditionEMA
// short entery conditions
shortConditionsStoRSI = (k < d)
shortSupertrend = close < (direction2 < 0? na : supertrend2)
shortconditionEMA = close < ta.ema(src1, len1)
shortconditions = shortConditionsStoRSI and shortSupertrend and shortconditionEMA
// long SL
longstop = (direction1 < 0 ? supertrend1 : na)
longstop := longconditions ? (direction1 < 0 ? supertrend1 : na) : longstop[1]
longprofit = close + ((close - longstop) * plInput)
// short SL
shortstop = (direction1 < 0? na : supertrend1)
shortstop := shortconditions ? (direction1 < 0? na : supertrend1) : shortstop[1]
shortprofit = close - ((shortstop - close) * plInput)
if (longconditions)
OpenTrade :=false
strategy.entry("Long", strategy.long,1, oca_type = strategy.oca.cancel)
strategy.exit("Exit Long", "Long", profit = longprofit, stop = longstop)
if (shortconditions)
strategy.entry("Short", strategy.short,1, oca_type = strategy.oca.cancel)
strategy.exit ("Exit Short", "Short", profit = shortprofit, stop = shortstop)
plot(longstop, "LS", color = color.red, style=plot.style_line, linewidth = 5)
plot(longprofit, "LP", color = color.green, style=plot.style_line, linewidth = 5)
plot(shortstop, "SS", color = color.orange, style=plot.style_line, linewidth = 5)
plot(shortprofit, "SP", color = color.blue, style=plot.style_line, linewidth = 5)