0
votes

Does anyone know if it is possible to store the series of value of an indicator, over, for example 1 year, or of a boolean function calculated in an EA?

I would like tho store the value of a particular function calculated on the difference between two different EMA that I defined into an EA.

I need to store the value of this double function, calculated in my EA over few years (say from 2015 to 2017) and print it in some output file (.txt or some other formats)

    int s15_60;
double B_M15_H1(int i) {

                          if (i>=0  && i<4  ) s15_60=0;
                     else if (i>=4  && i<8  ) s15_60=1;
                     else if (i>=8  && i<12 ) s15_60=2;
                     else if (i>=12 && i<16 ) s15_60=3;
                     else if (i>=16 && i<20 ) s15_60=4;

                     return NormalizeDouble(MathAbs(M15(i) - H1(s15_60)),Digits);

where M15 is a simple EMA calculated in the M15 timeframe, and H1 is the same EMA calculated in the H1 timeframe, and the double function is the distance between this two indicators calculated in M15 time steps.

My goal is to store this value in a output file for doing some statistical studies about this function.

EDIT:

This code works for my purpose:

#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for writing data to file
input string             InpFileName="BOX.csv";      // File name
input string             InpDirectoryName="Data";     // Folder name
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+



double H1  (int shift) {double val = iCustom(NULL,PERIOD_H1, "my_funct",100,2.0,30.0,2.0,2.0,0,1,0,shift); return(val);}
double H4  (int shift) {double val = iCustom(NULL,PERIOD_H4, "my_funct",100,2.0,30.0,2.0,2.0,0,1,0,shift); return(val);}


int s60_240;
double B_H1_H4(int i) {

                          if (i>=0  &&  i<4  ) s60_240=0;
                     else if (i>=4  &&  i<8  ) s60_240=1;
                     else if (i>=8  &&  i<12 ) s60_240=2;
                     else if (i>=12 &&  i<16 ) s60_240=3;
                     else if (i>=16 &&  i<20 ) s60_240=4;

                     return NormalizeDouble( 10000*MathAbs( H1(i) - H4(s60_240) ) , Digits);

                     } 


void OnStart()
  {

   double   box_buff[]; // array of indicator values
   datetime date_buff[]; // array of indicator dates
//--- copying the time from last 1000 bars
   int copied=CopyTime(NULL,PERIOD_H1,0,1000,date_buff);
   ArraySetAsSeries(date_buff,true);
//--- prepare box_buff array
   ArrayResize(box_buff,copied);


//--- copy the values of main line of the iCustom indicator
   for(int i=0;i<copied;i++)
     {
      box_buff[i]=B_H1_H4(i);
     }


//--- open the file for writing the indicator values (if the file is absent, it will be created automatically)
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, write the number of signals
      FileWrite(file_handle,copied);
      //--- write the time and values of signals to the file
      for(int i=0;i<copied;i++)
         FileWrite(file_handle,date_buff[i],box_buff[i]);
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is written, %s file is closed",InpFileName);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }
1

1 Answers

3
votes

Please provide your MCVE-code to see what you need. How would you like to store data and why you might need it? Indicator data is already stored in a buffer so simply call it using iCustom(). If you want to optimize your EA and indicator take much time to load and compute buffers - yes it is possible to compute indicator buffers once then write them into a file or DB and get before new optimization starts, in such case use CArrayObj or CArrayDouble as a dynamic array for storing large arrays