Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
SerialMA
ÿþ//+------------------------------------------------------------------+

//|                                                     SerialMA.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_plots   2

//--- plot MA

#property indicator_label1  "MA"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Point

#property indicator_label2  "Point"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- indicator buffers

double         BufferMA[];

double         BufferPoint[];

double         BufferTime[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferMA,INDICATOR_DATA);

   SetIndexBuffer(1,BufferPoint,INDICATOR_DATA);

   SetIndexBuffer(2,BufferTime,INDICATOR_CALCULATIONS);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(1,PLOT_ARROW,159);

//--- settings indicators parameters

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetString(INDICATOR_SHORTNAME,"Serial MA");

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferMA,true);

   ArraySetAsSeries(BufferPoint,true);

   ArraySetAsSeries(BufferTime,true);

//---

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

  {

//--- @>25@:0 =0 <8=8<0;L=>5 :>;8G5AB2> 10@>2 4;O @0AGQB0

   if(rates_total<3) return 0;

//--- #AB0=>2:0 8=45:A0F88 <0AA82>2 :0: C B09<A5@89

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int total=rates_total-2,limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=total;

      ArrayInitialize(BufferMA,EMPTY_VALUE);

      ArrayInitialize(BufferPoint,EMPTY_VALUE);

      ArrayInitialize(BufferTime,EMPTY_VALUE);

     }

//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      if(i==total)

         BufferTime[i]=(double)time[i];

      else

        {

         BufferTime[i]=BufferTime[i+1];

         int period_ma=BarShift(Symbol(),PERIOD_CURRENT,(datetime)BufferTime[i])-i+1;

         double summ=0;

         for(int n=0; n<period_ma; n++)

            summ+=close[i+n];

         if(period_ma!=0) BufferMA[i]=summ/period_ma;

         if((BufferMA[i]-close[i])*(BufferMA[i+1]-close[i+1])<0)

           {

            BufferTime[i]=(double)time[i];

            BufferPoint[i]=BufferMA[i]=close[i];

           }

         else

            BufferPoint[i]=EMPTY_VALUE;

        }

     }

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

   return(rates_total);

  }

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

//| >72@0I05B A<5I5=85 10@0 ?> 2@5<5=8                              |

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

int BarShift(string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time)

  {

   if(symbol_name==NULL || symbol_name=="") symbol_name=Symbol();

   int res=WRONG_VALUE;

   datetime last_bar=0;

   if(::SeriesInfoInteger(symbol_name,timeframe,SERIES_LASTBAR_DATE,last_bar))

     {

      if(time>last_bar) res=0;

      else

        {

         const int shift=::Bars(Symbol(),timeframe,time,last_bar);

         if(shift>0) res=shift-1;

        }

     }

   return(res);

  }

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

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 ---