Einzelnen Beitrag anzeigen
  #1 (permalink)  
Alt 31.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 lesen

Das Gegenstück zu FileWriteTicks.
Beide Programme wären auch sehr gut für ein Script geeignet.
FileWriteTicks wurde aber vor dem Hintergrund im Tester Tickdaten zu saugen als EA angelegt.
Des weiteren kann man sich FileWriteTicks als Indi umschreiben um die Kerzendaten zu bekommen.

Code:
//+------------------------------------------------------------------+
//|                                                FileReadTicks.mq5 |
//|                                Copyright © 2017 Ing. Otto Pauser |
//+------------------------------------------------------------------+
#property copyright   "Copyright © 2017 Ing. Otto Pauser"
#property version     "1.00"
#property description "Dieser EA liest die Tickdaten von FileWriteTicks"
#property description "und läuft sowohl im MT5 als auch im MT4!"
#property description "Der Symbolname wir automatisch dem Dateinamen vorgestellt.\n"
#property description "Als Script wäre diese Programm natürlich auch möglich\n"
#property description "Press <ESC> to stop reading tickdata or wait till finished"

//+------------------------------------------------------------------+

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_READ|FILE_ANSI|FILE_TXT;     // file flags
         
struct csvRec           // structure of a csv record
{
   datetime DateTime;
   double   Ask;
   double   Bid;
   int      Spread;
};

//+------------------------------------------------------------------+

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* file "+fn+" not found.");
         return(INIT_FAILED);
      }
      
   Comment("Reading tickdata from file: "+fn);     // information

   ReadFile();                      // read the file

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+

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

//+------------------------------------------------------------------+

void ReadFile()
{
   string stIN;                     // string to read
   ushort delim = 59;               // ASCII-Code of ";" the excel delimiter
   string stPart[];                 // the single values
   csvRec rec;                      // the csv record
   
   stIN=FileReadString(fh);         // read the heading
   StringSplit(stIN,delim,stPart);  // split it
   PrintFormat("%s       %s     %s     %s     %s",stPart[0],stPart[1],stPart[2],stPart[3],stPart[4]);
   
   while(!FileIsEnding(fh))
      {
         stIN=FileReadString(fh);                  // read the line
         StringSplit(stIN,delim,stPart);           // split it
         stIN=stPart[0]+" "+stPart[1];             // combine date time
         rec.DateTime=StringToTime(stIN);          // convert strings, put it to the structure
         rec.Ask     = XlsStrToDouble (stPart[2]);
         rec.Bid     = XlsStrToDouble (stPart[3]);
         rec.Spread  = (int)StringToInteger(stPart[4]);
         
         Print(rec.DateTime," ",DoubleToString(rec.Ask,_Digits)," ",DoubleToString(rec.Bid,_Digits)," ",rec.Spread);
      }
   ExpertRemove();
}

void OnTick()                                   // noting to do here
{
}

double XlsStrToDouble(string val)               // translate excelstring to double
{
   StringReplace(val, ",", ".");                // mql needs a dot not a comma
   return(StringToDouble(val));
}

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
}
Viel Erfolg beim Textdateien Schreiben und Lesen.

Demnächst in diesem Theater: Das ganze mit Binärdateien.
Angehängte Dateien
Dateityp: mq5 FileReadTicks.mq5 (8,1 KB, 5x aufgerufen)