//@version=6
// SPDX-License-Identifier: Apache-2.0
// © TrendyCoins
// Market Randomness Index — generated by mri/pine_nt3.py from export/mri_model_nt3.json; do not edit by hand.
//
// What the line shows: M — how many times a 64-bar window of this shape is more frequent (orange) or less frequent
// (blue) in real markets than in a random walk with the same volatility (bar-mirror surrogate in blocks of 4 bars).
// M = 1 — indistinguishable from random. The SuMo classifier (a streaming CNN, 36961 weights in 3 libraries imported by this script) scores
// the window and the 200 bars before it, in bars only (no clock, no calendar); Platt calibration of the symbol turns the
// score into M. M is a frequency ratio of the window's shape, not a forecast.
// Calibration: Platt of each table symbol, checked out of sample by half-years per symbol; each symbol and side has
// its ceiling (at most 21 more frequent / 13 less frequent; for symbols outside the table 8 / 1.5
// with the global Platt); above it the line stays at the ceiling, marked "N+".
// No repaint: the state changes only on confirmed bars; a window over a gap in the data is not drawn.
// Passport: weights prod_NT3_PRV_ch48_L64_a, data up to 2026-09-26 09:15:00 UTC, export of code c58990a.
indicator("Market Randomness Index", shorttitle = "MRI", overlay = false, precision = 2, max_labels_count = 500)
// weight libraries (pine/legacy/lib/), published under the author's TradingView user name
import TrendyCoins/MRIWeightsNT3a/1 as WA
import TrendyCoins/MRIWeightsNT3b/1 as WB
import TrendyCoins/MRIWeightsNT3c/1 as WC
// ---------------------------------------------------------------- inputs
float thr = input.float(3.0, "Threshold M", minval = 1.0, step = 0.25, tooltip = "Visual only: above the threshold the line is bright, below it is pale; dashed line at the threshold; optional marks on the price chart at M >= threshold.")
int paleT = input.int(70, "Transparency below the threshold", minval = 0, maxval = 100)
color colMore = input.color(color.rgb(242, 140, 40), "More frequent than random", tooltip = "M > 1 on the more frequent side: windows of this shape are M times more frequent in real markets than in a random walk.")
color colLess = input.color(color.rgb(47, 128, 237), "Less frequent than random", tooltip = "M > 1 on the less frequent side: windows of this shape are M times less frequent in real markets than in a random walk.")
bool showMarks = input.bool(false, "Marks on the price chart at M >= threshold")
bool dbg = input.bool(false, "Debug values (parity check)", group = "Advanced", tooltip = "Plots the internal values of each bar for checking against the Python reference (Export chart data).")
int calcBars = input.int(0, "Compute only the last N bars (0 = all history)", minval = 0, group = "Advanced", tooltip = "The network runs on the last N bars only (TradingView limits the script time on the whole history).")
// ---------------------------------------------------------------- symbol table: floor of the window divisor, Platt of the symbol, ceilings of M
buildFloors() =>
map<string, float> m = map.new<string, float>()
map.put(m, "BTCUSDT", 0.17557531595230103)
map.put(m, "ETHUSDT", 0.18518517911434174)
map.put(m, "DOGEUSDT", 0.1610305905342102)
map.put(m, "SOLUSDT", 0.20000000298023224)
map.put(m, "ADAUSDT", 0.20000000298023224)
map.put(m, "BNBUSDT", 0.16880816221237183)
map.put(m, "BCHUSDT", 0.20000000298023224)
map.put(m, "DOTUSDT", 0.20000000298023224)
map.put(m, "UNIUSDT", 0.20248538255691528)
map.put(m, "XRPUSDT", 0.19362260401248932)
map.put(m, "LTCUSDT", 0.19251661002635956)
map.put(m, "DASHUSDT", 0.20000000298023224)
map.put(m, "ZECUSDT", 0.20000000298023224)
map.put(m, "SPELLUSDT", 0.17500001192092896)
map.put(m, "ICPUSDT", 0.20000000298023224)
map.put(m, "NEARUSDT", 0.20000000298023224)
map.put(m, "ZENUSDT", 0.20000000298023224)
map.put(m, "AVAXUSDT", 0.20000000298023224)
map.put(m, "FETUSDT", 0.1666666716337204)
map.put(m, "RAYUSDT", 0.17996746301651)
map.put(m, "CAKEUSDT", 0.20000000298023224)
map.put(m, "INJUSDT", 0.19910044968128204)
map.put(m, "RUNEUSDT", 0.20000000298023224)
map.put(m, "PHAUSDT", 0.1666666716337204)
map.put(m, "QNTUSDT", 0.20000000298023224)
map.put(m, "HBARUSDT", 0.18859648704528809)
map.put(m, "AAVEUSDT", 0.20000000298023224)
map.put(m, "VTHOUSDT", 0.15904566645622253)
map.put(m, "FILUSDT", 0.20000000298023224)
map.put(m, "LINKUSDT", 0.20000000298023224)
map.put(m, "XLMUSDT", 0.19565217196941376)
m
buildPlattA() =>
map<string, float> m = map.new<string, float>()
map.put(m, "BTCUSDT", 0.8426300883293152)
map.put(m, "ETHUSDT", 1.1139812469482422)
map.put(m, "DOGEUSDT", 0.9797671437263489)
map.put(m, "SOLUSDT", 0.8909746408462524)
map.put(m, "ADAUSDT", 0.87144935131073)
map.put(m, "BNBUSDT", 0.7804573178291321)
map.put(m, "BCHUSDT", 0.7463998794555664)
map.put(m, "DOTUSDT", 0.834274172782898)
map.put(m, "UNIUSDT", 0.8404353260993958)
map.put(m, "XRPUSDT", 0.8526526689529419)
map.put(m, "LTCUSDT", 0.7245725393295288)
map.put(m, "DASHUSDT", 0.9396822452545166)
map.put(m, "ZECUSDT", 0.9461657404899597)
map.put(m, "SPELLUSDT", 0.7777805924415588)
map.put(m, "ICPUSDT", 0.7584550380706787)
map.put(m, "NEARUSDT", 0.730005145072937)
map.put(m, "ZENUSDT", 0.9120381474494934)
map.put(m, "AVAXUSDT", 0.8810052871704102)
map.put(m, "FETUSDT", 0.8788285851478577)
map.put(m, "RAYUSDT", 0.8500726819038391)
map.put(m, "CAKEUSDT", 0.794098973274231)
map.put(m, "INJUSDT", 0.7203945517539978)
map.put(m, "RUNEUSDT", 0.8577651381492615)
map.put(m, "PHAUSDT", 0.8255383372306824)
map.put(m, "QNTUSDT", 0.662527859210968)
map.put(m, "HBARUSDT", 0.6509257555007935)
map.put(m, "AAVEUSDT", 0.6829267144203186)
map.put(m, "VTHOUSDT", 0.7138074636459351)
map.put(m, "FILUSDT", 0.9378737211227417)
map.put(m, "LINKUSDT", 0.9228219985961914)
map.put(m, "XLMUSDT", 0.6268060803413391)
m
buildPlattB() =>
map<string, float> m = map.new<string, float>()
map.put(m, "BTCUSDT", -0.38505035638809204)
map.put(m, "ETHUSDT", -0.7899372577667236)
map.put(m, "DOGEUSDT", -0.5659359693527222)
map.put(m, "SOLUSDT", -0.4443643093109131)
map.put(m, "ADAUSDT", -0.4076251983642578)
map.put(m, "BNBUSDT", -0.27349528670310974)
map.put(m, "BCHUSDT", -0.23191514611244202)
map.put(m, "DOTUSDT", -0.34018248319625854)
map.put(m, "UNIUSDT", -0.3604070544242859)
map.put(m, "XRPUSDT", -0.37721118330955505)
map.put(m, "LTCUSDT", -0.23988619446754456)
map.put(m, "DASHUSDT", -0.49620935320854187)
map.put(m, "ZECUSDT", -0.535832405090332)
map.put(m, "SPELLUSDT", -0.2126852124929428)
map.put(m, "ICPUSDT", -0.2889304757118225)
map.put(m, "NEARUSDT", -0.2479057013988495)
map.put(m, "ZENUSDT", -0.4436146914958954)
map.put(m, "AVAXUSDT", -0.3969903588294983)
map.put(m, "FETUSDT", -0.3954277038574219)
map.put(m, "RAYUSDT", -0.2905670702457428)
map.put(m, "CAKEUSDT", -0.2935011386871338)
map.put(m, "INJUSDT", -0.2223573923110962)
map.put(m, "RUNEUSDT", -0.3130866587162018)
map.put(m, "PHAUSDT", -0.28626617789268494)
map.put(m, "QNTUSDT", -0.14830711483955383)
map.put(m, "HBARUSDT", -0.16655418276786804)
map.put(m, "AAVEUSDT", -0.2044578641653061)
map.put(m, "VTHOUSDT", -0.18268951773643494)
map.put(m, "FILUSDT", -0.46043795347213745)
map.put(m, "LINKUSDT", -0.4722996950149536)
map.put(m, "XLMUSDT", -0.1482810080051422)
m
buildCeilMore() =>
map<string, float> m = map.new<string, float>()
map.put(m, "BTCUSDT", 8.0)
map.put(m, "ETHUSDT", 13.0)
map.put(m, "DOGEUSDT", 13.0)
map.put(m, "SOLUSDT", 8.0)
map.put(m, "ADAUSDT", 8.0)
map.put(m, "BNBUSDT", 8.0)
map.put(m, "BCHUSDT", 8.0)
map.put(m, "DOTUSDT", 8.0)
map.put(m, "UNIUSDT", 8.0)
map.put(m, "XRPUSDT", 8.0)
map.put(m, "LTCUSDT", 8.0)
map.put(m, "DASHUSDT", 13.0)
map.put(m, "ZECUSDT", 13.0)
map.put(m, "SPELLUSDT", 8.0)
map.put(m, "ICPUSDT", 8.0)
map.put(m, "NEARUSDT", 8.0)
map.put(m, "ZENUSDT", 8.0)
map.put(m, "AVAXUSDT", 8.0)
map.put(m, "FETUSDT", 8.0)
map.put(m, "RAYUSDT", 8.0)
map.put(m, "CAKEUSDT", 8.0)
map.put(m, "INJUSDT", 8.0)
map.put(m, "RUNEUSDT", 8.0)
map.put(m, "PHAUSDT", 8.0)
map.put(m, "QNTUSDT", 8.0)
map.put(m, "HBARUSDT", 5.0)
map.put(m, "AAVEUSDT", 8.0)
map.put(m, "VTHOUSDT", 5.0)
map.put(m, "FILUSDT", 8.0)
map.put(m, "LINKUSDT", 8.0)
map.put(m, "XLMUSDT", 5.0)
m
buildCeilLess() =>
map<string, float> m = map.new<string, float>()
map.put(m, "BTCUSDT", 5.0)
map.put(m, "ETHUSDT", 13.0)
map.put(m, "DOGEUSDT", 13.0)
map.put(m, "SOLUSDT", 13.0)
map.put(m, "ADAUSDT", 8.0)
map.put(m, "BNBUSDT", 8.0)
map.put(m, "BCHUSDT", 8.0)
map.put(m, "DOTUSDT", 13.0)
map.put(m, "UNIUSDT", 13.0)
map.put(m, "XRPUSDT", 13.0)
map.put(m, "LTCUSDT", 8.0)
map.put(m, "DASHUSDT", 13.0)
map.put(m, "ZECUSDT", 8.0)
map.put(m, "SPELLUSDT", 8.0)
map.put(m, "ICPUSDT", 8.0)
map.put(m, "NEARUSDT", 8.0)
map.put(m, "ZENUSDT", 13.0)
map.put(m, "AVAXUSDT", 5.0)
map.put(m, "FETUSDT", 8.0)
map.put(m, "RAYUSDT", 8.0)
map.put(m, "CAKEUSDT", 5.0)
map.put(m, "INJUSDT", 8.0)
map.put(m, "RUNEUSDT", 8.0)
map.put(m, "PHAUSDT", 8.0)
map.put(m, "QNTUSDT", 5.0)
map.put(m, "HBARUSDT", 8.0)
map.put(m, "AAVEUSDT", 8.0)
map.put(m, "VTHOUSDT", 5.0)
map.put(m, "FILUSDT", 8.0)
map.put(m, "LINKUSDT", 5.0)
map.put(m, "XLMUSDT", 8.0)
m
var map<string, float> FLOORS = buildFloors()
var map<string, float> PLA = buildPlattA()
var map<string, float> PLB = buildPlattB()
var map<string, float> CEILO = buildCeilMore()
var map<string, float> CEILB = buildCeilLess()
bool known = syminfo.prefix == "BINANCE" and not str.endswith(syminfo.ticker, ".P") and map.contains(FLOORS, syminfo.ticker)
var float FLOOR = known ? map.get(FLOORS, syminfo.ticker) : 0.20000000298023224
var float PLATT_A = known ? map.get(PLA, syminfo.ticker) : 0.8142369985580444
var float PLATT_B = known ? map.get(PLB, syminfo.ticker) : -0.31652188301086426
var float CEIL_O = known ? map.get(CEILO, syminfo.ticker) : 8.0 // checked range of M, more frequent side
var float CEIL_B = known ? map.get(CEILB, syminfo.ticker) : 1.5 // checked range of M, less frequent side
bool tf5 = timeframe.period == "5"
float GAPMS = 1.5 * timeframe.in_seconds() * 1000.0 // interval > 1.5 bars -> grid gap
// ---------------------------------------------------------------- constants
const int L = 64
const int NN = 200 // bars of the range median (the norm of a bar) and of the history before a window
const int CH = 48 // conv channels
const int HID = 96 // hidden units of the head
const int NS = 17 // side inputs
const float EPS = 0.0001 // orientation tie rule: flip only if X_end < -EPS * D
const float SQ199 = 14.106735979665885
const float S_SD = 1.640539288520813
const float LVL_SD = 3.028594493865967
var array<float> SIDE_MU = array.from(-0.7283339500427246, -0.47306308150291443, -0.0017850924050435424, -0.009280271828174591, -0.010675457306206226, -0.012358603067696095, -0.008352444507181644, -0.00936533510684967, -0.00594957685098052, -0.010362531989812851, -0.01310903113335371, 0.2680075764656067, -0.013571053743362427, -0.05120363086462021, -0.021999502554535866, 0.17020025849342346, -0.7806834578514099)
var array<float> SIDE_SD = array.from(0.34057411551475525, 0.3331082761287689, 0.8273237347602844, 0.8481457829475403, 0.910289466381073, 0.9441472291946411, 0.9764606952667236, 0.9871980547904968, 1.0049999952316284, 0.9948281049728394, 0.5462812781333923, 0.36610549688339233, 0.517971932888031, 0.34881722927093506, 0.2256760150194168, 0.11898082494735718, 0.29244518280029297)
var array<float> HSIGNED = array.from(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0) // history inputs that take the orientation sign
// ---------------------------------------------------------------- weights: 36961 float32 in 3 libraries (MRIWeightsNT3a, MRIWeightsNT3b, MRIWeightsNT3c), 3 characters each, decoded once
buildAlpha() =>
map<string, int> m = map.new<string, int>()
array<string> cs = str.split("一丁丂七丄丅丆万丈三上下丌不与丏丐丑丒专且丕世丗丘丙业丛东丝丞丟丠両丢丣两严並丧丨丩个丫丬中丮丯丰丱串丳临丵丶丷丸丹为主丼丽举丿乀乁乂乃乄久乆乇么义乊之乌乍乎乏乐乑乒乓乔乕乖乗乘乙乚乛乜九乞也习乡乢乣乤乥书乧乨乩乪乫乬乭乮乯买乱乲乳乴乵乶乷乸乹乺乻乼乽乾乿亀亁亂亃亄亅了亇予争亊事二亍于亏亐云互亓五井亖亗亘亙亚些亜亝亞亟亠亡亢亣交亥亦产亨亩亪享京亭亮亯亰亱亲亳亴亵亶亷亸亹人亻亼亽亾亿什仁仂仃仄仅仆仇仈仉今介仌仍从仏仐仑仒仓仔仕他仗付仙仚仛仜仝仞仟仠仡仢代令以仦仧仨仩仪仫们仭仮仯仰仱仲仳仴仵件价仸仹仺任仼份仾仿伀企伂伃伄伅伆伇伈伉伊伋伌伍伎伏伐休伒伓伔伕伖众优伙会伛伜伝伞伟传伡伢伣伤伥伦伧伨伩伪伫伬伭伮伯估伱伲伳伴伵伶伷伸伹伺伻似伽伾伿佀佁佂佃佄佅但佇佈佉佊佋佌位低住佐佑佒体佔何佖佗佘余佚佛作佝佞佟你佡佢佣佤佥佦佧佨佩佪佫佬佭佮佯佰佱佲佳佴併佶佷佸佹佺佻佼佽佾使侀侁侂侃侄侅來侇侈侉侊例侌侍侎侏侐侑侒侓侔侕侖侗侘侙侚供侜依侞侟侠価侢侣侤侥侦侧侨侩侪侫侬侭侮侯侰侱侲侳侴侵侶侷侸侹侺侻侼侽侾便俀俁係促俄俅俆俇俈俉俊俋俌俍俎俏俐俑俒俓俔俕俖俗俘俙俚俛俜保俞俟俠信俢俣俤俥俦俧俨俩俪俫俬俭修俯俰俱俲俳俴俵俶俷俸俹俺俻俼俽俾俿倀倁倂倃倄倅倆倇倈倉倊個倌倍倎倏倐們倒倓倔倕倖倗倘候倚倛倜倝倞借倠倡倢倣値倥倦倧倨倩倪倫倬倭倮倯倰倱倲倳倴倵倶倷倸倹债倻值倽倾倿偀偁偂偃偄偅偆假偈偉偊偋偌偍偎偏偐偑偒偓偔偕偖偗偘偙做偛停偝偞偟偠偡偢偣偤健偦偧偨偩偪偫偬偭偮偯偰偱偲偳側偵偶偷偸偹偺偻偼偽偾偿傀傁傂傃傄傅傆傇傈傉傊傋傌傍傎傏傐傑傒傓傔傕傖傗傘備傚傛傜傝傞傟傠傡傢傣傤傥傦傧储傩傪傫催傭傮傯傰傱傲傳傴債傶傷傸傹傺傻傼傽傾傿僀僁僂僃僄僅僆僇僈僉僊僋僌働僎像僐僑僒僓僔僕僖僗僘僙僚僛僜僝僞僟僠僡僢僣僤僥僦僧僨僩僪僫僬僭僮僯僰僱僲僳僴僵僶僷僸價僺僻僼僽僾僿儀儁儂儃億儅儆儇儈儉儊儋儌儍儎儏儐儑儒儓儔儕儖儗儘儙儚儛儜儝儞償儠儡儢儣儤儥儦儧儨儩優儫儬儭儮儯儰儱儲儳儴儵儶儷儸儹儺儻儼儽儾儿兀允兂元兄充兆兇先光兊克兌免兎兏児兑兒兓兔兕兖兗兘兙党兛兜兝兞兟兠兡兢兣兤入兦內全兩兪八公六兮兯兰共兲关兴兵其具典兹兺养兼兽兾兿冀冁冂冃冄内円冇冈冉冊冋册再冎冏冐冑冒冓冔冕冖冗冘写冚军农冝冞冟冠冡冢冣冤冥冦冧冨冩冪冫冬冭冮冯冰冱冲决冴况冶冷冸冹冺冻冼冽冾冿净凁凂凃凄凅准凇凈凉凊凋凌凍凎减凐凑凒凓凔凕凖凗凘凙凚凛凜凝凞凟几凡凢凣凤凥処凧凨凩凪凫凬凭凮凯凰凱凲凳凴凵凶凷凸凹出击凼函凾凿刀刁刂刃刄刅分切刈刉刊刋刌刍刎刏刐刑划刓刔刕刖列刘则刚创刜初刞刟删刡刢刣判別刦刧刨利刪别刬刭刮刯到刱刲刳刴刵制刷券刹刺刻刼刽刾刿剀剁剂剃剄剅剆則剈剉削剋剌前剎剏剐剑剒剓剔剕剖剗剘剙剚剛剜剝剞剟剠剡剢剣剤剥剦剧剨剩剪剫剬剭剮副剰剱割剳剴創剶剷剸剹剺剻剼剽剾剿劀劁劂劃劄劅劆劇劈劉劊劋劌劍劎劏劐劑劒劓劔劕劖劗劘劙劚力劜劝办功加务劢劣劤劥劦劧动助努劫劬劭劮劯劰励劲劳労劵劶劷劸効劺劻劼劽劾势勀勁勂勃勄勅勆勇勈勉勊勋勌勍勎勏勐勑勒勓勔動勖勗勘務勚勛勜勝勞募勠勡勢勣勤勥勦勧勨勩勪勫勬勭勮勯勰勱勲勳勴勵勶勷勸勹勺勻勼勽勾勿匀匁匂匃匄包匆匇匈匉匊匋匌匍匎匏匐匑匒匓匔匕化北匘匙匚匛匜匝匞匟匠匡匢匣匤匥匦匧匨匩匪匫匬匭匮匯匰匱匲匳匴匵匶匷匸匹区医匼匽匾匿區十卂千卄卅卆升午卉半卋卌卍华协卐卑卒卓協单卖南単卙博卛卜卝卞卟占卡卢卣卤卥卦卧卨卩卪卫卬卭卮卯印危卲即却卵卶卷卸卹卺卻卼卽卾卿厀厁厂厃厄厅历厇厈厉厊压厌厍厎厏厐厑厒厓厔厕厖厗厘厙厚厛厜厝厞原厠厡厢厣厤厥厦厧厨厩厪厫厬厭厮厯厰厱厲厳厴厵厶厷厸厹厺去厼厽厾县叀叁参參叄叅叆叇又叉及友双反収叏叐发叒叓叔叕取受变叙叚叛叜叝叞叟叠叡叢口古句另叧叨叩只叫召叭叮可台叱史右叴叵叶号司叹叺叻叼叽叾叿吀吁吂吃各吅吆吇合吉吊吋同名后吏吐向吒吓吔吕吖吗吘吙吚君吜吝吞吟吠吡吢吣吤吥否吧吨吩吪含听吭吮启吰吱吲吳吴吵吶吷吸吹吺吻吼吽吾吿呀呁呂呃呄呅呆呇呈呉告呋呌呍呎呏呐呑呒呓呔呕呖呗员呙呚呛呜呝呞呟呠呡呢呣呤呥呦呧周呩呪呫呬呭呮呯呰呱呲味呴呵呶呷呸呹呺呻呼命呾呿咀咁咂咃咄咅咆咇咈咉咊咋和咍咎咏咐咑咒咓咔咕咖咗咘咙咚咛咜咝咞咟咠咡咢咣咤咥咦咧咨咩咪咫咬咭咮咯咰咱咲咳咴咵咶咷咸咹咺咻咼咽咾咿哀品哂哃哄哅哆哇哈哉哊哋哌响哎哏哐哑哒哓哔哕哖哗哘哙哚哛哜哝哞哟哠員哢哣哤哥哦哧哨哩哪哫哬哭哮哯哰哱哲哳哴哵哶哷哸哹哺哻哼哽哾哿唀唁唂唃唄唅唆唇唈唉唊唋唌唍唎唏唐唑唒唓唔唕唖唗唘唙唚唛唜唝唞唟唠唡唢唣唤唥唦唧唨唩唪唫唬唭售唯唰唱唲唳唴唵唶唷唸唹唺唻唼唽唾唿啀啁啂啃啄啅商啇啈啉啊啋啌啍啎問啐啑啒啓啔啕啖啗啘啙啚啛啜啝啞啟啠啡啢啣啤啥啦啧啨啩啪啫啬啭啮啯啰啱啲啳啴啵啶啷啸啹啺啻啼啽啾啿喀喁喂喃善喅喆喇喈喉喊喋喌喍喎喏喐喑喒喓喔喕喖喗喘喙喚喛喜喝喞喟喠喡喢喣喤喥喦喧喨喩喪喫喬喭單喯喰喱喲喳喴喵営喷喸喹喺喻喼喽喾喿嗀嗁嗂嗃嗄嗅嗆嗇嗈嗉嗊嗋嗌嗍嗎嗏嗐嗑嗒嗓嗔嗕嗖嗗嗘嗙嗚嗛嗜嗝嗞嗟嗠嗡嗢嗣嗤嗥嗦嗧嗨嗩嗪嗫嗬嗭嗮嗯嗰嗱嗲嗳嗴嗵嗶嗷嗸嗹嗺嗻嗼嗽嗾嗿", "")
for [i, c] in cs
map.put(m, c, i)
m
appendW(array<float> dst, array<float> src) =>
array.concat(dst, src)
true
var map<string, int> ALPHA = buildAlpha()
var array<float> WT = array.new<float>(0)
var bool WOK00 = appendW(WT, WA.w00(ALPHA))
var bool WOK01 = appendW(WT, WA.w01(ALPHA))
var bool WOK02 = appendW(WT, WA.w02(ALPHA))
var bool WOK03 = appendW(WT, WA.w03(ALPHA))
var bool WOK04 = appendW(WT, WA.w04(ALPHA))
var bool WOK05 = appendW(WT, WA.w05(ALPHA))
var bool WOK06 = appendW(WT, WA.w06(ALPHA))
var bool WOK07 = appendW(WT, WA.w07(ALPHA))
var bool WOK08 = appendW(WT, WA.w08(ALPHA))
var bool WOK09 = appendW(WT, WA.w09(ALPHA))
var bool WOK10 = appendW(WT, WB.w10(ALPHA))
var bool WOK11 = appendW(WT, WB.w11(ALPHA))
var bool WOK12 = appendW(WT, WB.w12(ALPHA))
var bool WOK13 = appendW(WT, WB.w13(ALPHA))
var bool WOK14 = appendW(WT, WB.w14(ALPHA))
var bool WOK15 = appendW(WT, WB.w15(ALPHA))
var bool WOK16 = appendW(WT, WB.w16(ALPHA))
var bool WOK17 = appendW(WT, WB.w17(ALPHA))
var bool WOK18 = appendW(WT, WB.w18(ALPHA))
var bool WOK19 = appendW(WT, WC.w19(ALPHA))
var bool WOK20 = appendW(WT, WC.w20(ALPHA))
var bool WOK21 = appendW(WT, WC.w21(ALPHA))
var bool WOK22 = appendW(WT, WC.w22(ALPHA))
var bool WOK23 = appendW(WT, WC.w23(ALPHA))
var bool WOK24 = appendW(WT, WC.w24(ALPHA))
var bool WOK25 = appendW(WT, WC.w25(ALPHA))
var bool WOK26 = appendW(WT, WC.w26(ALPHA))
var bool WOK27 = appendW(WT, WC.w27(ALPHA))
toMat(array<float> w, int off, int nr, int nc) =>
matrix<float> m = matrix.new<float>(nr, nc, 0.0)
for i = 0 to nr - 1
for j = 0 to nc - 1
matrix.set(m, i, j, array.get(w, off + i * nc + j))
m
var matrix<float> K1_0 = toMat(WT, 0, 48, 4)
var matrix<float> K1_1 = toMat(WT, 192, 48, 4)
var matrix<float> K1_2 = toMat(WT, 384, 48, 4)
var matrix<float> K2_0 = toMat(WT, 576, 48, 48)
var matrix<float> K2_1 = toMat(WT, 2880, 48, 48)
var matrix<float> K2_2 = toMat(WT, 5184, 48, 48)
var matrix<float> K3_0 = toMat(WT, 7488, 48, 48)
var matrix<float> K3_1 = toMat(WT, 9792, 48, 48)
var matrix<float> K3_2 = toMat(WT, 12096, 48, 48)
var matrix<float> K4_0 = toMat(WT, 14400, 48, 48)
var matrix<float> K4_1 = toMat(WT, 16704, 48, 48)
var matrix<float> K4_2 = toMat(WT, 19008, 48, 48)
var matrix<float> H0 = toMat(WT, 21312, 96, 161)
var matrix<float> HB0 = toMat(WT, 36768, 96, 1)
var array<float> H3 = array.copy(array.slice(WT, 36864, 36960))
var float HB3 = array.get(WT, 36960)
// ---------------------------------------------------------------- state
var array<float> RNG = array.new<float>(0) // high - low of the last 200 bars
var array<float> BODY = array.new<float>(0) // |close - open| of the last 200 bars
var array<float> WR = array.new<float>(L, 0.0) // r of the last 64 bars (0 on invalid bars), oldest first
var array<float> WABS = array.new<float>(L, 0.0) // |r| of the last 64 bars
var float prevC = na
var float prevD1 = na // (HL/2 - close) / norm of the previous bar
var float prevD2 = na // (HLC/3 - close) / norm of the previous bar
var int okRun = 0 // consecutive valid bars
var int firstR = -1 // bar_index of the first valid r
var int nConf = 0 // confirmed bars processed (debug: the state advanced)
// conv rings: inputs (4 x 2*3), layers 1..3 (CH x 2*5, 2*9, 2*17); layer 4 per stream (CH x 33)
var matrix<float> R0 = matrix.new<float>(4, 6, 0.0)
var matrix<float> R1 = matrix.new<float>(CH, 10, 0.0)
var matrix<float> R2 = matrix.new<float>(CH, 18, 0.0)
var matrix<float> R3 = matrix.new<float>(CH, 34, 0.0)
var matrix<float> R4P = matrix.new<float>(CH, 33, 0.0)
var matrix<float> R4M = matrix.new<float>(CH, 33, 0.0)
var matrix<float> ONES33 = matrix.new<float>(33, 1, 1.0 / 33.0)
var matrix<float> FEAT = matrix.new<float>(3 * CH + NS, 1, 0.0)
var int hc = -1 // heavy-part bar counter (ring position)
var array<float> DBG = array.new<float>(3 + NS, na) // values of the last window: D, X_end, sign, side inputs (debug)
slot(int cnt, int depth, int back) =>
(cnt - back + depth * 64) % depth
tap(matrix<float> R, int depth, int cnt, int back) =>
int s = slot(cnt, depth, back)
matrix.submatrix(R, 0, matrix.rows(R), 2 * s, 2 * s + 2)
conv3(matrix<float> R, int depth, int cnt, int d, matrix<float> W0, matrix<float> W1, matrix<float> W2) =>
matrix.sum(matrix.sum(matrix.mult(W0, tap(R, depth, cnt, 2 * d)), matrix.mult(W1, tap(R, depth, cnt, d))), matrix.mult(W2, tap(R, depth, cnt, 0)))
reluInto(matrix<float> y, matrix<float> R, int depth, int cnt) =>
int s = slot(cnt, depth, 0)
for i = 0 to CH - 1
matrix.set(R, i, 2 * s, math.max(matrix.get(y, i, 0), 0.0))
matrix.set(R, i, 2 * s + 1, math.max(matrix.get(y, i, 1), 0.0))
true
// one bar of the two conv streams: inputs u (stream +), ring writes for layers 1..4
convStep(int cnt, float u0, float u1, float u2, float u3) =>
int p0 = slot(cnt, 3, 0)
matrix.set(R0, 0, 2 * p0, u0)
matrix.set(R0, 1, 2 * p0, u1)
matrix.set(R0, 2, 2 * p0, u2)
matrix.set(R0, 3, 2 * p0, u3)
matrix.set(R0, 0, 2 * p0 + 1, -u0)
matrix.set(R0, 1, 2 * p0 + 1, -u1)
matrix.set(R0, 2, 2 * p0 + 1, -u2)
matrix.set(R0, 3, 2 * p0 + 1, u3)
reluInto(conv3(R0, 3, cnt, 1, K1_0, K1_1, K1_2), R1, 5, cnt)
reluInto(conv3(R1, 5, cnt, 2, K2_0, K2_1, K2_2), R2, 9, cnt)
reluInto(conv3(R2, 9, cnt, 4, K3_0, K3_1, K3_2), R3, 17, cnt)
matrix<float> y4 = conv3(R3, 17, cnt, 8, K4_0, K4_1, K4_2)
int p4 = slot(cnt, 33, 0)
for i = 0 to CH - 1
matrix.set(R4P, i, p4, math.max(matrix.get(y4, i, 0), 0.0))
matrix.set(R4M, i, p4, math.max(matrix.get(y4, i, 1), 0.0))
true
// logit of the window ending at this bar; hst = 15 history inputs of the window (before the orientation sign)
computeLogit(int cnt, array<float> hst) =>
float D = math.max(array.median(WABS), FLOOR)
float xe = 0.0
for i = 0 to L - 1
xe += i * array.get(WR, i)
xe /= L
float sgn = xe < -EPS * D ? -1.0 : 1.0
array.set(DBG, 0, D)
array.set(DBG, 1, xe)
array.set(DBG, 2, sgn)
matrix<float> M4 = sgn > 0 ? R4P : R4M
matrix<float> mean4 = matrix.mult(M4, ONES33)
int p4 = slot(cnt, 33, 0)
for i = 0 to CH - 1
matrix.set(FEAT, i, 0, matrix.get(mean4, i, 0) / D)
matrix.set(FEAT, CH + i, 0, array.max(matrix.row(M4, i)) / D)
matrix.set(FEAT, 2 * CH + i, 0, matrix.get(M4, i, p4) / D)
array<float> side = array.from(math.log(D), math.log(math.max(array.avg(WABS), FLOOR)))
for j = 0 to NS - 3
array.push(side, array.get(HSIGNED, j) > 0 ? array.get(hst, j) * sgn : array.get(hst, j))
for j = 0 to NS - 1
array.set(DBG, 3 + j, array.get(side, j))
matrix.set(FEAT, 3 * CH + j, 0, (array.get(side, j) - array.get(SIDE_MU, j)) / array.get(SIDE_SD, j))
matrix<float> z = matrix.sum(matrix.mult(H0, FEAT), HB0)
float acc = HB3
for i = 0 to HID - 1
acc += math.max(matrix.get(z, i, 0), 0.0) * array.get(H3, i)
acc
clipZ(float x) =>
nz(math.max(math.min(x, 20.0), -20.0))
clipLog(float v) =>
nz(math.max(math.min(math.log(math.max(v, 0.001)), 5.0), -5.0))
// ---------------------------------------------------------------- per-bar inputs (closed='left' norm)
float rngNow = high - low
float nrm = array.size(RNG) == NN ? array.median(RNG) : na
bool goodN = not na(nrm) and nrm > 0
bool gap = bar_index > 0 and (time - time[1]) > GAPMS
bool bad = not goodN or gap or na(prevC)
float r = bad ? na : (close - prevC) / nrm
float st1 = bad ? na : (hl2 - prevC) / nrm - prevD1
float st2 = bad ? na : (hlc3 - prevC) / nrm - prevD2
float gg = bad ? na : rngNow / nrm
bool ok = not na(r) and not na(st1) and not na(st2) and not na(gg)
// ---------------------------------------------------------------- history before the window, in bars (no time, no EMA)
// scales of a window starting at this bar, from the 200 bars before it
float rmeanB = array.size(RNG) == NN ? array.avg(RNG) : na
float bmedB = array.size(BODY) == NN ? math.max(array.median(BODY), 0.05 * nrm) : na
float r16B = array.size(RNG) == NN ? array.avg(array.slice(RNG, NN - 16, NN)) : na
float r64B = array.size(RNG) == NN ? array.avg(array.slice(RNG, NN - 64, NN)) : na
float hmaxB = ta.highest(high, NN - 1) // bars b-198 .. b
float lminB = ta.lowest(low, NN - 1)
float cmaB = ta.sma(close, NN - 1)
// the window ending at this bar starts at s = bar_index - 63: scales of bar s are [63], bars s-200 .. s-2 end at [65]
float rmedW = nrm[L - 1]
float rmeanW = rmeanB[L - 1]
float bmedW = bmedB[L - 1]
float hmaxW = hmaxB[L + 1]
float lminW = lminB[L + 1]
float wW = hmaxW - lminW
float c2W = close[L + 1]
// path before the window in 8 chunks over bars s-200 .. s-2, oldest first: (close of the chunk's last bar - open of its
// first bar) / (sqrt(n) * norm of s), clipped to +-20
float P1h = clipZ((close[193] - open[263]) / math.sqrt(71.0) / rmedW) // 71 bars
float P2h = clipZ((close[129] - open[192]) / math.sqrt(64.0) / rmedW) // 64 bars
float P3h = clipZ((close[97] - open[128]) / math.sqrt(32.0) / rmedW) // 32 bars
float P4h = clipZ((close[81] - open[96]) / math.sqrt(16.0) / rmedW) // 16 bars
float P5h = clipZ((close[73] - open[80]) / math.sqrt(8.0) / rmedW) // 8 bars
float P6h = clipZ((close[69] - open[72]) / math.sqrt(4.0) / rmedW) // 4 bars
float P7h = clipZ((close[67] - open[68]) / math.sqrt(2.0) / rmedW) // 2 bars
float P8h = clipZ((close[65] - open[66]) / math.sqrt(2.0) / rmedW) // 2 bars
float R1h = wW > 0 ? 2.0 * (c2W - lminW) / wW - 1.0 : 0.0 // position of close[s-2] in the 199-bar range
float R2h = nz(math.max(math.min(math.log(math.max(wW, 0.000000000001) / (SQ199 * rmedW)), 5.0), -5.0)) // range width
float R3h = clipZ((c2W - cmaB[L + 1]) / (SQ199 * rmedW)) // close[s-2] from the mean close
float V1h = clipLog(r16B[L - 1] / rmeanW) // mean range of the last 16 bars / of 200
float V2h = clipLog(r64B[L - 1] / rmeanW) // the same for 64 bars
float V3h = clipLog(rmeanW / rmedW) // mean / median range
float V4h = clipLog(bmedW / rmedW) // median body / median range
array<float> HST = array.from(P1h, P2h, P3h, P4h, P5h, P6h, P7h, P8h, R1h, R2h, R3h, V1h, V2h, V3h, V4h)
int heavyFrom = calcBars == 0 ? 0 : math.max(last_bar_index - calcBars - L, 0)
float logit = na
if barstate.isconfirmed
array.push(RNG, rngNow)
array.push(BODY, math.abs(close - open))
if array.size(RNG) > NN
array.shift(RNG)
array.shift(BODY)
prevD1 := goodN ? (hl2 - close) / nrm : na
prevD2 := goodN ? (hlc3 - close) / nrm : na
prevC := close
okRun := ok ? okRun + 1 : 0
nConf += 1
array.shift(WR)
array.push(WR, ok ? r : 0.0)
array.shift(WABS)
array.push(WABS, ok ? math.abs(r) : 0.0)
if ok and firstR < 0
firstR := bar_index
array.fill(DBG, na)
if barstate.isconfirmed and bar_index >= heavyFrom
hc += 1
convStep(hc, ok ? r / S_SD : 0.0, ok ? st1 / S_SD : 0.0, ok ? st2 / S_SD : 0.0, ok ? gg / LVL_SD : 0.0)
if okRun >= L and bar_index - (L - 1) >= NN and hc >= L - 1
logit := computeLogit(hc, HST)
// ---------------------------------------------------------------- display
float ll = na(logit) ? na : PLATT_A * logit + PLATT_B
float Mraw = na(ll) ? na : math.exp(math.abs(ll))
bool more = not na(ll) and ll > 0
bool less = not na(ll) and ll < 0
float Mshow = na(Mraw) ? na : math.min(Mraw, more ? CEIL_O : CEIL_B)
color baseC = more ? colMore : colLess
plot(Mshow, "M", color = Mraw >= thr ? baseC : color.new(baseC, paleT), linewidth = 2)
hline(1.0, "Random (M = 1)", color = color.gray, linestyle = hline.style_solid)
hline(thr, "Threshold", color = color.gray, linestyle = hline.style_dashed)
// at the ceiling: "N+" once at the start of each run
bool atO = more and Mraw >= CEIL_O
bool atB = less and Mraw >= CEIL_B
if barstate.isconfirmed and atO and not atO[1]
label.new(bar_index, CEIL_O, str.tostring(CEIL_O) + "+", style = label.style_none, textcolor = colMore, size = size.small)
if barstate.isconfirmed and atB and not atB[1]
label.new(bar_index, CEIL_B, str.tostring(CEIL_B) + "+", style = label.style_none, textcolor = colLess, size = size.small)
plotshape(showMarks and more and Mraw >= thr, "Mark: more frequent than random", shape.circle, location.abovebar, colMore, size = size.tiny, force_overlay = true)
plotshape(showMarks and less and Mraw >= thr, "Mark: less frequent than random", shape.circle, location.abovebar, colLess, size = size.tiny, force_overlay = true)
// ---------------------------------------------------------------- messages
var table MSG = table.new(position.top_right, 1, 3, bgcolor = color.new(color.gray, 85))
if barstate.islast
table.clear(MSG, 0, 0, 0, 2)
int row = 0
if not tf5
table.cell(MSG, 0, row, "MRI: the model was trained and checked on 5-minute bars only", text_size = size.small, text_color = chart.fg_color)
row += 1
if not known
table.cell(MSG, 0, row, "MRI: calibrated on Binance spot; accuracy on this chart is not verified", text_size = size.small, text_color = chart.fg_color)
row += 1
int left = (firstR < 0 ? NN + 1 : math.max(firstR, NN + 1)) + L - 1 - bar_index
if left > 0 and calcBars == 0
table.cell(MSG, 0, row, "MRI warm-up: " + str.tostring(left) + " bars to the first value", text_size = size.small, text_color = chart.fg_color)
// ---------------------------------------------------------------- debug values (parity with the Python reference, R10 addition 2)
display_dbg = dbg ? display.all : display.none
plot(logit, "dbg logit", display = display_dbg, precision = 10)
plot(Mraw, "dbg M", display = display_dbg, precision = 10)
plot(nrm, "dbg norm", display = display_dbg, precision = 10)
plot(r, "dbg r", display = display_dbg, precision = 10)
plot(st1, "dbg step_hl2", display = display_dbg, precision = 10)
plot(st2, "dbg step_hlc3", display = display_dbg, precision = 10)
plot(gg, "dbg g", display = display_dbg, precision = 10)
plot(array.get(DBG, 0), "dbg D", display = display_dbg, precision = 10)
plot(array.get(DBG, 1), "dbg x_end", display = display_dbg, precision = 10)
plot(array.get(DBG, 2), "dbg sgn", display = display_dbg, precision = 10)
plot(array.get(DBG, 3), "dbg side_log_D", display = display_dbg, precision = 10)
plot(array.get(DBG, 4), "dbg side_log_mean_abs_r", display = display_dbg, precision = 10)
plot(array.get(DBG, 5), "dbg side_path_71", display = display_dbg, precision = 10)
plot(array.get(DBG, 6), "dbg side_path_64", display = display_dbg, precision = 10)
plot(array.get(DBG, 7), "dbg side_path_32", display = display_dbg, precision = 10)
plot(array.get(DBG, 8), "dbg side_path_16", display = display_dbg, precision = 10)
plot(array.get(DBG, 9), "dbg side_path_8", display = display_dbg, precision = 10)
plot(array.get(DBG, 10), "dbg side_path_4", display = display_dbg, precision = 10)
plot(array.get(DBG, 11), "dbg side_path_2a", display = display_dbg, precision = 10)
plot(array.get(DBG, 12), "dbg side_path_2b", display = display_dbg, precision = 10)
plot(array.get(DBG, 13), "dbg side_range_pos", display = display_dbg, precision = 10)
plot(array.get(DBG, 14), "dbg side_range_width", display = display_dbg, precision = 10)
plot(array.get(DBG, 15), "dbg side_close_dev", display = display_dbg, precision = 10)
plot(array.get(DBG, 16), "dbg side_vol_16", display = display_dbg, precision = 10)
plot(array.get(DBG, 17), "dbg side_vol_64", display = display_dbg, precision = 10)
plot(array.get(DBG, 18), "dbg side_range_mean_med", display = display_dbg, precision = 10)
plot(array.get(DBG, 19), "dbg side_body_med", display = display_dbg, precision = 10)
plot(nConf, "dbg confirmed", display = display_dbg, precision = 0)
plot(bar_index, "dbg bar_index", display = display_dbg, precision = 0)
Show all 541 lines