Amount MultiSymbols iMA

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Amount MultiSymbols iMA
ÿþ//+------------------------------------------------------------------+

//|                                      Amount MultiSymbols iMA.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

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

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

#property copyright "Copyright © 2019, Vladimir Karputov"

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

#property version   "1.002"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot Fast

#property indicator_label1  "Fast"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Slow

#property indicator_label2  "Slow"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input string   InpSymbols  = "AUDUSD;EURUSD;GBPUSD;NZDUSD;USDCAD;USDCHF;USDJPY"; // Array Symbols

//--- MA

input int                  Inp_MA_Fast_ma_period   = 12;          // MA's Fast: averaging period

input int                  Inp_MA_Slow_ma_period   = 36;          // MA's Slow: averaging period

input int                  Inp_MA_ma_shift         = 0;           // MA: horizontal shift

input ENUM_MA_METHOD       Inp_MA_ma_method        = MODE_LWMA;   // MA: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_applied_price    = PRICE_CLOSE; // MA: type of price

//--- indicator buffers

double   FastBuffer[];

double   SlowBuffer[];

double   FastBufferCalculation[];

double   SlowBufferCalculation[];

//---

int      handles_iMA_fast[];                       // variable for storing the handle of the iMA indicator

int      handles_iMA_slow[];                       // variable for storing the handle of the iMA indicator

string   result[];                                 // an array to get strings

int      k;                                        // number of symbols

//---

int      bars_calculated=0;                        // we will keep the number of values in the Moving Average indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,FastBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SlowBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,FastBufferCalculation,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,SlowBufferCalculation,INDICATOR_CALCULATIONS);



   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);



   ArrayInitialize(FastBuffer,0.0);

   ArrayInitialize(SlowBuffer,0.0);

   ArrayInitialize(FastBufferCalculation,0.0);

   ArrayInitialize(SlowBufferCalculation,0.0);

//---

   string to_split=InpSymbols;               // a string to split into substrings

   StringTrimLeft(to_split);

   StringTrimRight(to_split);



   string sep=";";                           // a separator as a character

   ushort u_sep;                             // the code of the separator character

   u_sep=StringGetCharacter(sep,0);          // get the separator code

   k=StringSplit(to_split,u_sep,result); // split the string to substrings

//--- show a comment

   PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);

//--- now output all obtained strings

   if(k>0)

     {

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

        {

         PrintFormat("result[%d]=\"%s\"",i,result[i]);

         SymbolSelect(result[i],true);       // selects a symbol in the Market Watch window

        }

     }

   else

     {

      return(INIT_FAILED);

     }



   ArrayResize(handles_iMA_fast,k);

   ArrayResize(handles_iMA_slow,k);

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

     {

      int handle=ReturnsHandle(result[i],Inp_MA_Fast_ma_period);

      if(handle!=INVALID_HANDLE)

         handles_iMA_fast[i]=handle;

      else

         return(INIT_FAILED);



      handle=ReturnsHandle(result[i],Inp_MA_Slow_ma_period);

      if(handle!=INVALID_HANDLE)

         handles_iMA_slow[i]=handle;

      else

         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 calculated_min=INT_MAX;

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

     {

      //--- determine the number of values calculated in the indicator

      int calculated=BarsCalculated(handles_iMA_fast[i]);

      if(calculated<=0)

        {

         PrintFormat("iMA Fast Symbol %s BarsCalculated() returned %d, error code %d",result[i],calculated,GetLastError());

         return(0);

        }

      if(calculated_min>calculated)

         calculated_min=calculated;

      //--- determine the number of values calculated in the indicator

      calculated=BarsCalculated(handles_iMA_slow[i]);

      if(calculated<=0)

        {

         PrintFormat("iMA Slow Symbol %s BarsCalculated() returned %d, error code %d",result[i],calculated,GetLastError());

         return(0);

        }

      if(calculated_min>calculated)

         calculated_min=calculated;

     }

//--- number of values copied from the iMA indicator

   int values_to_copy;

//--- if it is the first start of calculation of the indicator or if the number of values in the iMA indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated_min!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the iMABuffer array is greater than the number of values in the iMA indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated_min>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated_min;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//---

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

     {

      //--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation

      if(!FillArrayFromBuffer(FastBufferCalculation,Inp_MA_ma_shift,handles_iMA_fast[i],values_to_copy))

        {

         ArrayInitialize(FastBuffer,0.0);

         ArrayInitialize(SlowBuffer,0.0);

         ArrayInitialize(FastBufferCalculation,0.0);

         ArrayInitialize(SlowBufferCalculation,0.0);

         return(0);

        }

      //--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation

      if(!FillArrayFromBuffer(SlowBufferCalculation,Inp_MA_ma_shift,handles_iMA_slow[i],values_to_copy))

        {

         ArrayInitialize(FastBuffer,0.0);

         ArrayInitialize(SlowBuffer,0.0);

         ArrayInitialize(FastBufferCalculation,0.0);

         ArrayInitialize(SlowBufferCalculation,0.0);

         return(0);

        }



      for(int j=rates_total-values_to_copy; j<rates_total; j++)

        {

         FastBuffer[j]=0.0;

         SlowBuffer[j]=0.0;

        }

      for(int j=rates_total-values_to_copy; j<rates_total; j++)

        {

         FastBuffer[j]+=FastBufferCalculation[j];

         SlowBuffer[j]+=SlowBufferCalculation[j];

        }

     }

//--- memorize the number of values in the Moving Average indicator

   bars_calculated=calculated_min;

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

   return(rates_total);

  }

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

//| Returns the handle of the Moving Average indicator               |

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

int ReturnsHandle(const string symbol,const int MA_ma_period)

  {

   int handle_iMA=INVALID_HANDLE;

//--- create handle of the indicator iMA

   handle_iMA=iMA(symbol,Period(),MA_ma_period,Inp_MA_ma_shift,

                  Inp_MA_ma_method,Inp_MA_applied_price);

//--- 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(handle_iMA);

     }

//---

   return(handle_iMA);

  }

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

//| Filling indicator buffers from the MA indicator                  |

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

bool FillArrayFromBuffer(double &values[],   // indicator buffer of Moving Average values

                         int shift,          // shift

                         int ind_handle,     // handle of the iMA indicator

                         int amount          // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0)

     {

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

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

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

      return(false);

     }

//--- everything is fine

   return(true);

  }



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

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