TradZoo - EMA Crossover Indicator

TradZoo · study · 95 行 · 点赞 456 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator('TradZoo - EMA Crossover Indicator', overlay = true)
 
// Define EMAs
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)
 
// Detect EMA Crosses
crossAbove = ta.crossover(ema50, ema200) // EMA 50 crosses above EMA 200
crossBelow = ta.crossunder(ema50, ema200) // EMA 50 crosses below EMA 200
 
// Track last crossover bars
var int lastCrossAboveBar = na
var int lastCrossBelowBar = na
var int lastBuySignalBar = na
var int lastSellSignalBar = na
var bool buyTriggered = false
var bool sellTriggered = false
 
if crossAbove
    lastCrossAboveBar := bar_index
    buyTriggered := false // Reset for new buy opportunity
    buyTriggered
 
if crossBelow
    lastCrossBelowBar := bar_index
    sellTriggered := false // Reset for new sell opportunity
    sellTriggered
 
// Condition for touching 200 EMA
bool candleTouchesEMA200 = low <= ema200 or low[1] <= ema200 // Either current or previous candle touches EMA 200
bool candleTouchesEMA200_Sell = high >= ema200 or high[1] >= ema200 // Either current or previous candle touches EMA 200
 
// Buy Condition: Touch 200 EMA + Next candle closes above previous candle's close + 30 candle gap
float previousClose = close[1]
bool buySignal = candleTouchesEMA200 and close > previousClose and not buyTriggered and not na(lastCrossAboveBar) and bar_index > lastCrossAboveBar and (na(lastBuySignalBar) or bar_index - lastBuySignalBar >= 30)
 
if buySignal
    buyTriggered := true
    lastBuySignalBar := bar_index // Store bar index of the latest buy signal
    lastBuySignalBar
 
// Sell Condition: Touch 200 EMA + Next candle closes below previous candle's close + 30 candle gap
bool sellSignal = candleTouchesEMA200_Sell and close < previousClose and not sellTriggered and not na(lastCrossBelowBar) and bar_index > lastCrossBelowBar and (na(lastSellSignalBar) or bar_index - lastSellSignalBar >= 30)
 
if sellSignal
    sellTriggered := true
    lastSellSignalBar := bar_index // Store bar index of the latest sell signal
    lastSellSignalBar
 
// Calculate Buy Target & Stop Loss
float previousLow = low[1]
float lastLow = low
float buyTarget = na
float buyStopLoss = na
if buySignal
    buyTarget := close + 2 * (close - (previousLow < lastLow ? previousLow : lastLow)) // Buy target above entry
    buyStopLoss := math.min(previousLow, lastLow) // Stop loss at the lowest low of last 2 candles
    buyStopLoss
 
// Calculate Sell Target & Stop Loss
float previousHigh = high[1]
float lastHigh = high
float sellTarget = na
float sellStopLoss = na
if sellSignal
    sellTarget := close - 2 * ((previousHigh > lastHigh ? previousHigh : lastHigh) - close) // Sell target below entry
    sellStopLoss := math.max(previousHigh, lastHigh) // Stop loss at the highest high of last 2 candles
    sellStopLoss
 
// Plot EMAs
plot(ema50, title = 'EMA 50', color = color.blue, linewidth = 2)
plot(ema200, title = 'EMA 200', color = color.red, linewidth = 2)
 
// Plot Buy & Sell Signals
plotshape(series = buySignal, location = location.belowbar, color = color.green, style = shape.labelup, title = 'Buy Signal', size = size.small)
plotshape(series = sellSignal, location = location.abovebar, color = color.red, style = shape.labeldown, title = 'Sell Signal', size = size.small)
 
// Plot Target Lines & Labels
if buySignal and not na(buyTarget)
    line.new(x1 = bar_index, y1 = buyTarget, x2 = bar_index + 5, y2 = buyTarget, width = 1, color = color.green, style = line.style_dotted)
    label.new(x = bar_index, y = buyTarget, text = 'Buy Target: ' + str.tostring(buyTarget, format.mintick), color = color.green, textcolor = color.white, size = size.tiny)
 
if sellSignal and not na(sellTarget)
    line.new(x1 = bar_index, y1 = sellTarget, x2 = bar_index + 5, y2 = sellTarget, width = 1, color = color.red, style = line.style_dotted)
    label.new(x = bar_index, y = sellTarget, text = 'Sell Target: ' + str.tostring(sellTarget, format.mintick), color = color.red, textcolor = color.white, size = size.tiny)
 
// Plot Stop Loss Lines & Labels (Dark Red Line)
if buySignal and not na(buyStopLoss)
    line.new(x1 = bar_index, y1 = buyStopLoss, x2 = bar_index + 5, y2 = buyStopLoss, width = 2, color = color.maroon, style = line.style_solid)
    label.new(x = bar_index, y = buyStopLoss, text = 'Buy Stop Loss: ' + str.tostring(buyStopLoss, format.mintick), color = color.maroon, textcolor = color.white, size = size.tiny)
 
if sellSignal and not na(sellStopLoss)
    line.new(x1 = bar_index, y1 = sellStopLoss, x2 = bar_index + 5, y2 = sellStopLoss, width = 2, color = color.maroon, style = line.style_solid)
    label.new(x = bar_index, y = sellStopLoss, text = 'Sell Stop Loss: ' + str.tostring(sellStopLoss, format.mintick), color = color.maroon, textcolor = color.white, size = size.tiny)

← 返回列表