我正在尝试为TradingView做一个松树脚本,将买卖基于MACD,但只有在变化足够大的情况下。但是,即使没有进行任何交易,我的lastLong和lastShort变量也会不断更新。我如何确保lastLong和lastShort只更新一个实际的交易?

编辑:不幸的是,答案并没有解决这个问题,红线(lastShort)消失了,蓝线(lastLong)仍然比交易更频繁地更新。

//@version=4
strategy("MACD Strategy", overlay=true)
// Variables
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
desiredGain = 1.05
desiredLoss = 1.025
lastLong = 1.0
lastLong := na(lastLong[1]) ? na : lastLong[1]
lastShort = 1.0
lastShort := na(lastShort[1]) ? na : lastShort[1]
inDateRange = (time >= timestamp(syminfo.timezone, 2021, 04, 01, 0, 0))// and (time < timestamp(syminfo.timezone, 2021, 05, 16, 0, 0))
// Calculation
fastMA = ema(close, fastLength)
slowMA = ema(close, slowlength)
macd = fastMA - slowMA
signal = sma(macd, MACDLength)
esignal = ema(macd, MACDLength)
delta = macd - esignal
didRiseEnough = close/lastLong > desiredGain
didFallEnough = lastShort/close > desiredLoss
if na(lastLong) // If lastLong is uninitialized, run the first entry without checking didFallEnough
if (inDateRange and crossover(macd, esignal))
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
lastLong := close
else
if (inDateRange and crossover(macd, esignal) and didFallEnough)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
lastLong := close
if na(lastShort) // If lastShort is uninitialized, run the first entry without checking didRiseEnough
if (inDateRange and crossunder(macd, esignal))
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
lastShort := close
else
if (inDateRange and crossunder(macd, esignal) and didRiseEnough)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
lastShort := close
// Plot
plot(macd * 10 + close/2, color=color.blue)
//plot(signal * 10 + close/2, color=color.orange)
plot(esignal * 10 + close/2, color=color.orange)
plot(lastLong, color=color.blue)
plot(lastShort, color=color.red)
plotshape(didRiseEnough, style=shape.arrowup, color=color.red)
plotshape(didFallEnough, style=shape.arrowdown, color=color.blue)
if (not inDateRange)
strategy.close_all()发布于 2021-05-16 03:30:01
您必须将它们声明为var,否则它们将在每个条形图上重新初始化。
此外,您必须在每个栏上调用crossover()和crossunder(),以避免编译器警告。
我在您的脚本中将它们命名为co和cu。
请看看这个能不能用。
我不能检查,因为你没有指定你的屏幕截图是在哪个报价器和时间范围内进行的。
//@version=4
strategy("MACD Strategy", overlay=true)
// Variables
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
desiredGain = 1.05
desiredLoss = 1.025
var lastLong = 1.0
lastLong := na(lastLong[1]) ? na : lastLong[1]
var lastShort = 1.0
lastShort := na(lastShort[1]) ? na : lastShort[1]
inDateRange = (time >= timestamp(syminfo.timezone, 2021, 04, 01, 0, 0))// and (time < timestamp(syminfo.timezone, 2021, 05, 16, 0, 0))
// Calculation
fastMA = ema(close, fastLength)
slowMA = ema(close, slowlength)
macd = fastMA - slowMA
signal = sma(macd, MACDLength)
esignal = ema(macd, MACDLength)
delta = macd - esignal
didRiseEnough = close/lastLong > desiredGain
didFallEnough = lastShort/close > desiredLoss
co = crossover(macd, esignal)
cu = crossunder(macd, esignal)
if na(lastLong) // If lastLong is uninitialized, run the first entry without checking didFallEnough
if (inDateRange and co)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
lastLong := close
else
if (inDateRange and co and didFallEnough)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
lastLong := close
if na(lastShort) // If lastShort is uninitialized, run the first entry without checking didRiseEnough
if (inDateRange and cu)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
lastShort := close
else
if (inDateRange and cu and didRiseEnough)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
lastShort := close
// Plot
plot(macd * 10 + close/2, color=color.blue)
//plot(signal * 10 + close/2, color=color.orange)
plot(esignal * 10 + close/2, color=color.orange)
plot(lastLong, color=color.blue)
plot(lastShort, color=color.red)
plotshape(didRiseEnough, style=shape.arrowup, color=color.red)
plotshape(didFallEnough, style=shape.arrowdown, color=color.blue)
if (not inDateRange)
strategy.close_all()更新1:下面的代码正在进行中,需要进行一些调试。
你会说:蓝色(lastLong)仍然比交易更频繁。
这是因为那里可能存在交易,因为在这些更新点进行交易的条件是有效的。但是,您的策略并不需要在那里进行交易,因为在策略的属性中,Pyramiding被设置为1。这意味着你的策略在任何时候都只会在每个方向上接受一个订单。如果增加Pyramiding,您将看到交易将在这些更新点进行。现在,这取决于你想要什么。你想在每个方向上只做一个交易吗?或者你只想在任何时候处于一个位置(多头或空头)?
//@version=4
strategy("MACD Strategy", overlay=true)
// Variables
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
desiredGain = 1.05
desiredLoss = 1.025
var float lastLong = na
// lastLong := na(lastLong[1]) ? na : lastLong[1]
var float lastShort = na
// lastShort := na(lastShort[1]) ? na : lastShort[1]
inDateRange = (time >= timestamp(syminfo.timezone, 2021, 04, 01, 0, 0))// and (time < timestamp(syminfo.timezone, 2021, 05, 16, 0, 0))
// Calculation
fastMA = ema(close, fastLength)
slowMA = ema(close, slowlength)
macd = fastMA - slowMA
signal = sma(macd, MACDLength)
esignal = ema(macd, MACDLength)
delta = macd - esignal
didRiseEnough = close/lastLong > desiredGain
didFallEnough = lastShort/close > desiredLoss
co = crossover(macd, esignal)
cu = crossunder(macd, esignal)
plotchar(na(lastLong),"na(lastLong)","")
plotchar(inDateRange and co, "inDateRange and co", "")
plotchar(inDateRange and co and didFallEnough, "inDateRange and co and didFallEnough", "")
if na(lastLong) // If lastLong is uninitialized, run the first entry without checking didFallEnough
if (inDateRange and co)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
lastLong := close
else
if (inDateRange and co and didFallEnough)
strategy.entry("MacdLE", strategy.long, comment="MacdLE")
lastLong := close
if na(lastShort) // If lastShort is uninitialized, run the first entry without checking didRiseEnough
if (inDateRange and cu)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
lastShort := close
else
if (inDateRange and cu and didRiseEnough)
strategy.entry("MacdSE", strategy.short, comment="MacdSE")
lastShort := close
// Plot
plot(macd * 10 + close/2, "macd", color=color.blue)
//plot(signal * 10 + close/2, color=color.orange)
plot(esignal * 10 + close/2, "esignal", color=color.orange)
plot(lastLong, "lastLong", color=color.blue)
plot(lastShort, "lastShort", color=color.red)
// plotshape(didRiseEnough, style=shape.arrowup, color=color.red)
// plotshape(didFallEnough, style=shape.arrowdown, color=color.blue)
if (not inDateRange)
strategy.close_all()https://stackoverflow.com/questions/67550341
复制相似问题