Author: Copyright � 2007, Amir Aliev
Tick_VWAP

This script is designed to provide a more precise way of calculating and visualizing price averages on a trading chart. Unlike standard moving averages that treat every completed bar (candle) with equal importance, this tool focuses on the actual activity—or "volume"—within those bars to create a more fluid and accurate representation of market movement.

How the Logic Works

The core objective of this script is to calculate a weighted average based on the volume of price action. Here is a breakdown of the logic in plain terms:

1. Activity-Based Weighting Instead of simply looking at the high and low prices of a candle, the script looks at how many "ticks" (individual price changes) occurred during that period. It calculates the average price of each bar and then multiplies that price by the volume of activity within that bar. This ensures that periods of high activity have a greater influence on the resulting line than periods of low activity.

2. The Dynamic Window System The script uses a user-defined threshold (the "Ticks" parameter) to determine how much historical data to include in its current calculation. Rather than looking back at a fixed number of candles (like a standard 14-period MA), it looks back far enough to accumulate a specific amount of total trading activity. For example, if your threshold is set to 1,000 ticks, the script will continuously aggregate data until it captures 1,000 ticks worth of movement.

3. Automatic Adjustment and Maintenance To ensure the tool remains functional across different timeframes, the script includes a "safety check." If you switch to a chart where the total volume of a single bar is higher than your set threshold, the script automatically adjusts its internal settings to ensure it always has enough data to provide a valid reading.

4. Smooth Processing As new price data arrives, the script manages a moving "window" of information. It adds the newest activity at the front and removes the oldest data from the back once the tick threshold is exceeded. By constantly balancing this "in-and-out" flow of data, it generates a smooth line that reflects the weighted average of the most recent active market movement.

Summary for the User

In short, this script ignores "quiet" candles and prioritizes "busy" market action. It produces a smoothed trend line that reacts more intelligently to actual market participation, providing you with a visual aid that is grounded in the intensity of the trading volume rather than just the passage of time.

Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
Tick_VWAP
//+------------------------------------------------------------------+
//|                                             Corrected SMA II.mq4 |
//|                                     Copyright © 2007, Amir Aliev |
//|                                       http://finmat.blogspot.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Amir Aliev"
#property link      "http://finmat.blogspot.com/"
 
#property indicator_chart_window
#property indicator_color1 Red
//---- input parameters
extern int MA_Ticks = 1000;
//---- indicator buffers
double sum = 0;                               
int ticks = 0;
bool collected = false;
bool started = false;
int fbar = 0;
double ExtMapBuffer[];
int oldRange = 0;
int lbarVol = 0;
int period;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   SetIndexStyle(0, DRAW_LINE);
//---- indicator buffers mapping
   SetIndexBuffer(0, ExtMapBuffer);
//---- initialization done

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int rest = Bars - IndicatorCounted();
   if(! rest)
       return (0);

   if (period!=Period())
   {
      Print("Period has been changed. Recalculation needed");
      rest=Bars;     
   }
   period=Period();   
   
   if (Volume[ArrayMaximum(Volume)] > MA_Ticks)
   {
      MA_Ticks = Volume[ArrayMaximum(Volume)] +1 ;
   Print ("MA_Ticks is too low for this period. MA_ticks has been changed to ",MA_Ticks);
   }
 /*
   Print("Ticks = ", ticks);
   Print("Rest = ", rest);
   Print("fbar = ", fbar);  
*/   
   rest--;
   fbar += rest;
   while(!collected && (rest >= 0))
     {
      if(ticks + Volume[rest] < MA_Ticks)
        {
         ticks += Volume[rest];
         sum += Volume[rest] * (High[rest]+Low[rest])/2;
         if(!started)
           {
            fbar = rest;
            started = true;
           }
         rest--;
         continue;
        } 
      collected = true;
     }
   if(! collected) 
       return (0);
 
   ticks += (Volume[rest] - lbarVol);
   sum += (Volume[rest] - lbarVol) * (High[rest]+Low[rest])/2;
   lbarVol = Volume[rest];
   while(ticks > MA_Ticks)
     {
       //Print("fbar-- because bar ticks reaches 1000");
       ticks -= Volume[fbar];
       sum -= Volume[fbar] * (High[fbar]+Low[fbar])/2;
       fbar--;
     }
   if (ticks!=0) ExtMapBuffer[rest] = sum / ticks;
   rest--;
   
   
   while(rest >= 0)
     {
      ticks += Volume[rest];
      sum += Volume[rest] * (High[rest]+Low[rest])/2;
      lbarVol = Volume[rest];
      while(ticks > MA_Ticks)
        {
         //Print("fbar-- because of new bar ");
         ticks -= Volume[fbar];
         sum -= Volume[fbar] * (High[fbar]+Low[fbar])/2;
         fbar--;
        }
      if (ticks!=0) ExtMapBuffer[rest] = sum / ticks;
      rest--;
     } 
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments