OPEN-SOURCE SCRIPT

Doji Break Signal

196
// ============================================================
// DOCUMENTATION: Doji Break Signal Indicator
// Version: 6 (Pine Script)
// ============================================================

// OVERVIEW
// --------
// This indicator detects Doji candles and generates Buy/Sell
// signals when price breaks above/below the Doji's high/low.
// It includes EMA filters, ATR distance limits, exit conditions
// with 2-bar confirmation, and a real-time dashboard.

// ============================================================
// INPUTS & SETTINGS
// ============================================================

// --- Core Settings ---
// Doji Body % of Range (default: 10)
// Defines what qualifies as a Doji. A candle is a Doji if
// its body size (|close - open|) is <= X% of its total range
// (high - low). Lower = stricter Doji detection.

// Hard Stop Loss (default: 15 pts)
// Maximum loss allowed per trade. If price moves against
// entry by this many points (checked via intrabar low/high),
// position is closed.

// Exit Offset Points (default: 10 pts)
// If the current candle's low drops more than X points below
// the previous candle's close (for longs), exit is triggered.
// Symmetric for shorts using high vs prev close.

// Max EMA30 ATR Distance - BUY (default: +2.5)
// Buy signal is blocked if price is more than 2.5x ATR
// ABOVE EMA30. Prevents buying into overextended upside moves.
// Formula: (close - EMA30) / ATR14 must be < 2.5

// Max EMA30 ATR Distance - SELL (default: -2.5)
// Sell signal is blocked if price is more than 2.5x ATR
// BELOW EMA30. Prevents selling into overextended downside moves.
// Formula: (close - EMA30) / ATR14 must be > -2.5

// --- Signal Settings ---
// Enable Buy Signals (default: true)
// Toggle to completely enable/disable all BUY signal generation.

// Enable Sell Signals (default: true)
// Toggle to completely enable/disable all SELL signal generation.

// --- Label Settings ---
// Show Prices on Labels (default: true)
// When enabled, BUY/SELL labels show the actual entry price
// (Doji high for buy, Doji low for sell). When disabled,
// labels show only "BUY" / "SELL" text.

// Show Buy/Sell Labels (default: true)
// Toggle visibility of BUY and SELL signal labels on chart.
// Signal logic still runs internally even when hidden.

// Show Exit Labels (default: true)
// Toggle visibility of EXIT LONG / EXIT SHORT labels on chart.
// Exit logic still runs and P&L is tracked even when hidden.

// Show ATR Labels on EMAs (default: true)
// Toggle visibility of ATR multiple labels at the right edge
// of each EMA line (EMA5/10/20/30).

// ============================================================
// DOJI DETECTION LOGIC
// ============================================================

// A Doji is detected when:
// - Candle has a non-zero range (high > low)
// - Body size (|close - open|) <= dojiBodyPercent% of range
//
// When a new Doji forms:
// - Its high/low are stored as dHigh/dLow
// - A shaded box is drawn covering dHigh to dLow
// - Three Fibonacci levels are drawn:
// 0.44 (red dashed) = dLow + 44% of range
// 0.50 (yellow dashed) = dLow + 50% of range
// 0.618 (green dashed) = dLow + 61.8% of range
// - All lines/labels extend rightward until next Doji forms
// - buyUsed and sellUsed flags reset to allow fresh signals
//
// Only current-day Dojis generate signals (buyUsed/sellUsed
// reset at start of each new day).

// ============================================================
// BUY SIGNAL CONDITIONS (all must be true)
// ============================================================

// 1. dojiActive: A Doji exists and current bar is at least
// 2 bars after the Doji (bar_index > dBar + 1)
// — prevents signal on the immediate Doji break candle

// 2. not buyUsed: Buy signal hasn't fired yet for this Doji
// — ensures only ONE buy per Doji

// 3. high > dHigh: Current candle's high breaks above Doji high
// — intrabar breakout detection

// 4. close > close[1]: Current close is above previous close
// — momentum confirmation, avoids wick-only breakouts

// 5. atrMult30 < ema30AtrLimitBuy:
// Price is not more than +2.5x ATR above EMA30
// — prevents buying overextended moves

// Entry price recorded as dHigh (the actual breakout level)

// ============================================================
// SELL SIGNAL CONDITIONS (all must be true)
// ============================================================

// 1. dojiActive: same as buy
// 2. not sellUsed: Sell signal hasn't fired yet for this Doji
// 3. low < dLow: Current candle's low breaks below Doji low
// 4. close < close[1]: Momentum confirmation downward
// 5. atrMult30 > ema30AtrLimitSell:
// Price is not more than -2.5x ATR below EMA30

// Entry price recorded as dLow (the actual breakdown level)

// ============================================================
// EXIT CONDITIONS (2-bar confirmation required)
// ============================================================

// For LONG positions (checked from bar after entry):
// Condition 1 - Back In Doji: close < eDHigh
// Price closes back inside the Doji range
// Condition 2 - Prev Close Broken: low < close[1] - exitOffset
// Current low drops X points below previous close
// Condition 3 - Hard Stop: low <= ePrice - hardStopPoints
// Price falls X points below entry price

// For SHORT positions:
// Condition 1 - Back In Doji: close > eDLow
// Condition 2 - Prev Close Broken: high > close[1] + exitOffset
// Condition 3 - Hard Stop: high >= ePrice + hardStopPoints

// 2-BAR CONFIRMATION: A pending flag is set when condition
// first triggers. Exit only fires if same condition is true
// on the NEXT bar too. If condition disappears, pending clears.
// This prevents premature exits on temporary price spikes.

// Exit price is recorded as:
// Back In Doji → current close
// Prev Close Broken → close[1] ± exitOffset
// Hard Stop → ePrice ± hardStopPoints

// ============================================================
// EMA LINES & ATR LABELS
// ============================================================

// Four EMAs plotted on chart:
// EMA5 → Yellow (fastest, most reactive)
// EMA10 → Aqua
// EMA20 → Orange
// EMA30 → Fuchsia (slowest, used for signal filter)

// ATR Multiple Labels (shown at right edge of chart):
// Format: "EMAx: N×"
// N = (close - EMAx) / ATR14
// Positive = price above EMA, Negative = price below EMA
// e.g. "EMA30: 1.5×" means price is 1.5 ATRs above EMA30
// e.g. "EMA30: -2.0×" means price is 2.0 ATRs below EMA30
// Dashboard ATR row turns RED if beyond buy/sell limits

// ============================================================
// DASHBOARD (top-right)
// ============================================================

// Position → LONG / SHORT / FLAT (current simulated state)
// Entry Price → Price at which current position was entered
// Doji Range → Most recent Doji's low - high levels
// Buy Pts Today→ Cumulative points from closed long trades today
// Sell Pts Today→ Cumulative points from closed short trades today
// Gap 5/10/20/30→ Raw point gap between close and each EMA
// ATR× 5/10/20/30→ Gap expressed as ATR multiples (RED if beyond limit)
// Avg Candle Range→ Average high-low range of all candles today (pts)

// ============================================================
// ALERTS
// ============================================================

// Four alert conditions available in TradingView:
// "Doji Buy" → BUY signal fired (high broke above Doji high)
// "Doji Sell" → SELL signal fired (low broke below Doji low)
// "Exit Long" → Long position exit triggered
// "Exit Short" → Short position exit triggered
//
// Recommended alert setting: "Once Per Bar Close"
// to avoid mid-candle repainting on live bars.

// ============================================================
// IMPORTANT NOTES
// ============================================================

// 1. This is an INDICATOR (not a strategy) — it simulates
// position tracking internally but places no real orders.
// Use alerts to trigger actual trades via broker/webhook.

// 2. Signals are close-based (except intrabar high/low checks
// for breakout detection and exit stop conditions).

// 3. One BUY and one SELL allowed per Doji independently.
// A new Doji resets both flags, allowing fresh signals.

// 4. Daily P&L (Buy Pts / Sell Pts) resets each new day.
// These are point-based, not currency/lot-size adjusted.

// 5. The indicator works on any timeframe and any symbol,
// but was designed for intraday NSE (Indian market) use.
// ATR and EMA values will vary by timeframe/instrument —
// calibrate ema30AtrLimitBuy/Sell accordingly.

// 6. Doji levels persist until the next Doji forms — signals
// can fire many bars after the Doji if conditions are met.
// There is no bar-count expiry window.

免責聲明

這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。