Well,
I'm quite sure this will repaint. It uses next bar calculations, and I'm not sure how to rework it so it wouldn't. The original does some trickery setting past values of an indexed array (which we can't do in ToS) and I have to resort to future values to achieve the same result.
It's not pretty code. I didn't put a lot of effort in.
I'm quite sure this will repaint. It uses next bar calculations, and I'm not sure how to rework it so it wouldn't. The original does some trickery setting past values of an indexed array (which we can't do in ToS) and I have to resort to future values to achieve the same result.
It's not pretty code. I didn't put a lot of effort in.
Code:
declare upper;
#
# Volume Imbalance
#
# After code from: https://in.tradingview.com/script/MDxlrsRo-ICT-GAPs-and-Volume-Imbalance/
#
# [partial] ToS conversion by mashume for the usethinkscript.com community 2022-12-24
#
# Probably Repaints -- USE AT YOUR OWN RISK
#
# Bullish Volume Imbalance
def bullimbalance = if open > close[1] and low <= high[1] and close > close[1] and open > open[1] then 1 else 0;
def bulltopline = if bullimbalance[-1] == 1 then min(OPEN, CLOSE) else if bullimbalance == 1 then bulltopline[1] else if bullimbalance[1] == 1 then bulltopline[1] else double.nan;
plot bullboxtop = bulltopline;
def bullbottomline = if bullimbalance[-1] == 1 then max(close[1], open[1]) else if bullimbalance == 1 then bullbottomline[1] else if bullimbalance[1] == 1 then bullbottomline[1] else double.nan;
plot bullboxbottom = bullbottomline;
bullboxtop.SetDefaultColor(color.green);
bullboxbottom.SetDefaultColor(color.green);
AddCloud(bullboxtop, bullboxbottom, color.green, color.green);
# Bearish Volume Imbalance
def bearimbalance = if open < close[1] and open < open[1] and high >= low[1] and close < close[1] and close < open[1] then 1 else 0;
def beartopline = if bearimbalance[-1] == 1 then min(close[1], open[1]) else if bearimbalance == 1 then beartopline[1] else if bearimbalance[1] then beartopline[1] else double.nan;
plot bearboxtop = beartopline;
def bearbottomline = if bearimbalance[-1] == 1 then max(open, close) else if bearimbalance == 1 then bearbottomline[1] else if bearimbalance[1] == 1 then bearbottomline[1] else double.nan;
plot bearboxbottom = bearbottomline;
AddCloud(bearboxtop, bearboxbottom, color.red, color.red);
bearboxtop.SetDefaultColor(color.red);
bearboxbottom.SetDefaultColor(color.red);