Einzelnen Beitrag anzeigen
  #1 (permalink)  
Alt 29.03.17
Kronenchakra Kronenchakra ist offline
Gesperrter Benutzer
 
Registriert seit: Feb 2016
Ort: 2100 Österreich
Beiträge: 313
Kronenchakra befindet sich auf einem aufstrebenden Ast
Standard Textdateien schreiben

Nachdem sehr oft nach Hilfe beim Dateischreiben gefragt wird, post ich hier ein Beispiel,
das als EA programmiert ist und Tickdaten in eine Excel csv-Datei schreibt.
Code:
//+------------------------------------------------------------------+
//|                                               FileWriteTicks.mq5 |
//|                                Copyright © 2017 Ing. Otto Pauser |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017 Ing. Otto Pauser"
#property version   "1.00"
#property description "Dieser EA schreibt die Tickdaten in eine Datei"
#property description "und läuft sowohl im MT5 als auch im MT4!"
#property description "Der Symbolname wir automatisch dem Dateinamen vorgestellt."
#property description "Dateipfad und Datei werden angelegt, falls nicht vorhanden"
#property description "Eine eventuell bestehende Datei wird überschrieben"
#property description "Press <ESC> to stop recording tickdata"

input string fPath = "Tickdata";    // File Path
input string fName = "Ticks.csv";   // File Name
input bool   fComm = false;         // Write to CommonFiles

string   fn = fPath+"\\"+_Symbol+"_"+fName;     // full filename
int      fh,                                    // file handle
         ff = FILE_WRITE|FILE_ANSI|FILE_TXT;    // file flags
         
int OnInit()
{
   if(fComm)                        // CommonFiles selected ?
      ff=ff|FILE_COMMON;            // merge the common flag to fileflags
      
   fh=FileOpen(fn,ff);              // try to open
   
   if(fh==INVALID_HANDLE)           // check the handle
      {
         Alert("*ERROR* creating "+fn);
         return(INIT_FAILED);
      }
      
   FileWriteString(fh,"Date;Time;Ask;Bid;Spread\n");  // write a header

   Comment("Recording tickdata to file: "+fn);        // information

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   Comment("");               // clear information
   if(fh!=INVALID_HANDLE)     // file open ?
      FileClose(fh);          // close the file
}

void OnTick()
{
   MqlTick  last_tick;        // for tickvalues
   int      last_spread;      // for the spread
   string   stOut;            // string to write
   string   delim = ";";      // excel delimiter
   
   if(!SymbolInfoTick(Symbol(),last_tick)) return;             // try to get tickinfo
   
   last_spread=(int)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);  // get spreadinfo
   
   stOut=TimeToString   (last_tick.time,TIME_DATE)   +delim+   // combine the line out
         TimeToString   (last_tick.time,TIME_SECONDS)+delim+
         DoubleToXlsStr (last_tick.ask)              +delim+
         DoubleToXlsStr (last_tick.bid)              +delim+
         IntegerToString(last_spread)+"\n" ;                   // don't forget \n for new line
         
   FileWriteString(fh,stOut);                                  // write the line to file
}

string DoubleToXlsStr(double val)               // translate double to excelstring
{
   string result = DoubleToString (val,_Digits);
   StringReplace(result, ".", ",");             // excel needs a comma not a dot
   return(result);
}

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
   if(id==CHARTEVENT_KEYDOWN)                   // key pressed ?
      if(lparam==27)                            // <ESC> ?
         ExpertRemove();                        // remove executes OnDeinit first, then removes itself
}
Angehängte Dateien
Dateityp: mq5 FileWriteTicks.mq5 (6,6 KB, 1x aufgerufen)