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)
-   -   Binärdatei schreiben (http://www.expert-advisor.com/forum/showthread.php?t=5595)

Kronenchakra 05.04.17 23:25

Binärdatei schreiben
 
Binärdateien haben den Vorteil, daß keine Typenumwandlungen durchgeführt werden müssen.
Sie sind daher schneller zu schreiben und zu lesen, und sie sind wesentlich kleiner als Textdateien.
Hier das Beispiel analog zu Textdatei schreiben, allerdings im Binärformat.
Wie die zwei vorgehenden Beispiele läuft dieses sowohl im MT5 als auch im MT4.
Code:

//+------------------------------------------------------------------+
//|                                            FileWriteTicksBin.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 Binärdatei"
#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.bin";  // File Name
input bool  fComm = true;          // Write to CommonFiles
//+------------------------------------------------------------------+

string  fn = fPath+"\\"+_Symbol+"_"+fName;    // full filename
int      fh,                                    // file handle
        ff = FILE_WRITE|FILE_BIN;              // file flags

struct tickStruct                  // define the structure of data
{
  datetime Time;
  double  Ask;
  double  Bid;
  int      Spread;                // eigentlich unnötig, da Ask-Bid=Spread
};

tickStruct  tick;                  // declare a variable       

//+------------------------------------------------------------------+
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);
      }
     
  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
  PlaySound("ok");
}

//+------------------------------------------------------------------+
void OnTick()
{
  MqlTick  last_tick;        // for tickvalues

  if(!SymbolInfoTick(Symbol(),last_tick)) return;    // try to get tickinfo
 
  tick.Time    =last_tick.time;                      // fill the structure with data
  tick.Ask    =last_tick.ask;
  tick.Bid    =last_tick.bid;
  tick.Spread  =(int)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);

  FileWriteStruct(fh,tick);                          // write the structure to file
}

//+------------------------------------------------------------------+
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
}

Vielleicht kann's wer brauchen, würde mich freuen!


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