Is something not right … attached is what I see on US30 on 1K bars on 1 min TF.
Thanks for the reports Grahal! there was indeed a bug that only showed up on high-priced instruments such as Wall Street, Nasdaq, Bitcoin or Berkshire-A. On those, the oscillator stayed pinned near the upper band instead of fluctuating.
Root cause: whenever a pivot was broken, the code marked it as “consumed” by overwriting its stored price with exp(10) (≈ 22 026), the idea being that no price could ever trade above that value, so the pivot would never be counted again. That assumption holds for stocks priced in tens or hundreds, but breaks down on assets quoted above ~22 000 — there, the condition closeTypeBull > exp(10) keeps evaluating to true bar after bar, so the same pivots are recounted indefinitely and the running count series saturates the RSI.
I’ve rewritten the pivot-break detection so it no longer depends on the magnitude of the price: broken pivots are popped out of the stack instead of marked with a “magic number”. I also removed a typo in the count-accumulation loop that was making the issue worse.
Updated code (v1):
//------------------------------------------------------------//
//PRC_Market Structure RSI
//version = 1
//05.05.26
//Iván González @ www.prorealcode.com
//Sharing ProRealTime knowledge
//
//v1: Bug fix for high-priced instruments (DJI, BTC, Berkshire,
// Nasdaq…). Replaced the exp(10) "deleted-pivot" marker —
// which fails when price > ~22 000 — with proper pop logic,
// and fixed a total-accumulation typo.
//------------------------------------------------------------//
//-----Inputs-------------------------------------------------//
rsiLength=20
OBlvl=80
OSlvl=20
strCloseType=0
maBool=1
maSelection=1
maLength=8
color=1
signals=0
//------------------------------------------------------------//
//-----Close Type---------------------------------------------//
if strCloseType then
closeTypeBull=close[1]
closeTypeBear=close[1]
else
closeTypeBull=high[1]
closeTypeBear=low[1]
endif
//------------------------------------------------------------//
//-----Get Pivots---------------------------------------------//
pH=high[3]<high[2] and high[2]>high[1]
pL=low[3]>low[2] and low[2]<low[1]
if pH then
z=z+1
$highprice[z]=high[2]
endif
if pL then
t=t+1
$lowprice[t]=low[2]
endif
//------------------------------------------------------------//
//-----Cascade-break detection (price-magnitude independent)--//
while z>0 and closeTypeBull>$highprice[z] do
total=total+1
z=z-1
wend
while t>0 and closeTypeBear<$lowprice[t] do
total=total-1
t=t-1
wend
//------------------------------------------------------------//
//-----RSI total----------------------------------------------//
rsitotal=rsi[rsiLength](total)
//------------------------------------------------------------//
//-----RSI Moving Average-------------------------------------//
if maBool then
rsmMATotal=average[maLength,maSelection](rsitotal)
else
rsmMATotal=undefined
endif
//------------------------------------------------------------//
//-----Plot configuration-------------------------------------//
if color then
if rsitotal > 50 then
rc=0
gc=255
else
rc=255
gc=0
endif
colorbetween(rsitotal,50,rc,gc,0,50)
endif
//------------------------------------------------------------//
//-----Draw Signals-------------------------------------------//
if signals then
if rsiTotal crosses under OBlvl then
drawpoint(barindex,OBlvl,2)coloured("red")
elsif rsiTotal crosses over OSlvl then
drawpoint(barindex,OSlvl,2)coloured("green")
endif
endif
//------------------------------------------------------------//
return rsitotal, rsmMATotal coloured("blue")style(line,4), OBlvl as "Overbought Level"style(dottedline2), OSlvl as "Oversold level"style(dottedline2)