iMA Multi Averaging Period

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
iMA Multi Averaging Period
ÿþ//+------------------------------------------------------------------+

//|                                   iMA Multi Averaging Period.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   4



#property indicator_label1  "Signal"

#property indicator_color1  clrRed

#property indicator_width1  1

#property indicator_style1  STYLE_SOLID

#property indicator_type1   DRAW_LINE



#property indicator_label2  "Main"

#property indicator_color2  clrSilver

#property indicator_width2  1

#property indicator_type2   DRAW_HISTOGRAM



#property indicator_label3  "Buy"

#property indicator_color3  clrRoyalBlue

#property indicator_width3  1

#property indicator_type3   DRAW_ARROW



#property indicator_label4  "Sell"

#property indicator_color4  clrTomato

#property indicator_width4  1

#property indicator_type4   DRAW_ARROW

//--- input parameters

input uchar       InpMaximumSignal  = 8;     // Maximum signal

//--- indicator buffers

double   SignalBuffer[];

double   MainBuffer[];

double   BuyBuffer[];

double   SellBuffer[];

//---

int      handles_iMA_multiply_by_two_close[];      // array for storing the handle of the iMA indicator

int      handles_iMA_multiply_by_two_median[];     // array for storing the handle of the iMA indicator

int      handles_iMA_multiply_by_three_median[];   // array for storing the handle of the iMA indicator

int      handles_iMA_multiply_by_three_open[];     // array for storing the handle of the iMA indicator

double   m_filter_points;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   m_filter_points=Point();

//---

   ArrayResize(handles_iMA_multiply_by_two_close,InpMaximumSignal);

   ArrayResize(handles_iMA_multiply_by_two_median,InpMaximumSignal);

   ArrayResize(handles_iMA_multiply_by_three_median,InpMaximumSignal);

   ArrayResize(handles_iMA_multiply_by_three_open,InpMaximumSignal);

   int handle_iMA=INVALID_HANDLE;

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

     {

      //--- create handle of the indicator iMA multiply_by_two_close

      handle_iMA=iMA(Symbol(),Period(),(i+1)*2,0,MODE_SMMA,PRICE_CLOSE);

      //--- if the handle is not created

      if(handle_iMA==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",

                     Symbol(),

                     EnumToString(Period()),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

      handles_iMA_multiply_by_two_close[i]=handle_iMA;



      //--- create handle of the indicator iMA multiply_by_two_median

      handle_iMA=iMA(Symbol(),Period(),(i+1)*2,0,MODE_SMMA,PRICE_MEDIAN);

      //--- if the handle is not created

      if(handle_iMA==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",

                     Symbol(),

                     EnumToString(Period()),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

      handles_iMA_multiply_by_two_median[i]=handle_iMA;



      //--- create handle of the indicator iMA multiply_by_three_median

      handle_iMA=iMA(Symbol(),Period(),(i+1)*3,0,MODE_SMMA,PRICE_MEDIAN);

      //--- if the handle is not created

      if(handle_iMA==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",

                     Symbol(),

                     EnumToString(Period()),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

      handles_iMA_multiply_by_three_median[i]=handle_iMA;



      //--- create handle of the indicator iMA multiply_by_three_open

      handle_iMA=iMA(Symbol(),Period(),(i+1)*3,0,MODE_SMMA,PRICE_OPEN);

      //--- if the handle is not created

      if(handle_iMA==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",

                     Symbol(),

                     EnumToString(Period()),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

      handles_iMA_multiply_by_three_open[i]=handle_iMA;

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,SignalBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,MainBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,BuyBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,SellBuffer,INDICATOR_DATA);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMaximumSignal*3+1);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpMaximumSignal*3+1);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpMaximumSignal*3+1);

   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,InpMaximumSignal*3+1);

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(2,PLOT_ARROW,241);

   PlotIndexSetInteger(3,PLOT_ARROW,242);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,-5);

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,5);

//--- the 0 (empty) value will mot participate in drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"MAP("+string(InpMaximumSignal)+")");

//---

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

  {

   if(rates_total<=InpMaximumSignal*3+1)

      return(0);

//---

   int limit=prev_calculated-2;

   if(prev_calculated==0)

      limit=InpMaximumSignal*3+1;

   for(int i=limit; i<rates_total-1; i++)

     {

      SignalBuffer[i]=0.0;

      MainBuffer[i]=0.0;

      BuyBuffer[i]=0.0;

      SellBuffer[i]=0.0;

      for(int j=InpMaximumSignal-1; j>0; j--)

        {

         double multiply_by_two_close[1];

         double multiply_by_two_median[1];

         double multiply_by_three_median[1];

         double multiply_by_three_open[1];



         if(!iGetArray(handles_iMA_multiply_by_two_close[j],0,rates_total-i,1,multiply_by_two_close) ||

            !iGetArray(handles_iMA_multiply_by_two_median[j],0,rates_total-i,1,multiply_by_two_median) ||

            !iGetArray(handles_iMA_multiply_by_three_median[j],0,rates_total-i,1,multiply_by_three_median) ||

            !iGetArray(handles_iMA_multiply_by_three_open[j],0,rates_total-i,1,multiply_by_three_open))

            return(0);

         //---

         double difference=multiply_by_two_close[0]-

                           multiply_by_three_median[0]+

                           multiply_by_two_median[0]-

                           multiply_by_three_open[0];

         if(!MathIsValidNumber(difference))

            continue;

         if(j>=InpMaximumSignal/2)

            MainBuffer[i]+=difference;

         if(difference!=0.0)

            difference/=(j+1)/2;

         if(j+1<=InpMaximumSignal)

            SignalBuffer[i]+=difference;

        }

      //---

      if(MainBuffer[i]>MainBuffer[i+1] && MainBuffer[i]>0.0)

        {

         SellBuffer[i]=Point();

        }

      else

         if(MainBuffer[i]<MainBuffer[i+1] && MainBuffer[i]<0.0)

           {

            BuyBuffer[i]=-Point();

           }

     }

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

   return(rates_total);

  }

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

//| Get value of buffers                                             |

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

bool iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[])

  {

   bool result=true;

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);

   if(copied!=count)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

   return(result);

  }

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

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