Author: Copyright 2018, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
SpreadMap
ÿþ//+------------------------------------------------------------------+

//|                                                    SpreadMap.mq4 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Spread

#property indicator_label1  "Spread"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SpArray

#property indicator_label2  "SpreadMap"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellow

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- indicator buffers

extern int    ExtSize=0;

input bool     InpPrint=false;

double         SpreadBuffer[];

double         SpreadMapBuffer[];

const string SpreadMapName="SpreadMap";

int Showbars=ExtSize;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

   Showbars=(ExtSize<=0)?(int)ChartGetInteger(0,CHART_WIDTH_IN_BARS,ChartWindowFind()):ExtSize;//ChartGetInteger(0,CHART_WINDOWS_TOTAL)CHART_VISIBLE_BARS

   if(ChartSetInteger(0,CHART_SHOW_ASK_LINE,0,true))

      ChartSetInteger(0,CHART_COLOR_ASK,clrSnow);

   if(ChartSetInteger(0,CHART_SHOW_BID_LINE,0,true))

      ChartSetInteger(0,CHART_COLOR_BID,clrSilver);

   if(ObjectFind(SpreadMapName)< 0) //ObjectFind(Name)ÿif (ObjectFind(name + " Label") != 0)

     {

      ObjectCreate(SpreadMapName, OBJ_ARROW_RIGHT_PRICE,ChartWindowFind(),TimeCurrent(), 0);

      ObjectSet(SpreadMapName, OBJPROP_SELECTABLE,false);

      ObjectSet(SpreadMapName, OBJPROP_COLOR, clrYellow);

      ObjectSet(SpreadMapName, OBJPROP_STYLE, STYLE_SOLID);

      ObjectSet(SpreadMapName, OBJPROP_WIDTH, 1);//—|Æ~

     }

//--- indicator buffers mapping

   SetIndexEmptyValue(0,0.0);

   SetIndexBuffer(0,SpreadBuffer);

   SetIndexBuffer(1,SpreadMapBuffer);

//---

   ArraySetAsSeries(SpreadBuffer,true);

   ArraySetAsSeries(SpreadMapBuffer,true);

   ZeroMemory(SpreadBuffer);

   ZeroMemory(SpreadMapBuffer);

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

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[])

  {

//---

   static MqlTick last_tick;

   int size = prev_calculated;

   if(iRefreshRates(last_tick))//last_tick.bid!=Bid

     {

      if(size<Showbars && size<rates_total)

         size+=1;

      if(size>1)

         ArrayCopy(SpreadBuffer,SpreadBuffer,1,0,size-1);



      double mspread=MarketInfo(_Symbol,MODE_SPREAD);

      if(mspread<=0)

         mspread=(last_tick.ask-last_tick.bid)/_Point;

      SpreadBuffer[0]=mspread;

      //---

      double sum=0.0;

      for(int i=0; i<size; i++)

         sum+=SpreadBuffer[i]; // Summation for the double

      if(size<=4)

         SpreadMapBuffer[0]=size>0?(sum/size):SpreadBuffer[0]; // Just divide the sum by the number

      else                                                     //--- now, get the highest value itself in the array

        {

         double terminal=SpreadBuffer[ArrayMaximum(SpreadBuffer)]+SpreadBuffer[ArrayMinimum(SpreadBuffer)];

         SpreadMapBuffer[0]=(sum-terminal)/(size-2);

        }

      ArrayInitialize(SpreadMapBuffer,SpreadMapBuffer[0]);

      ObjectMove(SpreadMapName, 0, last_tick.time, SpreadMapBuffer[0]);//îO9eûy¨R

     }

//--- return value of prev_calculated for next call

   return(size);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id == CHARTEVENT_CHART_CHANGE)

     {

      long v_bars;

      if(ExtSize<=0)

         if(ChartGetInteger(0,CHART_VISIBLE_BARS,0,v_bars) || ChartGetInteger(0,CHART_WIDTH_IN_BARS,0,v_bars))

           {

            if(Showbars!=v_bars)

              {

               Showbars=(int)v_bars;

               if(InpPrint)

                  Print("ArrarySize Freshed:"+IntegerToString(Showbars));

              }

           }

     }

  }

//+------------------------------------------------------------------+

bool iRefreshRates(MqlTick &last_tick,string symbol=NULL)export

  {

   if(symbol==NULL)

      symbol=_Symbol;

   if(!IsConnected())

      return(false);



   if(SymbolInfoTick(symbol,last_tick))

      return(true);

//--- refresh rates

   if(symbol==_Symbol&&RefreshRates())

      return(true);

   Print("RefreshRates failed");

   return(false);

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---