Building a Volume Bubble Indicator in MQL5 Using Standard Deviation

Introduction

Volume is one of the most important yet misunderstood elements in trading. Many traders rely on volume to confirm trends, detect reversals, and measure market participation. However, when you look at the default volume histogram in MetaTrader, you are only seeing raw values without any context. A spike may look significant, but there is no clear way to determine whether it is truly unusual or simply part of normal market behavior.

This lack of context creates a serious limitation. Traders are forced to visually estimate whether volume is high or low, which can lead to inconsistent decisions. What we need is a smarter way to interpret volume one that adapts to changing market conditions and provides clear visual feedback.

In this article, we will solve that problem by building a Volume Bubble Indicator in MQL5 using standard deviation. Instead of displaying volume as simple bars, we will convert it into a normalized value based on recent activity. Then, we will represent that value visually using bubbles plotted directly on the chart.

At 4xPip, this is exactly the kind of practical, trader-focused solution we believe in. Every tool we develop is not just coded, it is tested across multiple environments including MT4, MT5, forex brokers, stock markets, indices, commodities, crypto, and metals. Our goal is to remove guesswork so traders can focus on execution rather than spending hours on backtesting and optimization.

Project Overview and Implementation Plan

Before writing any code, it’s important to understand how the indicator will function as a complete system. This ensures that every component works together smoothly.

The Volume Bubble Indicator will be displayed directly on the price chart. Instead of a separate histogram, each candle will have a bubble above it. The size of that bubble will represent how strong the volume is compared to recent activity. The color will indicate whether volume is increasing or decreasing. In extreme cases, we will also display a label showing the exact volume.

At its core, the indicator answers a single question:

Is the current volume significantly different from recent market behavior?

To achieve this, we divide the implementation into logical stages.

First, we define a lookback window. This determines how many recent candles are used to calculate what “normal” volume looks like. Anything outside this range is ignored.

Next, we calculate the mean volume within this window. This gives us a baseline.

After that, we calculate variance and standard deviation. These measurements tell us how much volume typically fluctuates around the average.

Once we have the standard deviation, we normalize the volume of each candle. This converts raw volume into a relative score that shows how unusual it is.

Finally, we map this normalized value into bubble sizes and display them on the chart using graphical objects.

This structured approach is exactly how 4xPip Developers develop our indicators and Expert Advisors. Every feature is built step by step, tested under different market conditions, and optimized to ensure traders get reliable performance without needing to do additional tuning themselves.

Understanding Volume in MQL5

Volume represents how active the market is during a specific period. It tells us how many participants are involved in a price move. High volume indicates strong participation, while low volume suggests weak interest.

In MQL5, there are two types of volume available: tick volume and real volume.

Tick volume measures how many times the price changes during a candle. Every price update counts as one tick. While this does not represent actual traded quantity, it serves as a strong approximation of market activity, especially in forex markets where centralized volume data is not available.

Real volume, on the other hand, reflects the actual number of contracts or units traded. This type of volume is available in centralized markets such as stocks and futures.

In most forex trading environments, tick volume is the primary data source. Fortunately, it has been shown to closely correlate with real volume, making it reliable for analysis.

MetaTrader 5 provides access to volume through built-in arrays. These arrays allow us to directly retrieve volume data inside our custom indicator.

Basic Indicator Structure

#property indicator_chart_window
#property indicator_plots 0
input int N = 30; // Lookback period
int OnInit()
{
  return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
               const int prev_calculated,
               const datetime &time[],
               const double &open[],
               const double &high[],
               const double &low[],
               const double &close[],
               const long &tick_volume[],
               const long &volume[],
               const int &spread[])
{
  if(rates_total < N)
     return 0;
  return rates_total;
}

Explanation

This code defines a custom indicator that operates directly on the chart. The parameter N determines how many candles are used for calculations. The OnCalculate function runs every time new data arrives. Before doing any calculations, it checks whether enough data is available. This prevents errors and ensures accuracy.

Creating Volume Bubbles

Now we move into the core logic of the indicator.

Step 1: Calculating the Mean

double sum = 0.0;
double mean;
int window_start = rates_total - N;
for(int i = window_start; i < rates_total; i++)
  sum += (double)tick_volume[i];
mean = sum / N;

Explanation

Here we calculate the average volume over the last N candles. This value represents the “normal” level of activity. Without this baseline, it would be impossible to judge whether current volume is high or low.

Step 2: Calculating Standard Deviation

double variance = 0.0;
double diff;
double stddev;
for(int i = window_start; i < rates_total; i++)
{
  diff = (double)tick_volume[i] - mean;
  variance += diff * diff;
}
variance /= N;
stddev = MathSqrt(variance);
if(stddev == 0.0)
  return rates_total;

Explanation

This section measures how much volume varies around the average. Standard deviation gives us a scale for typical fluctuations. If all values are identical, the standard deviation becomes zero, and calculations stop to avoid errors.

Step 3: Normalizing Volume and Mapping Bubble Size

long v;
double n_vol;
int bubble_size;
for(int i = window_start; i < rates_total; i++)
{
  v = tick_volume[i];
  n_vol = (double)v / stddev;
  if(n_vol < 1)
     bubble_size = 24;
  else if(n_vol < 2)
     bubble_size = 32;
  else if(n_vol < 3)
     bubble_size = 40;
  else if(n_vol < 4)
     bubble_size = 48;
  else
     bubble_size = 56;
}

Explanation

Each candle’s volume is converted into a normalized value. This tells us how far it is from the average in terms of standard deviation. The result is then translated into bubble sizes. Larger bubbles indicate stronger activity.

Step 4: Drawing Bubbles on Chart

string name;
color bubble_color;
name = "Bubble" + IntegerToString((int)time[i]);
if(ObjectFind(0, name) < 0)
  ObjectCreate(0, name, OBJ_TEXT, 0, time[i], high[i]);
bubble_color = (tick_volume[i] > tick_volume[i-1]) ? clrLime : clrOrange;
ObjectSetString(0, name, OBJPROP_TEXT, "●");
ObjectSetInteger(0, name, OBJPROP_COLOR, bubble_color);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, bubble_size);
ObjectMove(0, name, 0, time[i], high[i]);

Explanation

Each bubble is drawn as a text object positioned above the candle. The color indicates whether volume increased or decreased compared to the previous candle. This gives traders immediate visual feedback.

Indicator Overview and Demo

When applied to a chart, this indicator transforms raw volume into an intuitive visual system. Small bubbles represent low activity, while large bubbles highlight strong participation. The color adds another layer of insight by showing whether activity is rising or falling.

In real trading conditions, this becomes extremely useful. For example, if a breakout occurs with a large green bubble, it indicates strong participation and increases confidence in the move. On the other hand, a breakout with small bubbles suggests weak support and a higher chance of failure.

Our Professional MQL Programmers build tools like this with real trading scenarios in mind. Before releasing any product, we test it across multiple asset classes including forex pairs, indices, commodities, crypto, and metals. We also run extensive backtesting and optimization so traders don’t have to spend time doing it themselves. Our goal is simple: deliver tools that are ready to use from day one.

Conclusion

In this article, we built a complete Volume Bubble Indicator using MQL5. We started by understanding the limitations of raw volume data and then introduced a statistical approach using mean and standard deviation. By normalizing volume and converting it into bubble sizes, we created a system that adapts to market conditions and provides clear visual insights.

This approach is far more powerful than traditional volume histograms because it focuses on context rather than raw numbers. Traders can instantly identify unusual activity, confirm trends, and make better decisions.

At 4xPip, this is exactly how we approach trading tools. We combine strong mathematical logic with practical usability, then validate everything through real-world testing across multiple markets and platforms. This ensures that when traders use our indicators and EAs, they are not experimenting in fact they are executing with confidence.

Don't forget to share this post!

Building a Volume Bubble Indicator in MQL5 Using Standard Deviation

Don't forget to share this post!

Related Articles