spreadmin

This tool is designed as a specialized visual indicator for the MetaTrader platform that tracks and records the "Minimum Spread" experienced on a specific financial instrument. In trading, the spread represents the difference between the buying and selling price; this script focuses specifically on capturing the lowest points of that difference over time.

How the Logic Works

1. Data Loading and Synchronization When the indicator is first loaded onto your chart, it automatically looks for an existing data file on your computer. It scans this historical record to populate your current chart with past "minimum spread" values. This ensures that when you open a chart, you aren't starting from zero but can see the historical context of how spreads have behaved in the past.

2. Real-Time Monitoring The script constantly monitors the live market feed. Every time the price updates (every "tick"), the tool compares the current spread against the lowest spread it has recorded during the current session.

  • If the current spread is lower than any previously seen in the current period, the script updates its memory to recognize this new "all-time low" for that session.
  • If the current spread is higher, the script remembers and maintains the lowest value it found previously.

3. Automated Logging One of the key features of this logic is its persistence. Every time the market moves, the script writes the most recent data to a dedicated local file. This means that even if you close your trading platform or restart your computer, the "minimum spread" data is saved externally. When you reopen the platform, the tool can read this file to continue where it left off.

4. Visual Representation On your chart, these values are displayed as a histogram in a separate window. This provides a clear, visual history of the minimum spreads, allowing you to observe patterns, identify periods of high/low liquidity, and see how the market's costs have fluctuated over time without cluttering your main price candles.

Miscellaneous
Implements a curve of type %1Uses files from the file systemIt reads information from a fileIt writes information to file
2 Views
0 Downloads
0 Favorites
spreadmin
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  Red
#property indicator_width1  2
#property indicator_minimum 0 // Set indicator fixed min is 0
//---
double spreadbufferMIN[]; // Buffer to hold the array of spreads
double tempmin=0;
//---
string FileName;
bool SymbolIsValid;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(0); // We don't need to display fractions
//---
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,indicator_width1,indicator_color1);
   SetIndexBuffer(0,spreadbufferMIN);
//---
   IndicatorShortName("spreadmin");
   SetIndexLabel(0,"Spread");
//---
   SymbolIsValid=((MarketInfo(Symbol(),MODE_BID)!=0));
   FileName=Symbol()+EnumToString(PERIOD_CURRENT)+"-MINs.dat";
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(SymbolIsValid) {WriteSpread();} return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void GetData()
  {
   double MIN;
   int i;
   int handle=FileOpen(FileName,FILE_BIN|FILE_READ);
//---
   if(handle < 0) return;
//---
   while(!FileIsEnding(handle))
     {
      datetime MyTime=(datetime)FileReadLong(handle);
      MIN=FileReadDouble(handle);
      //---
      i=iBarShift(Symbol(),Period(),MyTime,TRUE);
      //---
      if(i>=0)
        {
         spreadbufferMIN[i]=MIN;
        }
     }
   FileClose(handle);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void WriteSpread()
  {
   int handle=FileOpen(FileName,FILE_BIN|FILE_READ|FILE_WRITE);
//---
   FileSeek(handle,0,SEEK_END);
   FileWriteLong(handle,Time[0]);
   FileWriteDouble(handle,spreadbufferMIN[0]);
   FileClose(handle);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   double MySpread=MarketInfo(Symbol(),MODE_SPREAD);;
//---
   static bool FirstRun=TRUE;
//---
   if(!SymbolIsValid) {GetData(); return(rates_total);}
//---
   if(FirstRun)
     {
      GetData();
      FirstRun=FALSE;
      tempmin = MySpread;
      return(rates_total);
     }
//---
   if(MySpread<=spreadbufferMIN[0])
     {spreadbufferMIN[0]=MySpread; tempmin=MySpread; }
   else
     {spreadbufferMIN[0]=tempmin;}
//---
   WriteSpread();
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments