ADX/DMI Crossing/Delta Screener 3TF

DanubeMetrics · study · 541 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
 
 
indicator('ADX/DMI Crossing/Delta Screener 3TF V4', overlay = true, max_bars_back = 5000)
 
 
 
// ===== TF1 Inputs =====
dilenD        = input.int(14, 'DI Length TF1', minval=1, group="Timeframe 1 (TF1)", tooltip="Period for calculating +DI and -DI on TF1 (ta.dmi, RMA smoothing as in the TradingView standard DMI). Smaller values react faster but produce more noise. Standard: 14.")
adxlenD       = input.int(14, 'ADX Smoothing TF1', minval=1, maxval=50, group="Timeframe 1 (TF1)", tooltip="Smoothing length (RMA) of the ADX on TF1. Smaller values make the ADX react faster to trend changes. Standard: 14.")
adxThresholdD = input.float(20, 'ADX Threshold TF1', group="Timeframe 1 (TF1)", tooltip="Minimum ADX on TF1 for a trend signal (▲/▼ in the Signal column): ADX must be above this value, ADX must be rising and the DI difference must exceed 'Min. DI Diff.'. Also the minimum ADX for a DI line crossing the ADX from below. If an ADX crossing occurs below this value, the ADX cell is colored with 'ADX Below Threshold Background Color'.")
adxRiseMinD   = input.float(0.1, 'Min. ADX Delta TF1', minval=0.01, maxval=10, step=0.01, group="Timeframe 1 (TF1)", tooltip="Minimum ADX increase per bar on TF1 for 'rising ADX'. Each of the last n bars ('ADX Rise over n Bars TF1') must rise by at least this value. Required for the trend signal in the Signal column and for the rising-ADX path of the triple logic.")
adxRiseBarsD  = input.int(1, 'ADX Rise over n Bars TF1', minval=1, maxval=20, group="Timeframe 1 (TF1)", tooltip="Number of consecutive TF1 bars over which the ADX must rise by at least 'Min. ADX Delta TF1'. The same number of bars is used for the falling-ADX check ('Min. ADX Delta Falling TF1'). 1 = current bar only.")
adxCrossingThresholdD = input.float(20, 'ADX Crossing Threshold TF1', group="Timeframe 1 (TF1)", tooltip="Upper limit for the ADX crossing on TF1: the ADX crossing +DI or -DI from below only counts if the ADX is at or below this value (early trend start). Additionally the ADX must rise by at least 'ADX Delta Golden Crossing'. An ADX crossing colors the Signal cell with 'ADX Crossing Background Color'.")
 
 
 
// +++++++ new parameter TF1 for falling ADX
adxFallMinD = input.float(0.05, 'Min. ADX Delta Falling TF1', minval=0.01, maxval=10, step=0.01, group="Timeframe 1 (TF1)", tooltip="Minimum ADX decrease per bar on TF1 for 'falling ADX'. Each of the last n bars ('ADX Rise over n Bars TF1') must fall by more than this value. Only used in the triple signal logic (falling-ADX path), and only if 'TF1 Rising ADX' is not checked.")
 
 
 
// ===== TF2 Inputs =====
dilenW        = input.int(14, 'DI Length TF2', minval=1, group="Timeframe 2 (TF2)", tooltip="Period for calculating +DI and -DI on TF2 (ta.dmi, RMA smoothing as in the TradingView standard DMI). Smaller values react faster but produce more noise. Standard: 14.")
adxlenW       = input.int(14, 'ADX Smoothing TF2', minval=1, maxval=50, group="Timeframe 2 (TF2)", tooltip="Smoothing length (RMA) of the ADX on TF2. Smaller values make the ADX react faster to trend changes. Standard: 14.")
adxThresholdW = input.float(20, 'ADX Threshold TF2', group="Timeframe 2 (TF2)", tooltip="Minimum ADX on TF2 for a trend signal (▲/▼ in the Signal column): ADX must be above this value, ADX must be rising and the DI difference must exceed 'Min. DI Diff.'. Also the minimum ADX for a DI line crossing the ADX from below. If an ADX crossing occurs below this value, the ADX cell is colored with 'ADX Below Threshold Background Color'.")
adxRiseMinW   = input.float(0.1, 'Min. ADX Delta TF2', minval=0.01, maxval=10, step=0.01, group="Timeframe 2 (TF2)", tooltip="Minimum ADX increase per bar on TF2 for 'rising ADX'. Each of the last n bars ('ADX Rise over n Bars TF2') must rise by at least this value. Required for the trend signal in the Signal column and for the rising-ADX path of the triple logic.")
adxRiseBarsW  = input.int(1, 'ADX Rise over n Bars TF2', minval=1, maxval=20, group="Timeframe 2 (TF2)", tooltip="Number of consecutive TF2 bars over which the ADX must rise by at least 'Min. ADX Delta TF2'. The same number of bars is used for the falling-ADX check ('Min. ADX Delta Falling TF2'). 1 = current bar only.")
adxCrossingThresholdW = input.float(20, 'ADX Crossing Threshold TF2', group="Timeframe 2 (TF2)", tooltip="Upper limit for the ADX crossing on TF2: the ADX crossing +DI or -DI from below only counts if the ADX is at or below this value (early trend start). Additionally the ADX must rise by at least 'ADX Delta Golden Crossing'. An ADX crossing colors the Signal cell with 'ADX Crossing Background Color'.")
 
 
 
// +++++++ new parameter TF2 for falling ADX
adxFallMinW = input.float(0.05, 'Min. ADX Delta Falling TF2', minval=0.01, maxval=10, step=0.01, group="Timeframe 2 (TF2)", tooltip="Minimum ADX decrease per bar on TF2 for 'falling ADX'. Each of the last n bars ('ADX Rise over n Bars TF2') must fall by more than this value. Only used in the triple signal logic (falling-ADX path), and only if 'TF2 Rising ADX' is not checked.")
 
 
 
// ===== TF3 Inputs =====
dilenM        = input.int(14, 'DI Length TF3', minval=1, group="Timeframe 3 (TF3)", tooltip="Period for calculating +DI and -DI on TF3 (ta.dmi, RMA smoothing as in the TradingView standard DMI). Smaller values react faster but produce more noise. Standard: 14.")
adxlenM       = input.int(14, 'ADX Smoothing TF3', minval=1, maxval=50, group="Timeframe 3 (TF3)", tooltip="Smoothing length (RMA) of the ADX on TF3. Smaller values make the ADX react faster to trend changes. Standard: 14.")
adxThresholdM = input.float(20, 'ADX Threshold TF3', group="Timeframe 3 (TF3)", tooltip="Minimum ADX on TF3 for a trend signal (▲/▼ in the Signal column): ADX must be above this value, ADX must be rising and the DI difference must exceed 'Min. DI Diff.'. Also the minimum ADX for a DI line crossing the ADX from below. If an ADX crossing occurs below this value, the ADX cell is colored with 'ADX Below Threshold Background Color'.")
adxRiseMinM   = input.float(0.1, 'Min. ADX Delta TF3', minval=0.01, maxval=10, step=0.01, group="Timeframe 3 (TF3)", tooltip="Minimum ADX increase per bar on TF3 for 'rising ADX'. Each of the last n bars ('ADX Rise over n Bars TF3') must rise by at least this value. Required for the trend signal in the Signal column and for the rising-ADX path of the triple logic.")
adxRiseBarsM  = input.int(1, 'ADX Rise over n Bars TF3', minval=1, maxval=20, group="Timeframe 3 (TF3)", tooltip="Number of consecutive TF3 bars over which the ADX must rise by at least 'Min. ADX Delta TF3'. The same number of bars is used for the falling-ADX check ('Min. ADX Delta Falling TF3'). 1 = current bar only.")
adxCrossingThresholdM = input.float(20, 'ADX Crossing Threshold TF3', group="Timeframe 3 (TF3)", tooltip="Upper limit for the ADX crossing on TF3: the ADX crossing +DI or -DI from below only counts if the ADX is at or below this value (early trend start). Additionally the ADX must rise by at least 'ADX Delta Golden Crossing'. An ADX crossing colors the Signal cell with 'ADX Crossing Background Color'.")
 
 
 
// +++++++ new parameter TF3 for falling ADX
adxFallMinM = input.float(0.05, 'Min. ADX Delta Falling TF3', minval=0.01, maxval=10, step=0.01, group="Timeframe 3 (TF3)", tooltip="Minimum ADX decrease per bar on TF3 for 'falling ADX'. Each of the last n bars ('ADX Rise over n Bars TF3') must fall by more than this value. Only used in the triple signal logic (falling-ADX path), and only if 'TF3 Rising ADX' is not checked.")
 
 
 
// *** NEW: Common DMI crossing parameters (for all TFs) ***
diCrossThreshold   = input.float(18, "ADX Threshold for DMI Crossing", group="ADX/DMI Crossing Parameters", tooltip="Applies to all TFs. A DMI crossing (+DI crosses -DI or vice versa) with ADX at or above this value is a STRONG crossing (Trend cell: '+DI/-DI Crosses' colors). Below this value it is only a WEAK crossing if the ADX is rising ('Min. ADX Delta for DMI Crossing'), shown with the 'below threshold, but strong' colors.")
diCrossRiseMin     = input.float(0.05, "Min. ADX Delta for DMI Crossing", minval=0.01, maxval=10, step=0.01, group="ADX/DMI Crossing Parameters", tooltip="Applies to all TFs. Minimum ADX increase per bar required for a WEAK DMI crossing (crossing below 'ADX Threshold for DMI Crossing').")
diCrossRiseBars    = input.int(1, "ADX Rise over n Bars for DMI Crossing", minval=1, maxval=20, group="ADX/DMI Crossing Parameters", tooltip="Applies to all TFs. Number of bars (starting at the crossing bar) over which the ADX must rise by at least 'Min. ADX Delta for DMI Crossing' for a WEAK DMI crossing.")
diCrossBarsBack    = input.int(1, "Check DMI Cross over n Bars Back", minval=1, maxval=10, group="ADX/DMI Crossing Parameters", tooltip="Applies to all TFs. Number of bars (incl. the current bar) in which a DMI crossing is searched. 1 = only a crossing on the current bar counts. Larger values keep the crossing colors and the 2-TF DMI Cross active for longer.")
 
 
 
// *** NEW: Golden crossing ADX delta parameters ***
adxDeltaGoldenCross   = input.float(0.5, "ADX Delta Golden Crossing", minval=0.01, maxval=10, step=0.01, group="ADX/DMI Crossing Parameters", tooltip="Applies to all TFs. Minimum ADX increase per bar for an ADX (golden) crossing, i.e. the ADX crossing +DI or -DI from below at or below 'ADX Crossing Threshold'. Higher values only accept a strongly rising ADX.")
adxRiseBarsGoldenCross = input.int(1, "ADX Rise over n Bars for Golden Crossing", minval=1, maxval=20, group="ADX/DMI Crossing Parameters", tooltip="Applies to all TFs. Number of consecutive bars over which the ADX must rise by at least 'ADX Delta Golden Crossing' for an ADX (golden) crossing.")
 
 
 
// Common parameters
trendDiffMin = input.float(5, 'Min. DI Diff. for Trend/DMI Crossing', minval=0.1, maxval=60, step=0.1, tooltip="Applies to all TFs. Minimum distance between +DI and -DI. Trend signal (▲/▼ in the Signal column): +DI - -DI must be greater than this value (long) or smaller than its negative (short). DMI crossing: only counts if |+DI - -DI| is at least this value on the crossing bar.")
 
 
 
// ADX high threshold coloring
adxHighThreshold = input.float(40, 'ADX Upper Threshold for Coloring', tooltip="If the ADX is at or above this value, the ADX cell is colored with 'ADX High Background Color' (highest priority). Visual only, no effect on signals. Typical: 40 = very strong / possibly exhausted trend.")
adxHighCol       = input.color(color.new(#f57c00, 0), 'ADX High Background Color', group="Background Colors", tooltip="Background color of the ADX cell when the ADX is at or above 'ADX Upper Threshold for Coloring'.")
 
 
 
// Text color
txtCol       = input.color(color.white, 'Text Color', group="Text Colors", tooltip="Text color of the Trend and Signal columns.")
 
 
 
// Input field for symbol column (visual only, no logic)
symbolHeader = input.string('F-Screen 1-1', 'Symbol Column Title', group="Table", tooltip="Title of the first table column (symbol column). Visual only, no effect on the logic - e.g. to distinguish several screener instances.")
 
 
 
// Table position
tabPosition  = input.string('bottom_right', 'Table Position', options = ['top_right', 'top_left', 'bottom_right', 'bottom_left','middle_right', 'middle_left', 'top_center', 'bottom_center', 'middle_center'], group="Table", tooltip="Position of the screener table on the chart.")
 
 
 
// Colors
crossCol     = input.color(color.new(#5b9cf6, 0), "ADX Crossing Background Color", group="Background Colors", tooltip="Background color of the Signal cell when an ADX crossing occurs on that TF (ADX crosses a DI line, or a DI line crosses the ADX). Also used for the symbol cell if a triple signal is active and at least one TF has an ADX crossing.")
adxWeakCol   = input.color(color.new(#5b9cf6, 0), "ADX Below Threshold Background Color", group="Background Colors", tooltip="Background color of the ADX cell when an ADX crossing occurs on that TF while the ADX is below 'ADX Threshold TFx'.")
diBullCrossCol  = input.color(color.new(#1a9c00, 0), "Signal: +DI Crosses -DI Background Color", group="Background Colors", tooltip="Background color of the Trend cell for a STRONG bullish DMI crossing: +DI crosses above -DI with ADX at or above 'ADX Threshold for DMI Crossing'.")
diBearCrossCol  = input.color(color.new(#ff0095, 0), "Signal: -DI Crosses +DI Background Color", group="Background Colors", tooltip="Background color of the Trend cell for a STRONG bearish DMI crossing: -DI crosses above +DI with ADX at or above 'ADX Threshold for DMI Crossing'.")
diBullCrossWeakCol  = input.color(color.new(#00beab, 0), "Trend: +DI Crosses -DI (below threshold, but strong) Background Color", group="Background Colors", tooltip="Background color of the Trend cell for a WEAK bullish DMI crossing: +DI crosses above -DI with ADX below 'ADX Threshold for DMI Crossing', but the ADX is rising ('Min. ADX Delta for DMI Crossing').")
diBearCrossWeakCol  = input.color(color.new(#ffa600, 0), "Trend: -DI Crosses +DI (below threshold, but strong) Background Color", group="Background Colors", tooltip="Background color of the Trend cell for a WEAK bearish DMI crossing: -DI crosses above +DI with ADX below 'ADX Threshold for DMI Crossing', but the ADX is rising ('Min. ADX Delta for DMI Crossing').")
 
 
 
// *** NEW: Symbol column 2-TF DMI cross ***
group_symbol_dual = "Symbol Column: 2-TF DMI Cross"
dualBullCol = input.color(color.new(#0077ff, 0), "2-TF Bull DMI Cross Color", group=group_symbol_dual, tooltip="Background color of the symbol cell when a bullish DMI crossing (strong or weak) occurs on at least 2 of the 3 TFs at the same time. Has the highest priority in the symbol cell.")
dualBearCol = input.color(color.new(#ffa600, 0), "2-TF Bear DMI Cross Color", group=group_symbol_dual, tooltip="Background color of the symbol cell when a bearish DMI crossing (strong or weak) occurs on at least 2 of the 3 TFs at the same time. Used if there is no 2-TF bull DMI cross.")
 
 
 
// +++++++ V4.3: Fixed data source for futures (independent of chart settings)
group_data = "Data Source (Futures)"
useBadj   = input.bool(false, "Back-adjust Contract Changes (B-ADJ)", group=group_data, tooltip="Checked: the ADX/DMI of all TFs is calculated from back-adjusted continuous futures data (roll gaps removed, like the B-ADJ button on the chart). Unchecked: unadjusted data (roll gaps remain and can cause True Range spikes at contract changes). Applied identically on every chart timeframe, independent of the chart's B-ADJ setting. No effect on symbols without back-adjustment support.")
useSettle = input.bool(true, "Use Settlement as Close (D/W)", group=group_data, tooltip="Checked: daily and higher TFs use the exchange settlement price as close (TradingView default for futures). Unchecked: last traded price. Intraday TFs (e.g. 240) always use last prices. Applied identically on every chart timeframe, so D/W values no longer change between a 4h and a D/W chart.")
 
 
 
// Timeframes
tfD = input.timeframe('240', 'Timeframe 1', tooltip="Timeframe 1 (TF1), columns 1-4 of the table. Works on any chart timeframe: TFs higher than or equal to the chart TF show the current, still open bar (values can change until the bar closes); TFs lower than the chart TF show the last sub-bar of the chart bar.")
tfW = input.timeframe('D', 'Timeframe 2', tooltip="Timeframe 2 (TF2), columns 5-8 of the table. Works on any chart timeframe: TFs higher than or equal to the chart TF show the current, still open bar (values can change until the bar closes); TFs lower than the chart TF show the last sub-bar of the chart bar.")
tfM = input.timeframe('W', 'Timeframe 3', tooltip="Timeframe 3 (TF3), columns 9-12 of the table. Works on any chart timeframe: TFs higher than or equal to the chart TF show the current, still open bar (values can change until the bar closes); TFs lower than the chart TF show the last sub-bar of the chart bar.")
 
 
 
// *** NEW: Triple signal logic (AND/OR per TF) ***
group_triple = "Triple Signal Logic"
tfD_and = input.bool(false, "TF1: AND in Triple?", group=group_triple, tooltip="Checked: TF1 is a required (AND) condition for the triple signal (▲/▼ next to the symbol). As soon as at least one AND box is set, all AND timeframes must be true and timeframes without AND are ignored. A TF without 'Rising ADX' fulfills the condition with rising ADX / ADX crossing in trend direction OR with falling ADX in the opposite trend (fading counter-trend). If no AND box is set, the TFs are combined with OR (only rising ADX / ADX crossing).")
tfW_and = input.bool(true, "TF2: AND in Triple?", group=group_triple, tooltip="Checked: TF2 is a required (AND) condition for the triple signal (▲/▼ next to the symbol). As soon as at least one AND box is set, all AND timeframes must be true and timeframes without AND are ignored. A TF without 'Rising ADX' fulfills the condition with rising ADX / ADX crossing in trend direction OR with falling ADX in the opposite trend (fading counter-trend). If no AND box is set, the TFs are combined with OR (only rising ADX / ADX crossing).")
tfM_and = input.bool(true, "TF3: AND in Triple?", group=group_triple, tooltip="Checked: TF3 is a required (AND) condition for the triple signal (▲/▼ next to the symbol). As soon as at least one AND box is set, all AND timeframes must be true and timeframes without AND are ignored. A TF without 'Rising ADX' fulfills the condition with rising ADX / ADX crossing in trend direction OR with falling ADX in the opposite trend (fading counter-trend). If no AND box is set, the TFs are combined with OR (only rising ADX / ADX crossing).")
 
 
 
// +++++++ new checkboxes "TFx rising ADX"
tfD_rising_adx = input.bool(false, "TF1 Rising ADX", group=group_triple, tooltip="Checked: TF1 can only contribute to the triple signal with rising ADX / ADX crossing in trend direction - the falling-ADX path is disabled for TF1. Without any AND box, only the TFs checked here are considered (combined with OR).")
tfW_rising_adx = input.bool(true, "TF2 Rising ADX", group=group_triple, tooltip="Checked: TF2 can only contribute to the triple signal with rising ADX / ADX crossing in trend direction - the falling-ADX path is disabled for TF2. Without any AND box, only the TFs checked here are considered (combined with OR).")
tfM_rising_adx = input.bool(false, "TF3 Rising ADX", group=group_triple, tooltip="Checked: TF3 can only contribute to the triple signal with rising ADX / ADX crossing in trend direction - the falling-ADX path is disabled for TF3. Without any AND box, only the TFs checked here are considered (combined with OR).")
 
 
 
// Symbols
sym1  = input.symbol('CME:6N1!',        'Symbol 1', tooltip="Symbol in table row 1. All 3 TFs are calculated for this symbol.")
sym2  = input.symbol('CME:6S1!',        'Symbol 2', tooltip="Symbol in table row 2. All 3 TFs are calculated for this symbol.")
sym3  = input.symbol('CME:6Z1!',        'Symbol 3', tooltip="Symbol in table row 3. All 3 TFs are calculated for this symbol.")
sym4  = input.symbol('ICEUS:CC1!',      'Symbol 4', tooltip="Symbol in table row 4. All 3 TFs are calculated for this symbol.")
sym5  = input.symbol('NYMEX:CL1!',      'Symbol 5', tooltip="Symbol in table row 5. All 3 TFs are calculated for this symbol.")
sym6  = input.symbol('ICEUS:CT1!',      'Symbol 6', tooltip="Symbol in table row 6. All 3 TFs are calculated for this symbol.")
sym7  = input.symbol('CME:DC1!',        'Symbol 7', tooltip="Symbol in table row 7. All 3 TFs are calculated for this symbol.")
sym8  = input.symbol('CBOT_MINI:YM1!',  'Symbol 8', tooltip="Symbol in table row 8. All 3 TFs are calculated for this symbol.")
sym9  = input.symbol('CME_MINI:ES1!',   'Symbol 9', tooltip="Symbol in table row 9. All 3 TFs are calculated for this symbol.")
sym10 = input.symbol('COMEX:GC1!',      'Symbol 10', tooltip="Symbol in table row 10. All 3 TFs are calculated for this symbol.")
sym11 = input.symbol('CME:GF1!',        'Symbol 11', tooltip="Symbol in table row 11. All 3 TFs are calculated for this symbol.")
sym12 = input.symbol('COMEX:HG1!',      'Symbol 12', tooltip="Symbol in table row 12. All 3 TFs are calculated for this symbol.")
sym13 = input.symbol('NYMEX:HO1!',      'Symbol 13', tooltip="Symbol in table row 13. All 3 TFs are calculated for this symbol.")
 
 
 
// Table position helper function
f_table_pos(pos) =>
    pos == 'top_right' ? position.top_right : pos == 'top_left' ? position.top_left : pos == 'bottom_right' ? position.bottom_right : pos == 'bottom_left' ? position.bottom_left : pos == 'middle_right' ? position.middle_right : pos == 'middle_left' ? position.middle_left : pos == 'top_center' ? position.top_center : pos == 'bottom_center' ? position.bottom_center : position.middle_center
 
 
 
// *** Global auto logic (AFTER inputs) ***
und_count = (tfD_and ? 1 : 0) + (tfW_and ? 1 : 0) + (tfM_and ? 1 : 0)
 
 
 
// +++++++ new variable: count of "rising ADX" checkboxes
rising_adx_count = (tfD_rising_adx ? 1 : 0) + (tfW_rising_adx ? 1 : 0) + (tfM_rising_adx ? 1 : 0)
 
 
 
// *** CHANGED: New signature with DMI crossing parameters +++++++
// +++++++ new parameter adxFallMin_tf
f_adx_calc(dilen_tf, adxlen_tf, adxRiseMin_tf, adxFallMin_tf, adxRiseBars_tf, adxThreshold_tf, adxCrossingThreshold_tf, diCrossThreshold, diCrossRiseMin, diCrossRiseBars, diCrossBarsBack) =>
    // Calculate +DI, -DI and ADX using TradingView standard (RMA)
    [plus, minus, adx_raw] = ta.dmi(dilen_tf, adxlen_tf)
    plus  := fixnan(plus)
    minus := fixnan(minus)
    adx   = fixnan(adx_raw)
 
 
 
    adxDiff = adx - nz(adx[1], adx)
 
 
 
    // 1. ADXRiseMin for TREND signals (path 2) - remains strict
    adxRisingCont = true
    for i = 0 to adxRiseBars_tf - 1
        if (adx[i] - nz(adx[i+1], adx[i])) < adxRiseMin_tf
            adxRisingCont := false
            break
    adxRise = adxRisingCont ? 1 : 0
 
 
 
    // 1b. FALLING ADX for triple logic +++++++
    // +++++++ uses new parameter adxFallMin_tf instead of adxRiseMin_tf
    adxFallingCont = true
    for i = 0 to adxRiseBars_tf - 1
        if (nz(adx[i], adx) - nz(adx[i+1], adx[i])) > -adxFallMin_tf
            adxFallingCont := false
            break
    // +++++++ CHANGED: adxFalling as bool instead of int
    adxFalling = adxFallingCont
 
 
 
    // 2. Simple ADX rising (for crossings)
    adxRisingSimple0 = adx > nz(adx[1], adx)
    adxRisingSimple1 = adx[1] > nz(adx[2], adx[1])
 
 
 
    // 3. Golden ADX delta over lookback period
    adxGoldenRising = true
    for i = 0 to adxRiseBarsGoldenCross - 1
        if (adx[i] - nz(adx[i+1], adx[i])) < adxDeltaGoldenCross
            adxGoldenRising := false
            break
 
 
 
    // +++++++ V4.2: Golden ADX delta for crossings on the previous bar (shifted by 1 bar)
    adxGoldenRising1 = true
    for i = 0 to adxRiseBarsGoldenCross - 1
        if (adx[i+1] - nz(adx[i+2], adx[i+1])) < adxDeltaGoldenCross
            adxGoldenRising1 := false
            break
 
 
 
    // 4. Crossing conditions (ADX crosses DI from below, below adxCrossingThreshold_tf)
    crossPlusADX1  = adxRisingSimple0 and adxGoldenRising and (adx[1] <= plus[1])  and (adx[0] > plus[0])  and (adx[0] <= adxCrossingThreshold_tf)
    crossMinusADX1 = adxRisingSimple0 and adxGoldenRising and (adx[1] <= minus[1]) and (adx[0] > minus[0]) and (adx[0] <= adxCrossingThreshold_tf)
    crossPlusADX2  = adxRisingSimple1 and adxGoldenRising1 and (adx[2] <= plus[2])  and (adx[1] > plus[1])  and (adx[1] <= adxCrossingThreshold_tf)
    crossMinusADX2 = adxRisingSimple1 and adxGoldenRising1 and (adx[2] <= minus[2]) and (adx[1] > minus[1]) and (adx[1] <= adxCrossingThreshold_tf)
 
 
 
    crossDIplus1   = adxRisingSimple0 and (plus[1] <= adx[1])  and (plus[0] > adx[0])  and (adx[0] >= adxThreshold_tf)
    crossDIminus1  = adxRisingSimple0 and (minus[1] <= adx[1]) and (minus[0] > adx[0]) and (adx[0] >= adxThreshold_tf)
    crossDIplus2   = adxRisingSimple1 and (plus[2] <= adx[2])  and (plus[1] > adx[1])  and (adx[1] >= adxThreshold_tf)
    crossDIminus2  = adxRisingSimple1 and (minus[2] <= adx[2]) and (minus[1] > adx[1]) and (adx[1] >= adxThreshold_tf)
 
 
 
    adxCross = crossPlusADX1 or crossMinusADX1 or crossPlusADX2 or crossMinusADX2 or
               crossDIplus1 or crossDIminus1 or crossDIplus2 or crossDIminus2
 
 
 
    // DMI crossings with lookback check
    bool anyBullStrong = false
    bool anyBearStrong = false
    bool anyBullWeak   = false
    bool anyBearWeak   = false
 
 
 
    for i = 0 to diCrossBarsBack - 1
        // +++++++ V4.2: explicit crossover check instead of ta.crossover() inside the loop
        bullCrossThisBar = plus[i] > minus[i] and plus[i+1] <= minus[i+1]
        bearCrossThisBar = minus[i] > plus[i] and minus[i+1] <= plus[i+1]
 
 
 
        if bullCrossThisBar and math.abs(plus[i] - minus[i]) >= trendDiffMin
            adxRiseThisBar = true
            for j = 0 to diCrossRiseBars - 1
                shift = i + j
                if (adx[shift] - nz(adx[shift + 1], adx[shift])) < diCrossRiseMin
                    adxRiseThisBar := false
                    break
            if adx[i] >= diCrossThreshold
                anyBullStrong := true
            else if adxRiseThisBar
                anyBullWeak := true
 
 
 
        if bearCrossThisBar and math.abs(plus[i] - minus[i]) >= trendDiffMin
            adxRiseThisBar = true
            for j = 0 to diCrossRiseBars - 1
                shift = i + j
                if (adx[shift] - nz(adx[shift + 1], adx[shift])) < diCrossRiseMin
                    adxRiseThisBar := false
                    break
            if adx[i] >= diCrossThreshold
                anyBearStrong := true
            else if adxRiseThisBar
                anyBearWeak := true
 
 
 
    diBullCrossStrong = anyBullStrong
    diBearCrossStrong = anyBearStrong
    diBullCrossWeak   = anyBullWeak
    diBearCrossWeak   = anyBearWeak
 
 
 
    // +++++++ new return value adxFalling at the end (as bool)
    [plus, minus, adx, adxRise, adxDiff, adxCross, diBullCrossStrong, diBearCrossStrong, diBullCrossWeak, diBearCrossWeak, adxFalling]
 
 
 
// *** CHANGED: Pass new parameters to request.security +++++++
// +++++++ new parameter adxFallMin_tf in signature
f_get_dmi_adx_tf(sym, tf, dilen_tf, adxlen_tf, adxRiseMin_tf, adxFallMin_tf, adxRiseBars_tf, adxThreshold_tf, adxCrossingThreshold_tf) =>
    // +++++++ V4.1: Timeframe independence
    // Requested TF >= chart TF -> lookahead_on  (current, still open higher-TF bar, as before)
    // Requested TF <  chart TF -> lookahead_off (LAST intrabar of the chart bar instead of the first one)
    // -> table shows the same values on every chart timeframe
    tfLookahead = timeframe.in_seconds(tf) < timeframe.in_seconds(timeframe.period) ? barmerge.lookahead_off : barmerge.lookahead_on
    // +++++++ adxFallMin_tf is passed to f_adx_calc
    // +++++++ V4.3: fixed B-ADJ / settlement setting instead of inheriting the chart settings
    symMod = ticker.modify(sym, backadjustment = useBadj ? backadjustment.on : backadjustment.off, settlement_as_close = useSettle ? settlement_as_close.on : settlement_as_close.off)
    request.security(symMod, tf, f_adx_calc(dilen_tf, adxlen_tf, adxRiseMin_tf, adxFallMin_tf, adxRiseBars_tf, adxThreshold_tf, adxCrossingThreshold_tf, diCrossThreshold, diCrossRiseMin, diCrossRiseBars, diCrossBarsBack), lookahead=tfLookahead)
 
 
 
// Trend label
trend_label(p, m) =>
    bull = p > m
    bear = m > p
    // +++++++ V4.2: +DI == -DI -> Neutral instead of Bearish
    [bull ? '▲ Bullish' : bear ? '▼ Bearish' : '■ Neutral', bull ? color.green : bear ? color.red : color.gray]
 
 
 
// Initialize table
var table t = table.new(f_table_pos(tabPosition), 13, 14, border_width = 1)
 
 
 
// Header row
if barstate.isfirst
    table.cell(t, 0, 0, symbolHeader, bgcolor=color.new(#ffffff, 0), text_size=size.small)
    table.cell(t, 1, 0, 'Trend ' + tfD,   bgcolor=color.new(#21f3d0, 0), text_size=size.small)
    table.cell(t, 2, 0, 'Signal ' + tfD,  bgcolor=color.new(#21f3d0, 0), text_size=size.small)
    table.cell(t, 3, 0, 'ADX ' + tfD,     bgcolor=color.new(#21f3d0, 0), text_size=size.small)
    table.cell(t, 4, 0, 'ADX Δ ' + tfD,   bgcolor=color.new(#21f3d0, 0), text_size=size.small)
    table.cell(t, 5, 0, 'Trend ' + tfW,   bgcolor=color.new(#ffd900, 0), text_size=size.small)
    table.cell(t, 6, 0, 'Signal ' + tfW,  bgcolor=color.new(#ffd900, 0), text_size=size.small)
    table.cell(t, 7, 0, 'ADX ' + tfW,     bgcolor=color.new(#ffd900, 0), text_size=size.small)
    table.cell(t, 8, 0, 'ADX Δ ' + tfW,   bgcolor=color.new(#ffd900, 0), text_size=size.small)
    table.cell(t, 9, 0,  'Trend ' + tfM,  bgcolor=color.new(#fffc3b, 0), text_size=size.small)
    table.cell(t, 10, 0, 'Signal ' + tfM, bgcolor=color.new(#fffc3b, 0), text_size=size.small)
    table.cell(t, 11, 0, 'ADX ' + tfM,    bgcolor=color.new(#fffc3b, 0), text_size=size.small)
    table.cell(t, 12, 0, 'ADX Δ ' + tfM,  bgcolor=color.new(#fffc3b, 0), text_size=size.small)
 
 
 
// Symbol logic & fill table
f_set_symbol(sym, row) =>
    // +++++++ CHANGED: 11 values per timeframe + new return value adxFalling
    [pD_val, mD_val, aD_val, rD_val, dD_val, cD_val, diBullCrossStrongD, diBearCrossStrongD, diBullCrossWeakD, diBearCrossWeakD, adxFallingD] = f_get_dmi_adx_tf(sym, tfD, dilenD, adxlenD, adxRiseMinD, adxFallMinD, adxRiseBarsD, adxThresholdD, adxCrossingThresholdD)
    [pW, mW, aW, rW, dW, cW, diBullCrossStrongW, diBearCrossStrongW, diBullCrossWeakW, diBearCrossWeakW, adxFallingW] = f_get_dmi_adx_tf(sym, tfW, dilenW, adxlenW, adxRiseMinW, adxFallMinW, adxRiseBarsW, adxThresholdW, adxCrossingThresholdW)
    [pM, mM, aM, rM, dM, cM, diBullCrossStrongM, diBearCrossStrongM, diBullCrossWeakM, diBearCrossWeakM, adxFallingM] = f_get_dmi_adx_tf(sym, tfM, dilenM, adxlenM, adxRiseMinM, adxFallMinM, adxRiseBarsM, adxThresholdM, adxCrossingThresholdM)
 
 
 
    // 2. *** 2-TF BLOCK ***
    bullCrossCount = (diBullCrossStrongD or diBullCrossWeakD ? 1 : 0) + 
                     (diBullCrossStrongW or diBullCrossWeakW ? 1 : 0) + 
                     (diBullCrossStrongM or diBullCrossWeakM ? 1 : 0)
 
 
 
    bearCrossCount = (diBearCrossStrongD or diBearCrossWeakD ? 1 : 0) + 
                     (diBearCrossStrongW or diBearCrossWeakW ? 1 : 0) + 
                     (diBearCrossStrongM or diBearCrossWeakM ? 1 : 0)
 
 
 
    dualTF_bullCross = bullCrossCount >= 2
    dualTF_bearCross = bearCrossCount >= 2
 
 
 
    [tD, bgD] = trend_label(pD_val, mD_val)
    [tW, bgW] = trend_label(pW, mW)
    [tM, bgM] = trend_label(pM, mM)
 
 
 
   // *** NEW: Trend background with priority ***
   // 1) strong cross (above threshold) -> diBullCrossCol / diBearCrossCol
   // 2) weak but confirmed cross (below threshold, adxRise == 1) -> weak colors
   // 3) otherwise default trend color from trend_label
    bgTrendD = diBullCrossStrongD ? diBullCrossCol : diBearCrossStrongD ? diBearCrossCol : diBullCrossWeakD ? diBullCrossWeakCol : diBearCrossWeakD ? diBearCrossWeakCol : bgD
    bgTrendW = diBullCrossStrongW ? diBullCrossCol : diBearCrossStrongW ? diBearCrossCol : diBullCrossWeakW ? diBullCrossWeakCol : diBearCrossWeakW ? diBearCrossWeakCol : bgW
    bgTrendM = diBullCrossStrongM ? diBullCrossCol : diBearCrossStrongM ? diBearCrossCol : diBullCrossWeakM ? diBullCrossWeakCol : diBearCrossWeakM ? diBearCrossWeakCol : bgM
 
 
 
    // TF1 - signal logic unchanged
    bullD = pD_val > mD_val
    bearD = mD_val > pD_val
    crossD = cD_val
    sigD_pfeil = (crossD and bullD) or ((aD_val > adxThresholdD) and (rD_val != 0) and ((pD_val - mD_val) > trendDiffMin))
    shortD_pfeil = (crossD and bearD) or ((aD_val > adxThresholdD) and (rD_val != 0) and ((pD_val - mD_val) < -trendDiffMin))
    bgSigD = crossD ? crossCol : sigD_pfeil ? color.green : shortD_pfeil ? color.red : color.silver
 
 
 
    // TF2 - signal logic unchanged
    bullW = pW > mW
    bearW = mW > pW
    crossW = cW
    sigW_pfeil = (crossW and bullW) or ((aW > adxThresholdW) and (rW != 0) and ((pW - mW) > trendDiffMin))
    shortW_pfeil = (crossW and bearW) or ((aW > adxThresholdW) and (rW != 0) and ((pW - mW) < -trendDiffMin))
    bgSigW = crossW ? crossCol : sigW_pfeil ? color.green : shortW_pfeil ? color.red : color.silver
 
 
 
    // TF3 - signal logic unchanged
    bullM = pM > mM
    bearM = mM > pM
    crossM = cM
    sigM_pfeil = (crossM and bullM) or ((aM > adxThresholdM) and (rM != 0) and ((pM - mM) > trendDiffMin))
    shortM_pfeil = (crossM and bearM) or ((aM > adxThresholdM) and (rM != 0) and ((pM - mM) < -trendDiffMin))
    bgSigM = crossM ? crossCol : sigM_pfeil ? color.green : shortM_pfeil ? color.red : color.silver
 
 
 
        // *** NEW: Falling ADX in triple logic +++++++
    // bullish: falling ADX + DI- above DI+
    // bearish: falling ADX + DI+ above DI-
    sigD_fall_long  = adxFallingD and (mD_val > pD_val)
    sigW_fall_long  = adxFallingW and (mW > pW)
    sigM_fall_long  = adxFallingM and (mM > pM)
    sigD_fall_short = adxFallingD and (pD_val > mD_val)
    sigW_fall_short = adxFallingW and (pW > mW)
    sigM_fall_short = adxFallingM and (pM > mM)
 
 
 
    // +++++++ TF-specific base signals
    sigD_rise_long  = (bullD and sigD_pfeil) or (crossD and bullD)
    sigW_rise_long  = (bullW and sigW_pfeil) or (crossW and bullW)
    sigM_rise_long  = (bullM and sigM_pfeil) or (crossM and bullM)
    sigD_rise_short = (bearD and shortD_pfeil) or (crossD and bearD)
    sigW_rise_short = (bearW and shortW_pfeil) or (crossW and bearW)
    sigM_rise_short = (bearM and shortM_pfeil) or (crossM and bearM)
 
 
 
    // +++++++ TF-specific combined signals
    sigD_allow_long  = tfD_rising_adx ? sigD_rise_long  : (sigD_rise_long  or sigD_fall_long)
    sigW_allow_long  = tfW_rising_adx ? sigW_rise_long  : (sigW_rise_long  or sigW_fall_long)
    sigM_allow_long  = tfM_rising_adx ? sigM_rise_long  : (sigM_rise_long  or sigM_fall_long)
    sigD_allow_short = tfD_rising_adx ? sigD_rise_short : (sigD_rise_short or sigD_fall_short)
    sigW_allow_short = tfW_rising_adx ? sigW_rise_short : (sigW_rise_short or sigW_fall_short)
    sigM_allow_short = tfM_rising_adx ? sigM_rise_short : (sigM_rise_short or sigM_fall_short)
 
 
 
    // +++++++ V4.2: Unified triple logic (recalculated on every bar, no "var")
    // - No AND box:  OR over the TFs with "Rising ADX" (if none checked: all TFs),
    //                only rising ADX / ADX cross counts
    // - AND boxes:   all AND TFs must be true, TFs without AND are ignored.
    //                Each TF uses sig*_allow_*: with "Rising ADX" only rising ADX / ADX cross,
    //                otherwise rising ADX / ADX cross OR falling ADX + DI condition
    bool tripleSig   = false
    bool tripleShort = false
 
 
 
    if und_count == 0
        if rising_adx_count == 0
            tripleSig   := sigD_rise_long  or sigW_rise_long  or sigM_rise_long
            tripleShort := sigD_rise_short or sigW_rise_short or sigM_rise_short
        else
            tripleSig   := (tfD_rising_adx and sigD_rise_long)  or (tfW_rising_adx and sigW_rise_long)  or (tfM_rising_adx and sigM_rise_long)
            tripleShort := (tfD_rising_adx and sigD_rise_short) or (tfW_rising_adx and sigW_rise_short) or (tfM_rising_adx and sigM_rise_short)
    else
        tripleSig   := (not tfD_and or sigD_allow_long)  and (not tfW_and or sigW_allow_long)  and (not tfM_and or sigM_allow_long)
        tripleShort := (not tfD_and or sigD_allow_short) and (not tfW_and or sigW_allow_short) and (not tfM_and or sigM_allow_short)
 
 
 
    // +++++++ V4.2: long AND short at the same time (conflicting TFs, OR modes only) -> neutral "◆"
    tripleConflict = tripleSig and tripleShort
    symLabel = tripleConflict ? (sym + " ◆") : tripleSig ? (sym + " ▲") : tripleShort ? (sym + " ▼") : sym
    symColor = tripleConflict ? color.rgb(120, 0, 200) : tripleSig ? color.rgb(0, 255, 42) : tripleShort ? color.rgb(255, 0, 0) : color.black
 
 
 
    crossAny = crossD or crossW or crossM
    bgSymbol = dualTF_bullCross ? dualBullCol : dualTF_bearCross ? dualBearCol : (tripleSig or tripleShort) and crossAny ? crossCol : (tripleSig or tripleShort) ? color.silver : color.silver                            
 
 
 
    bgDiffD = na(dD_val) ? color.black : dD_val > 0 ? color.new(color.green, 70) : color.new(color.red, 70)
    bgDiffW = na(dW)     ? color.black : dW > 0     ? color.new(color.green, 70) : color.new(color.red, 70)
    bgDiffM = na(dM)     ? color.black : dM > 0     ? color.new(color.green, 70) : color.new(color.red, 70)
 
 
 
    // ADX high threshold takes priority
    adxBgD = aD_val >= adxHighThreshold ? adxHighCol : (crossD and aD_val < adxThresholdD) ? adxWeakCol : color.black
    adxBgW = aW >= adxHighThreshold ? adxHighCol : (crossW and aW < adxThresholdW) ? adxWeakCol : color.black
    adxBgM = aM >= adxHighThreshold ? adxHighCol : (crossM and aM < adxThresholdM) ? adxWeakCol : color.black
 
 
 
    // +++++++ V4.2: write table cells on the last bar only (faster loading)
    if barstate.islast
        table.cell(t, 0, row, symLabel, bgcolor=bgSymbol, text_color=symColor, text_size=size.small)
        table.cell(t, 1, row, tD, bgcolor=bgTrendD, text_color=txtCol, text_size=size.small)
        table.cell(t, 2, row, sigD_pfeil ? '▲' : shortD_pfeil ? '▼' : '', bgcolor=bgSigD, text_color=txtCol, text_size=size.small)
        table.cell(t, 3, row, na(aD_val) ? '-' : str.tostring(aD_val, '#.##'), bgcolor=adxBgD, text_color=color.white, text_size=size.small)
        table.cell(t, 4, row, na(dD_val) ? '-' : str.tostring(dD_val, '#0.00'), bgcolor=bgDiffD, text_color=color.white, text_size=size.small)
        table.cell(t, 5, row, tW, bgcolor=bgTrendW, text_color=txtCol, text_size=size.small)
        table.cell(t, 6, row, sigW_pfeil ? '▲' : shortW_pfeil ? '▼' : '', bgcolor=bgSigW, text_color=txtCol, text_size=size.small)
        table.cell(t, 7, row, na(aW) ? '-' : str.tostring(aW, '#.##'), bgcolor=adxBgW, text_color=color.white, text_size=size.small)
        table.cell(t, 8, row, na(dW) ? '-' : str.tostring(dW, '#0.00'), bgcolor=bgDiffW, text_color=color.white, text_size=size.small)
        table.cell(t, 9, row, tM, bgcolor=bgTrendM, text_color=txtCol, text_size=size.small)
        table.cell(t, 10, row, sigM_pfeil ? '▲' : shortM_pfeil ? '▼' : '', bgcolor=bgSigM, text_color=txtCol, text_size=size.small)
        table.cell(t, 11, row, na(aM) ? '-' : str.tostring(aM, '#.##'), bgcolor=adxBgM, text_color=color.white, text_size=size.small)
        table.cell(t, 12, row, na(dM) ? '-' : str.tostring(dM, '#0.00'), bgcolor=bgDiffM, text_color=color.white, text_size=size.small)
 
 
 
// Calculate symbols & fill table
f_set_symbol(sym1, 1)
f_set_symbol(sym2, 2)
f_set_symbol(sym3, 3)
f_set_symbol(sym4, 4)
f_set_symbol(sym5, 5)
f_set_symbol(sym6, 6)
f_set_symbol(sym7, 7)
f_set_symbol(sym8, 8)
f_set_symbol(sym9, 9)
f_set_symbol(sym10, 10)
f_set_symbol(sym11, 11)
f_set_symbol(sym12, 12)
f_set_symbol(sym13, 13)

← 返回列表