как запретить Pine script открывать новую сделку, пока первая не достигнет S/L
Я новичок в сосновом скрипте (а также в кодировании), для начала мне удалось закодировать некоторые индикаторы, и теперь у меня проблемы с кодированием стратегии, но я почти застрял на попрошайничестве ... В основном проблема в том, что стратегия продолжает открываться сделок и закрытие предыдущих сделок до того, как ему удалось достичь SL. Я пытался искать ответы в Интернете, и все предложения, которые я мог найти, не сработали. Вот код, и я был бы ДЕЙСТВИТЕЛЬНО признателен за ваши ответы, а также некоторые предложения о способах узнать больше о сосновом скрипте (сейчас я изучил руководство, 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)
1 ответ
проверить положение с помощью
strategy.opentrades
var secondTrade = false
isFirstTrade() =>
if not secondTrade and strategy.opentrades == 0
true
else
false
if (longconditions and (isFirstTrade() or secondTrade) )
strategy.entry("Long", strategy.long,1, oca_type = strategy.oca.cancel)
strategy.exit("Exit Long", "Long", profit = longprofit, stop = longstop)
secondTrade := true
if (shortconditions and (isFirstTrade() or secondTrade) )
strategy.entry("Short", strategy.short,1, oca_type = strategy.oca.cancel)
strategy.exit ("Exit Short", "Short", profit = shortprofit, stop = shortstop)
secondTrade := true