Heikin-Ashi Streak Reversal Alert

Trend_Informer · study · 156 行 · 点赞 28 · TradingView 原页

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

Pine Script

//@version=6
indicator("HA Streak Reversal Alert", shorttitle="HA Rev Alert", overlay=true)
 
// ============================================================================
// HA STREAK REVERSAL ALERT
// ============================================================================
// This indicator detects a potential Heikin Ashi reversal after a sustained move.
//
// HOW IT WORKS:
// 1. It calculates standard Heikin Ashi candles (green when haClose > haOpen).
// 2. It tracks consecutive candles in the same direction, but only counts
//    closed (confirmed) bars so signals do not repaint while a candle is forming.
// 3. When a streak of at least "Minimum Previous Streak Length" candles occurs
//    in one direction, followed by "Consecutive Opposite Candles Required"
//    candles in the opposite direction, it triggers a signal.
//
// DEFAULT SETTINGS:
// - Alert after 10+ candles in one direction, then 2 consecutive candles reverse.
//
// HOW TO USE:
// 1. Add the script to your chart.
// 2. Adjust the inputs if you want a different streak length or confirmation count.
// 3. Optional: turn on "Overlay Heikin Ashi Candles" to see the HA candles.
// 4. Create an alert: right-click the indicator name → Add alert →
//    choose "Bullish HA Reversal" or "Bearish HA Reversal".
// 5. Set the alert condition to "Once Per Bar Close".
//
// NOTES:
// - Signals only appear on closed bars (no repainting).
// - The table (top-right) shows the current and previous streak for verification.
// - This is not financial advice. Always manage risk.
// ============================================================================
 
// ============================================================================
// INPUTS
// ============================================================================
minStreak = input.int(10, "Minimum Previous Streak Length", minval=1, 
     tooltip="How many consecutive Heikin Ashi candles in one direction are required before looking for a reversal")
 
oppCount = input.int(2, "Consecutive Opposite Candles Required", minval=1, 
     tooltip="How many consecutive opposite-direction HA candles must close to trigger the alert")
 
showSignals = input.bool(true, "Show Signal Shapes on Chart")
showHACandles = input.bool(false, "Overlay Heikin Ashi Candles")
showTable = input.bool(true, "Show Streak Info Table")
 
// ============================================================================
// HEIKIN ASHI CALCULATIONS
// Standard formula - color is determined by haClose vs haOpen
// ============================================================================
haClose = (open + high + low + close) / 4.0
haOpen = float(na)
haOpen := na(haOpen[1]) ? (open + close) / 2.0 : (haOpen[1] + haClose[1]) / 2.0
haHigh = math.max(high, math.max(haOpen, haClose))
haLow = math.min(low, math.min(haOpen, haClose))
 
// Direction of the current Heikin Ashi candle (bullish = close > open)
bool isUp = haClose > haOpen
 
// ============================================================================
// STREAK TRACKING - ONLY UPDATED ON CONFIRMED (CLOSED) BARS
// This prevents alerts from firing while candles are still forming
// ============================================================================
var int currentStreak = 0
var bool currentIsUp = false
var int prevStreak = 0
var bool prevIsUp = false
 
if barstate.isconfirmed
    if currentStreak == 0
        // First bar initialization
        currentStreak := 1
        currentIsUp := isUp
    else if isUp == currentIsUp
        // Same direction - extend the streak
        currentStreak += 1
    else
        // Direction changed - save previous streak and start new one
        prevStreak := currentStreak
        prevIsUp := currentIsUp
        currentStreak := 1
        currentIsUp := isUp
 
// ============================================================================
// SIGNAL DETECTION
// Triggers only when the required number of opposite candles have CLOSED
// after a sufficiently long previous streak
// ============================================================================
bool bullishReversal = barstate.isconfirmed and 
     currentStreak == oppCount and 
     currentIsUp and 
     not prevIsUp and 
     prevStreak >= minStreak
 
bool bearishReversal = barstate.isconfirmed and 
     currentStreak == oppCount and 
     not currentIsUp and 
     prevIsUp and 
     prevStreak >= minStreak
 
// ============================================================================
// VISUALS
// ============================================================================
// Signal shapes
plotshape(showSignals and bullishReversal, title="Bullish Reversal", 
     style=shape.triangleup, location=location.belowbar, 
     color=color.new(color.green, 0), size=size.small, text="↑")
 
plotshape(showSignals and bearishReversal, title="Bearish Reversal", 
     style=shape.triangledown, location=location.abovebar, 
     color=color.new(color.red, 0), size=size.small, text="↓")
 
// Optional HA candle overlay
plotcandle(showHACandles ? haOpen : na, showHACandles ? haHigh : na, 
     showHACandles ? haLow : na, showHACandles ? haClose : na, 
     title="Heikin Ashi", 
     color=isUp ? color.new(color.teal, 30) : color.new(color.red, 30),
     wickcolor=isUp ? color.teal : color.red,
     bordercolor=isUp ? color.teal : color.red)
 
// Info table
var table infoTable = table.new(position.top_right, 2, 3, 
     bgcolor=color.new(color.black, 70), border_width=1)
 
if showTable and (barstate.islast or barstate.isconfirmed)
    table.cell(infoTable, 0, 0, "Current Streak", text_color=color.white, text_size=size.small)
    table.cell(infoTable, 1, 0, str.tostring(currentStreak) + (currentIsUp ? " UP" : " DOWN"), 
         text_color=currentIsUp ? color.green : color.red, text_size=size.small)
    
    table.cell(infoTable, 0, 1, "Previous Streak", text_color=color.white, text_size=size.small)
    table.cell(infoTable, 1, 1, str.tostring(prevStreak) + (prevIsUp ? " UP" : " DOWN"), 
         text_color=prevIsUp ? color.green : color.red, text_size=size.small)
    
    table.cell(infoTable, 0, 2, "Settings", text_color=color.gray, text_size=size.tiny)
    table.cell(infoTable, 1, 2, str.tostring(minStreak) + " then " + str.tostring(oppCount), 
         text_color=color.gray, text_size=size.tiny)
 
// ============================================================================
// ALERTS
// Create alerts in TradingView using these conditions.
// IMPORTANT: Set the alert trigger to "Once Per Bar Close"
// ============================================================================
alertcondition(bullishReversal, 
     title="Bullish HA Reversal", 
     message="Bullish reversal confirmed: consecutive HA up candles closed after a long down streak")
 
alertcondition(bearishReversal, 
     title="Bearish HA Reversal", 
     message="Bearish reversal confirmed: consecutive HA down candles closed after a long up streak")
 
// Optional dynamic alerts
if bullishReversal
    alert("Bullish HA Reversal: " + str.tostring(oppCount) + " UP after " + str.tostring(prevStreak) + " DOWN", alert.freq_once_per_bar_close)
 
if bearishReversal
    alert("Bearish HA Reversal: " + str.tostring(oppCount) + " DOWN after " + str.tostring(prevStreak) + " UP", alert.freq_once_per_bar_close)

← 返回列表