SpreadMap_2

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

//|                                                    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 uint    ExtSize=0;

input bool     InpPrint=false;

double         SpreadBuffer[];

double         SpreadMapBuffer[];

const string SpreadMapName="SpreadMap";

uint Showbars=ExtSize;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   Showbars=(ExtSize<=0)?(uint)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);

   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;

   static double Datas[];

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

     {

      double Spread=MarketInfo(NULL,MODE_SPREAD);

      int fresh=(Spread>0)?arradd(Datas,Spread,Showbars,true):-1;//>07R°e

      SpreadMapBuffer[0]=arraver(Datas);

      ArrayInitialize(SpreadMapBuffer,SpreadMapBuffer[0]);

      ArrayCopy(SpreadBuffer,Datas,0,0,WHOLE_ARRAY);

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

      if(InpPrint)

        {

         for(int i=0; i<ArraySize(Datas); i++)

            printf("[%d], value=%f",i,Datas[i]);

         printf("FreshSize=%d, Average=%f",fresh,SpreadMapBuffer[0]);

        }

     }

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

   return(rates_total);

  }

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

//|                                                                  |

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

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));

              }

           }

      //--- 9hncÔk‹Oôf9ech½[¦^

      /*if(ChartGetInteger(0,CHART_SCALE) > 2)

         PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);

      else

         PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);*/

      //PlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);

     }

  }

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

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);

  }

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

//|  peÄ~nbc                                                        |

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

void arrrotate(double &arr[],double data, bool asseries=true)

  {

   const int size=ArraySize(arr);

   if(size<=1)

     {

      arr[0]=data;

      return;

     }

   double cache[];

   ArrayResize(cache,size);

   if(asseries)

     {

      ArrayCopy(cache,arr,1,0,size-1);

      cache[0]=data;

     }

   else

     {

      ArrayCopy(cache,arr,0,1,size-1);

      cache[size-1]=data;

     }

   ArrayCopy(arr,cache,0,0,WHOLE_ARRAY);

  }

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

//| peÄ~žX Rÿôf°e                                                   |

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

int arradd(double &arr[],double data,const int size=10,bool asseries=true)

  {

   const int s=ArraySize(arr);

   if(size>0)

     {

      int newsize=(s<size)?s+1:(s>size)?size:0;

      if(newsize>0)

         ArrayResize(arr,newsize,size);

      if(s<size&&!asseries)

         arr[newsize-1]=data;//æ]§OžX R

      else

         arrrotate(arr,data,asseries);

      return(newsize);

     }

   else

      arrrotate(arr,data,asseries);

   return(0);

  }

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

//|peÄ~RËY<P^÷S                                                    |

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

int arrinzero(double &arr[],const double initial=0.0,bool asseries=true)

  {

   if(initial==EMPTY_VALUE||initial>=DBL_MAX)

      return(-1);

   const int s=ArraySize(arr);

   int i=s-1;

   while(i>=0&&!IsStopped())//i<s

     {

      int d=asseries?(s-i-1):i;

      if(arr[d]<=initial)

         return(d); //{i=d;break;}

      i--;

     }

   return(i);

  }

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

//|                                                                  |

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

double arraver(const double & array[])

  {

   uint size=ArraySize(array);

   if(size<=0)

      return 0.0;

   double sum=0.0;

   double aver;

//---

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

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

   if(size<=4)

      aver=sum/size; // Just divide the sum by the number

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

     {

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

      aver=(sum-terminal)/(size-2);

     }

//---

   return aver;

  }

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

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 ---