Convert Volume Pressure To MTF

Bear.22

New member
VIP
Guys,

Not sure if this is the right forum; Requesting a modification (if possible) to the indicator below. I am looking to add a time selection input of 30min, 1 hour, 4 hour, Day, so that the buy and sell percentage labels reflect the input selected time frame? Is this possible? If so, can somebody write the appropriate script additions for me?

Code:
plot Data = close;declare upper;

def today = GetDay() == GetLastDay();
def midPrice = (high + low) / 2;

# Only count volume during today's session
def buyVol = if close > midPrice and today then volume else 0;
def sellVol = if close < midPrice and today then volume else 0;

# Accumulate today's buy/sell volume
def buyVolSum = TotalSum(buyVol);
def sellVolSum = TotalSum(sellVol);
def totalVol = buyVolSum + sellVolSum;

# Calculate %
def buyPct = if totalVol > 0 then (buyVolSum / totalVol) * 100 else 0;
def sellPct = if totalVol > 0 then (sellVolSum / totalVol) * 100 else 0;

# Display labels
AddLabel(yes, "Buy %: " + Round(buyPct, 1) + "%", Color.darK_GREEN);
AddLabel(yes, "Sell %: " + Round(sellPct, 1) + "%", Color.RED);

Thanks Much!
G
 
Last edited by a moderator:
Solution
On Daily+ charts, the GetDay() == GetLastDay() restriction would otherwise make the script useless, so I bypassed it on larger aggregations. Although, I suspect you might want something like only using the latest month on Daily, or maybe the latest Year on Weekly, or something like that. I wasn't sure how you need that handled. Let me know...

Code:
declare upper;
#
input SetAgg = AggregationPeriod.HOUR;
def Agg =
    if (SetAgg < GetAggregationPeriod(), GetAggregationPeriod() ,SetAgg);
AddLabel(SetAgg < GetAggregationPeriod(),
    " Bad Agg, Using Current: " + GetAggregationPeriod() / 60000 + " Min. "
,color.light_red);

def today =
    (Agg >= AggregationPeriod.DAY)
    or GetDay() == GetLastDay();
#
def midPrice = (high(period = Agg) +...
On Daily+ charts, the GetDay() == GetLastDay() restriction would otherwise make the script useless, so I bypassed it on larger aggregations. Although, I suspect you might want something like only using the latest month on Daily, or maybe the latest Year on Weekly, or something like that. I wasn't sure how you need that handled. Let me know...

Code:
declare upper;
#
input SetAgg = AggregationPeriod.HOUR;
def Agg =
    if (SetAgg < GetAggregationPeriod(), GetAggregationPeriod() ,SetAgg);
AddLabel(SetAgg < GetAggregationPeriod(),
    " Bad Agg, Using Current: " + GetAggregationPeriod() / 60000 + " Min. "
,color.light_red);

def today =
    (Agg >= AggregationPeriod.DAY)
    or GetDay() == GetLastDay();
#
def midPrice = (high(period = Agg) + low(period = Agg)) / 2;#

# Only count volume during today's session
def buyVol = if close(period = Agg) > midPrice and today then volume(period = Agg) else 0;#
def sellVol = if close(period = Agg) < midPrice and today then volume(period = Agg) else 0;#

# Accumulate today's buy/sell volume
def buyVolSum = TotalSum(buyVol);
def sellVolSum = TotalSum(sellVol);
def totalVol = buyVolSum + sellVolSum;

# Calculate %
def buyPct = if totalVol > 0 then (buyVolSum / totalVol) * 100 else 0;
def sellPct = if totalVol > 0 then (sellVolSum / totalVol) * 100 else 0;

# Display labels
AddLabel(yes, "Buy %: " + Round(buyPct, 1) + "%", Color.LIME);
AddLabel(yes, "Sell %: " + Round(sellPct, 1) + "%", Color.Orange);
 
Last edited:
Solution
On Daily+ charts, the GetDay() == GetLastDay() restriction would otherwise make the script useless, so I bypassed it on larger aggregations. Although, I suspect you might want something like only using the latest month on Daily, or maybe the latest Year on Weekly, or something like that. I wasn't sure how you need that handled. Let me know...

Code:
declare upper;
#
input SetAgg = AggregationPeriod.HOUR;
def Agg =
    if (SetAgg < GetAggregationPeriod(), GetAggregationPeriod() ,SetAgg);
AddLabel(SetAgg < GetAggregationPeriod(),
    " Bad Agg, Using Current: " + GetAggregationPeriod() / 60000 + " Min. "
,color.light_red);

def today =
    (Agg >= AggregationPeriod.DAY)
    or GetDay() == GetLastDay();
#
def midPrice = (high(period = Agg) + low(period = Agg)) / 2;#

# Only count volume during today's session
def buyVol = if close(period = Agg) > midPrice and today then volume(period = Agg) else 0;#
def sellVol = if close(period = Agg) < midPrice and today then volume(period = Agg) else 0;#

# Accumulate today's buy/sell volume
def buyVolSum = TotalSum(buyVol);
def sellVolSum = TotalSum(sellVol);
def totalVol = buyVolSum + sellVolSum;

# Calculate %
def buyPct = if totalVol > 0 then (buyVolSum / totalVol) * 100 else 0;
def sellPct = if totalVol > 0 then (sellVolSum / totalVol) * 100 else 0;

# Display labels
AddLabel(yes, "Buy %: " + Round(buyPct, 1) + "%", Color.LIME);
AddLabel(yes, "Sell %: " + Round(sellPct, 1) + "%", Color.Orange);
Joshua, I am by no means an indicator expert. Looking at the original script, can you explain to me the from where (time-wise) the script is calculating and presenting the data from? If it helps my chart viewing is typically set at 1D:1M.

Thanks!
 
It'll calculate based on which ever setting you select under SetAgg.

But, secondary aggregations can only go higher, not lower. So if the chart itself is something like 30 minutes, but you select 5 minutes as SetAgg, it won't work. In that case, it will just default to the chart's native aggregation of 30 minutes to avoid throwing an script error.

The original script also only calculated based on today's data exclusively. Therefore, if you were to select Daily or higher, it would only be calculating based on a single bar, which is pointless... So, I removed that condition for Daily+ aggregations.. it just uses length of the chart in those cases. But, I suppose that if you're only using one day's worth of one minute bars, it reintroduces the problem.

I can probably add a second input, for a length of Days/Weeks/Moths, which could extract data from beyond the beginning of the chart if those aggregations are used as the SetAgg setting. Again, it depends on how you would prefer it handled though.
 
It'll calculate based on which ever setting you select under SetAgg.

But, secondary aggregations can only go higher, not lower. So if the chart itself is something like 30 minutes, but you select 5 minutes as SetAgg, it won't work. In that case, it will just default to the chart's native aggregation of 30 minutes to avoid throwing an script error.

The original script also only calculated based on today's data exclusively. Therefore, if you were to select Daily or higher, it would only be calculating based on a single bar, which is pointless... So, I removed that condition for Daily+ aggregations.. it just uses length of the chart in those cases. But, I suppose that if you're only using one day's worth of one minute bars, it reintroduces the problem.

I can probably add a second input, for a length of Days/Weeks/Moths, which could extract data from beyond the beginning of the chart if those aggregations are used as the SetAgg setting. Again, it depends on how you would prefer it handled though.
Thanks Joshua, going to pay around a bit with your code.
 

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
1267 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