Repaints Volume Imbalance For ThinkOrSwim

Repaints

mashume

Expert
VIP
Lifetime
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.

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);
 
Thank you! what timeframe is that? I don't see it being accurate on the 1 hr or 5 min when testing
It should run on whatever timeframe you put it on. I do not have anything to use for reference. I know code, but not what the desired behaviour is in this case. If you have some specific examples (or a chart showing a comparison of the desired and actual plots) that would help, certainly. Simply telling me that it is "not accurate" doesn't help fix whatever issues there may be.

-mashume
 
It should run on whatever timeframe you put it on. I do not have anything to use for reference. I know code, but not what the desired behaviour is in this case. If you have some specific examples (or a chart showing a comparison of the desired and actual plots) that would help, certainly. Simply telling me that it is "not accurate" doesn't help fix whatever issues there may be.

-mashume

Thank you for taking the time to write the script. I checked it and it's not really calculating properly and I appreciate that you need to have a better explanation so I put together the below picture so you get the ideas if what I am talking about. I hope this helps and thank you!


2022-12-29-22-53-17.jpg
 
Try this:
Code:
declare upper;

def bearish = if open < close[1] and open[1] > close[1] then 1 else 0;

def bearish_top = if bearish == 1 then close[1] else bearish_top[1];
def bearish_bot = if bearish == 1 then open else bearish_bot[1];
plot bear_imbalance_top_line = bearish_top;
plot bear_imbalance_bot_line = bearish_bot;
addCloud(bear_imbalance_top_line, bear_imbalance_bot_line, color.red, color.red);
bear_imbalance_top_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_bot_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_top_line.SetDefaultColor(color.red);
bear_imbalance_bot_line.SetDefaultColor(color.red);

def bullish = if open > close[1] and open[1] < close[1] then 1 else 0;

def bullish_top = if bullish == 1 then open else bullish_top[1];
def bullish_bot = if bullish == 1 then close[1] else bullish_bot[1];
plot bull_imbalance_top_line = bullish_top;
plot bull_imbalance_bot_line = bullish_bot;
addCloud(bull_imbalance_top_line, bull_imbalance_bot_line, color.green, color.green);

bull_imbalance_top_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_bot_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_top_line.SetDefaultColor(color.green);
bull_imbalance_bot_line.SetDefaultColor(color.green);

I'm taking a guess here, since what you're asking for is not exactly what the previously provided script did for calculating "imbalances".

-mashume
 
@mashume Appreciate what youve written!
Is there a way to extend the indicator until it is mitigated? Perhaps even put a threshold on what we might consider mitigation?
There are scripts coming out in Pinescript that may help. I do not know how to convert Pinescript so I will give you a reference if youd like to continue working on it.
Thanks!
Gaps + Volume Imbalances (by Vulnerable_Human_x on TradingView)
 
@mashume Appreciate what youve written!
Is there a way to extend the indicator until it is mitigated? Perhaps even put a threshold on what we might consider mitigation?
There are scripts coming out in Pinescript that may help. I do not know how to convert Pinescript so I will give you a reference if youd like to continue working on it.
Thanks!
Gaps + Volume Imbalances (by Vulnerable_Human_x on TradingView)
if you have a definition of mitigation (the more specific the better), it can likely be coded.

-mashume
 
Thank you for making this script! I am getting some false positives when testing. Please see in the below image. Thank you again!

2023-01-02-14-54-02.jpg
 
if you have a definition of mitigation (the more specific the better), it can likely be coded.

-mashume


Mitigation in the sense that price has traded back into the range.
To define further maybe we could include a time or volume minimum to consider "true" mitigation.
I have found that simply revisiting price ranges or "closing the gap" is sufficient for my needs.
I think of the behavior as similar to vPOC and give myself ideas of where price might go, not necessarily what its going to do once there.
Balanced Price Ranges can also be incorporated into the definition of mitigation. BPR is an ICT concept that considers back to back fair value gaps (Price Imbalances) and price action returning to those zones to "fill in" buyside/sellside liquidity and thus balancing the area.
Sometimes prices comes to simply tap a VolumeImbalance zone and sometimes it moves to consolidate so I am not familiar enough to say definitively.
** EDIT ** Loving the study so far and I am recognizing the challenge of declaring something mitigated.
 
Last edited:
Thanks for your work @mashume
I made an edit to the bullish/bearish definitions as the original was getting plots that were not what we were looking for as a "Volume Imbalance"
Here is an update that is working so far, with an added MTF feature.
I am still interested in adding a feature to end plot once Mitigated. The challenge is that some of the zones seem to continue to provide insight even after price passes through or to them.
My suggestion would be to instead of ending plot once a new plot is made, we either:
  • Create an input for (x) amount of plots to remain on the chart.
  • Create a definition for the number of times price has passed through the plot
  • Create a definition for the amount of time price has spent at or within the zone
  • Calculate a cumulative volume for the zone that creates a plot, then once the price returns and trades that volume by a factor of x (1:1, 1:2..) then declare the zone mitigated.
7BCAD720-BD66-4279-9F24-16D747875ABB.png

http://tos.mx/EOTZp0H
 
Last edited by a moderator:
Thank you all for the awesome work! this script works and does what it suppose to, however, the "mtf agg" time doesn't work. so I I try to plot 1min Volume Imbalance on a 5min chart it will plot the 5min volume imbalance instead.

I was just coming from the fact that the FVG script posted here is allowing you to change the time so it paints another time on while you are on different time in TOS.
What is the benefit of having the time frame drop down if it won't matter what you select?
 
Last edited by a moderator:
Thank you all for the awesome work! this script works and does what it suppose to, however, the "mtf agg" time doesn't work. so I I try to plot 1min Volume Imbalance on a 5min chart it will plot the 5min volume imbalance instead.

I was just coming from the fact that the FVG script posted here is allowing you to change the time so it paints another time on while you are on different time in TOS.
What is the benefit of having the time frame drop down if it won't matter what you select?
I am not sure what you are saying or asking.
Obviously it "matters" what timeframe you select.

Putting a daily aggregation on a 5min chart would not be helpful. It would be repainting all day long.
A 15min timeframe on a 5min chart is beneficial as it gives you the bigger picture of what is going on with trend.

You can choose whatever timeframe that you want. As long as it is a HIGHER timeframe than your current chart.
The ToS platform does not support adding timeframes that are lower than the current chart.

Read more here:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-58081
 
Thank you, make sense. However, my point was that in this script, when I change the "mtf agg" time lower or higher (5 min chart and 1 min aggregation or 1 min chart and 5 min aggregation) it is not changing anything in this particilar script.
 
Thanks for your work @mashume
I made an edit to the bullish/bearish definitions as the original was getting plots that were not what we were looking for as a "Volume Imbalance"
Here is an update that is working so far, with an added MTF feature.
I am still interested in adding a feature to end plot once Mitigated. The challenge is that some of the zones seem to continue to provide insight even after price passes through or to them.
My suggestion would be to instead of ending plot once a new plot is made, we either:
  • Create an input for (x) amount of plots to remain on the chart.
  • Create a definition for the number of times price has passed through the plot
  • Create a definition for the amount of time price has spent at or within the zone
  • Calculate a cumulative volume for the zone that creates a plot, then once the price returns and trades that volume by a factor of x (1:1, 1:2..) then declare the zone mitigated.
7BCAD720-BD66-4279-9F24-16D747875ABB.png

http://tos.mx/EOTZp0H
Please post your code here so that we can see what you did without having to install the indicator. There are questions below about the MTF implementation, but no code that can be seen to resolve them without installation.
-mashume
 
Please post your code here so that we can see what you did without having to install the indicator. There are questions below about the MTF implementation, but no code that can be seen to resolve them without installation.
-mashume
#Created by Mashume
#Edited by Marcellulose

declare upper;

Input UseChartAgg = yes;
Input MTFAgg = AggregationPeriod.FIVE_MIN;

Def Period = If USEChartAgg==no then MTFAgg else getAggregationPeriod();

Def Open = Open(period=period);
Def Close = Close(period=period);


DefineGlobalColor("Bullish Imbalance", Createcolor(35,110,85));
DefineGlobalColor("Bearish Imbalance", CreateColor(175,0,85));
DefineGlobalColor("Bullish Imbalance Cloud", Createcolor(85,0,85));
DefineGlobalColor("Bearish Imbalance Cloud", CreateColor(85,0,85));


def bearish = if open < close[1] and
close < close[1] and
open < Open[1] then 1 else 0;

def bearish_top = if bearish == 1 then close[1] else bearish_top[1];
def bearish_bot = if bearish == 1 then open else bearish_bot[1];
plot bear_imbalance_top_line = bearish_top;
plot bear_imbalance_bot_line = bearish_bot;
addCloud(bear_imbalance_top_line, bear_imbalance_bot_line,GlobalColor("Bearish Imbalance Cloud"),GlobalColor("Bearish Imbalance Cloud"));
bear_imbalance_top_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_bot_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bear_imbalance_top_line.SetDefaultColor(GlobalColor("Bearish Imbalance"));
bear_imbalance_bot_line.SetDefaultColor(GlobalColor("Bearish Imbalance"));

def bullish = if open > close[1] and
open > Open[1] and
Close > open then 1 else 0;

def bullish_top = if bullish == 1 then open else bullish_top[1];
def bullish_bot = if bullish == 1 then close[1] else bullish_bot[1];
plot bull_imbalance_top_line = bullish_top;
plot bull_imbalance_bot_line = bullish_bot;
addCloud(bull_imbalance_top_line, bull_imbalance_bot_line,GlobalColor("Bullish Imbalance Cloud") ,GlobalColor("Bullish Imbalance Cloud"));

bull_imbalance_top_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_bot_line.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bull_imbalance_top_line.SetDefaultColor(GlobalColor("Bullish Imbalance"));
bull_imbalance_bot_line.SetDefaultColor(GlobalColor("Bullish Imbalance"));
 
Thank you, make sense. However, my point was that in this script, when I change the "mtf agg" time lower or higher (5 min chart and 1 min aggregation or 1 min chart and 5 min aggregation) it is not changing anything in this particilar script.
From looking over @MP432 's code, if you don't set UseChartAgg to "no" then it won't matter if you change the mtf agg setting.

@MP432 , it's generally not a good idea to use reserved words as your variables. It doesn't throw errors, but it makes the code harder to understand than it needs to be. You have this:
Code:
Def Open = Open(period=period);
Def Close = Close(period=period);
in your script, and then reference open and close through the rest of the script. It would be cleaner and easier to read code if your defs at the outset were like this:
Code:
Def v_Open = Open(period=period);
Def v_Close = Close(period=period);
v_ just reminds me that I've set it as a variable rather than the built in data holder. The bullish and bearish conditionals then look like this:
Code:
def bearish = if v_open < v_close[1] and
v_close < v_close[1] and
v_open < v_Open[1] then 1 else 0;
and
Code:
def bullish = if v_open > v_close[1] and
v_open > v_Open[1] and
v_Close > v_open then 1 else 0;
and then we don't have any problem deciding why the mtf aspects of the code do or don't work (we know for certain that we have defined the v_close and v_open to be for the correct aggregation period, where using close and open we hope so but can't be 100%).

This is style stuff, which borders on function and debugging stuff. Hope it helps.

-mashume
 
@MP432 Hi, may I know if you manage to create the new script that are able to plot the conditions you listed below?

My suggestion would be to instead of ending plot once a new plot is made, we either:
  • Create an input for (x) amount of plots to remain on the chart.
  • Create a definition for the number of times price has passed through the plot
  • Create a definition for the amount of time price has spent at or within the zone
  • Calculate a cumulative volume for the zone that creates a plot, then once the price returns and trades that volume by a factor of x (1:1, 1:2..) then declare the zone mitigated.
I have a similar request which is to end the Volume imbalance plot once there are 5 body closes over the gap.

Thanks
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
493 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