Pine 脚本 - 满足多个条件的 TradingView 买入/卖出指标

问题描述 投票:0回答:2

我正在尝试使用 EMA、MACD 和 Supertrend 三个指标为 TradingView 构建买入/卖出指标。

  • 条件 1:快速 EMA 向上穿越慢速 EMA。
  • 条件2:MACD线位于信号线上方。
  • 条件3:超级趋势处于上升趋势。
  • 条件4:之前的信号必须是“卖出”。

我试图在满足所有三个条件的情况下添加“购买”标签(plotshape),反之亦然。但这不起作用。除了“购买”标签之外,一切似乎都运行良好。

我的代码:

//@version=5
indicator('SuperTrend + MACD + EMA', overlay=true)

src = close
fast_ema = ta.ema(src, 9)
slow_ema = ta.ema(src, 18)

fast_macd = ta.ema(src, 12)
slow_macd = ta.ema(src, 26)
macd = fast_macd - slow_macd
signal = ta.ema(macd, 9)
hist = macd - signal

// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)

// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)

// Buy only if the buy signal is triggered and we are not already long
LONG = not isLong and fast_ema > slow_ema and macd > signal

// Sell only if the sell signal is triggered and we are not already short
SHORT = not isShort and fast_ema < slow_ema and macd < signal

if LONG
    isLong := true
    isShort := false
    isShort

if SHORT
    isLong := false
    isShort := true
    isShort

// Supertrend Calculation
atr = 3 * ta.atr(10)
longStop = hl2 - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = hl2 + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and close > shortStopPrev ? 1 : dir == 1 and close < longStopPrev ? -1 : dir


// Line Plots
bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(dir == 1 ? longStop : na, title='Up Trend Line', style=plot.style_linebr, linewidth=1, color=color.new(color.green, 0))
downTrend = plot(dir != 1 ? shortStop : na, title='Down Trend Line', style=plot.style_linebr, linewidth=1, color=color.new(color.red, 0))

//Fill Background
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false, title='Uptrend Background')
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false, title='Downtrend Background')

//Bar Colour
barcolor(dir == 1 and isLong ? color.green : dir != 1 and isShort ? color.red : color.blue)

// Buy/Sell Label Plots
confirmLONG = not isLong and fast_ema > slow_ema and macd > signal and longStop
confirmSHORT = not isShort and fast_ema < slow_ema and macd < signal and shortStop
plotshape(confirmLONG, title='Buy Label', style=shape.labelup, location=location.belowbar, size=size.normal, text='Buy', textcolor=color.new(color.white, 0), color=color.new(color.green, 0))
plotshape(confirmSHORT, title='Sell Label', style=shape.labeldown, location=location.abovebar, size=size.normal, text='Sell', textcolor=color.new(color.white, 0), color=color.new(color.red, 0))
pine-script moving-average pine-script-v5 pine-editor
2个回答
2
投票

您可以更改条件和绘图,而不是使用

isLong
isShort
等变量,如下所示:

// Buy/Sell Label Plots
confirmLONG = fast_ema > slow_ema and macd > signal and longStop
confirmSHORT = fast_ema > slow_ema and macd > signal and longStop
plotshape(not confirmLONG[1] and confirmLONG, title='Buy Label', style=shape.labelup, location=location.belowbar, size=size.normal, text='Buy', textcolor=color.new(color.white, 0), color=color.new(color.green, 0))
plotshape(not confirmSHORT[1] and confirmSHORT, title='Sell Label', style=shape.labeldown, location=location.abovebar, size=size.normal, text='Sell', textcolor=color.new(color.white, 0), color=color.new(color.red, 0))

这应该可以解决您的主要问题,但您的

confirmSHORT
变量是
confirmLONG
的副本,可能是复制/粘贴错误。


1
投票

去除过多的信号:

代码:

//@version=5
indicator(title='SuperTrend + MACD + EMA', overlay=true)

//EMA
fast_ema = ta.ema(close, 9)
slow_ema = ta.ema(close, 18)
plot(series=fast_ema, title='Fast EMA', color=#00FFFF, linewidth=1)
plot(series=slow_ema, title='Slow EMA', color=#FF00FF, linewidth=1)

//MACD
fastInput = 12
slowInput = 26
[macdLine, signalLine, histLine] = ta.macd(close, fastInput, slowInput, 9)

//Supertrend
atrPeriod = 10
factor = 3
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
linecolor = direction < 0 ? #00ff00 : #ff0000
plot(series=supertrend, title='Supertrend', color=linecolor, linewidth=1)

// Conditions
long_condition = barstate.isconfirmed and
                 fast_ema > slow_ema and
                 macdLine > signalLine and
                 close > supertrend

short_condition = barstate.isconfirmed and
                 fast_ema < slow_ema and
                 macdLine < signalLine and
                 close < supertrend

// Function to Remove Excessive Signals
exrem(condition_1, condition_2) =>
    var entry_signal = 0
    entry_signal := condition_1 ? 1 : condition_2 ? -1 : entry_signal[1]
    entry = entry_signal != entry_signal[1]
    buy = entry and entry_signal == 1
    sell = entry and entry_signal == -1
    [buy, sell]

// Signals
[long_signal, short_signal] = exrem(long_condition, short_condition)

// Plot
plotshape(long_signal, style=shape.triangleup, color=#00FF00, text='BUY', textcolor=#FFFFFF, editable=false, location=location.belowbar, size=size.small)
plotshape(short_signal, style=shape.triangledown, color=#FF0000, text='SELL', textcolor=#FFFFFF, editable=false, location=location.abovebar, size=size.small)

结果:

© www.soinside.com 2019 - 2024. All rights reserved.