Spread_Logger_Read
Download scriptThis script is designed to analyze and visualize historical market "spread" data (the difference between the bid and ask prices) by merging real-time market information with a custom external database. Instead of just showing what the spread is right now, it allows you to look back at specific historical periods provided in a separate file.
Core Functionality
1. External Data Integration The tool begins by looking for a specific CSV file located in your platform's files directory. It reads this file to import two primary pieces of information: timestamps (when the data was recorded) and spread values (the actual cost at those moments). If the file is missing or formatted incorrectly, the script provides an on-screen alert to notify you of the error.
2. Statistical Analysis Once the data is loaded from your file, the script automatically performs several calculations to provide a summary of the market conditions:
- Mean Spread: It calculates the average spread across all data points provided in your file.
- Mode: It identifies the most frequently occurring spread value and calculates what percentage of the time that specific value appeared.
- Time Duration: It automatically calculates the total timeframe covered by your data (e.g., minutes, hours, days, or months) and displays this clearly on your chart.
3. Visual Mapping and Overlay The script aligns the historical data from your file with your current trading chart. As you scroll through different timeframes on your screen, the indicator "maps" the historical spread values onto your current view. This allows you to see how the historical spreads compare to the live market spread currently being quoted.
4. Real-Time Monitoring In addition to the historical data, the script continuously monitors the "live" market. It calculates and displays the current active spread in pips directly on your chart window. This ensures that you can see your current trading costs side-by-side with the statistical averages derived from your historical files.
User Interface Elements
The script provides a clean, text-based dashboard on the chart that updates automatically:
- Time Tested: Shows the total duration of the data being analyzed.
- Mean Spread: Displays the average cost over that period.
- Mode: Shows the most common spread and its frequency.
- Current Spread: Displays the live pip value for the current symbol.
- Status Messages: Provides real-time feedback on whether the file was loaded successfully or if there is an error with the file path.
//+==================================================================+
//| Spread_Read.mq4 |
//| Copyright © 2011, Yevgeny Getman |
//+==================================================================+
#property copyright "Yevgeny Getman"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
extern string csv_filename = "my_spread_data";
//---------------------------------------------+
double MapBuffer[];
int TimeArray[];
double SpreadArray[];
bool have_error;
static datetime data_start;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName("Spread");
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,MapBuffer);
SetLevelStyle(STYLE_DASHDOT,1);
SetLevelValue(0,1);
SetLevelValue(1,2);
SetLevelValue(2,3);
//-------------------------------------+
data_start = Time[0];
int handle=FileOpen(csv_filename+".csv",FILE_CSV|FILE_READ,';');
if(handle>0)
{
int n=0;
DrawFxLbl("msg","Reading file...",0,4,25,10,"Times New Roman",White,false);
DrawFxLbl("file","terminal/experts/files/",0,3,40,8,"Courier",White,false);
DrawFxLbl("file2",csv_filename+ ".csv",0,187,40,8,"Courier",DeepSkyBlue,false);
while(!FileIsEnding(handle))
{
double val = FileReadNumber(handle);
if(val > 100) //--- Time value
{
ArrayResize(TimeArray,ArraySize(TimeArray)+1);
TimeArray[ArraySize(TimeArray)-1]=val;
}
else //--- Spread value
{
ArrayResize(SpreadArray,ArraySize(SpreadArray)+1);
SpreadArray[ArraySize(SpreadArray)-1]=val;
}
n++;
}
FileClose(handle);
DrawFxLbl("msg","File Read Successfully!! ",0,4,26,10,"Times New Roman",White,false);
}
else if(handle<1) //--- Error Handling
{
int code=GetLastError();
DrawFxLbl("error","error: "+DoubleToStr(code,0),0,4,25,10,"Times New Roman",Red,false);
Alert("error",code);
if(code==4103)
DrawFxLbl("nofile","cannot open "+csv_filename+".csv",0,4,35,10,"Times New Roman",White,false);
Print("cannot open "+csv_filename+".csv ; make sure csv_filename is correct and file is placed in terminal/experts/files directory");
have_error=true;
return(0);
}
//-------------------------------------+
int m;
double sec_tested=TimeArray[ArraySize(TimeArray)-1]-TimeArray[0],
timedisplayed=FormatTime(sec_tested);
double SpreadVals[][2];
string units=FormatUnits(sec_tested);
//-------------------------------------+
double sum=0;
ArrayResize(SpreadVals,ArrayRange(SpreadVals,0)+1);
SpreadVals[ArrayRange(SpreadVals,0)-1][1]=SpreadArray[0];
SpreadVals[ArrayRange(SpreadVals,0)-1][0]=1;
for(m=0;m<ArraySize(SpreadArray);m++)
{
sum=sum+SpreadArray[m];
bool have_val=false;
for(n=0;n<ArraySize(SpreadVals);n++)
if(SpreadVals[n][1]==SpreadArray[m])
{
have_val=true;
SpreadVals[n][0]=SpreadVals[n][0]+1;
break;
}
if(!have_val && SpreadArray[m]!=0)
{
ArrayResize(SpreadVals,ArrayRange(SpreadVals,0)+1);
SpreadVals[ArrayRange(SpreadVals,0)-1][1]=SpreadArray[m];
}
}
double MeanSpread = sum/ArraySize(SpreadArray);
int index=0;
double max_num=SpreadVals[index][0];
for(n=0;n<ArrayRange(SpreadVals,0);n++)
if(SpreadVals[n][0] > max_num) {max_num=SpreadVals[n][0]; index=n;}
double Mode = SpreadVals[index][1];
//---------------------------------+
DrawFxLbl("TTested","Time Tested: "+DoubleToStr(timedisplayed,1)+" "+units+" ("+ArraySize(SpreadArray)+" bars)",0,4,55,9,"Times New Roman",White,false);
DrawFxLbl("MeanSpread","Mean Spread: "+DoubleToStr(MeanSpread,1),0,4,70,9,"Times New Roman",White,false);
DrawFxLbl("Mode","Mode: "+DoubleToStr(Mode,1)+" ("+DoubleToStr(max_num,1)+"%)",0,4,85,9,"Times New Roman",White,false);
//--------------------------------------------------+(max_num/ArraySize(SpreadArray))*100
return(0);
}
//===============================================================================================================|
//===============================================================================================================|
int start()
{
if(have_error) return(-1);
int m, i=Bars-IndicatorCounted()-1;
//--------------------------------------------------+
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);
//--------------------------------------------------+
while (i>=0)
{
if(Time[i]==Time[0])
MapBuffer[i]=AdjSpread;
else
for(m=0;m<ArraySize(TimeArray);m++)
if(Time[i]==TimeArray[m])
MapBuffer[i]=SpreadArray[m];
i--;
}
//--------------------------------------------------+
return(0);
}
//===============================================================================================================|
//+------------------------------------------------------------------+
double FormatTime(double sec)
{
if(sec<60)
return(sec);
else if(sec>=60 && sec<3600)
return(sec/60);
else if(sec>=3600 && sec<86400)
return(sec/3660);
else if(sec>=86400 && sec<604800)
return(sec/86400);
else if(sec>=604800 && sec<2419200)
return(sec/604800);
else if(sec>=2419200)
return(sec/31536000);
}
//+------------------------------------------------------------------+
string FormatUnits(double sec)
{
if(sec<60)
return("sec");
else if(sec>=60 && sec<3600)
return("min");
else if(sec>=3600 && sec<86400)
return("hrs");
else if(sec>=86400 && sec<604800)
return("days");
else if(sec>=604800 && sec<2419200)
return("wks");
else if(sec>=2419200)
return("mo");
}
//+------------------------------------------------------------------+
//------------------- Draw Fixed Label Function ------------------------|
void DrawFxLbl(string OName, string TEXT, 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, TEXT, FSize, Font, FColor);
}
//----------------------------------------------------------------------|
//+------------------+
int deinit()
{
ObjectsDeleteAll();
return(0);
}
//+------------------+
Comments