IND_Monitoring-Spread

Download script
IND_Monitoring-Spread

This tool is designed to monitor and visualize the behavior of "spreads"—the difference between the buying and selling prices of an asset—over time. Rather than just showing you a single snapshot of current costs, this script captures and archives the fluctuations in market liquidity to help traders understand price volatility patterns.

How the Logic Works

1. Data Collection and Logging The tool operates by tracking three specific metrics simultaneously: the highest spread (maximum cost), the lowest spread (minimum cost), and the average spread over a period of time. As the market moves, the script continuously updates these values. Every time a new candle or time interval begins, it saves this accumulated data into a dedicated local file. This ensures that your historical data is preserved and can be reloaded even after the software is restarted.

2. Dynamic Tracking The logic follows a "rolling" calculation:

  • Max/Min Tracking: It constantly checks if the current spread is higher than the highest recorded for that period or lower than the lowest.
  • Averaging: It keeps a running total of all spreads encountered during a specific timeframe and divides them by the number of observations to provide a balanced average.
  • Real-Time Updating: Every second, the script compares the current time with the previous one. If the time hasn't changed, it updates the running statistics; if a new timeframe begins, it "locks in" the old data and starts a fresh calculation for the new period.

3. Visual Presentation The results are displayed in a separate window as a three-color histogram:

  • Blue: Represents the Maximum Spread.
  • Lime: Represents the Average Spread.
  • Red: Represents the Minimum Spread.

Key Functional Benefits

  • Historical Persistence: By using a file-based system, the tool can reconstruct past market behavior by reading data from previous sessions.
  • Automatic Synchronization: The script automatically detects the symbol and timeframe you are currently viewing, creating a specific data log for that exact pair.
  • Volatility Awareness: By visualizing the gap between the Red (Min) and Blue (Max) bars, traders can quickly identify periods of high market stress or low liquidity, which often precede significant price movements.
Miscellaneous
Implements a curve of type %1Uses files from the file systemIt reads information from a fileIt reads information from a fileIt writes information to fileIt writes information to file
11 Views
0 Downloads
0 Favorites
IND_Monitoring-Spread
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_minimum -1

double MaxSpread[], AvgSpread[], MinSpread[];
bool Flag;

int MinSp, MaxSp, Vol, PrevTime;
double AvgSp;
string FileName;

bool RealSymbol( string Str )
{
  return(MarketInfo(Str, MODE_BID) != 0);
}

void init()  
{   
  int i;
   
  SetIndexBuffer(0, MaxSpread);
  SetIndexStyle(0, DRAW_HISTOGRAM); 
   
  SetIndexBuffer(1, AvgSpread);
  SetIndexStyle(1, DRAW_HISTOGRAM);
   
  SetIndexBuffer(2, MinSpread);
  SetIndexStyle(2, DRAW_HISTOGRAM);

  for (i = 0; i <= 8; i++)
   SetLevelValue(i, i * 10);
   
  Flag = RealSymbol(Symbol());
  FileName = Symbol() + Period() + "_Spread.dat";
         
 return;  
}

void GetData()
{
  double Min, Avg, Max;
  int T, i;
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ);
  
  if (handle < 0)
    return;

  while (!FileIsEnding(handle))
  {
    T = FileReadInteger(handle);     
    Max = FileReadDouble(handle);
    Avg = FileReadDouble(handle);
    Min = FileReadDouble(handle);
      
    i = iBarShift(Symbol(), Period(), T, TRUE);
    
    if (i >= 0)
    {
      MaxSpread[i] = Max;
      AvgSpread[i] = Avg;
      MinSpread[i] = Min;
    }
  }
  
  FileClose(handle);
  
  return; 
}

void CreateNewSpread()
{
  MinSp = (Ask - Bid) / Point + 0.1;
  MaxSp = MinSp;
  AvgSp = MinSp;
  Vol = 1;
  PrevTime = Time[0];
  
  return;
}

void ModifySpread()
{
  int Spread = (Ask - Bid) / Point + 0.1;
  
  if (Spread > MaxSp)
    MaxSp = Spread;
  else if (Spread < MinSp)
    MinSp = Spread;
    
  AvgSp += Spread;
  Vol++;
  
  PrevTime = Time[0];
  
  return;
}

void WriteSpread()
{
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ|FILE_WRITE);
  
  AvgSp /= Vol;
  
  FileSeek(handle, 0, SEEK_END);
  FileWriteInteger(handle, PrevTime);
  FileWriteDouble(handle, MaxSp);
  FileWriteDouble(handle, AvgSp);
  FileWriteDouble(handle, MinSp);
  
  FileClose(handle);

  return;  
}

void deinit()
{
  if (Flag)
    WriteSpread();
  
  return;
}

void start()  
{       
  static bool FirstRun = TRUE;
  
  if (!Flag)
  {
    GetData();
    
    return;
  }
  
  if (FirstRun)
  {
    GetData();
    CreateNewSpread();
    
    FirstRun = FALSE;
    
    return;
  }

  if (PrevTime == Time[0])
    ModifySpread();
  else
  {
    WriteSpread();
    
    CreateNewSpread();
  }
    
  MaxSpread[0] = MaxSp;
  AvgSpread[0] = AvgSp / Vol;
  MinSpread[0] = MinSp;
   
  return;  
}

Comments