Perfected FlowScript
ADX like PineScript
ADX with +DI/-DI — Perfected Edition is indexed as 180_adx_like_pinescript.flowscript. The exact FlowScript is viewable here; chart observe and trading automation use the current mmflow chart/recipe signal proxy until FlowScript v2 transpilation is available.
This exact FlowScript uses the v2 source dialect. It is viewable and connected to the chart/automation proxy, but direct editor execution needs the FlowScript v2 transpiler.
Dialect
flowscript-v2
Compatibility
flowscript-v2-needs-transpile
Position
offchart
Axis
true
Size
12.9 KB
Lines
424
Native Signal Candidates
5 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.
DI Cross Up
line 338:9
Trigger
(showShapes && showDI) && (diCrossUp)
Value
plusDI
Deps
diCrossUp:var, showDI:input, showShapes:input
DI Cross Down
line 348:9
Trigger
(showShapes && showDI) && (diCrossDown)
Value
minusDI
Deps
diCrossDown:var, showDI:input, showShapes:input
ADX Strong
line 362:9
Trigger
(showThreshShapes && showAdx) && (adxStrongUp)
Value
adx
Deps
adxStrongUp:var, showAdx:input, showThreshShapes:input
ADX Extreme
line 372:9
Trigger
(showThreshShapes && showAdx) && (adxExtremeUp)
Value
adx
Deps
adxExtremeUp:var, showAdx:input, showThreshShapes:input
ADX Weak
line 382:9
Trigger
(showThreshShapes && showAdx) && (adxWeakDown)
Value
adx
Deps
adxWeakDown:var, showAdx:input, showThreshShapes:input
180_adx_like_pinescript.flowscript
source path: kscripts/perfected/180_adx_like_pinescript.flowscript · sha256:475261578f05a6940bfd195d20d12fb0e3970ec608a7a3ebb4bf4f5ad4103347
//@version=2
// Source: kiyotaka.ai/hub/kscript (original) — Perfected by kScript_Perfection_Engineer
// ============================================================================
// ADX Like PineScript — Average Directional Index with +DI/-DI [Perfected]
// Author: D (@Danny Rodriguez) — Perfected by kScript_Perfection_Engineer
// Tags: SPECIALIZED, CORE, #ADX, #DMI, #trendstrength, #directionalmovement
//
// Production-quality ADX implementation with full directional movement index
// (+DI/-DI), trend strength regime detection, dynamic color coding, fill
// between directional indicators, and comprehensive shape markers for
// crossovers and threshold events. Properly implements Wilder's DMI system
// with RMA smoothing and stateful divergence/tracking via static variables.
//
// Research papers underpinning this design:
// - Wilder (1978), "New Concepts in Technical Trading Systems": ADX, +DI, -DI
// invented here. ADX = SMA(DX, n) where DX = |+DI - -DI| / |+DI + -DI| × 100.
// - Rahman (2024), "Optimising Supertrend Parameters using Bayesian Optimisation":
// ATR-based trend indicators effective with proper parameter optimization.
// - Ponnusamy & Manual (2016): ADX < 20 = weak/ranging; ADX > 40 = strong;
// ADX > 50 = near exhaustion. Validates threshold-based trend strength.
// - Chande & Kroll (1994), "The New Technical Trader": momentum oscillator
// design principles; regime detection via threshold crossings.
// ============================================================================
define(
title="ADX with +DI/-DI — Perfected Edition",
position="offchart",
axis=true
)
// ---------------------------------------------------------------------------
// 1. USER INPUTS — grouped with constraints
// ---------------------------------------------------------------------------
// --- DI Settings ---
var len = input(
name="len",
type="number",
defaultValue=14,
label="DI Length",
constraints={min: 5, max: 50, step: 1},
group="DI Settings"
)
var adxLen = input(
name="adxLen",
type="number",
defaultValue=14,
label="ADX Smoothing Length",
constraints={min: 5, max: 50, step: 1},
group="DI Settings"
)
// --- Thresholds ---
var weakLevel = input(
name="weakLevel",
type="number",
defaultValue=20,
label="Weak Trend Level",
constraints={min: 5, max: 40, step: 1},
group="Thresholds"
)
var strongLevel = input(
name="strongLevel",
type="number",
defaultValue=40,
label="Strong Trend Level",
constraints={min: 25, max: 60, step: 1},
group="Thresholds"
)
var extremeLevel = input(
name="extremeLevel",
type="number",
defaultValue=50,
label="Extreme/Exhaustion Level",
constraints={min: 35, max: 70, step: 1},
group="Thresholds"
)
// --- Display ---
var showAdx = input(
name="showAdx",
type="boolean",
defaultValue=true,
label="Show ADX Line",
group="Display"
)
var showDI = input(
name="showDI",
type="boolean",
defaultValue=true,
label="Show +DI/-DI Lines",
group="Display"
)
var showFill = input(
name="showFill",
type="boolean",
defaultValue=true,
label="Show DI Fill",
group="Display"
)
var showShapes = input(
name="showShapes",
type="boolean",
defaultValue=true,
label="Show Crossover Shapes",
group="Display"
)
var showThreshShapes = input(
name="showThreshShapes",
type="boolean",
defaultValue=true,
label="Show Threshold Shapes",
group="Display"
)
var lineWidth = input(
name="lineWidth",
type="number",
defaultValue=2,
label="Line Width",
constraints={min: 1, max: 4, step: 1},
group="Display"
)
var shapeSize = input(
name="shapeSize",
type="number",
defaultValue=2,
label="Shape Size",
constraints={min: 1, max: 4, step: 1},
group="Display"
)
// --- Colors ---
var adxColorWeak = input(
name="adxColorWeak",
type="color",
defaultValue="#ffa726",
label="ADX Weak Color",
group="Colors"
)
var adxColorStrong = input(
name="adxColorStrong",
type="color",
defaultValue="#42a5f5",
label="ADX Strong Color",
group="Colors"
)
var adxColorExtreme = input(
name="adxColorExtreme",
type="color",
defaultValue="#d500f9",
label="ADX Extreme Color",
group="Colors"
)
var dipColor = input(
name="dipColor",
type="color",
defaultValue="#26a69a",
label="+DI Color",
group="Colors"
)
var dinColor = input(
name="dinColor",
type="color",
defaultValue="#ef5350",
label="-DI Color",
group="Colors"
)
var crossBullColor = input(
name="crossBullColor",
type="color",
defaultValue="#00e676",
label="Bullish Cross Color (+DI over -DI)",
group="Colors"
)
var crossBearColor = input(
name="crossBearColor",
type="color",
defaultValue="#ff5252",
label="Bearish Cross Color (-DI over +DI)",
group="Colors"
)
// ---------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// ---------------------------------------------------------------------------
timeseries price = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries close = price.close
timeseries high = price.high
timeseries low = price.low
if (isnan(close[0]) || isnan(high[0]) || isnan(low[0])) {
return
}
// ---------------------------------------------------------------------------
// 3. DIRECTIONAL MOVEMENT
// ---------------------------------------------------------------------------
// Per Wilder (1978): +DM = upMove > dnMove && upMove > 0 ? upMove : 0
// -DM = dnMove > upMove && dnMove > 0 ? dnMove : 0
timeseries upMove = high - high[1]
timeseries dnMove = low[1] - low
timeseries plusDM = (upMove > dnMove && upMove > 0) ? upMove : 0
timeseries minusDM = (dnMove > upMove && dnMove > 0) ? dnMove : 0
// ---------------------------------------------------------------------------
// 4. TRUE RANGE & WILDER SMOOTHING (RMA)
// ---------------------------------------------------------------------------
timeseries tr = math.max(
high - low,
math.max(
math.abs(high - close[1]),
math.abs(low - close[1])
)
)
// RMA = Wilder's smoothed moving average (EMA with alpha = 1/N)
timeseries atr = rma(tr, len)
timeseries smoothPlusDM = rma(plusDM, len)
timeseries smoothMinusDM = rma(minusDM, len)
// ---------------------------------------------------------------------------
// 5. DIRECTIONAL INDICATORS & DX / ADX
// ---------------------------------------------------------------------------
// +DI = 100 × smoothed +DM / ATR; -DI = 100 × smoothed -DM / ATR
timeseries plusDI = 100.0 * smoothPlusDM / math.max(atr, 0.0001)
timeseries minusDI = 100.0 * smoothMinusDM / math.max(atr, 0.0001)
// DX = 100 × |+DI - -DI| / |+DI + -DI|
timeseries dxSum = math.abs(plusDI - minusDI)
timeseries dxRange = plusDI + minusDI
timeseries dx = 100.0 * dxSum / math.max(dxRange, 0.0001)
// ADX = RMA of DX over adxLen
timeseries adx = rma(dx, adxLen)
// ---------------------------------------------------------------------------
// 6. REGIME DETECTION
// ---------------------------------------------------------------------------
// ADX trend strength classification
var adxWeak = adx < weakLevel
var adxStrong = adx >= strongLevel && adx < extremeLevel
var adxExtreme = adx >= extremeLevel
// Directional regime (which side is winning)
var bullDI = plusDI > minusDI
var bearDI = minusDI > plusDI
// Regime color for ADX
var adxColor = adxExtreme ? adxColorExtreme : (adxStrong ? adxColorStrong : adxColorWeak)
// ---------------------------------------------------------------------------
// 7. CROSSOVER DETECTION (static variables for stateful tracking)
// ---------------------------------------------------------------------------
// Track +DI/-DI crossovers for reliable shape markers
static prevPlusDI = -1.0
static prevMinusDI = -1.0
static prevAdx = -1.0
var diCrossUp = false
var diCrossDown = false
var adxStrongUp = false
var adxExtremeUp = false
var adxWeakDown = false
if (prevPlusDI >= 0) {
// +DI crosses above -DI (bullish)
diCrossUp = plusDI > minusDI && prevPlusDI <= prevMinusDI
// -DI crosses above +DI (bearish)
diCrossDown = minusDI > plusDI && prevMinusDI <= prevPlusDI
// ADX crosses into strong zone
adxStrongUp = adx >= strongLevel && prevAdx < strongLevel
// ADX crosses into extreme zone
adxExtremeUp = adx >= extremeLevel && prevAdx < extremeLevel
// ADX falls below weak level (trend loss)
adxWeakDown = adx < weakLevel && prevAdx >= weakLevel
}
prevPlusDI = plusDI
prevMinusDI = minusDI
prevAdx = adx
// ---------------------------------------------------------------------------
// 8. PLOTTING
// ---------------------------------------------------------------------------
// --- ADX Line (regime-colored) ---
if (showAdx) {
plotLine(
value=adx,
colors=[adxColorWeak, adxColorStrong, adxColorExtreme],
colorIndex=adxExtreme ? 2 : (adxStrong ? 1 : 0),
width=lineWidth,
label=["ADX"],
desc=["Average Directional Index (trend strength: weak/strong/extreme)"]
)
}
// --- +DI / -DI Lines with Fill ---
if (showDI) {
if (showFill) {
plotLine(
value=[plusDI, minusDI],
colors=[opacity(dipColor, 20), opacity(dinColor, 20)],
colorIndex=bullDI ? 0 : 1,
fill=true,
width=1,
label=["+DI", "-DI"],
desc=["Plus Directional Indicator fill", "Minus Directional Indicator fill"]
)
}
plotLine(
value=plusDI,
colors=[dipColor],
width=lineWidth,
label=["+DI"],
desc=["Plus Directional Indicator (bullish directional movement)"]
)
plotLine(
value=minusDI,
colors=[dinColor],
width=lineWidth,
label=["-DI"],
desc=["Minus Directional Indicator (bearish directional movement)"]
)
}
// --- Reference Levels ---
hline(value=extremeLevel, color=transparency(adxColorExtreme, 60), width=1)
hline(value=strongLevel, color=transparency(adxColorStrong, 60), width=1)
hline(value=weakLevel, color=transparency("#ffffff", 70), width=1)
// --- Shape Markers: +DI/-DI Crossovers ---
if (showShapes && showDI) {
if (diCrossUp) {
plotShape(
value=plusDI,
shape="triangleup",
colorIndex=crossBullColor,
width=shapeSize,
label=["DI Cross Up"],
desc=["+DI crossed above -DI: bullish directional control shift"]
)
}
if (diCrossDown) {
plotShape(
value=minusDI,
shape="triangledown",
colorIndex=crossBearColor,
width=shapeSize,
label=["DI Cross Down"],
desc=["-DI crossed above +DI: bearish directional control shift"]
)
}
}
// --- Shape Markers: ADX Threshold Events ---
if (showThreshShapes && showAdx) {
if (adxStrongUp) {
plotShape(
value=adx,
shape="circle",
colorIndex=adxColorStrong,
width=shapeSize,
label=["ADX Strong"],
desc=["ADX entered strong trend zone (>= " + strongLevel + ")"]
)
}
if (adxExtremeUp) {
plotShape(
value=adx,
shape="xcross",
colorIndex=adxColorExtreme,
width=shapeSize,
label=["ADX Extreme"],
desc=["ADX entered extreme/exhaustion zone (>= " + extremeLevel + ")"]
)
}
if (adxWeakDown) {
plotShape(
value=adx,
shape="arrowdown",
colorIndex=adxColorWeak,
width=shapeSize,
label=["ADX Weak"],
desc=["ADX fell below weak trend threshold (< " + weakLevel + ")"]
)
}
}
// ---------------------------------------------------------------------------
// 9. REGIME TABLE (last bar only)
// ---------------------------------------------------------------------------
if (isLastBar) {
var regimeText = adxExtreme ? "EXTREME" : (adxStrong ? "STRONG" : (adxWeak ? "WEAK" : "MODERATE"))
var regimeTextColor = adxExtreme ? adxColorExtreme : (adxStrong ? adxColorStrong : (adxWeak ? adxColorWeak : "#ffa726"))
var dirText = bullDI ? "BULLISH (+DI > -DI)" : (bearDI ? "BEARISH (-DI > +DI)" : "NEUTRAL (+DI = -DI)")
var dirTextColor = bullDI ? dipColor : (bearDI ? dinColor : "#9e9e9e")
plotTable(
data=[
["ADX — Perfected", regimeText],
["ADX Value", adx[0]],
["+DI", plusDI[0]],
["-DI", minusDI[0]],
["Direction", dirText],
["DX", dx[0]]
],
position="top_right",
headerRow=true,
headerColumn=true,
textColor="#e2e8f0",
headerTextColor="#ffffff",
backgroundColor=transparency("#0f172a", 40),
headerBackgroundColor=transparency("#1e293b", 40),
borderColor=transparency("#334155", 40),
borderWidth=1,
fontSize=10,
cellPadding=4
)
}