moving_average_ex
Download scriptThis script is a specialized visual indicator designed for the MetaTrader trading platform. Its primary purpose is to project long-term trend data—specifically an Exponential Moving Average (EMA)—from a higher timeframe onto a lower timeframe chart.
Core Functionality
While most indicators only show data relevant to the specific timeframe you are currently viewing, this tool allows you to "overlay" historical context from a larger perspective. For example, it can take a 20-day average calculated on a Daily chart and display it clearly on a much smaller chart (like a 1-hour or 4-hour chart). This helps traders stay aware of the "big picture" trends without having to constantly switch back and forth between different timeframes.
How the Logic Works
The script follows a specific set of rules to ensure the data remains accurate and easy to read:
- Timeframe Filtering: The tool first checks if the current chart you are viewing is smaller than the target timeframe (e.g., Daily). If you switch to a timeframe larger than the target, the indicator automatically pauses to prevent confusing data overlays.
- Dynamic Alignment: Because one "Day" on a daily chart spans many "Hours" on an hourly chart, the script performs a translation. It identifies which specific daily bar corresponds to the current price point on your smaller chart and pulls the corresponding moving average value for that moment.
- Seamless Visualization: The logic is designed to bridge the gap between these two timeframes. It ensures that as new price data comes in, the line updates smoothly to represent the higher-level trend accurately across the bars of the lower timeframe.
- Customization: Users have full control over the mathematical components of the calculation, including:
- The length of the average (how many days/periods to look back).
- The "shift" (moving the line forward or backward on the chart).
- The specific calculation method (Exponential vs. Simple moving averages).
- The price point used for the calculation (such as the closing price of the candle).
Summary for Traders
Essentially, this script acts as a visual "bridge." It takes the smoothed trend information from a high-level perspective and maps it onto your active trading screen, allowing you to see how current price action aligns with long-term moving averages in real-time.
// Copyright © 2015 Alexandre Borela (alexandre.borela@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#property copyright "Copyright 2015, Alexandre Borela"
#property link ""
#property version "1.00"
#property strict
#property indicator_buffers 1
#property indicator_chart_window
#property indicator_color1 C'101,160,226'
#property indicator_style1 STYLE_DASHDOTDOT
input string Label="20 Day EMA"; // Custom Label
input int MAPeriod=20; // Period
input int MAShift=1; // Shift
input ENUM_TIMEFRAMES MATimeFrame=PERIOD_D1; // Time Frame
input ENUM_MA_METHOD MAMethod=MODE_EMA; // Method
input ENUM_APPLIED_PRICE MAPrice=PRICE_CLOSE; // Applied Price
double buffer[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,buffer);
SetIndexLabel(0,Label);
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(Period()>MATimeFrame)
return( rates_total );
int target=-1;
int oldTarget= -1;
double value = 0;
int totalBars= rates_total-prev_calculated;
for(int i=1; i<totalBars; i++)
{
target=iBarShift(Symbol(),MATimeFrame,time[i]);
if(oldTarget!=target)
{
oldTarget=target;
value=iMA(Symbol(),MATimeFrame,MAPeriod,MAShift,MAMethod,MAPrice,target);
if(oldTarget !=-1 && Period() !=MATimeFrame)
buffer[i-1]=EMPTY_VALUE;
}
buffer[i]=value;
}
return( rates_total );
}
//+------------------------------------------------------------------+
Comments