backtest on external file

 

Hello,

Maybe someone can help which is highly appreciated.

I want to  backtest an EA in order to find out the optimal stop loss/profitability. Ea runs based on external value from another software.

I have the CSV file for one month(file stored in \tester\files):

e.g.

2017.05.01 00:00:00,1
2017.05.01 00:01:00,2
2017.05.01 00:02:00,1
2017.05.01 00:03:00,2
2017.05.01 00:04:00,1
2017.05.01 00:05:00,2

.........................


And if I run the program below nothing happens, no useful message in log:

#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot Label1

#property indicator_color1  clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_separate_window
//--- parameters for receiving data
input string  InpFileName="N2.TXT"; // file name
input string  InpDirectoryName="C:/Documents and Settings/crisram/Application Data/MetaQuotes/Terminal/Common/Files";  // directory name

double            Lots          =0.1;
int             ticket1, ticket2,a ;
//+------------------------------------------------------------------+
//| Structure for storing candlestick data                           |
//+------------------------------------------------------------------+
struct candlesticks
  {
      datetime          date;  // date
      double            val;  // val price
  };
//--- indicator buffers
double       val_buff[];

//--- global variables
candlesticks cand_buff[];
int          size=0;
int          ind=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   int default_size=100;
   ArrayResize(cand_buff,default_size);
//--- val the file
   ResetLastError();
   int file_handle=FileOpen("VGDEV_12SN2.TXT",FILE_READ|FILE_BIN|FILE_COMMON,',');
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- read data from the file
      while(!FileIsEnding(file_handle))
        {
         //--- write data to the array
         FileReadStruct(file_handle,cand_buff[size]);
         size++;
         //--- check if the array is overflown
         if(size==default_size)
           {
            //--- increase the array size
            default_size+=100;
            ArrayResize(cand_buff,default_size);
           }
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is read, %s file is closed",InpFileName);
     }
   else
     {
      PrintFormat("Failed to val %s file, Error code = %d",InpFileName,GetLastError());
      return(INIT_FAILED);
     }
//--- indicator buffers mapping
   SetIndexBuffer(0,val_buff,INDICATOR_DATA);
//--- empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &val[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])

  {
   ArraySetAsSeries(time,false);
//--- the loop for the candlesticks that have not been handled yet
   for(int i=prev_calculated;i<rates_total;i++)
     {
      //--- 0 by default
      val_buff[i]=0;
      //--- check if any data is still present
      if(ind<size)
        {
         for(int j=ind;j<size;j++)
           {
            //--- if dates coincide, the value from the file is used
            if(time[i]==cand_buff[j].date)
              {
               val_buff[i]=cand_buff[j].val;
              
      //assign to variable "a" value from buffer         
      a=val_buff[i];

               //--- increase the counter
               ind=j+1;
               break;
              }
           }
        }
     }
//--- return value of prev_calculated for next call
   return(a);
  }

void OnStart()
{

      if (a==1 )
      {    
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
      }
      if (a==2 )
      {
      ticket1=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
      }
 
CristianNicolae:

Hello,

Maybe someone can help which is highly appreciated.

I want to  backtest an EA in order to find out the optimal stop loss/profitability. Ea runs based on external value from another software.

I have the CSV file for one month(file stored in \tester\files):

e.g.

2017.05.01 00:00:00,1
2017.05.01 00:01:00,2
2017.05.01 00:02:00,1
2017.05.01 00:03:00,2
2017.05.01 00:04:00,1
2017.05.01 00:05:00,2

.........................


And if I run the program below nothing happens, no useful message in log:

it says that file is read, location of file and file closed.
Reason: