Download MetaTrader 5

Writing bid-ask prices on a csv file

To add comments, please log in or register
hardhu
90
hardhu  

Hello everybody,

what I would like to do is to write a simple expert advisor that does this three steps:

1) open a csv file for writing;

2) writes bid and ask prices in that csv files, in different lines;

3) closes the file.

 

I would like to put these three steps respectively in OnInit, OnTick and OnDeinit function, but I am not able to pass the filehandle of the file from OnInit to the other two functions, can anybody help me? Here's my code:

 

void OnInit()
  {
   ResetLastError();
   int filehandle=FileOpen("bidask.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {      
      Print("File correctly opened");
     }
   else Print("Errorin opening file,",GetLastError());
    }

void OnTick()
  {
   MqlTick last_tick;
   if(SymbolInfoTick(Symbol(),last_tick))
     {
     Print(last_tick.time,": Bid = ",last_tick.bid,
            " Ask = ",last_tick.ask,");

      FileWrite(filehandle,last_tick.bid,last_tick.ask);
     }
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
    }

void OnDeinit()
  {
  FileClose(filehandle);
  }
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
  • 2010.04.07
  • Denis Zyatkevich
  • www.mql5.com
MetaQuotes Programming Language 5 (MQL5), included in MetaTrader 5 Client Terminal, has many new possibilities and higher performance, compared to MQL4. This article will help you to get acquainted with this new programming language. The simple examples of how to write an Expert Advisor and Custom Indicator are presented in this article. We will also consider some details of MQL5 language, that are necessary to understand these examples.
Alain Verleyen
Moderator
32814
Alain Verleyen  
hardhu:

Hello everybody,

what I would like to do is to write a simple expert advisor that does this three steps:

1) open a csv file for writing;

2) writes bid and ask prices in that csv files, in different lines;

3) closes the file.

 

I would like to put these three steps respectively in OnInit, OnTick and OnDeinit function, but I am not able to pass the filehandle of the file from OnInit to the other two functions, can anybody help me? Here's my code:

 

You have to use a global variable for your file handle.
hardhu
90
hardhu  
angevoyageur:
You have to use a global variable for your file handle.

Thanks for your suggestion. So, is in your opinion this modification the proper way to use a global variable?

int indfile;

int OnInit()
  {
   ResetLastError();
   int filehandle=FileOpen("bidask.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {      
      Print("File opened correctly");
     }
   else Print("Error in opening file,",GetLastError());
   indfile=filehandle;
   return(INIT_SUCCEEDED);
    }


void OnTick()
{
    MqlTick last_tick;
   if(SymbolInfoTick(Symbol(),last_tick))
     {
     Print(last_tick.time,": Bid = ",last_tick.bid,
            " Ask = ",last_tick.ask,"  Volume = ",last_tick.volume);

      FileWrite(indfile,last_tick.bid,last_tick.ask);
     }
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
  }
  
 

void OnDeinit(const int reason)
  {
 FileClose(indfile);
  }
Alain Verleyen
Moderator
32814
Alain Verleyen  
hardhu:

Thanks for your suggestion. So, is in your opinion this modification the proper way to use a global variable?

That should work, but why add an other variable ? you can define filehandle as global.

int filehandle;

int OnInit()
  {
   ResetLastError();
   filehandle=FileOpen("bidask.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {      
      Print("File opened correctly");
     }
   else Print("Error in opening file,",GetLastError());

   return(INIT_SUCCEEDED);
    }
void OnTick()
{
    MqlTick last_tick;
   if(SymbolInfoTick(Symbol(),last_tick))
     {
     Print(last_tick.time,": Bid = ",last_tick.bid,
            " Ask = ",last_tick.ask,"  Volume = ",last_tick.volume);

      FileWrite(filehandle,last_tick.bid,last_tick.ask);
     }
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
  }
  
 

void OnDeinit(const int reason)
  {
 FileClose(filehandle);
  }
To add comments, please log in or register