I'm currently working on a volume imbalance indicator which seems to be executing quite well.
This started as a result of me discovering this TradingView study: https://www.tradingview.com/script/MDxlrsRo-ICT-GAPs-and-Volume-Imbalance/
My idea is that once the a bar has closed above or below the corresponding gaps, it indicates that this imbalance has been corrected. However, I have run into an issue where the previous VI stops plotting before hitting the intended condition if a new loop starts for another VI gap. I would like for these VI to stay plotted until they have been corrected despite new VI forming.
share link: http://tos.mx/JDtTM3L
Any help is appreciated! Thank you.
This started as a result of me discovering this TradingView study: https://www.tradingview.com/script/MDxlrsRo-ICT-GAPs-and-Volume-Imbalance/
My idea is that once the a bar has closed above or below the corresponding gaps, it indicates that this imbalance has been corrected. However, I have run into an issue where the previous VI stops plotting before hitting the intended condition if a new loop starts for another VI gap. I would like for these VI to stay plotted until they have been corrected despite new VI forming.
Code:
#Volume Imbalance Indicator
#SymmetricalM
#V.01
#START
declare upper;
input Time_Frame = AggregationPeriod.FIFTEEN_MIN;
input ShowClouds = yes;
def H = high(period = Time_Frame);
def L = low(period = Time_Frame);
def O = open(period = Time_Frame);
def C = close(period = Time_Frame);
input tick = 0.25;
input FirstAGG = yes;
def na = double.nan;
input crossed_line = {"invisible", default "dashes", "solid"};
def cross;
if crossed_line == crossed_line."invisible" then {
cross = 1;
} else if crossed_line == crossed_line."dashes" then {
cross = 2;
} else if crossed_line == crossed_line."solid" then {
cross= 3;
} else {
cross = 0;
};
#--------------Bearish VI---------------#
#Bearish conditions:
def B1 = C[1] < o; #Main criteria
def B2 = c > o; #Current Bar criteria
def B3 = c[1] > o[1] and (o - c[1]) >= tick; #Previous Bar criteria
def triggerBe = if B1 and B2 and B3 then 1 else 0;
def bearish_bot = if triggerBe == 1 and FirstAGG then c[1] else bearish_bot[1];
def bearish_top = if triggerBe == 1 and FirstAGG then o else bearish_top[1];
# Flag to track if the condition has been met
def stopCondBe = c < bearish_top;
def holdBe = if triggerBe == 1 then 0
else if holdBe[1] == 1 then holdBe[1]
else if stopCondBe then 1
else holdBe[1];
plot bear_imbalance_top_line = if holdBe[1] == 1 then cross else bearish_top;
plot bear_imbalance_bot_line = if holdBe[1] == 1 then cross else bearish_bot;
bear_imbalance_top_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_bot_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_top_line.SetDefaultColor(color.plum);
bear_imbalance_bot_line.SetDefaultColor(color.plum);
def bearish_bot1 = if triggerBe == 1 and FirstAGG then c[1] else na;
def bearish_top1 = if triggerBe == 1 and FirstAGG then o else na;
plot bear_imbalance_top_line1 = If (bearish_top1, o, na);
bear_imbalance_top_line1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_top_line1.SetDefaultColor(color.plum);
bear_imbalance_top_line1.SetLineWeight(1);
bear_imbalance_top_line1.HideBubble();
#addCloud(bear_imbalance_top_line, bear_imbalance_bot_line, color.red, color.red);
plot bear_imbalance_bot_line1 = If (bearish_bot1, c[1], na);
bear_imbalance_bot_line1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_bot_line1.SetDefaultColor(color.plum);
bear_imbalance_bot_line1.SetLineWeight(1);
bear_imbalance_bot_line1.HideBubble();
#--------------Bullish VI---------------#
#Bullish conditions:
def Bu1 = C[1] > O; #Main criteria
def Bu2 = o > c; #Current Bar criteria
def Bu3 = o[1] > c[1] and (c[1] - o) >= tick; #Previous Bar criteria
def triggerBu = if Bu1 and Bu2 and Bu3 then 1 else 0;
def bullish_top = if triggerBu == 1 and FirstAGG then c[1] else bullish_top[1];
def bullish_bot = if triggerBu == 1 and FirstAGG then o else bullish_bot[1];
# Flag to track if the condition has been met
def stopCondBu = c > bullish_bot;
def holdBu = if triggerBu == 1 then 0
else if holdBu[1] == 1 then holdBu[1]
else if stopCondBu then 1
else holdBu[1];
plot bull_imbalance_top_line = if holdBu[1] == 1 then cross else bullish_top;
plot bull_imbalance_bot_line = if holdBu[1] == 1 then cross else bullish_bot;
bull_imbalance_top_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_bot_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_top_line.SetDefaultColor(color.plum);
bull_imbalance_bot_line.SetDefaultColor(color.plum);
#addCloud(bull_imbalance_top_line, bull_imbalance_bot_line, color.green, color.green);
def bullish_top1 = if triggerBu == 1 and FirstAGG then c[1] else na;
def bullish_bot1 = if triggerBu == 1 and FirstAGG then o else na;
plot bull_imbalance_top_line1 = If (bullish_top1, c[1], na);
bull_imbalance_top_line1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_top_line1.SetDefaultColor(color.plum);
bull_imbalance_top_line1.SetLineWeight(1);
bull_imbalance_top_line1.HideBubble();
plot bull_imbalance_bot_line1 = If (bullish_bot1, o, na);
bull_imbalance_bot_line1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_bot_line1.SetDefaultColor(color.plum);
bull_imbalance_bot_line1.SetLineWeight(1);
bull_imbalance_bot_line1.HideBubble();
#END
share link: http://tos.mx/JDtTM3L
Any help is appreciated! Thank you.