1. Component Deconstruction¶
Parabolic SAR (PSAR)¶
Specific Configuration:
Price Source: Implicitly uses
highandlowof the bar, as is standard for theta.sarfunction.Start (Initial Acceleration Factor):
0.02(configurable viastartinput).Increment (Acceleration Step):
0.02(configurable viaincrementinput).Maximum (Acceleration Factor Cap):
0.20(configurable viamaximuminput).
Functional Modification:
The script does not modify the internal mathematics of the PSAR calculation. It utilizes the standard
ta.sarfunction.Its primary functional adaptation is in signal generation. Instead of using the PSAR’s direction as the signal itself, it uses the crossover event between the
closeprice and the calculatedpsarvalue (ta.crossover(close, psar)andta.crossunder(close, psar)). This defines the raw, unfiltered entry trigger.
Exponential Moving Average (EMA)¶
Specific Configuration:
Price Source:
closeprice of the bar.Smoothing Period (Length):
50(configurable viaemaLengthinput).
Functional Modification:
This is a standard EMA calculation (
ta.ema).It is not used as a signal generator (e.g., via crossovers) but as a binary state filter. Its function is to define the permissible trade direction. For a long signal to be valid, the
closemust be above the EMA. For a short signal, theclosemust be below it. This transforms the continuous EMA value into a booleantrue/falsecondition for trend alignment.
Average Directional Index (ADX)¶
Specific Configuration:
DMI/ADX Period:
14(configurable viaadxLengthinput). The script uses the same length for both the Directional Movement calculation (+DI/-DI) and the subsequent ADX smoothing, which is a standard implementation.Price Source: Implicitly uses
high,low, andclosefor the underlying True Range and Directional Movement calculations.
Functional Modification:
The script uses the standard
ta.dmifunction, extracting only the third return value,adxValue.Its function is not to determine trend direction but to quantify trend intensity or strength. It acts as a “regime filter” by comparing the calculated
adxValueagainst a static threshold (adxMin, default 15.0). The script only considers signals valid if the ADX is above this minimum threshold, effectively deactivating the strategy in low-momentum, range-bound environments. The checknot na(adxValue)is included to prevent logic errors during the initial calculation period of the indicator.
2. Logic Layering & Confluence¶
The script’s engine is a hierarchical filtering cascade designed to increase the signal-to-noise ratio of the raw PSAR flips.
Interaction Dynamics:
Confluence: The core mechanic is the requirement for simultaneous agreement between multiple, distinct market metrics. A valid signal is not just a PSAR flip but a PSAR flip that occurs within a pre-qualified market environment.
Threshold Crosses: The engine relies on two primary threshold-crossing events:
Price/Indicator Cross: The
closeprice crossing over or under thepsarvalue. This is the tactical trigger.Indicator/Value Cross: The
adxValuebeing greater than or equal to theadxMinconstant. This is the strategic strength filter.
Divergences: The script does not calculate or look for any form of price-oscillator divergence.
Hierarchical Filtering: The logic is processed in a sequence where each filter acts as a “gatekeeper” for the next. A signal must pass through all active gates to be considered valid.
Primary Trigger (Raw Signal): The process begins with a
rawBuySignalorrawSellSignal, generated by acloseprice cross of the PSAR. This is the most frequent and unfiltered event.Gatekeeper 1: Trend Direction Filter (EMA): The raw signal is immediately tested against the EMA filter (
trendLongOk/trendShortOk).If
useEMAFilteristrue, arawBuySignalis invalidated unlessclose > emaValue.If
useEMAFilteristrue, arawSellSignalis invalidated unlessclose < emaValue.This layer ensures the tactical entry is aligned with the broader, medium-term market current.
Gatekeeper 2: Trend Strength Filter (ADX): If the signal passes the trend direction filter, it is then tested against the ADX strength filter (
strengthOk).If
useADXFilteristrue, any signal (long or short) is invalidated unlessadxValue >= adxMin.This layer acts as a regime filter, preventing trades during periods of low momentum where PSAR is prone to whipsaws.
Gatekeeper 3 (Optional): Micro-Confirmation Filters:
Candle Confirmation (
candleLongOk/candleShortOk): If enabled, requires the signal bar itself to be bullish (close > open) for a long signal or bearish (close < open) for a short signal. This adds a layer of immediate momentum confirmation.Signal Cooldown (
cooldownOk): If enabled, prevents a new signal from firing within a specified number of bars (cooldownBars) after a previous valid signal. This is managed by thelastSignalBarstate variable and prevents signal “spam” during choppy price action around the PSAR level.
Final Confirmation (
barConfirmed): By default (confirmOnClose = true), the entire logic is only evaluated on the close of a bar. This prevents signals from triggering and then disappearing intra-bar (repainting), ensuring the final signal is based on a permanent bar state.
3. The Execution Engine¶
The final buySignal and sellSignal are boolean variables that resolve to true only when a cascading series of conditions are met.
Boolean Logic: The execution trigger is a logical
ANDoperation across the raw signal and all enabled filter outputs.For a
buySignalto betrue:rawBuySignal(Price crosses above PSAR)ANDtrendLongOk(useEMAFilteris false ORclose > emaValue)ANDstrengthOk(useADXFilteris false ORadxValue >= adxMin)ANDcandleLongOk(useCandleFilteris false ORclose > open)ANDcooldownOk(useCooldownis false ORbar_index - lastSignalBar > cooldownBars)ANDbarConfirmed(confirmOnCloseis false ORbarstate.isconfirmedis true)The logic for
sellSignalis symmetrical. The use ofnot use[Filter] or [condition]is a standard and efficient way to make each filter optional via its corresponding input toggle.Mathematical Constants:
emaLength = 50: A 50-period EMA is a widely recognized proxy for the intermediate-term trend. Its selection establishes the timeframe for the “trend alignment” filter. A shorter length would make it more responsive but less effective as a macro filter; a longer length would make it more stable but slower to react to trend changes.adxLength = 14: This is the standard lookback period for ADX as defined by its creator, J. Welles Wilder Jr. It represents a balance between responsiveness and smoothness.adxMin = 15.0: This is a critical threshold that directly governs the script’s selectivity. An ADX below 20 is often considered indicative of a weak or non-existent trend. Setting the minimum at 15.0 is a relatively permissive configuration, designed to filter out only the most directionless, choppy markets while still allowing entries in the early stages of a developing trend. Increasing this value would make the script more selective, resulting in fewer but potentially higher-probability signals.cooldownBars = 1: A value of 1 ensures that signals cannot occur on consecutive bars. This forces a one-bar “refractory period” after a signal, which can help avoid immediate re-entries if price hovers exactly at the PSAR level for a brief period.