I decided write mql4 in MT5

To add comments, please log in or register
Ma Yuliang
293
Ma Yuliang  

I start to learn mql5 today. It is a difficult for me . so I decided to write mql4 in mt5. and I start this project.

#property library
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#include <Indicators\indicator.mqh>
//#include <K\predefinedVar.mqh>KPreVar var;
//+------------------------------------------------------------------+
//| My function                                                      |
//+------------------------------------------------------------------+
// int MyCalculator(int value,int value2) export
//   {
//    return(value+value2);
//   }
//+------------------------------------------------------------------+
class KPreVar
  {
protected:

private:
   int               m_handle;
   bool              copyRates(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT);
   bool              copyTime(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT);
   bool              copyTicks(string a_sym=NULL);
   
   MqlRates          m_rates[1];
   MqlTick           m_ticks[1];
   datetime          m_time[1];
public:
                     KPreVar(void){};
                    ~KPreVar(void){};
   //Open  //Series array that contains open prices of each bar of the current chart
   double            open(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].open;};
   //Close //Series array that contains close prices for each bar of the current chart
   double            close(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].close;};
   //High  //Series array that contains the highest prices of each bar of the current chart
   double            high(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].high;};
   //Low   //Series array that contains the lowest prices of each bar of the current chart
   double            low(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].low;};
   int               spread(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].spread;};
   //Volume//Series array that contains tick volumes of each bar of the current chart
   long              tick_volume(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].tick_volume;};
   long              real_volume(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyRates(a_index,a_sym,a_tf);return m_rates[0].real_volume;};
   //Time  //Series array that contains open time of each bar of the current chart
   datetime          time(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT){copyTime(a_index,a_sym,a_tf);return m_time[0];};
   //Ask   //The latest known seller's price (ask price) of the current symbol
   double            Ask(string a_sym=NULL){copyTicks(a_sym);return m_ticks[0].ask;};
   //Bid   //The latest known buyer's price (offer price, bid price) of the current symbol
   double            Bid(string a_sym=NULL){copyTicks(a_sym);return m_ticks[0].bid;};

  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/* 
Bars  //Number of bars in the current chart

Digits//Number of digits after decimal point for the current symbol prices
Point //The current symbol point value in the quote currency
*/
bool KPreVar::copyRates(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT)
  {

   a_sym =  (a_sym   == NULL)?            Symbol() :  a_sym;
   a_tf  =  (a_tf    == PERIOD_CURRENT)?  Period() :  a_tf;

//ArraySetAsSeries(rates,true);
   int copied=CopyRates(a_sym,a_tf,a_index,1,m_rates);
   bool ret=(copied>0)? true: false;
   return ret;
  }
bool KPreVar::copyTime(int a_index=0,string a_sym=NULL,ENUM_TIMEFRAMES a_tf=PERIOD_CURRENT)
  {
   a_sym =  (a_sym   == NULL)?            Symbol() :  a_sym;
   a_tf  =  (a_tf    == PERIOD_CURRENT)?  Period() :  a_tf;
//ArraySetAsSeries(rates,true);
   int copied=CopyTime(a_sym,a_tf,a_index,1,m_time);
   bool ret=(copied>0)? true: false;
   return ret;
  }
bool KPreVar::copyTicks(string a_sym=NULL)
  {
   a_sym =  (a_sym   == NULL)?            Symbol() :  a_sym;
//ArraySetAsSeries(rates,true);
   int copied=CopyTicks(a_sym,m_ticks,COPY_TICKS_ALL);
   bool ret=(copied>0)? true: false;
   return ret;

  }  

//+------------------------------------------------------------------+
//|                                                    indicator.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property library
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#include <Indicators\indicator.mqh>
//#include <K\indicator.mqh>KIndicator ind;
//+------------------------------------------------------------------+
//| My function                                                      |
//+------------------------------------------------------------------+
// int MyCalculator(int value,int value2) export
//   {
//    return(value+value2);
//   }
//+------------------------------------------------------------------+
class KIndicator
  {
protected:

private:
   int               m_handle;

public:
                     KIndicator(void){};
                    ~KIndicator(void){};
   double            iMA(const string a_symbol,const ENUM_TIMEFRAMES a_tf,int a_period,int a_index);

  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
double KIndicator::iMA(const string a_symbol,const ENUM_TIMEFRAMES a_tf,int a_period,int a_index)
  {
   MqlParam params[];
//int      h_MA,h_MACD; 
//--- 创建 iMA("EURUSD",PERIOD_M15,8,0,MODE_EMA,PRICE_CLOSE); 
   ArrayResize(params,4);

//--- 设置 ma_period 
   params[0].type         =TYPE_INT;
   params[0].integer_value=a_period;
//--- 设置 ma_shift 
   params[1].type         =TYPE_INT;
   params[1].integer_value=0;
//--- 设置 ma_method 
   params[2].type         =TYPE_INT;
   params[2].integer_value=MODE_SMA;
//--- 数组 applied_price 
   params[3].type         =TYPE_INT;
   params[3].integer_value=PRICE_CLOSE;
   m_handle=IndicatorCreate(a_symbol,a_tf,IND_MA,0,params);

   double a_buffer[1];
   a_buffer[0]=0;
//ArraySetAsSeries(a_buffer,true);

   if(m_handle!=INVALID_HANDLE)
     {
      
      CopyBuffer(m_handle,0,a_index,1,a_buffer);
     }
     else
       {
        Print("INVALID_HANDLE");
       }
   return a_buffer[0];
  }
//+------------------------------------------------------------------+

by these class I can use  Ask Open and iMA  as in mql4.

I tried to use standard library but failed to get iMA result.

  CObject

      CArray

          CArrayObj

              CSeries

                  CIndicator

                      CiMA

 although I tried to solve this problem by my own way but still want to learn how others get a MA price by the standard lib.  

Maybe they use this function


Main

Returns the buffer element by the specified index.

double  Main(
   int  index      // index
   )



//+------------------------------------------------------------------+
//| Access to buffer of "Moving Average"                             |
//+------------------------------------------------------------------+
double CiMA::Main(const int index) const
  {
   CIndicatorBuffer *buffer=At(0);
//--- check
   if(buffer==NULL)
      return(EMPTY_VALUE);
//---
   return(buffer.At(index));
  }


//+------------------------------------------------------------------+
//| Access to data in the specified position                         |
//+------------------------------------------------------------------+
double CArrayDouble::At(const int index) const
  {
//--- check
   if(index<0 || index>=m_data_total)
      return(DBL_MAX);
//--- result
   return(m_data[index]);
  }

because the help file write it can Access to data.

but what I got is always like below. what's wrong? or where I can got a example code?

2018.04.08 23:19:03.833 test_Script (EURUSD,M30) 1.797693134862316e+308



//--- example for CArrayShort::At(int) 
#include <Arrays\ArrayShort.mqh> 
//--- 
void OnStart() 
  { 
   CArrayShort *array=new CArrayShort; 
   //--- 
   if(array==NULL) 
     { 
      printf("Object create error"); 
      return; 
     } 
   //--- add arrays elements 
   //--- . . . 
   for(int i=0;i<array.Total();i++) 
     { 
      short result=array.At(i); 
      if(result==SHORT_MAX && GetLastError()==ERR_OUT_OF_RANGE) 
        { 
         //--- error of reading from array 
         printf("Get element error"); 
         delete array; 
         return; 
        } 
      //--- use element 
      //--- . . . 
     } 
   //--- delete array 
   delete array; 
  }

In help file I got code like this . where I hope can find some example code write "//--- use element " OOh my Dog!!

To add comments, please log in or register