Volume Imbalance MTF

SymmetricalM

New member
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.

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.
 

Attachments

  • VI.PNG
    VI.PNG
    50.3 KB · Views: 636

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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.

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.
Try using this - I think this is what you are looking for - Fair Value Gap
input threshold = 0.1;
input aggregation1 = AggregationPeriod.FIFTEEN_MIN;
input aggregation2 = AggregationPeriod.TEN_MIN;
input aggregation3 = AggregationPeriod.FIVE_MIN;
input showAggregation2 = yes;
input showAggregation3 = yes;
input rthOnly = no;
input fillGaps = yes;

# Note that aggregation periods below then current chart period will NOT show

def rth = if SecondsTillTime(0930) <= 0 and SecondsTillTime(1600) > 0 then 1 else 0;
def rthCheck = if rthOnly then if rth then yes else no else yes;

# timeframe1

def low1 = low(period = aggregation1);
def high1 = high(period = aggregation1);

def fvgBear1 = if low1[2] - high1 > 0 then 1 else 0;
def fvgBearPctg1 = if AbsValue((high1 -low1[2])/low1[2]) > threshold/100 then 1 else 0;

def fvgBull1 = if low1 - high1[2] > 0 then 1 else 0;
def fvgBullPctg1 = if AbsValue((low1 - high1[2])/high1[2]) > threshold/100 then 1 else 0;

def fvgBearH1 = if fvgBear1 and fvgBearPctg1 then low1[2] else 0;
def fvgBearL1 = if fvgBearH1 then high1 else Double.NaN;

def fvgBullH1 = if fvgBull1 and fvgBullPctg1 then low1 else 0;
def fvgBullL1 = if fvgBullH1 then high1[2] else Double.NaN;

def fvgBear1MemHigh = if fvgBearH1 then fvgBearH1 else if high1 > fvgBear1MemHigh[1] then Double.NaN else fvgBear1MemHigh[1];
def fvgBear1MemLow = if fvgBearH1 then fvgBearL1 else if high1 > fvgBear1MemHigh[1] then Double.NaN else if fillGaps and high1 > fvgBear1MemLow[1] then high1 else fvgBear1MemLow[1];

def fvgBull1MemLow = if fvgBullH1 then fvgBullL1 else if low1 < fvgBull1MemLow[1] then Double.NaN else fvgBull1MemLow[1];
def fvgBull1MemHigh = if fvgBullH1 then fvgBullH1 else if low1 < fvgBull1MemLow[1] then Double.NaN else if fillGaps and low1 < fvgBull1MemHigh[1] then low1 else fvgBull1MemHigh[1];

def fvgBear1MH = if fvgBear1MemHigh then fvgBear1MemHigh else double.nan;
def fvgBear1ML = if fvgBear1MemLow then fvgBear1MemLow else double.nan;

def fvgBull1MH = if fvgBull1MemHigh then fvgBull1MemHigh else double.nan;
def fvgBull1ML = if fvgBull1MemLow then fvgBull1MemLow else double.nan;

# timeframe 2

def low2 = low(period = aggregation2);
def high2 = high(period = aggregation2);

def fvgBear2 = if low2[2] - high2 > 0 then 1 else 0;
def fvgBearPctg2 = if AbsValue((high2 -low2[2])/low2[2]) > threshold/100 then 1 else 0;

def fvgBull2 = if low2 - high2[2] > 0 then 1 else 0;
def fvgBullPctg2 = if AbsValue((low2 - high2[2])/high2[2]) > threshold/100 then 1 else 0;

def fvgBearH2 = if fvgBear2 and fvgBearPctg2 then low2[2] else 0;
def fvgBearL2 = if fvgBearH2 then high2 else Double.NaN;

def fvgBullH2 = if fvgBull2 and fvgBullPctg2 then low2 else 0;
def fvgBullL2 = if fvgBullH2 then high2[2] else Double.NaN;

def fvgBear2MemHigh = if fvgBearH2 then fvgBearH2 else if high2 > fvgBear2MemHigh[1] then Double.NaN else fvgBear2MemHigh[1];
def fvgBear2MemLow = if fvgBearH2 then fvgBearL2 else if high2 > fvgBear2MemHigh[1] then Double.NaN else if fillGaps and high2 > fvgBear2MemLow[1] then high2 else fvgBear2MemLow[1];

def fvgBull2MemLow = if fvgBullH2 then fvgBullL2 else if low2 < fvgBull2MemLow[1] then Double.NaN else fvgBull2MemLow[1];
def fvgBull2MemHigh = if fvgBullH2 then fvgBullH2 else if low2 < fvgBull2MemLow[1] then Double.NaN else if fillGaps and low2 < fvgBull2MemHigh[1] then low2 else fvgBull2MemHigh[1];

def fvgBear2MH = if fvgBear2MemHigh then fvgBear2MemHigh else double.nan;
def fvgBear2ML = if fvgBear2MemLow then fvgBear2MemLow else double.nan;

def fvgBull2MH = if fvgBull2MemHigh then fvgBull2MemHigh else double.nan;
def fvgBull2ML = if fvgBull2MemLow then fvgBull2MemLow else double.nan;

# timeframe 3

def low3 = low(period = aggregation3);
def high3 = high(period = aggregation3);

def fvgBear3 = if low3[2] - high3 > 0 then 1 else 0;
def fvgBearPctg3 = if AbsValue((high3 -low3[2])/low3[2]) > threshold/100 then 1 else 0;

def fvgBull3 = if low3 - high3[2] > 0 then 1 else 0;
def fvgBullPctg3 = if AbsValue((low3 - high3[2])/high3[2]) > threshold/100 then 1 else 0;

def fvgBearH3 = if fvgBear3 and fvgBearPctg3 then low3[2] else 0;
def fvgBearL3 = if fvgBearH3 then high3 else Double.NaN;

def fvgBullH3 = if fvgBull3 and fvgBullPctg3 then low3 else 0;
def fvgBullL3 = if fvgBullH3 then high3[2] else Double.NaN;

def fvgBear3MemHigh = if fvgBearH3 then fvgBearH3 else if high3 > fvgBear3MemHigh[1] then Double.NaN else fvgBear3MemHigh[1];
def fvgBear3MemLow = if fvgBearH3 then fvgBearL3 else if high3 > fvgBear3MemHigh[1] then Double.NaN else if fillGaps and high3 > fvgBear3MemLow[1] then high3 else fvgBear3MemLow[1];

def fvgBull3MemLow = if fvgBullH3 then fvgBullL3 else if low3 < fvgBull3MemLow[1] then Double.NaN else fvgBull3MemLow[1];
def fvgBull3MemHigh = if fvgBullH3 then fvgBullH3 else if low3 < fvgBull3MemLow[1] then Double.NaN else if fillGaps and low3 < fvgBull3MemHigh[1] then low3 else fvgBull3MemHigh[1];

def fvgBear3MH = if fvgBear3MemHigh then fvgBear3MemHigh else double.nan;
def fvgBear3ML = if fvgBear3MemLow then fvgBear3MemLow else double.nan;

def fvgBull3MH = if fvgBull3MemHigh then fvgBull3MemHigh else double.nan;
def fvgBull3ML = if fvgBull3MemLow then fvgBull3MemLow else double.nan;

# plotting

DefineGlobalColor("Aggregation 1", Color.GREEN);
DefineGlobalColor("Aggregation 2", Color.BLUE);
DefineGlobalColor("Aggregation 3", Color.MAGENTA);

AddCloud(if rthCheck then fvgBear1MH else double.nan, if rthCheck then fvgBear1ML else double.nan, GlobalColor("Aggregation 1"));
AddCloud(if rthCheck then fvgBull1MH else double.nan, if rthCheck then fvgBull1ML else double.nan, GlobalColor("Aggregation 1"));

AddCloud(if rthCheck and showAggregation2 then fvgBear2MH else double.nan, if rthCheck and showAggregation2 then fvgBear2ML else double.nan, GlobalColor("Aggregation 2"));
AddCloud(if rthCheck and showAggregation2 then fvgBull2MH else double.nan, if rthCheck and showAggregation2 then fvgBull2ML else double.nan, GlobalColor("Aggregation 2"));

AddCloud(if rthCheck and showAggregation3 then fvgBear3MH else double.nan, if rthCheck and showAggregation3 then fvgBear3ML else double.nan, GlobalColor("Aggregation 3"));
AddCloud(if rthCheck and showAggregation3 then fvgBull3MH else double.nan, if rthCheck and showAggregation3 then fvgBull3ML else double.nan, GlobalColor("Aggregation 3"));
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
518 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top