vr_moving_average
Download scriptThis tool is a visual trading indicator designed to help traders identify trends by analyzing price movements over a specific period. It simplifies complex market data into a clear, color-coded visual guide on your chart.
How the Logic Works
The script operates through three primary stages of data processing:
1. Baseline Calculation The core of the tool starts by calculating a standard Moving Average (MA). This represents the "average" price of an asset over a user-defined number of previous candles. By smoothing out the constant fluctuations of the market, this baseline helps reveal the underlying direction of the price—whether it is generally heading up, down, or staying flat.
2. Trend Detection Once the average line is established, the script performs a comparison between the current average value and the value from the previous period. It looks for "inflection points"—the specific moments where the average changes direction.
- If the average value is decreasing compared to the previous point, the logic identifies a downward trend.
- If the average value is increasing, it identifies an upward trend.
3. Visual Representation To make this information easy to read at a glance, the script translates these mathematical trends into two distinct colored lines on your chart:
- Upward Trend: When the price momentum is positive, the indicator highlights this movement with a specific color (Blue by default).
- Downward Trend: When the price momentum shifts into a negative phase, the indicator switches to a different color (Red by default).
Key Benefits for the User
Instead of manually interpreting every price tick, this tool automatically categorizes market behavior. It allows you to see at a glance whether the current "flow" of the market is bullish or bearish, helping you stay aligned with the prevailing trend without being distracted by minor, short-term market noise.
//+------------------------------------------------------------------+
//| VR Moving Average.mq4 |
//| Copyright 2015, Trading-go Project. |
//| http://trading-go.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Trading-go Project."
#property link "http://trading-go.ru"
#property version "15.12" // 14.12.2015
#property strict
#property indicator_chart_window
#property indicator_buffers 3
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
input int period = 20; // Period
input ENUM_MA_METHOD method = MODE_EMA; // Method
input ENUM_APPLIED_PRICE Price = PRICE_CLOSE; // Price
input int width = 2; // Width
input color UpLine = clrBlue; // Up Line
input color DwLine = clrRed; // Dw Line
double ExtUpBuffer[];
double ExtDnBuffer[];
double ExtBuffer[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
Comment("");
SetIndexStyle(0,DRAW_LINE,0,width,UpLine);
SetIndexStyle(1,DRAW_LINE,0,width,DwLine);
SetIndexStyle(2,DRAW_NONE);
SetIndexBuffer(0,ExtUpBuffer);
SetIndexBuffer(1,ExtDnBuffer);
SetIndexBuffer(2,ExtBuffer);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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<period-1 || period<2)
return(0);
int limit=0;
if(prev_calculated==0) // ïðîâåðêà íà ïåðâûé ñòàðò
limit=rates_total-1;
else
limit=rates_total-prev_calculated+1;
for(int i=0;i<limit;i++)
ExtBuffer[i]=iMA(NULL,0,period,0,method,Price,i);
for(int i=0;i<limit-1;i++)
{
if(ExtBuffer[i+1]>ExtBuffer[i])
{
ExtDnBuffer[i]=ExtBuffer[i];
ExtDnBuffer[i+1]=ExtBuffer[i+1];
}
else
{
ExtUpBuffer[i]=ExtBuffer[i];
ExtUpBuffer[i+1]=ExtBuffer[i+1];
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Comments