PROTECTED SOURCE SCRIPT
KJ Swing Stats

For Internal Use Only, //version=5
indicator("ADR / ATR / LoD / %FromMA / ATR-MA (inspiration-matched)", shorttitle="ADR-ATR-LoD-MA", overlay=true, max_bars_back=500)
// ---------------- Inputs ----------------
adrp_len = input.int(20, "ADR Length (days)", minval=1)
atr_len = input.int(14, "ATR Length (days)", minval=1)
ma_len = input.int(50, "MA50 Length (days)", minval=1)
posTable = input.string("Top Right", "Table Position", options=["Top Left","Top Right","Bottom Left","Bottom Right"])
tbl_size = input.string("Normal", "Table Size", options=["Tiny","Small","Normal","Large"])
txt_col = input.color(color.orange, "Text Color")
bg_col = input.color(#00000000, "Background Color")
show_adr_pct = input.bool(true, "Show ADR%")
show_adr_price = input.bool(false,"Show ADR (price)")
show_atr_pct = input.bool(true, "Show ATR%")
show_pct_from_ma = input.bool(true, "Show % from MA50")
show_atr_multiple= input.bool(true, "Show ATR multiple (|price - MA50| / ATR)")
show_lod = input.bool(true, "Show LoD Dist")
// which ADR formula to use (inspiration uses ratio default)
adr_mode = input.string("Ratio (sma(high/low)-1)", "ADR% formula", options=["Ratio (sma(high/low)-1)", "Range/Close (sma(high-low)/daily_close)"])
// LoD denom default to ATR like your inspiration snippet
lod_denom_choice = input.string("ATR", "LoD denominator", options=["ATR","ADR"])
// Price used for live measures: makes numbers stable across TFs when set to "Daily"
price_source = input.string("Daily", "Price source for live measures", options=["Daily","Intraday"])
// ---------------- Helpers ----------------
tablePos = switch posTable
"Top Left" => position.top_left
"Top Right" => position.top_right
"Bottom Left" => position.bottom_left
=> position.bottom_right
size_tbl = switch tbl_size
"Tiny" => size.tiny
"Small" => size.small
"Large" => size.large
=> size.normal
// ---------------- Daily-locked series (to avoid TF-mixing) ----------------
sym = syminfo.tickerid
// daily H/L/C (locked)
d_high = request.security(sym, "D", high)
d_low = request.security(sym, "D", low)
d_close = request.security(sym, "D", close)
// ADR price as avg(high-low) on daily and ADR% (two options)
d_adr_price = request.security(sym, "D", ta.sma(high - low, adrp_len))
// ADR% - ratio style (inspiration): 100 * (sma(high/low) - 1)
d_adr_ratio_pct = request.security(sym, "D", 100 * (ta.sma(high / low, adrp_len) - 1))
// ADR% - range/close style
d_adr_range_pct = d_adr_price == 0 ? na : 100 * d_adr_price / d_close
// choose ADR% final
d_adr_pct = adr_mode == "Ratio (sma(high/low)-1)" ? d_adr_ratio_pct : d_adr_range_pct
// ATR daily and ATR%
d_atr = request.security(sym, "D", ta.atr(atr_len))
d_atr_pct = d_atr == 0 ? na : 100 * d_atr / d_close
// MA50 on daily
d_ma50 = request.security(sym, "D", ta.sma(close, ma_len))
// ---------------- Price used for "live" measures ----------------
price_used = price_source == "Daily" ? d_close : close // close = chart timeframe close (intraday)
// ---------------- Calculations ----------------
// Percent distance FROM MA50 (correct formula = (price / MA50 - 1) * 100)
float pct_from_ma = na
pct_from_ma := (d_ma50 == 0 or na(d_ma50)) ? na : 100 * (price_used / d_ma50 - 1)
// ATR multiple = abs(price - MA50) / ATR (how many ATRs away price is from MA50)
float atr_multiple = na
atr_multiple := (d_atr == 0 or na(d_atr)) ? na : math.abs(price_used - d_ma50) / d_atr
// LoD Dist: (price_used - daily_low) / denom * 100
lod_denom = lod_denom_choice == "ADR" ? d_adr_price : d_atr
lod_dist = (lod_denom == 0 or na(lod_denom)) ? na : 100 * (price_used - d_low) / lod_denom
// ---------------- Formatting ----------------
fmtPct(v) => na(v) ? "-" : str.tostring(v, "0.00") + "%"
fmtSignedPct(v) => na(v) ? "-" : (v > 0 ? "+" + str.tostring(v, "0.00") + "%" : str.tostring(v, "0.00") + "%")
fmtNum(v, f) => na(v) ? "-" : str.tostring(v, f)
// ---------------- Table ----------------
var table t = table.new(tablePos, 2, 8, bgcolor=bg_col)
if barstate.islast
// empty top row like your inspiration
table.cell(t, 0, 0, "")
table.cell(t, 1, 0, "")
r = 1
if show_adr_pct
table.cell(t, 0, r, "ADR% (" + str.tostring(adrp_len) + "d)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, fmtPct(d_adr_pct), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_adr_price
table.cell(t, 0, r, "ADR (price)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, na(d_adr_price) ? "-" : str.tostring(d_adr_price, "0.000"), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_atr_pct
table.cell(t, 0, r, "ATR% (" + str.tostring(atr_len) + "d)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, fmtPct(d_atr_pct), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_pct_from_ma
table.cell(t, 0, r, "% from MA" + str.tostring(ma_len), text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, fmtSignedPct(pct_from_ma), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_atr_multiple
table.cell(t, 0, r, "ATR multiple (|price-MA|/ATR)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, na(atr_multiple) ? "-" : str.tostring(atr_multiple, "0.00"), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_lod
table.cell(t, 0, r, "LoD Dist (" + lod_denom_choice + ")", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, na(lod_dist) ? "-" : str.tostring(lod_dist, "0.0") + "%", text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
indicator("ADR / ATR / LoD / %FromMA / ATR-MA (inspiration-matched)", shorttitle="ADR-ATR-LoD-MA", overlay=true, max_bars_back=500)
// ---------------- Inputs ----------------
adrp_len = input.int(20, "ADR Length (days)", minval=1)
atr_len = input.int(14, "ATR Length (days)", minval=1)
ma_len = input.int(50, "MA50 Length (days)", minval=1)
posTable = input.string("Top Right", "Table Position", options=["Top Left","Top Right","Bottom Left","Bottom Right"])
tbl_size = input.string("Normal", "Table Size", options=["Tiny","Small","Normal","Large"])
txt_col = input.color(color.orange, "Text Color")
bg_col = input.color(#00000000, "Background Color")
show_adr_pct = input.bool(true, "Show ADR%")
show_adr_price = input.bool(false,"Show ADR (price)")
show_atr_pct = input.bool(true, "Show ATR%")
show_pct_from_ma = input.bool(true, "Show % from MA50")
show_atr_multiple= input.bool(true, "Show ATR multiple (|price - MA50| / ATR)")
show_lod = input.bool(true, "Show LoD Dist")
// which ADR formula to use (inspiration uses ratio default)
adr_mode = input.string("Ratio (sma(high/low)-1)", "ADR% formula", options=["Ratio (sma(high/low)-1)", "Range/Close (sma(high-low)/daily_close)"])
// LoD denom default to ATR like your inspiration snippet
lod_denom_choice = input.string("ATR", "LoD denominator", options=["ATR","ADR"])
// Price used for live measures: makes numbers stable across TFs when set to "Daily"
price_source = input.string("Daily", "Price source for live measures", options=["Daily","Intraday"])
// ---------------- Helpers ----------------
tablePos = switch posTable
"Top Left" => position.top_left
"Top Right" => position.top_right
"Bottom Left" => position.bottom_left
=> position.bottom_right
size_tbl = switch tbl_size
"Tiny" => size.tiny
"Small" => size.small
"Large" => size.large
=> size.normal
// ---------------- Daily-locked series (to avoid TF-mixing) ----------------
sym = syminfo.tickerid
// daily H/L/C (locked)
d_high = request.security(sym, "D", high)
d_low = request.security(sym, "D", low)
d_close = request.security(sym, "D", close)
// ADR price as avg(high-low) on daily and ADR% (two options)
d_adr_price = request.security(sym, "D", ta.sma(high - low, adrp_len))
// ADR% - ratio style (inspiration): 100 * (sma(high/low) - 1)
d_adr_ratio_pct = request.security(sym, "D", 100 * (ta.sma(high / low, adrp_len) - 1))
// ADR% - range/close style
d_adr_range_pct = d_adr_price == 0 ? na : 100 * d_adr_price / d_close
// choose ADR% final
d_adr_pct = adr_mode == "Ratio (sma(high/low)-1)" ? d_adr_ratio_pct : d_adr_range_pct
// ATR daily and ATR%
d_atr = request.security(sym, "D", ta.atr(atr_len))
d_atr_pct = d_atr == 0 ? na : 100 * d_atr / d_close
// MA50 on daily
d_ma50 = request.security(sym, "D", ta.sma(close, ma_len))
// ---------------- Price used for "live" measures ----------------
price_used = price_source == "Daily" ? d_close : close // close = chart timeframe close (intraday)
// ---------------- Calculations ----------------
// Percent distance FROM MA50 (correct formula = (price / MA50 - 1) * 100)
float pct_from_ma = na
pct_from_ma := (d_ma50 == 0 or na(d_ma50)) ? na : 100 * (price_used / d_ma50 - 1)
// ATR multiple = abs(price - MA50) / ATR (how many ATRs away price is from MA50)
float atr_multiple = na
atr_multiple := (d_atr == 0 or na(d_atr)) ? na : math.abs(price_used - d_ma50) / d_atr
// LoD Dist: (price_used - daily_low) / denom * 100
lod_denom = lod_denom_choice == "ADR" ? d_adr_price : d_atr
lod_dist = (lod_denom == 0 or na(lod_denom)) ? na : 100 * (price_used - d_low) / lod_denom
// ---------------- Formatting ----------------
fmtPct(v) => na(v) ? "-" : str.tostring(v, "0.00") + "%"
fmtSignedPct(v) => na(v) ? "-" : (v > 0 ? "+" + str.tostring(v, "0.00") + "%" : str.tostring(v, "0.00") + "%")
fmtNum(v, f) => na(v) ? "-" : str.tostring(v, f)
// ---------------- Table ----------------
var table t = table.new(tablePos, 2, 8, bgcolor=bg_col)
if barstate.islast
// empty top row like your inspiration
table.cell(t, 0, 0, "")
table.cell(t, 1, 0, "")
r = 1
if show_adr_pct
table.cell(t, 0, r, "ADR% (" + str.tostring(adrp_len) + "d)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, fmtPct(d_adr_pct), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_adr_price
table.cell(t, 0, r, "ADR (price)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, na(d_adr_price) ? "-" : str.tostring(d_adr_price, "0.000"), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_atr_pct
table.cell(t, 0, r, "ATR% (" + str.tostring(atr_len) + "d)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, fmtPct(d_atr_pct), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_pct_from_ma
table.cell(t, 0, r, "% from MA" + str.tostring(ma_len), text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, fmtSignedPct(pct_from_ma), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_atr_multiple
table.cell(t, 0, r, "ATR multiple (|price-MA|/ATR)", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, na(atr_multiple) ? "-" : str.tostring(atr_multiple, "0.00"), text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
if show_lod
table.cell(t, 0, r, "LoD Dist (" + lod_denom_choice + ")", text_color=txt_col, text_size=size_tbl)
table.cell(t, 1, r, na(lod_dist) ? "-" : str.tostring(lod_dist, "0.0") + "%", text_color=txt_col, text_size=size_tbl)
table.cell_set_text_halign(t, 0, r, text.align_left)
table.cell_set_text_halign(t, 1, r, text.align_right)
r += 1
Script protégé
Ce script est publié en source fermée. Toutefois, vous pouvez l'utiliser librement et sans aucune restriction - en savoir plus ici.
Clause de non-responsabilité
Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.
Script protégé
Ce script est publié en source fermée. Toutefois, vous pouvez l'utiliser librement et sans aucune restriction - en savoir plus ici.
Clause de non-responsabilité
Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.