//+------------------------------------------------------------------+
//| MTFMA.mq4 |
//| Copyright © 2006, Keris2112 |
//| fixed for lower timeframes by Renato |
//| http://www.reniza.com/forex/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Keris2112"
#property link "http://www.forex-tsd.com"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- Name for DataWindow and indicator subwindow label
string short_name;
//---- indicator parameters
extern int TimeFrame=0;
extern int MA_Period=13;
extern int MA_Shift=0;
extern int MA_Method=MODE_SMA;
extern int Applied_Price=3;
extern int Shift=0;
//----
int ExtCountedBars;
int limit;
int HTFlimit;
datetime HTFTimeArray[];
double HTFPriceArray[];
//---- indicator buffers
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
//---- drawing settings
if ( MA_Period<2 ) MA_Period=2;
int draw_begin=MA_Period;
SetIndexStyle(0,DRAW_LINE);
SetIndexShift(0,MA_Shift);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- indicator short name
switch(MA_Method) {
case 1 : short_name="MTF_EMA("; break;
case 2 : short_name="MTF_SMMA("; break;
case 3 : short_name="MTF_LWMA("; break;
default : short_name="MTF_SMA("; MA_Method=0;
}
short_name=short_name+MA_Period+") (";
switch(Applied_Price) {
case 0 : short_name=short_name+"Open"; break;
case 1 : short_name=short_name+"Low"; break;
case 2 : short_name=short_name+"High"; break;
case 3 : short_name=short_name+"Close"; break;
default : short_name=short_name+"Close"; Applied_Price=3;
}
short_name=short_name+":"+Symbol()+",";
if ( TimeFrame==0 )
TimeFrame=Period();
switch(TimeFrame) {
case 1 : short_name=short_name+"M1"; break;
case 5 : short_name=short_name+"M5"; break;
case 15 : short_name=short_name+"M15"; break;
case 30 : short_name=short_name+"M30"; break;
case 60 : short_name=short_name+"H1"; break;
case 240 : short_name=short_name+"H4"; break;
case 1440 : short_name=short_name+"D1"; break;
case 10080 : short_name=short_name+"W1"; break;
case 43200 : short_name=short_name+"MN1"; break;
}
short_name=short_name+")";
IndicatorShortName(short_name);
SetIndexDrawBegin(0,draw_begin);
Comment(short_name);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
//----
Comment("");
//----
return(0);
}
//+------------------------------------------------------------------+
//| MTF Moving Average |
//+------------------------------------------------------------------+
int start()
{
if( Bars<=MA_Period ) return(0);
ExtCountedBars=IndicatorCounted();
//---- check for possible errors
if ( ExtCountedBars<0 ) return(-1);
if ( ExtCountedBars>0 ) ExtCountedBars=0;
limit=Bars-MA_Period-1;
HTFlimit=ArrayCopySeries(HTFTimeArray,MODE_TIME,Symbol(),TimeFrame)-1;
ArrayCopySeries(HTFPriceArray,Applied_Price,Symbol(),TimeFrame);
int i,y;
for(i=0,y=0;i<limit;i++) {
if ( TimeFrame>Period() )
if (Time[i]<HTFTimeArray[y]) y++;
if ( TimeFrame<=Period() )
while(Time[i]<HTFTimeArray[y]) y++;
switch(MA_Method) {
case 0 : ExtMapBuffer[i]=SMA(HTFPriceArray,MA_Period,y); break;
case 1 : ExtMapBuffer[i]=EMA(HTFPriceArray,MA_Period,y); break;
}
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
double SMA(double PriceArray[], int MA_Period, int shift) {
double sum=0;
for(int i=shift+MA_Period-1;i>=shift;i--)
sum+=PriceArray[i];
return(sum/MA_Period);
}
double EMA(double PriceArray[], int MA_Period, int shift) {
double pr=2.0/(MA_Period+1);
double sum=PriceArray[shift+2*MA_Period-2];
for(int i=shift+2*MA_Period-1;i>=shift;i--)
sum=PriceArray[i]*pr+sum*(1-pr);
return(sum);
}
Comments