本文介绍了如何编写脚本,以便在SMA 50高于SMA 200时仅绘制买入MACD信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似的策略,当SMA50高于SMA200时,在图表上只绘制MACD买入信号。不幸的是,它提示我错误:
"line 31: Mismatched input 'MACD_buy' expecting 'end of line without line continuation'
这是我的代码:
//@version=4
study("Testing MACD Strategy", overlay=true)
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD
// EMA or SMA input //
len50 = input(50, minval=1, title="SMA 50")
len200 = input(200, minval=1, title="SMA 200")
src50 = input(close, title="SMA 50 Source")
src200 = input(close, title="SMA 200 Source")
sma50 = sma(src50, len50)
sma200 = sma(src200, len200)
//aboveEMA10 = ema10 > ema20
//belowEMA10 = ema10 < ema20
//barcolor(aboveEMA10 ? color.olive : na)
plot(sma50, color=color.blue, linewidth=3, title="SMA 50")
plot(sma200, color=color.red, linewidth=4, title="SMA 200")
if sma50 > sma200
MACD_buy = crossover(delta, 0)
plotshape(MACD_buy, title='MACD BUY', style=shape.triangleup,
text='B', location=location.belowbar, color=#00BCD4,
textcolor=color.gray, size=size.tiny)
if sma50 < sma200
MACD_sell = crossunder(delta, 0)
plotshape(MACD_sell, title='MACD SELL', style=shape.triangledown,
text='S', location=location.abovebar, color=#E040FB,
textcolor=color.gray, size=size.tiny)
推荐答案
使用IF函数时,需要将第31行和第35行移动4个空格或按1次Tab键。注意:您不能将PlotShape与IF函数一起使用。我更正了代码。
您可以删除第30、31、32、34、35、36行(代码内部不需要,但我将它们留了下来,以便您可以看到这4个空格)
您可以看到代码在这里工作,https://www.tradingview.com/x/KPz31eWv/
//@version=4
study("Testing MACD Strategy", overlay=true)
fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD
zeroline = input(0)
// EMA or SMA input //
len50 = input(50, minval=1, title="SMA 50")
len200 = input(200, minval=1, title="SMA 200")
src50 = input(close, title="SMA 50 Source")
src200 = input(close, title="SMA 200 Source")
sma50 = sma(src50, len50)
sma200 = sma(src200, len200)
//aboveEMA10 = ema10 > ema20
//belowEMA10 = ema10 < ema20
//barcolor(aboveEMA10 ? color.olive : na)
plot(sma50, color=color.blue, linewidth=3, title="SMA 50")
plot(sma200, color=color.red, linewidth=4, title="SMA 200")
if sma50 > sma200
MACD_buy = crossover(delta, 0)
//plotshape(MACD_buy, title='MACD BUY', style=shape.triangleup, text='B', location=location.belowbar, color=#00BCD4, textcolor=color.gray, size=size.tiny)
if sma50 < sma200
MACD_sell = crossunder(delta, 0)
//plotshape(MACD_sell, title='MACD SELL', style=shape.triangledown, text='S', location=location.abovebar, color=#E040FB, textcolor=color.gray, size=size.tiny)
buy = sma50 > sma200 and crossover(delta, 0)
sell = sma50 < sma200 and crossunder(delta, 0)
plotshape(buy, title='MACD BUY', style=shape.triangleup, text='B', location=location.belowbar, color=#00BCD4, textcolor=color.gray, size=size.tiny)
plotshape(sell, title='MACD SELL', style=shape.triangledown, text='S', location=location.abovebar, color=#E040FB, textcolor=color.gray, size=size.tiny)
这篇关于如何编写脚本,以便在SMA 50高于SMA 200时仅绘制买入MACD信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!