Writing contents of a variable to file during optimization

 

Hello.

I'm trying to figure out how to write data from a variable for every pass in an optimization simultaneously on every tick to a file.


I am banging my head against the wall trying to figure this out.


This is my pathetic attempt to make something happen

double currentAsk;


void OnTick()
  {
       {
      currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
       
      FrameAdd("stats",1,0,currentAsk);
       }    
  }


  double OnTester()

  {
  
 int file = FileOpen("filetest",FILE_CSV|FILE_WRITE);
 
  }


One thing I don't understand is how to tell the FrameAdd function to write to the file.


Data get's added to the "frame: and then what?


Would appreciate the assistance.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 

Write ticks data to a file during an optimization doesn't make sense. Every pass will have the same ticks.

About FrameAdd and file, search on the forum. There is a full example I did myself years ago.

 

No I want to write any arbitrary information (indicator data, profit, drawdown, ect) on every tick, to a file, for every pass in the optimization. Simultaneously.


I did look at your example, I've spent the entire day trying to understand it.

To begin with, your example has errors during compile.


But also 

bool  FrameAdd(
   const string  name,        // Public name/label
   long          id,          // Public ID
   double        value,       // Value
   const void&   data[]       // Array of any type
   );

The options for FrameAdd do not instruct on how to tell the function to write to the file, and I can't see how it works in the examples.

I am thoroughly confused.

 
Colin Leamy:

No I want to write any arbitrary information (indicator data, profit, drawdown, ect) on every tick, to a file, for every pass in the optimization. Simultaneously.


I did look at your example, I've spent the entire day trying to understand it.

To begin with, your example has errors during compile.


But also 

The options for FrameAdd do not instruct on how to tell the function to write to the file, and I can't see how it works in the examples.

I am thoroughly confused.

My example compile fine.

Post your code if you need coding help.

FrameAdd using data file
FrameAdd using data file
  • 2017.01.03
  • www.mql5.com
Hello all, According to the MQL5 Official Documentation, the FrameAdd function can be called using two different sets of parameters...
 

I  did post my code. It's in my first post.


Here I will post again.


double currentAsk;


void OnTick()
  {
       {
      currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
       
      FrameAdd("stats",1,0,currentAsk);
       }    
  }


  double OnTester()

  {
  
 int file = FileOpen("filetest",FILE_CSV|FILE_WRITE);
 
  }

It compiles fine until I add the FileOpen function. I have no idea how to get FrameAdd to write into the file.

The official documentation says that the FrameAdd structure can only be used to write data 'from' a file not 'to' a file. 


1. Adding data from a file

bool  FrameAdd(
   const string  name,        // Public name/label
   long          id,          // Public ID
   double        value,       // Value
   const string  filename     // Name of a data file
   );

2. Adding data from an array of any type

bool  FrameAdd(
   const string  name,        // Public name/label
   long          id,          // Public ID
   double        value,       // Value
   const void&   data[]       // Array of any type
   );


So if the 'frame' is reading data from an array or file, I'm assuming the Frame() function is simply a container for this information before it is written to the file?

But my issue remains in that I do not understand the logic of how it sends the data to the file.


The only thing I can find that you wrote is 

//+------------------------------------------------------------------+
//| Optimization start                                               |
//+------------------------------------------------------------------+
void OnTesterInit()
  {
//--- If writing of optimization results is enabled
   Print(__FUNCTION__,"(): Start Optimization \n-----------",TerminalInfoString(TERMINAL_DATA_PATH));
  }
//+------------------------------------------------------------------+
//| Test completion event handler                                    |
//+------------------------------------------------------------------+
double OnTester()
  {
//--- If writing of optimization results is enabled
   int hl=FileOpen("filetest",FILE_CSV|FILE_WRITE);
   if(hl==INVALID_HANDLE)
     {
      printf("Error %i creating tester file",GetLastError());
     }
   else
     {
      int rndval=MathRand();
      FileWriteString(hl,StringFormat("this is a random test %i",rndval));
      FileClose(hl);

      //--- Create a frame
      if(!FrameAdd("Statistics",rndval,0,"filetest"))
         printf("FrameAdd failed with error %i",GetLastError());
      else
        {
         Print("Frame added");
        }
     }
//---
   return(0.0);
  }
//+------------------------------------------------------------------+
//| Next optimization pass                                           |
//+------------------------------------------------------------------+
void OnTesterPass()
  {
   ulong pass;
   string name;
   long id;
   double value;
   ushort data[];

   if(!FrameNext(pass,name,id,value,data))
      printf("Error #%i with FrameNext",GetLastError());
   else
      printf("%s : new frame pass:%llu name:%s id:%lli value:%f",__FUNCTION__,pass,name,id,value);

   string receivedData=ShortArrayToString(data);
   printf("Size: %i %s",ArraySize(data),receivedData);
   Comment(receivedData);
  }
//+------------------------------------------------------------------+
//| End of optimization                                              |
//+------------------------------------------------------------------+
void OnTesterDeinit()
  {
   Print("-----------\n",__FUNCTION__,"(): End Optimization");
  }

The only thing I see here is a bunch of if/if not/else statements that output errors and some FileWrite commands which do not work in optimization. I do not see how this can write to a file using Frame function.

 

Okay so I think I'm beginning to understand. 

So FrameAdd is just a function that authorizes FileWrite function to work within optimization?


double currentAsk;
   
         

void OnTick()
  
   
       
         {
         

      currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
//--- If writing of optimization results is enabled
   int hl=FileOpen("filetest",FILE_CSV|FILE_WRITE|FILE_SHARE_WRITE);
   if(hl==INVALID_HANDLE)
     {
      printf("Error %i creating tester file",GetLastError());
     }
   else
     {
      FileSeek(hl,0,SEEK_END); 
      FileWriteString(hl,StringFormat("this is a random test %i",currentAsk));
      FileClose(hl);

      //--- Create a frame
      if(!FrameAdd("Statistics",currentAsk,0,"filetest"))
         printf("FrameAdd failed with error %i",GetLastError());
      else
        {
         Print("Frame added");
        }
     }
//---
   
   }

I managed to get it working, however it outputs the file into a very obscure location. 

I need to figure out how to place it in a custom folder.

Also it does not output as .CSV, it outputs as .File.

Also it overwrites the data on every tick.

I tried adding the FileSeek function to correct this but it did not work.


Any advice?

 
Colin Leamy:

Okay so I think I'm beginning to understand. 

So FrameAdd is just a function that authorizes FileWrite function to work within optimization?


I managed to get it working, however it outputs the file into a very obscure location. 

I need to figure out how to place it in a custom folder.

Also it does not output as .CSV, it outputs as .File.

Also it overwrites the data on every tick.

I tried adding the FileSeek function to correct this but it did not work.


Any advice?

   int hl=FileOpen("filetest.csv",FILE_CSV|FILE_WRITE|FILE_SHARE_WRITE|FILE_COMMON);
 

I see! Thank you!


I have one more question.


I am trying to add a function to the Frame but I cannot compile for reasons I can't seem to figure out.

  input int g = 5;
  double total = 0;
  int y = 0;

   
int OnInit()
 
 {
   
   double shortSmaData[];
   ArraySetAsSeries(shortSmaData,true);  
   int shortSmaControlPanel = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE); 
   int numberOfShortSmaData = CopyBuffer(shortSmaControlPanel, 0, 0, 201, shortSmaData); 


 }

   
  
   

void OnTick()
{
 
   double MA (int g = 5)
      {
         for (y = g; y <= 200; y = y +5)
             {
             total += shortSmaData[y];
             } 
         return total;
      }

  // ------------------------------------------------------------------
      
   int hl=FileOpen("filetest.csv",FILE_CSV|FILE_WRITE|FILE_SHARE_WRITE);
   if(hl==INVALID_HANDLE)
         {
         printf("Error %i creating tester file",GetLastError());
         }
   else
     {
      FileSeek(hl,0,SEEK_END); 
      FileWrite(hl,ma(g));
      FileClose(hl);

      //--- Create a frame
    if(!FrameAdd("Statistics",MA(g),0,"filetest"))
         printf("FrameAdd failed with error %i",GetLastError());
    else
          {
          Print("Frame added");
          }
     }

   
 }
   
   

  

Could you inform me of my mistake? It all looks good to me.

Appreciated.

 

I tried again with an array this time and unlike the previous one, it compiles.

However I get a 'File Open Error [2]' message when running the optimization


 void OnTick()
  
{
         
   int  data[10];
        data[1] = 1;
        data[0] = 2;
     
  // int values(int& data[])   
    
   int hl=FileOpen("filetest",FILE_CSV|FILE_WRITE|FILE_SHARE_WRITE|FILE_ANSI|FILE_COMMON);
   
   if(hl==INVALID_HANDLE) 
         {
         printf("Error %i creating tester file",GetLastError());
         }
   else
     {
      FileSeek(hl,0,SEEK_END); 
      FileWrite(hl,data[0]);
      FileClose(hl);

     
      if(!FrameAdd("Statistics",0,0,data[0]))
         printf("FrameAdd failed with error %i",GetLastError());
      else
         {
         Print("Frame added");
         }
     }

   
}

I spent the entire day trying to get these two to work. I have no idea what to do.

 
I have an order open in the freelance section of this site if anyone wants to get paid to help me sort this out.
Reason: