Попытка преобразовать сложную стратегию в предупреждение - PullRequest
0 голосов
/ 27 сентября 2019

Я пытаюсь преобразовать эту стратегию в оповещение, чтобы автоматизировать стратегию с 3 запятыми через оповещения.

Я пытался преобразовать стратегию в исследование, но получил ошибку, так как условие включает в себя Strategy.closetrades, которые не допускаются в исследовании.Я не уверен, как это сделать альтернативным способом.

strategy(shorttitle = "Squeeze MOM", title="Squeeze Momentum on Reversal Strategy", overlay=false)

len = input(title="Length", type=integer, defval=14)
th = input(title="threshold", type=integer, defval=20)

TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0


SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus = nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)

length = input(30, title="BB Length")
mult = input(3.0,title="BB MultFactor")
lengthKC=input(30, title="KC Length")
multKC = input(1.0, title="KC MultFactor")
strength = input(0.0018, title="Reversal Strength")

useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)

// Calculate BB
source = close
basis = sma(source, length)
dev = multKC * stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = ema(source, lengthKC)
range = atr(length)
rangema = ema(range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) or (upperBB > upperKC)

highest = highest(high, lengthKC)
lowest = lowest(low, lengthKC)
lastsma = ema(close,lengthKC)

valin = linreg(source  -  avg(avg(highest, lowest), lastsma), lengthKC,0)

bcolor = iff( valin > 0, iff( valin > nz(valin[1]), lime, green), iff( valin < nz(valin[1]), red, maroon))
scolor = sqzOn ? black : gray 
plot(valin, color=bcolor, style=histogram, linewidth=4)
plot(0, color=scolor, style=circles, linewidth=2)

longcondition = valin > strength and valin > nz(valin[1])  and DIPlus > DIMinus and DX > 15 and strategy.closedtrades < 1000  and (sqzOn[1] or sqzOn[2]or sqzOn[3] or sqzOn[4]) // line become maroon
shortcondition = valin < strength and valin < nz(valin[1]) and DIPlus < DIMinus and DX > 15 and strategy.closedtrades < 1000 and (sqzOn[1] or sqzOn[2]or sqzOn[3] or sqzOn[4]) // line become green
exitlong = valin < nz(valin[1]) and strategy.closedtrades < 1000
exitshort = valin > nz(valin[1]) and strategy.closedtrades < 1000

if(longcondition)
    strategy.entry("BUY", strategy.long, qty = (strategy.equity/(close*1.01)))
strategy.exit("BUY TO COV", "BUY", trail_price = strategy.position_avg_price, trail_offset = (.8*range*100))
strategy.close("BUY", when = exitlong)
if(shortcondition)
    strategy.entry("SELL", strategy.short, qty = (strategy.equity/(close*1.01)))
strategy.exit("SELL TO COV", "SELL", trail_price = strategy.position_avg_price, trail_offset = (.8*range*100))
strategy.close("SELL", when = exitshort)

1 Ответ

0 голосов
/ 27 сентября 2019

Вы можете использовать две переменные, одну в качестве сигнала на покупку, одну в качестве сигнала на продажу.

Это пример стратегии по умолчанию в Tradingview:

//@version=4
strategy("My Strategy", overlay=true)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

Вы можете преобразовать это вИндикатор выглядит следующим образом:

//@version=4
study("My Script", overlay=true)

var isLong = false
var isShort = false

buySignal = not isLong and crossover(sma(close, 14), sma(close, 28))        // only buy if we are not already long
sellSignal = not isShort and crossunder(sma(close, 14), sma(close, 28))     // only sell if we are not already short

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true

alertcondition(condition=buySignal, title="BUY", message="BUY")
alertcondition(condition=sellSignal, title="SELL", message="SELL")

plotshape(series=buySignal, text="BUY", style=shape.triangleup, color=color.green, location=location.belowbar, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, color=color.red, location=location.abovebar, size=size.small)

Примечание: Вам необходимо настроить оповещения вручную, как описано здесь .

enter image description here

...