MTF_Moving_Double
Download scriptThis tool is designed to provide traders with a visual representation of price trends by calculating and displaying two distinct Moving Averages on a trading chart. By analyzing historical price data, the script helps identify market momentum and potential turning points.
Core Functionality
The indicator works by tracking two different types of averages simultaneously:
- Fast Moving Average: This reacts more quickly to recent price fluctuations, highlighting short-term changes in the market.
- Slow Moving Average: This follows the broader trend by incorporating a larger amount of historical data, smoothing out minor "noise" or temporary price spikes.
Multi-Timeframe Logic
One of the primary features of this script is its ability to synchronize data from different timeframes. Rather than just looking at the current chart you are viewing, the tool allows for a specific timeframe selection (e.g., calculating 1-hour trends while viewing a 5-minute chart).
The script includes intelligent logic to handle this:
- Dynamic Scaling: If you are viewing a timeframe smaller than the one specified in the settings, it pulls data from that larger timeframe to give you a "big picture" perspective.
- Alignment: It automatically aligns the data points so that the moving averages stay consistent with the start of each candle, ensuring that the visual lines accurately represent the period intended by the user.
Visual Representation
The script outputs two distinct lines on your chart:
- A line representing the Fast Moving Average, typically used to spot immediate momentum.
- A line representing the Slow Moving Average, used to identify the primary trend direction.
By comparing where these two lines sit in relation to one another and to the current price, traders can more easily interpret whether the market is accelerating, slowing down, or potentially reversing.
//+------------------------------------------------------------------+
//| MTF_Moving.mq4 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com/ru/users/melnik |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com/ru/users/melnik"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_width1 2
#property indicator_width2 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
double ma_buffer_slow[];
double ma_buffer_fast[];
//--- input parameters
input int PeriodMaSlow=21; //Period slow Ma
input int PeriodMaFast=13; //Pertiod fast Ma
input ENUM_APPLIED_PRICE PriceMa=0; //Applied price
input ENUM_MA_METHOD MethodMa=0; //Method Ma
input ENUM_TIMEFRAMES Timeframe=60; //Timeframe for calculate
ENUM_TIMEFRAMES prd;
int index=-1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0, ma_buffer_slow, INDICATOR_DATA);
SetIndexBuffer(1, ma_buffer_fast, INDICATOR_DATA);
//---
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-prev_calculated-PeriodMaSlow)<=0)return(0);
if(Period()>Timeframe) prd=PERIOD_CURRENT;
if(Period()<=Timeframe) prd=prd=Timeframe;
for(int i=rates_total-prev_calculated-PeriodMaSlow-1;i>=0;i--)
{
if(TimeMinute(time[i])==0)index=iBarShift(Symbol(), prd, time[i], false);
ma_buffer_fast[i]=iMA(Symbol(), prd, PeriodMaFast, 0, MethodMa, PriceMa, index);
ma_buffer_slow[i]=iMA(Symbol(), prd, PeriodMaSlow, 0, MethodMa, PriceMa, index);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Comments