MQL4: Help With Data Logging EA

 

Hello,

I'm trying to build a relatively simple EA that should log:

a) All trades that have been closed (either manually or by TP/SL);

b) Time, open, high, low and close data for bars between trade entry and trade exit.

I have attempted to use the CopyTime, High etc. functions, and assign the data into arrays which are to then be written into a .csv/tsv. The purpose of doing so is so that I can use D3.js (a front-end JS library) to render charts on a website. However, because I'm not familiar with MQL4/C++ being a web dev, I'm having serious trouble with the EA.

I am including my code with SRC as follows:

 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
 {
  Alert ("Now Grabbing Data for TPB");
  return(0);
 }
//+------------------------------------------------------------------+
//| Start() stub                                                     |
//+------------------------------------------------------------------+
 
 int start()
 {
    int _GetLastError = 0;
    static bool first = true;
    
    int AllOrdersTotal = 0;
    
    bool OrderClosed = true;
    bool PendingOrderTrig = false;
    
    int CurrOrdersTotal = 0;
    int CurrOrdersTicket[];
    int CurrOrdersType[];
    
    int PrevOrdersTotal = 0;
    int PrevOrdersTicket[];
    int PrevOrdersType[];
    
    int PrevOrderTicket = 0;
    int PrevOrderType = 0;
    int close_type = -1;
    
    int ClosedOrdersTotal = 0;
    int ClosedOrdersTicket[];
    int ClosedOrdersType[];
    double ClosedOrdersOpenPrice[];
    double ClosedOrdersClosePrice[];
    datetime ClosedOrdersOpenTime[];
    datetime ClosedOrdersCloseTime[];
    
    datetime TimeOpen;
    datetime TimeClose;
    
    double PositionBarOpen[];
    double PositionBarHigh[];
    double PositionBarClose[];
    double PositionBarLow[];
    
    while (!IsStopped())
    {
      //+-----------------------------------------------+
      //| For loop through existing orders and push all |
      //| eligible orders into FilterOrdersArray        |
      //+-----------------------------------------------+
      
      AllOrdersTotal = OrdersTotal();
      CurrOrdersTotal = 0;
      
      //loop through each order in the order pool
      for (int i = AllOrdersTotal; i >=0; i--)
      {
         //select the order
         if (!OrderSelect (i, SELECT_BY_POS))
         {
            _GetLastError = GetLastError();
            Print ("OrderSelect( ", i, ",SELECT_BY_POST) - Error #", _GetLastError);
            continue;
         }
         
         //Check if the selected order matches the current symbol
         if (OrderSymbol() == Symbol())
         //push data into structure
         { 
            CurrOrdersTicket[CurrOrdersTotal] = OrderTicket();
            CurrOrdersType[CurrOrdersTotal] = OrderType();
            CurrOrdersTotal++;
         }
      }
      
      // resize arrays to accomodate current eligible orders found
      ArrayResize(CurrOrdersTicket, MathMax(CurrOrdersTotal, 1));
      ArrayResize(CurrOrdersType, MathMax(CurrOrdersTotal, 1));
   
      //+----------------------------------------------+
      //| Check if any orders have been closed, and    |
      //| assign Ticket No. and Close Type to Array    |
      //+----------------------------------------------+
      
      //check eligible orders logged in previous cycle
      for (int j = 0; j < PrevOrdersTotal; j++) // PrevOrdersTotal = CurrOrdersTotal on last cycle
          {
            PrevOrderTicket = PrevOrdersTicket[j];
            PrevOrderType = PrevOrdersType[j];
            
            OrderClosed = true;
            PendingOrderTrig = false;
            
            for (int k = 0; k < CurrOrdersTotal; k++)
            {
               if (PrevOrderTicket == CurrOrdersTicket[k]) // if there is a match between prev and current order pool ticket nos., the position is open
               {
                  OrderClosed = false;
                  
                  if (PrevOrderType != CurrOrdersType[k]) // if the type has changed, a pending order has been triggered
                  {
                     PendingOrderTrig = true;
                  }
                  break;
               }
            }

            if (OrderClosed)
            {
               if (!OrderSelect(PrevOrderTicket, SELECT_BY_TICKET))
               {
                  _GetLastError = GetLastError();
                  Print ("OrderSelect( ", PrevOrderTicket, ", SELECT_BY_TICKET ) - Error #", _GetLastError);
                  continue;
               }
         
               if (PrevOrderType < 2)
               {
                  // Closed Orders: 0 = manual (buy/sell), 1 = sl, 2 = tp
                  close_type = 0;
                  if (StringFind(OrderComment(), "[sl]") >=0) close_type = 1;
                  if (StringFind(OrderComment(), "[tp]") >=0) close_type = 2;
               }
    
               //push closed orders into array
               ClosedOrdersTicket[ClosedOrdersTotal] = OrderTicket();
               ClosedOrdersType[ClosedOrdersTotal] = close_type;
               ClosedOrdersOpenPrice[ClosedOrdersTotal] = OrderOpenPrice();
               ClosedOrdersClosePrice[ClosedOrdersTotal] = OrderClosePrice();
               ClosedOrdersOpenTime[ClosedOrdersTotal] = OrderOpenTime();
               ClosedOrdersCloseTime[ClosedOrdersTotal] = OrderCloseTime();
               ClosedOrdersTotal ++;               
            }  
            
            ArrayResize(ClosedOrdersTicket, MathMax(ClosedOrdersTotal, 1));
            ArrayResize(ClosedOrdersType, MathMax(ClosedOrdersTotal, 1));
            ArrayResize(ClosedOrdersOpenPrice, MathMax(ClosedOrdersTotal, 1));
            ArrayResize(ClosedOrdersClosePrice, MathMax(ClosedOrdersTotal, 1));
            ArrayResize(ClosedOrdersOpenTime, MathMax(ClosedOrdersTotal, 1));
            ArrayResize(ClosedOrdersCloseTime, MathMax(ClosedOrdersTotal, 1));                      
         }
      
      //+----------------------------------------------+
      //| Get OCHL of all bars between and including   |
      //| entry and exit bars and assign to array      |
      //+----------------------------------------------+
   
         for(int l = 0; l < ClosedOrdersTotal; l++)
         {
            TimeOpen = ClosedOrdersOpenTime[l];
            TimeClose = ClosedOrdersCloseTime[l];
            double PBO = PositionBarOpen[l];
            double PBH = PositionBarHigh[l];
            double PBC = PositionBarClose[l];
            double PBL = PositionBarLow[l];
            
            int CopyOpen (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBO);
            int CopyHigh (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBH);
            int CopyClose (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBC);
            int CopyLow (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBL);
            
            ArrayResize(PositionBarOpen, MathMax(ClosedOrdersTotal, l));
            ArrayResize(PositionBarHigh, MathMax(ClosedOrdersTotal, l));
            ArrayResize(PositionBarClose, MathMax(ClosedOrdersTotal, l));
            ArrayResize(PositionBarLow, MathMax(ClosedOrdersTotal, l));
         }
      //+----------------------------------------------+
      //| Export all data to data-date.tsv if they     |
      //| have not yet been exported                   |
      //+----------------------------------------------+
         if (!first)
         {
         
         }
   Sleep(100);      
   }   
 return(0);
 }      

 

Calling the functions gives me a "function can be declared only in the global scope" error during compilation - I cannot seem to find the reason behind this. Also, if you would be so kind as to check for any other errors in my script, I would be very grateful. Dealing with C-style arrays is really doing my head in.

 
Read the Book or the Documentation (Links at the top, Language Basics!) or the reference (part of your editor) of how to work with functions!
 
gooly:
Read the Book or the Documentation (Links at the top, Language Basics!) or the reference (part of your editor) of how to work with functions!

I've already done that and don't see what I'm doing wrong. If I did, I wouldn't ask. If you don't want to help, then don't bother replying at all. 

 
iantanwx:

Hello,

I'm trying to build a relatively simple EA that should log:

a) All trades that have been closed (either manually or by TP/SL);

b) Time, open, high, low and close data for bars between trade entry and trade exit.

I have attempted to use the CopyTime, High etc. functions, and assign the data into arrays which are to then be written into a .csv/tsv. The purpose of doing so is so that I can use D3.js (a front-end JS library) to render charts on a website. However, because I'm not familiar with MQL4/C++ being a web dev, I'm having serious trouble with the EA.

I am including my code with SRC as follows:

 

 

Calling the functions gives me a "function can be declared only in the global scope" error during compilation - I cannot seem to find the reason behind this. Also, if you would be so kind as to check for any other errors in my script, I would be very grateful. Dealing with C-style arrays is really doing my head in.

What this code is supposed to do ? You can't include such declaration there.

            int CopyOpen (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBO);
            int CopyHigh (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBH);
            int CopyClose (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBC);
            int CopyLow (const string EURUSD, ENUM_TIMEFRAMES PERIOD_CURRENT, datetime TimeOpen, datetime TimeClose, double PBL);
 
WHRoeder:

What you are doing wrong is :

  1. Posting two function definitions (init and start.) Irrelevant. Nowhere have you posted any function call, and a function call is not a function declaration.
  2. Not posting the code above the error.
  3. Indicating what line indicates the error. There are no mind readers here.

Pretty sure I stated very clearly:

iantanwx 

I have attempted to use the CopyTime, High etc. functions, and assign the data into arrays which are to then be written into a .csv/tsv. The purpose of doing so is so that I can use D3.js (a front-end JS library) to render charts on a website. However, because I'm not familiar with MQL4/C++ being a web dev, I'm having serious trouble with the EA.

Calling the functions gives me a "function can be declared only in the global scope" error during compilation - I cannot seem to find the reason behind this. Also, if you would be so kind as to check for any other errors in my script, I would be very grateful. Dealing with C-style arrays is really doing my head in. 

For the sake of clarity, the calls are below. I am NOT familiar with strictly-typed languages and so am not sure about syntax for making function calls in MQL4. Now I have removed the type declarations as someone here suggested, and am getting 'CopyXXXX' - no one of the overloads can be applied to the function call. The problem is, I am not overloading the function (as far as I can tell); I am passing in the parameters in accordance with what is written in documentation about the THIRD version of making calls to CopyOpen/High etc.

I have also declared the parameters with the correct types as far as I can tell (datetime and double).

  datetime ClosedOrdersOpenTime[];
  datetime ClosedOrdersCloseTime[];
    
  datetime TradeTimeOpen = 0;
  datetime TradeTimeClose = 0;
    
  double PositionBarOpen[];
  double PositionBarHigh[];
  double PositionBarClose[];
  double PositionBarLow[];

Thank for the help...Function calls below:

for(int l = 0; l < ClosedOrdersTotal; l++)
         {
            TradeTimeOpen = ClosedOrdersOpenTime[l];
            TradeTimeClose = ClosedOrdersCloseTime[l];
            double PBO = PositionBarOpen[l];
            double PBH = PositionBarHigh[l];
            double PBC = PositionBarClose[l];
            double PBL = PositionBarLow[l];
            
            CopyOpen (Symbol(), PERIOD_CURRENT, TradeTimeOpen, TradeTimeClose, PBO);
            CopyHigh (Symbol(), PERIOD_CURRENT, TradeTimeOpen, TradeTimeClose, PBH);
            CopyClose (Symbol(), PERIOD_CURRENT, TradeTimeOpen, TradeTimeClose, PBC);
            CopyLow (Symbol(), PERIOD_CURRENT, TradeTimeOpen, TradeTimeClose, PBL);
            
            ArrayResize(PositionBarOpen, MathMax(ClosedOrdersTotal, l));
            ArrayResize(PositionBarHigh, MathMax(ClosedOrdersTotal, l));
            ArrayResize(PositionBarClose, MathMax(ClosedOrdersTotal, l));
            ArrayResize(PositionBarLow, MathMax(ClosedOrdersTotal, l));
         }
Reason: