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 lesen (http://www.expert-advisor.com/forum/showthread.php?t=5579)

Kronenchakra 31.03.17 01:09

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


Alle Zeitangaben in WEZ +2. Es ist jetzt 03:25 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