Bid_MAX_counter_set_as_OPEN_or_CLOSE_price

Author: 3021, Nikolay Mitrofanov
0 Views
0 Downloads
0 Favorites
Bid_MAX_counter_set_as_OPEN_or_CLOSE_price
ÿþ#property copyright  "3021, Nikolay Mitrofanov"

#property version   "1.0"



#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   1

//#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrLimeGreen, clrWhiteSmoke

#property indicator_width1  5





#include <Generic\HashMap.mqh>





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



enum ColoredType

  {

   BID_AS_OPEN, // Bid as Open price

   BID_AS_CLOSE  // Bid as Close price

  };

input ColoredType coloredType = BID_AS_CLOSE; // color sheme



enum DrawType {

   CANDLES, BARS, LINE, ZIGZAG

};



input DrawType drawType = CANDLES;



CHashMap<double, int> *storageArrayBID;





static int n;



double ExtOBuffer[];

double ExtHBuffer[];

double ExtLBuffer[];

double ExtCBuffer[];

double ExtColorBuffer[];

double ExtDataBuffer[];





long c1, c2, c3, c4, c5;



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

//|                                                                  |

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

void OnInit()

  {

   SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);



   c1 = ChartGetInteger(0, CHART_COLOR_CANDLE_BEAR);

   c2 = ChartGetInteger(0, CHART_COLOR_CANDLE_BULL);

   c3 = ChartGetInteger(0, CHART_COLOR_CHART_UP);

   c4 = ChartGetInteger(0, CHART_COLOR_CHART_DOWN);

   c5 = ChartGetInteger(0, CHART_COLOR_CHART_LINE);



   ChartSetInteger(0, CHART_COLOR_CANDLE_BEAR, clrNONE);

   ChartSetInteger(0, CHART_COLOR_CANDLE_BULL, clrNONE);

   ChartSetInteger(0, CHART_COLOR_CHART_UP, clrNONE);

   ChartSetInteger(0, CHART_COLOR_CHART_DOWN, clrNONE);

   ChartSetInteger(0, CHART_COLOR_CHART_LINE, clrNONE);





   IndicatorSetInteger(INDICATOR_DIGITS, 0);

   IndicatorSetString(INDICATOR_SHORTNAME,"Max BID and close candles chart");

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);



   switch(drawType) {

      case CANDLES: PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_CANDLES); break;

      case BARS: PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_BARS); break;

      case LINE: PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_LINE); break;

      case ZIGZAG: PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_ZIGZAG); break;

   }

 



   storageArrayBID = 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)

     {

      ExtLBuffer[0]=Low[0];

      ExtHBuffer[0]=High[0];

      ExtOBuffer[0]=Open[0];

      ExtCBuffer[0]=Close[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();



         if(c > 0)

           {

            /**

               calculate BID counter 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);

                 }

              }



            /**

               Finding the maximum counter for bid prices

            */

            double keys[];

            int values[];

            storageArrayBID.CopyTo(keys, values);

            int maxBidNumber = values[ArrayMaximum(values)];

            double maxBidPrice = keys[ArrayBsearch(values, maxBidNumber)];





            /**

               Fill data and colors

            */

            ExtLBuffer[i]=Low[i];

            ExtHBuffer[i]=High[i];







            switch(coloredType)

              {

               case BID_AS_OPEN:

                 {

                  ExtCBuffer[i]=Close[i];

                  ExtOBuffer[i]=maxBidPrice;

                 }

               break;



               case BID_AS_CLOSE:

                 {

                  ExtCBuffer[i]=maxBidPrice;

                  ExtOBuffer[i]=Open[i];

                 }

               break;

              }

               (ExtOBuffer[i] < ExtCBuffer[i] ? ExtColorBuffer[i] = 0 : 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;



   ChartSetInteger(0, CHART_COLOR_CANDLE_BEAR, c1);

   ChartSetInteger(0, CHART_COLOR_CANDLE_BULL, c2);

   ChartSetInteger(0, CHART_COLOR_CHART_UP, c3);

   ChartSetInteger(0, CHART_COLOR_CHART_DOWN, c4);

   ChartSetInteger(0, CHART_COLOR_CHART_LINE, c5);





   ChartRedraw(0);

  }





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



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

Comments