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:
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); }
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); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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: