0

我正在尝试通过结合 EMA、MACD 和 Supertrend 三个指标来构建 TradingView 的买入/卖出指标。

买入信号的条件:

  • MACD 线(绿色)高于信号线(红色)
  • 9 EMA LINE(绿色)在 18 EMA LINE(红色)上方
  • SUPERTREND 处于上升趋势(绿色区域)。
  • 如果之前的信号也是买入信号,它将忽略当前的买入信号。

卖出信号的条件:

  • MACD 线(绿色)低于信号线(红色)
  • 9 EMA LINE(绿色)低于 18 EMA LINE(红色)
  • 超级趋势处于下降趋势(红色区域)。
  • 如果之前的信号也是卖出信号,它将忽略当前的卖出信号。

当满足上述条件时,它应该给出相应的买入或卖出信号。

但是,我无法匹配 plotshape 中的条件以获得满足所有条件的正确买入/卖出信号。

我的代码:

//@version=5
indicator("3-in-1 Indicator", 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

atrPeriod = input(10, "ATR Length")
factor = input.float(3.0, "Factor", step = 0.01)

[supertrend, direction] = ta.supertrend(factor, atrPeriod)

bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)

//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')

plotshape(direction < 0 and direction[1] > 0 and isLong, title='Buy', style=shape.labelup, location=location.belowbar, size=size.normal, text='Buy', textcolor=color.new(color.white, 0), color=color.new(color.green, 0))
plotshape(direction > 0 and direction[1] < 0 and isShort, title='Sell', style=shape.labeldown, location=location.abovebar, size=size.normal, text='Sell', textcolor=color.new(color.white, 0), color=color.new(color.red, 0))

//Bar Colour
barcolor(direction < 0 and isLong ? color.green : direction > 0 and isShort ? color.red : color.blue)

// Multiple EMA
plot(ta.ema(close, 9), linewidth=2, title='9 EMA', display=display.none)
plot(ta.ema(close, 18), linewidth=2, title='18 EMA', display=display.none)

任何帮助将不胜感激!

4

0 回答 0