试图将复杂的策略转化为警报
Trying to convert complex strategy into alert
我正在尝试将此策略转换为警报,以便通过警报自动执行带有 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)
您可以使用两个变量,一个作为买入信号,一个作为卖出信号。
这是 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)
注意:您需要按照here所述手动设置警报。
我正在尝试将此策略转换为警报,以便通过警报自动执行带有 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)
您可以使用两个变量,一个作为买入信号,一个作为卖出信号。
这是 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)
注意:您需要按照here所述手动设置警报。