Author: Copyright � 2005, MetaQuotes Software Corp.
OBV

The On Balance Volume (OBV) indicator is a momentum-based tool designed to measure the flow of volume in a financial market. It operates on the core principle that price changes are driven by volume; therefore, it tracks whether volume is "buying" or "selling" pressure to help identify potential trends.

How the Logic Works

The script processes market data by looking at two primary pieces of information: Price Movement and Volume. Here is a step-by-step breakdown of how it calculates the data you see on your chart:

1. Identifying Price Direction The indicator first looks at the current price (based on a specific point like the Close, Open, High, or Low) and compares it to the price of the previous candle. It categorizes the movement into three scenarios:

  • Price Increase: If the current price is higher than the previous price, the market is considered to have "buying pressure."
  • Price Decrease: If the current price is lower than the previous price, the market is considered to have "selling pressure."
  • No Change: If the price remains exactly the same, there is no recorded pressure change.

2. Calculating Cumulative Volume Once the direction is determined, the script adds or subtracts the volume of the current candle from the total accumulated value:

  • Upward Trend: If the price went up, the current candle's volume is added to the previous running total.
  • Downward Trend: If the price went down, the current candle's volume is subtracted from the previous running total.
  • Flat Movement: If the price stayed the same, the total remains unchanged from the previous candle.

3. The Cumulative Result Instead of showing you individual spikes in volume, this script creates a continuous line. This line represents the "running balance" of all volume over time. By looking at the slope of this line, traders can see if the underlying volume supports the current price trend.

Key Takeaways for Users

  • Trend Confirmation: If the price is rising and the OBV line is also moving upward, it suggests that the move is backed by strong buying volume.
  • Divergence: If the price makes a new high but the OBV line fails to do so, it may indicate that the price increase is "weak" and lacks sufficient volume support, potentially signaling a reversal.
  • Customizable Inputs: You can choose which specific price point (such as the High, Low, or Close) the indicator uses to determine "direction," allowing you to tailor the logic to your specific trading style.
Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
OBV
//+------------------------------------------------------------------+
//|                                            On Balance Volume.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int ExtOBVAppliedPrice=0;
//---- buffers
double ExtOBVBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string sShortName;
//---- indicator buffer mapping
   SetIndexBuffer(0,ExtOBVBuffer);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
//---- sets default precision format for indicators visualization
   IndicatorDigits(0);     
//---- name for DataWindow and indicator subwindow label
   sShortName="OBV";
   IndicatorShortName(sShortName);
   SetIndexLabel(0,sShortName);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| On Balance Volume                                                |
//+------------------------------------------------------------------+
int start()
  {
   int    i,nLimit,nCountedBars;
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();
//---- last counted bar will be recounted
   if(nCountedBars>0) nCountedBars--;
   nLimit=Bars-nCountedBars-1;
//---- 
   for(i=nLimit; i>=0; i--)
     {
      if(i==Bars-1)
         ExtOBVBuffer[i]=Volume[i];
      else
        {
         double dCurrentPrice=GetAppliedPrice(ExtOBVAppliedPrice, i);
         double dPreviousPrice=GetAppliedPrice(ExtOBVAppliedPrice, i+1);
         if(dCurrentPrice==dPreviousPrice)
            ExtOBVBuffer[i]=ExtOBVBuffer[i+1];
         else
           {
            if(dCurrentPrice<dPreviousPrice)
               ExtOBVBuffer[i]=ExtOBVBuffer[i+1]-Volume[i];  
            else
               ExtOBVBuffer[i]=ExtOBVBuffer[i+1]+Volume[i]; 
           }
        }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetAppliedPrice(int nAppliedPrice, int nIndex)
  {
   double dPrice;
//----
   switch(nAppliedPrice)
     {
      case 0:  dPrice=Close[nIndex];                                  break;
      case 1:  dPrice=Open[nIndex];                                   break;
      case 2:  dPrice=High[nIndex];                                   break;
      case 3:  dPrice=Low[nIndex];                                    break;
      case 4:  dPrice=(High[nIndex]+Low[nIndex])/2.0;                 break;
      case 5:  dPrice=(High[nIndex]+Low[nIndex]+Close[nIndex])/3.0;   break;
      case 6:  dPrice=(High[nIndex]+Low[nIndex]+2*Close[nIndex])/4.0; break;
      default: dPrice=0.0;
     }
//----
   return(dPrice);
  }
//+------------------------------------------------------------------+

Comments