tickrecorder

Download script
Author: Copyright (c) 2016 David Ruzicka
tickrecorder

This tool is designed to act as a high-precision data logger for financial markets. Its primary purpose is to capture and record every individual "tick"—the smallest possible price movement—and save that information into a structured spreadsheet format (CSV file) on your computer.

Core Functionality

1. Automatic File Management When you first load the script, it automatically creates a dedicated data file named after the specific financial instrument you are viewing (for example, if you are looking at EURUSD, it creates a corresponding tick file). If the file already exists, the script is smart enough to jump to the end of the existing data so that it doesn't overwrite your previous records.

2. Precise Data Collection Every time the market moves, the script captures four specific pieces of information simultaneously:

  • The Exact Timestamp: It records the date and time, including millisecond precision, allowing you to see exactly when a movement occurred.
  • Bid Price: The current price at which you can sell.
  • Ask Price: The current price at which you can buy.
  • Tick Volume: It calculates the difference in volume between the current move and the previous one, providing a clear picture of market activity.

3. Efficient Data Recording To ensure the script doesn't slow down your trading platform, it uses an intelligent "flushing" system. Instead of forcing the computer to save to the hard drive every single millisecond (which can be taxing on system resources), it holds the data in a temporary buffer and writes it to the file every 10 seconds. This provides a perfect balance between high-speed data collection and smooth platform performance.

4. Error Handling and Transparency The script is built with "safety checks." If it encounters an issue—such as being unable to create a file or failing to write data—it will immediately send an alert notification on your screen. This ensures that you are always aware of the status of your data collection without having to monitor the background processes manually.

Summary for the User

Essentially, this script serves as a "black box" flight recorder for your trading charts. It allows you to export a raw, chronological history of every single price change, which can then be opened in programs like Excel for deep analysis, backtesting, or auditing market behavior.

Miscellaneous
It writes information to fileIt issuies visual alerts to the screenUses files from the file system
14 Views
0 Downloads
0 Favorites
tickrecorder
//+--------------------------------------------+
//|                           TickRecorder.mq4 |
//|           Copyright (c) 2016 David Ruzicka |
//| https://www.mql5.com/en/users/davidruzicka |
//+--------------------------------------------+
#property copyright "Copyright (c) 2016 David Ruzicka"
#property link      "https://www.mql5.com/en/users/davidruzicka"
#property indicator_chart_window
//+------------------------------------------------------------------+
string fileName;
int fileHandle;
//+------------------------------------------------------------------+
long lastTickVolume=-1;
datetime lastFlushTime;
//+------------------------------------------------------------------+
#import "kernel32.dll"
void GetLocalTime(int &TimeArray[]);
#import
//+------------------------------------------------------------------+
int OnInit()
  {
   int result=INIT_FAILED;

   fileName=Symbol()+"_tick.csv";

   if(OpenFileHandle(fileHandle,fileName))
     {
      result=INIT_SUCCEEDED;
     }

   lastFlushTime=TimeCurrent();

   return( result );
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(fileHandle!=INVALID_HANDLE)
     {
      FileClose(fileHandle);
     }
  }
//+------------------------------------------------------------------+
int OnCalculate(
                const int rates_total,      // size of input time series 
                const int prev_calculated,  // bars handled in previous call 
                const datetime& time[],     // Time 
                const double& open[],       // Open 
                const double& high[],       // High 
                const double& low[],        // Low 
                const double& close[],      // Close 
                const long& tick_volume[],  // Tick Volume 
                const long& volume[],       // Real Volume 
                const int& spread[]         // Spread 
                )
  {
   int TimeArray[4];
   GetLocalTime(TimeArray);
   int milliSeconds=TimeArray[3]>>16;
   string milliSecondsStr=IntegerToString(milliSeconds,3,'0');

   datetime serverTime=TimeCurrent();

   string dateTimeStr=TimeToString(serverTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS);

   ArraySetAsSeries(tick_volume,true);
   long tickVolume=tick_volume[0];
   long tickVolumeDiff=(lastTickVolume==-1) ? 1 :(tickVolume>lastTickVolume ? tickVolume-lastTickVolume : tickVolume);
   lastTickVolume=tickVolume;

   uint bytesWritten=FileWrite(fileHandle,dateTimeStr+"."+milliSecondsStr,NormalizeDouble(Bid,Digits),NormalizeDouble(Ask,Digits),tickVolumeDiff);
   if(bytesWritten==0)
     {
      Alert(StringFormat("Failed to write tick file data into \"%s\"; error: %d",fileName,GetLastError()));
     }

// flush after 10 seconds
   if(serverTime-lastFlushTime>10)
     {
      FileFlush(fileHandle);
      lastFlushTime=TimeCurrent();
     }

   return(0);
  }
//+------------------------------------------------------------------+
bool OpenFileHandle(int &handle,string name)
  {
   handle=FileOpen(name,FILE_SHARE_READ|FILE_CSV|FILE_WRITE|FILE_READ,',');
   if(handle==INVALID_HANDLE)
     {
      Alert("Failed to open tick file \"",name,"\"; error: ",GetLastError());
      return(false);
     }
   else if(FileSize(handle)==0)
     {
      uint bytesWritten=FileWrite(handle,"datetime","bid","ask","volume");
      if(bytesWritten==0)
        {
         Alert("Failed to write tick file header into \"",name,"\"; error: ",GetLastError());
         return(false);
        }
     }
   else
     {
      FileSeek(handle,0,SEEK_END);
     }

   return(true);
  }
//+------------------------------------------------------------------+

Comments