Ron_Spread001

Download script
Ron_Spread001

This tool is designed to monitor and visualize the "Spread"—the difference between the buy and sell prices of a financial instrument—in real-time. For traders, understanding this cost is essential because it represents the transaction fee imposed by the market provider.

How the Logic Works

The script operates by continuously tracking every price movement (or "tick") that occurs on your chart. Instead of just showing you a single snapshot of the current spread, it performs a series of calculations to give you a clearer picture of market behavior during the current time period:

  • Dynamic Tracking: The tool identifies the start of a new candle (time bar). Every time a new candle appears, the script resets its internal memory to start fresh for that specific period.
  • Maximum Spread Detection: As prices fluctuate, the script constantly monitors the difference between the Ask and Bid prices. It keeps a record of the highest spread reached during the current period, allowing you to see the "worst-case scenario" for costs at any given time.
  • Cumulative Averaging: The script maintains a running total of every spread value recorded since the current candle began. By dividing this total by the number of price movements (ticks) that have occurred, it calculates a rolling average. This helps smooth out temporary spikes and provides a more stable view of the typical cost over time.
  • Real-Time Feedback: Throughout the trading session, the script updates a text overlay on your screen. This provides an instant summary including:
    • The current live spread.
    • The highest spread encountered so far in the period.
    • The total sum of spreads recorded.
    • The count of price ticks processed.
    • The calculated average spread.

Visual Output

The script is configured to display these values visually on a separate window below your main chart. It utilizes three distinct lines (coded in Yellow, Green, and Red) to map out these data points, providing a visual history of how the spread has behaved as the market moves.

Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
Ron_Spread001
//+-----------+
//| Spread    |
//+-----------+
#property  copyright "Copyright 2005 Ron Thompson"
#property  link      "http://www.forexmt4.com/"

//---- indicator settings
#property  indicator_separate_window

#property indicator_buffers   3
#property indicator_color1  Yellow      
#property indicator_color2  Green
#property indicator_color3  Red

//---- buffers
double ExtMapBuffer1[];  //Yellow
double ExtMapBuffer2[];  //Green
double ExtMapBuffer3[];  //Red

//specific to indicator
double spread;
double spreadtot;
int    spreadticks;
double maxspread;


// bar open handling
int    bartime;

//+-----------+
//| Init      |
//+-----------+
int init()
  {
   // 233 up arrow
   // 234 down arrow
   // 158 little dot
   // 159 big dot
   // 168 open square
   // 120 box with X
   SetIndexBuffer(0,ExtMapBuffer1);   //Yellow
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);

   SetIndexBuffer(1,ExtMapBuffer2);  //Green
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);

   SetIndexBuffer(2,ExtMapBuffer3);  //Red
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 2);

   Print("Init complete");
  }

//+-----------+
//| DE-Init   |
//+-----------+
int deinit()
  {
   Print("DE-Init complete");
  }


//+-----------+
//| Each Tick |
//+-----------+
int start()
  {   
   int    pos;

   // draw once at open of bar 
   if(bartime!=Time[0])
     {
      bartime=Time[0];
      spreadtot=0;
      spreadticks=0;
      maxspread=0;
      ExtMapBuffer1[0]=0;
      //ExtMapBuffer2[0]=0;

     }    

   spread=Ask-Bid;

   if(spread>maxspread) maxspread=spread;

   spreadtot=spreadtot+spread;
   spreadticks++;

   ExtMapBuffer1[0] = spreadtot/spreadticks;
   //ExtMapBuffer2[0] = maxspread;
      
   Comment("Spread="+spread+"  MAXspread="+maxspread+"  Total="+spreadtot+"  Ticks="+spreadticks+"  Average=" + spreadtot/spreadticks);


  }//start


Comments