Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools

Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools (http://www.expert-advisor.com/forum/index.php)
-   Codeschnipsel (http://www.expert-advisor.com/forum/forumdisplay.php?f=292)
-   -   Textdateien schreiben (http://www.expert-advisor.com/forum/showthread.php?t=5575)

Kronenchakra 29.03.17 20:32

Textdateien schreiben
 
Liste der Anhänge anzeigen (Anzahl: 1)
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
}



Alle Zeitangaben in WEZ +2. Es ist jetzt 15:35 Uhr.

Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.1
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum