Mean_Indicator

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Series array that contains open time of each bar
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Mean_Indicator
ÿþ//+------------------------------------------------------------------+

//|                                               Mean_Indicator.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 description "Mean Indicator"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   3

//--- plot Mean

#property indicator_label1  "Mean"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot UP

#property indicator_label2  "Up"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrLimeGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot DN

#property indicator_label3  "Down"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input uint     InpDayStart =  0;    // Day hour begin

//--- indicator buffers

double         BufferMean[];

double         BufferUP[];

double         BufferDN[];

double         BufferTemp[];

//--- global variables

int            begin;

int            bars_current;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   begin=(int)InpDayStart;

   bars_current=BarsCurrentInTimeframe(PERIOD_D1);

   if(Period()>PERIOD_H1)

     {

      Alert("This indicator works only on H1 charts and lower");

      ChartSetSymbolPeriod(0,NULL,PERIOD_H1);

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferMean,INDICATOR_DATA);

   SetIndexBuffer(1,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(2,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(3,BufferTemp,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_ARROW,159);

   PlotIndexSetInteger(1,PLOT_ARROW,159);

   PlotIndexSetInteger(2,PLOT_ARROW,159);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Mean Indicator ("+(string)begin+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferMean,true);

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferTemp,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[])

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<fmax(bars_current,4) || !IsTimeframeDataReady(PERIOD_D1)) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferMean,EMPTY_VALUE);

      ArrayInitialize(BufferUP,EMPTY_VALUE);

      ArrayInitialize(BufferDN,EMPTY_VALUE);

      ArrayInitialize(BufferTemp,0);

     }



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

   double daily_mean=0;

   int prev_start=0;

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

     {

      BufferDN[i]=BufferUP[i]=BufferMean[i]=EMPTY_VALUE;

      if(TimeHour(time[i])==begin)

        {

         int day_bar=BarShift(NULL,PERIOD_D1,time[i]);

         if(day_bar==WRONG_VALUE)

            continue;

         datetime day_time=Time(NULL,PERIOD_D1,day_bar+1);

         if(day_time==0)

            continue;

         prev_start=BarShift(NULL,PERIOD_CURRENT,day_time);

         if(prev_start==WRONG_VALUE)

            continue;

         //---

         int n=0;

         daily_mean=0;

         for(int j=prev_start; j>i; j--)

           {

            if(j>rates_total-1)

               continue;

            daily_mean+=close[j];

            n++;

           }

         daily_mean=(daily_mean/n);

         prev_start=i;

        }

      BufferMean[i]=BufferTemp[i]=daily_mean;



      if(i<prev_start)

        {

         int n=0;

         double current_mean=0;

         for(int j=prev_start; j>i; j--)

           {

            current_mean+=close[j];

            n++;

           }

         BufferTemp[i]=current_mean/n;

        }

      if(BufferTemp[i]>=BufferTemp[i+1])

         BufferUP[i]=BufferTemp[i];

      else

         BufferDN[i]=BufferTemp[i];

     }

   

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

   return(rates_total);

  }

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

//| >72@0I05B :>;8G5AB2> <8=CB 2 ?5@8>45                            |

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

int MinutesInPeriod(ENUM_TIMEFRAMES timeframe)

  {

   if(timeframe==PERIOD_CURRENT) timeframe=Period();

   return PeriodSeconds(timeframe)/60;

  }  

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

//| >72@0I05B :>;8G5AB2> 10@>2 2 ?5@8>45                            |

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

int BarsCurrentInTimeframe(ENUM_TIMEFRAMES timeframe)

  {

   return MinutesInPeriod(timeframe)/MinutesInPeriod(PERIOD_CURRENT);

  }  

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

//| >72@0I05B G0A C:070==>3> 2@5<5=8                                |

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

int TimeHour(const datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm)) return WRONG_VALUE;

   return tm.hour;

  }

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

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

//| https://www.mql5.com/ru/forum/743/page11#comment_7010041         |

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

int BarShift(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time,bool exact=false)

  {

   int res=Bars(symbol_name,timeframe,time+1,UINT_MAX);

   if(exact) if((timeframe!=PERIOD_MN1 || time>TimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE);

   return res;

  }

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

//| >72@0I05B Time                                                  |

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

datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)

  {

   datetime array[];

   ArraySetAsSeries(array,true);

   return(CopyTime(symbol_name,timeframe,shift,1,array)==1 ? array[0] : 0);

  }

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

//| @>25@:0 =0;8G8O 8AB>@8G5A:8E 40==KE ?> B09<D@59<C               |

//| https://www.mql5.com/ru/forum/280448#comment_8762312             |

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

bool IsTimeframeDataReady(ENUM_TIMEFRAMES timeframe)

  {

   ResetLastError();

   iTime(NULL,timeframe,1);

   return GetLastError()==ERR_SUCCESS;

  }

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

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