Author: Copyright 2015,fxMeter
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MA(mtf)
ÿþ

//https://www.mql5.com/zh/code/29686/

#property copyright "Copyright 2015,fxMeter"

#property link      "https://www.mql5.com/zh/users/fxmeter"

#property version   "1.00"

#property strict

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot Ma

#property indicator_label1  "Ma"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2



input bool               AutoMatchTF  = true;

extern ENUM_TIMEFRAMES   TimeFrame    = PERIOD_CURRENT;

input int                MaPeriods    = 12;

input ENUM_MA_METHOD     MaMethod     = MODE_EMA;

input ENUM_APPLIED_PRICE MaPrice      = PRICE_CLOSE;



//--- indicator buffers

double         MaBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

{

//--- indicator buffers mapping

   SetIndexBuffer(0,MaBuffer);   

   for(int i=0; i<1; i++)

   {

      SetIndexEmptyValue(i,0.0);

      SetIndexDrawBegin(i,MaPeriods);

   }

   

//--- /f&TꁨR9SM‘hTg 

   if(AutoMatchTF)

   {

      if(Period()==1)TimeFrame =30;

      else if(Period()==5)TimeFrame =240;

      else if(Period()==15||Period()==30||Period()==60)TimeFrame=PERIOD_D1;

      else if(Period()==240)TimeFrame = PERIOD_W1;

      else if(Period()>=1440)TimeFrame = PERIOD_MN1;

   }

   else

     {

      if(TimeFrame<=Period())TimeFrame=0;

     }



   string name = StringFormat("MA mtf(%s,%s,%d)",StringSubstr(EnumToString(TimeFrame),7), StringSubstr(EnumToString(MaMethod),5),MaPeriods);

   IndicatorShortName(name);

   IndicatorDigits(Digits);



   if(MaPeriods<=0)

   {

      return(INIT_FAILED);

   }



//---

   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[])

{

//---

   int limit=0;

   if(rates_total<MaPeriods)return(0);

   if(prev_calculated>0) limit = rates_total - prev_calculated +1;

   else if(prev_calculated<=0)limit = rates_total;



   if(TimeFrame==0)

   {

      for(int i=0; i<limit; i++)

      {

         if(i>=rates_total-1-MaPeriods-1)continue;

         MaBuffer[i] = iMA(NULL,0,MaPeriods,0,MaMethod,MaPrice,i);

      }

   }

   else //èhTg

   {

      if(iBars(Symbol(),TimeFrame)<MaPeriods)return(0);

      int max = iBarShift(NULL,0,iTime(NULL,TimeFrame,1));

      limit  = MathMax(limit,max);

      for(int i=0; i<limit; i++)

      {

         if(i>=rates_total-1-MaPeriods-1)continue;         

         int shift =iBarShift(NULL,TimeFrame,time[i]);

         MaBuffer[i]  =iMA(NULL,TimeFrame,MaPeriods,0,MaMethod,MaPrice,shift);

      }

   }



//--- return value of prev_calculated for next call

   return(rates_total);

}

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---