Spread_Logger_Write

Download script
Author: Yevgeny Getman
Spread_Logger_Write

This tool is designed to automatically monitor and record the "spread"—the difference between the buying and selling prices of a financial instrument—directly into a spreadsheet-compatible file. It acts as an automated data logger that provides real-time visual feedback on your trading platform while systematically archiving market conditions for later analysis.

How it Works

1. Real-Time Monitoring and Display The script constantly tracks the current spread of the active currency pair or asset you are viewing. It automatically converts this raw numerical data into a human-readable format (pips), which is then displayed directly on your trading screen as a text label. This allows you to see exactly how much it costs to enter a trade at any given second.

2. Smart Data Recording Instead of recording data every millisecond—which would create unnecessary clutter—the script uses a "New Bar" logic. It waits for the start of a new candle (a new price bar) on your chart before capturing the information. This ensures that you receive a clean, organized snapshot of the spread at regular intervals corresponding to your timeframe.

3. Automated File Export Once a new bar begins, the script automatically performs the following background tasks:

  • It opens (or creates) a specific CSV file in your platform's designated data folder.
  • It appends the exact time of the new bar alongside the current adjusted spread.
  • It saves this information so that you can easily open the resulting file in software like Microsoft Excel or Google Sheets.

4. User Feedback and Status Updates To ensure everything is working correctly, the script provides live status updates on your screen. It communicates its current state to you through visual labels, such as indicating when it is waiting for a new bar, when it is actively writing data to the file, or if an error has occurred. If any issues arise during the saving process, it will highlight the specific error code to help with troubleshooting.

Summary of Benefits

This tool eliminates the need for manual data entry. By automatically capturing spread fluctuations at every new candle, it allows you to build a historical database of market liquidity and costs, which is essential for analyzing how trading conditions change throughout the day or over different sessions.

Miscellaneous
Uses files from the file systemIt writes information to file
3 Views
0 Downloads
0 Favorites
Spread_Logger_Write
//+==================================================================+
//|                                                 Spread_Write.mq4 |
//|                                 Copyright © 2011, Yevgeny Getman |
//+==================================================================+
#property copyright "Yevgeny Getman"
#property indicator_chart_window

extern string csv_filename     = "my_spread_data";
//+------------------------------------------------------------------+
int init()
{
   DrawFxLbl("msg"+csv_filename,"Waiting for next bar...",0,4,25,10,"Times New Roman",White,false);
   return(0);
}
//+------------------------------------------------------------------+
int start()
{
   int handle;
   double Spread = MarketInfo(Symbol(),MODE_SPREAD);
   double AdjSpread = Spread/(0.0001/MathPow(10,-Digits));
   DrawFxLbl("spread","Current Spread: "+DoubleToStr(AdjSpread,1)+" pip",0,4,12,10,"Times New Roman",White,false);
   //--------------------------------------------------+
   if(Volume[0]==1)
   {    
       handle=FileOpen(csv_filename+".csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
       if(handle>0)
       {
          DrawFxLbl("msg"+csv_filename,"Writing data to file...",0,4,26,10,"Times New Roman",White,false);
          DrawFxLbl("file"+csv_filename,"terminal/experts/files/",0,3,40,8,"Courier",White,false);
          DrawFxLbl("file2"+csv_filename,csv_filename+ ".csv",0,187,40,8,"Courier",DeepSkyBlue,false);
          //-----------------------------------------------------------------------------------------+
          FileSeek(handle,0,SEEK_END);
          FileWrite(handle, Time[0], AdjSpread);
          FileClose(handle);
       }
       else if(handle<1)
       {
          DrawFxLbl("error","error: "+DoubleToStr(GetLastError(),0),0,4,25,10,"Times New Roman",Red,false);
          Print("error",GetLastError());
          return(0);
       }
    }
   //--------------------------------------------------+    
   return(0);
}
//+------------------------------------------------------------------+

//------------------- Draw Fixed Label Function ------------------------|
void DrawFxLbl(string OName, string Capt, int Corner, int DX, int DY, int FSize, string Font, color FColor, bool BG)
{
   if(ObjectFind(OName) < 0) 
   ObjectCreate(OName, OBJ_LABEL, 0, 0, 0);
   ObjectSet(OName, OBJPROP_CORNER, Corner);
   ObjectSet(OName, OBJPROP_XDISTANCE, DX);
   ObjectSet(OName, OBJPROP_YDISTANCE, DY);
   ObjectSet(OName, OBJPROP_BACK, BG);      
   ObjectSetText(OName, Capt, FSize, Font, FColor);
}
//----------------------------------------------------------------------|

//+-------------------+
int deinit()
{
   ObjectsDeleteAll();
   return(0);
}
//+-------------------+

Comments