Bid_Ask_Strength_Histogram

Author: 3021, Nikolay Mitrofanov
0 Views
0 Downloads
0 Favorites
Bid_Ask_Strength_Histogram
ÿþ//+------------------------------------------------------------------+

//|                                                      ProjectName |

//|                                      Copyright 2020, CompanyName |

//|                                       http://www.companyname.net |

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

#property copyright  "3021, Nikolay Mitrofanov"

#property version   "1.0"



#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   1

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrOrange, clrDeepSkyBlue

#property indicator_width1  5





#include <Generic\HashMap.mqh>



input int InpCandlesNumber = 1000; // number of candles for calculations

input bool InpTickVolueBoost = false; // boost result with ticks volumes





CHashMap<double, int> *storageArrayBID;

CHashMap<double, int> *storageArrayASK;



static int n;



double ExtColorBuffer[];

double ExtDataBuffer[];





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

//|                                                                  |

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

void OnInit()

  {

   SetIndexBuffer(0,ExtDataBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtColorBuffer,INDICATOR_COLOR_INDEX);





   IndicatorSetInteger(INDICATOR_DIGITS, 0);

   IndicatorSetString(INDICATOR_SHORTNAME,"BID ASK Strength");

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);



   storageArrayBID = new CHashMap<double, int>();

   storageArrayASK = new CHashMap<double, int>();



  }

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

//|                                                                  |

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

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 &TickVolume[],

                const long &Volume[],

                const int &Spread[])

  {

   int i,limit;

   if(prev_calculated==0)

     {

      limit=1;

     }

   else

     {

      limit=prev_calculated-1;

     }



   n = rates_total - InpCandlesNumber; // begin calculation after last 'n' values to current candle



   for(i=limit; i<rates_total && !IsStopped(); i++)

     {

      if(i > n)

        {

         /**

            Evaluating tick extracion time period

         */

         MqlDateTime dt;

         TimeToStruct(Time[i], dt);

         datetime startT;

         datetime endT;

         endT = StructToTime(dt);

         startT = StructToTime(dt) + PeriodSeconds(_Period);



         ResetLastError();



         MqlTick ticks_array[];

         int c = CopyTicksRange(_Symbol, ticks_array, COPY_TICKS_INFO, startT*1000, endT*1000);



         storageArrayBID.Clear();

         storageArrayASK.Clear();



         if(c > 0)

           {

            /**

               calculate BID and ASK counters for prices

            */

            for(int a = 0; a<ArraySize(ticks_array); a++)

              {

               double bid = ticks_array[a].bid;

               if(storageArrayBID.ContainsKey(bid))

                 {

                  int bidCount;

                  storageArrayBID.TryGetValue(bid, bidCount);

                  bidCount++;

                  storageArrayBID.TrySetValue(bid, bidCount);

                 }

               else

                 {

                  storageArrayBID.Add(bid, 1);

                 }





               double ask = ticks_array[a].ask;

               if(storageArrayASK.ContainsKey(ask))

                 {

                  int askCount;

                  storageArrayASK.TryGetValue(ask, askCount);

                  askCount++;

                  storageArrayASK.TrySetValue(ask, askCount);

                 }

               else

                 {

                  storageArrayASK.Add(ask, 1);

                 }



              }



            /**

               Finding the maximum counter for bid prices

            */

            double keys[];

            int values[];

            storageArrayBID.CopyTo(keys, values);

            double maxBidNumber = values[ArrayMaximum(values)];





            /**

               Finding the maximum counter for ask prices

            */

            ArrayFree(keys);

            ArrayFree(values);

            storageArrayASK.CopyTo(keys, values);

            double maxAskNumber = values[ArrayMaximum(values)];







            /**

               Fill data and colors

            */

            if(maxAskNumber > maxBidNumber)

              {

               ExtDataBuffer[i] = (InpTickVolueBoost ? maxAskNumber * TickVolume[i] : maxAskNumber);

               ExtColorBuffer[i] = 0;

              }

            else

              {

               ExtDataBuffer[i] = (InpTickVolueBoost ? maxBidNumber * TickVolume[i] : maxBidNumber);

               ExtColorBuffer[i] = 1;

              }





           }

         else

           {

            /**

               Has no history or something else

            */

            if(GetLastError() != 0)

              {

               Print("Copy tick error ", GetLastError());

              }

           }

        }

     }

//--- done

   return(rates_total);

  }

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









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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   delete storageArrayBID;

   delete storageArrayASK;

  }





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

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