iMA MultiCurrency MultiTimeFrame

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
0 Views
0 Downloads
0 Favorites
iMA MultiCurrency MultiTimeFrame
ÿþ//+------------------------------------------------------------------+

//|                             iMA MultiCurrency MultiTimeFrame.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"

//---

#include <12774\CTradingEngine3.mqh>

#include <Files\FileTxt.mqh>

#include <Arrays\ArrayObj.mqh>

#include <Trade\SymbolInfo.mqh>

CFileTxt       m_file_txt;    // file txt objects

CArrayObj      m_array_obj;   // CArrayObj object

CSymbolInfo    m_temp_symbol; // symbol info object

//--- input parameters

input string   m_file_name="iMA.txt"; // File name, in \Terminal\Common\Files\

//--- input parameters

input ushort   InpStopLoss          = 15;          // Stop Loss, in pips (1.00045-1.00055=1 pips)

input ushort   InpTakeProfit        = 46;          // Take Profit, in pips (1.00045-1.00055=1 pips)

input ushort   InpTrailingFrequency = 10;          // Trailing, in seconds (< "10" -> only on a new bar)

input ushort   InpSignalsFrequency  = 9;           // Search signals, in seconds (< "10" -> only on a new bar)

input ushort   InpTrailingStop      = 25;          // Trailing Stop (min distance from price to Stop Loss, in pips

input ushort   InpTrailingStep      = 5;           // Trailing Step, in pips (1.00045-1.00055=1 pips)

input ENUM_LOT_OR_RISK InpLotOrRisk = risk;        // Money management: Lot OR Risk

input double   InpVolumeLotOrRisk   = 3.0;         // The value for "Money management"

//--- MA Fast and Slow

input string               Inp_MA_symbol              = "EURUSD";    // MA Fast and Slow: symbol

input ENUM_TIMEFRAMES      Inp_MA_period              = PERIOD_D1;   // MA Fast and Slow: timeframe

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

input int                  Inp_MA_Fast_ma_shift       = 0;           // MA Fast: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Fast_ma_method      = MODE_SMA;    // MA Fast: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Fast_applied_price  = PRICE_CLOSE; // MA Fast: type of price

//--- MA Slow

input int                  Inp_MA_Slow_ma_period      = 60;          // MA Slow: averaging period

input int                  Inp_MA_Slow_ma_shift       = 0;           // MA Slow: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Slow_ma_method      = MODE_SMA;    // MA Slow: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Slow_applied_price  = PRICE_CLOSE; // MA Slow: type of price

//---

input bool     InpOnlyOne           = true;        // Only one positions 

input bool     InpReverse           = false;       // Reverse

input bool     InpCloseOpposite     = false;       // Close opposite

input bool     InpPrintLog          = false;       // Print log

input ulong    InpMagic             = 200;         // Magic number

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

//| Expert initialization function                                   |

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

int OnInit()

  {

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

//| struct Input                                                     |

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

   struct Struct_Input

     {

      ushort            StopLoss;                  // Stop Loss, in pips (1.00045-1.00055=1 pips)

      ushort            TakeProfit;                // Take Profit, in pips (1.00045-1.00055=1 pips)

      ushort            TrailingFrequency;         // Trailing, in seconds (< "10" -> only on a new bar)

      ushort            SignalsFrequency;          // Search signals, in seconds (< "10" -> only on a new bar)

      ushort            TrailingStop;              // Trailing Stop (min distance from price to Stop Loss, in pips

      ushort            TrailingStep;              // Trailing Step, in pips (1.00045-1.00055=1 pips)

      ENUM_LOT_OR_RISK  LotOrRisk;                 // Money management: Lot OR Risk

      double            VolumeLotOrRisk;           // The value for "Money management"

      string            _MA_symbol;                // MA Fast and Slow: symbol

      ENUM_TIMEFRAMES   _MA_period;                // MA Fast and Slow: timeframe

      int               _MA_Fast_ma_period;        // MA Fast: averaging period

      int               _MA_Fast_ma_shift;         // MA Fast: horizontal shift

      ENUM_MA_METHOD    _MA_Fast_ma_method;        // MA Fast: smoothing type

      ENUM_APPLIED_PRICE _MA_Fast_applied_price;   // MA Fast: type of price

      int               _MA_Slow_ma_period;        // MA Slow: averaging period

      int               _MA_Slow_ma_shift;         // MA Slow: horizontal shift

      ENUM_MA_METHOD    _MA_Slow_ma_method;        // MA Slow: smoothing type

      ENUM_APPLIED_PRICE _MA_Slow_applied_price;   // MA Slow: type of price

      bool              OnlyOne;                   // Only one positions 

      bool              Reverse;                   // Reverse

      bool              CloseOpposite;             // Close opposite

      bool              PrintLog;                  // Print log

      ulong             Magic;                     // Magic number

     };

//---

   Struct_Input SInput[];

   if(m_file_txt.Open(m_file_name,FILE_READ|FILE_COMMON)==INVALID_HANDLE)

     {

      PrintFormat("ERROR: \"%s\" file in the Files folder is not. Use input parameters ",m_file_name);

      ArrayResize(SInput,1);

      SInput[0].StopLoss               = InpStopLoss;

      SInput[0].TakeProfit             = InpTakeProfit;

      SInput[0].TrailingFrequency      = InpTrailingFrequency;

      SInput[0].SignalsFrequency       = InpSignalsFrequency;

      SInput[0].TrailingStop           = InpTrailingStop;

      SInput[0].TrailingStep           = InpTrailingStep;

      SInput[0].LotOrRisk              = InpLotOrRisk;

      SInput[0].VolumeLotOrRisk        = InpVolumeLotOrRisk;

      SInput[0]._MA_symbol             = Inp_MA_symbol;

      SInput[0]._MA_period             = Inp_MA_period;

      SInput[0]._MA_Fast_ma_period     = Inp_MA_Fast_ma_period;

      SInput[0]._MA_Fast_ma_shift      = Inp_MA_Fast_ma_shift;

      SInput[0]._MA_Fast_ma_method     = Inp_MA_Fast_ma_method;

      SInput[0]._MA_Fast_applied_price = Inp_MA_Fast_applied_price;

      SInput[0]._MA_Slow_ma_period     = Inp_MA_Slow_ma_period;

      SInput[0]._MA_Slow_ma_shift      = Inp_MA_Slow_ma_shift;

      SInput[0]._MA_Slow_ma_method     = Inp_MA_Slow_ma_method;

      SInput[0]._MA_Slow_applied_price = Inp_MA_Slow_applied_price;

      SInput[0].OnlyOne                = InpOnlyOne;

      SInput[0].Reverse                = InpReverse;

      SInput[0].CloseOpposite          = InpCloseOpposite;

      SInput[0].PrintLog               = InpPrintLog;

      SInput[0].Magic                  = InpMagic;

     }

   else

     {

      while(!m_file_txt.IsEnding())

        {

         //--- read and print the text 

         string to_split=m_file_txt.ReadString();  // A string to split into substrings 

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

         ushort u_sep;                             // The code of the separator character 

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

         //--- Get the separator code 

         u_sep=StringGetCharacter(sep,0);

         //--- Split the string to substrings 

         int k=StringSplit(to_split,u_sep,result);

         //--- 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==23)

           {

            int size=ArraySize(SInput);

            ArrayResize(SInput,size+1);

            //---

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

            SInput[size].StopLoss               = StringToInteger(result[0]);

            SInput[size].TakeProfit             = StringToInteger(result[1]);

            SInput[size].TrailingFrequency      = StringToInteger(result[2]);

            SInput[size].SignalsFrequency       = StringToInteger(result[3]);

            SInput[size].TrailingStop           = StringToInteger(result[4]);

            SInput[size].TrailingStep           = StringToInteger(result[5]);

            SInput[size].LotOrRisk              = StringToInteger(result[6]);

            SInput[size].VolumeLotOrRisk        = StringToDouble(result[7]);

            SInput[size]._MA_symbol             = result[8];

            SInput[size]._MA_period             = StringToInteger(result[9]);

            SInput[size]._MA_Fast_ma_period     = StringToInteger(result[10]);

            SInput[size]._MA_Fast_ma_shift      = StringToInteger(result[11]);

            SInput[size]._MA_Fast_ma_method     = StringToInteger(result[12]);

            SInput[size]._MA_Fast_applied_price = StringToInteger(result[13]);

            SInput[size]._MA_Slow_ma_period     = StringToInteger(result[14]);

            SInput[size]._MA_Slow_ma_shift      = StringToInteger(result[15]);

            SInput[size]._MA_Slow_ma_method     = StringToInteger(result[16]);

            SInput[size]._MA_Slow_applied_price = StringToInteger(result[17]);

            SInput[size].OnlyOne                = StringToInteger(result[18]);

            SInput[size].Reverse                = StringToInteger(result[19]);

            SInput[size].CloseOpposite          = StringToInteger(result[20]);

            SInput[size].PrintLog               = StringToInteger(result[21]);

            SInput[size].Magic                  = StringToInteger(result[22]);

           }

         else

           {

            m_file_txt.Close();

            return(INIT_FAILED);

           }

        }

      m_file_txt.Close();

     }

//---

   int size=ArraySize(SInput);

   if(size==0)

      return(INIT_FAILED);

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

     {

      CTradingEngine3 *multi=new CTradingEngine3;

      if(multi==NULL)

        {

         Print("Object CTradingEngine3 create error");

         return(INIT_FAILED);

        }

      m_array_obj.Add(multi);

      int init=multi.OnInit(

                            SInput[i].StopLoss,

                            SInput[i].TakeProfit,

                            SInput[i].TrailingFrequency,

                            SInput[i].SignalsFrequency,

                            SInput[i].TrailingStop,

                            SInput[i].TrailingStep,

                            SInput[i].LotOrRisk,

                            SInput[i].VolumeLotOrRisk,

                            SInput[i]._MA_symbol,

                            SInput[i]._MA_period,

                            SInput[i]._MA_Fast_ma_period,

                            SInput[i]._MA_Fast_ma_shift,

                            SInput[i]._MA_Fast_ma_method,

                            SInput[i]._MA_Fast_applied_price,

                            SInput[i]._MA_Slow_ma_period,

                            SInput[i]._MA_Slow_ma_shift,

                            SInput[i]._MA_Slow_ma_method,

                            SInput[i]._MA_Slow_applied_price,

                            SInput[i].OnlyOne,

                            SInput[i].Reverse,

                            SInput[i].CloseOpposite,

                            SInput[i].PrintLog,

                            SInput[i].Magic

                            );

      if(init!=INIT_SUCCEEDED)

         return(init);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   for(int i=0;i<m_array_obj.Total();i++)

     {

      CTradingEngine3 *multi=m_array_obj.At(i);

      if(multi==NULL)

        {

         //--- Error reading from array 

         Print("Object CMultiiMATrend create error");

         return;

        }

      multi.OnDeinit(reason);

     }

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   for(int i=0;i<m_array_obj.Total();i++)

     {

      CTradingEngine3 *multi=m_array_obj.At(i);

      if(multi==NULL)

        {

         //--- Error reading from array 

         Print("Object CMultiiMATrend create error");

         return;

        }

      multi.OnTick();

     }

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---

   for(int i=0;i<m_array_obj.Total();i++)

     {

      CTradingEngine3 *multi=m_array_obj.At(i);

      if(multi==NULL)

        {

         //--- Error reading from array 

         Print("Object CMultiiMATrend create error");

         return;

        }

      multi.OnTradeTransaction(trans,request,result);

     }

  }

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

Comments