Author: Roman.
0 Views
0 Downloads
0 Favorites
Pro Box
ÿþ//+------------------------------------------------------------------+

//|                                                     Futprint.mq4 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

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

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

#property strict

#property version     "1.0"

#property copyright   "Roman."



#property description "If you like the indicator, you can thank the author"

#property description "Webmoney: Z330494414179, R222585888867"

#property description " "

#property description "Thank you very much."

#property indicator_chart_window



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

//| Custom indicator initialization function                         |

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

enum PColor

  {

   C1=0, // Mono Color

   C2=1  // Gauss Bell Color

  };



enum enum_bUp

  {

   U1=1, // Draw on Back

   U2=2  // Hide on Top

  };



extern string ID_Chart="Box";//Indicator unique ID



extern enum_bUp bUp = U1; //Draw on Top/Back

extern string KeyTop="A";  //Key Change Draw Top/Back



extern PColor Color = C2;  //Color Scheme

extern string KeyRoll="Q";  //Key Change Color Scheme



extern color MainClr = clrTomato;  //Mono/Gauss 68 % Color

extern color Rate1 = clrYellow;   //Gauss 95 % Color

extern color Rate2 = clrAqua;   //Over 5 % Color



extern int Percent = 80; //Length as Percentage of Box



extern string KeyFix="Z"; //Key Change Fix 1:1



int Box[], DrawHigh[];

string NameBox[];

double doubleStartBox[], doubleEndBox[];

datetime dtStartBox[], dtEndBox[];



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

//|                                                                  |

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

void Draw()

  {

   bool ReDrawFlage = false;

   int intObjTotal=ObjectsTotal(), ind, subIndex = ArraySize(NameBox);

   double H[], L[], F[];

   long V[];

   string Redraw[];

   ArrayFree(Redraw);



   for(ind = 0; ind < subIndex; ind ++)

      if(ObjectFind(0,NameBox[ind])<0)

        {

         ReDrawFlage = true;

         break;

        }



   if(!ReDrawFlage)

      for(ind = 0; ind < intObjTotal; ind ++)

        {

         string stName = ObjectName(ind);

         if(ObjectGetInteger(0,stName,OBJPROP_TYPE)!=OBJ_RECTANGLE)

            continue;

         if(ArraySize(NameBox) < 1)

           {

            ReDrawFlage = true;

            break;

           }

         ReDrawFlage = true;

         for(subIndex = 0; subIndex < ArraySize(NameBox); subIndex++)

           {

            if(NameBox[subIndex] == stName)

              {

               ReDrawFlage = false;

               break;

              }

           }

         if(ReDrawFlage)

            break;

         if(dtStartBox[subIndex]!=ObjectGetInteger(0,NameBox[subIndex],OBJPROP_TIME1)||

            dtEndBox[subIndex]!=ObjectGetInteger(0,NameBox[subIndex],OBJPROP_TIME2)||

            doubleStartBox[subIndex]!=ObjectGetDouble(0,NameBox[subIndex],OBJPROP_PRICE1)||

            doubleEndBox[subIndex]!=ObjectGetDouble(0,NameBox[subIndex],OBJPROP_PRICE2)||

            (MathMin(dtStartBox[subIndex], dtEndBox[subIndex])<Time[0] && MathMax(dtStartBox[subIndex], dtEndBox[subIndex])>Time[0]))

           {

            ArrayResize(Redraw,ArraySize(Redraw)+1);

            Redraw[ArraySize(Redraw)-1] = stName;

            dtStartBox[subIndex]=(datetime)ObjectGetInteger(0,NameBox[subIndex],OBJPROP_TIME1);

            dtEndBox[subIndex]=(datetime)ObjectGetInteger(0,NameBox[subIndex],OBJPROP_TIME2);

            doubleStartBox[subIndex]=ObjectGetDouble(0,NameBox[subIndex],OBJPROP_PRICE1);

            doubleEndBox[subIndex]=ObjectGetDouble(0,NameBox[subIndex],OBJPROP_PRICE2);

           };

        }



   int StartVisibleBar = ChartFirstVisibleBar(), CountVisibleBars = ChartVisibleBars(), EndVisibleBar = StartVisibleBar-CountVisibleBars;

   EndVisibleBar = (EndVisibleBar < 0) ? 0: EndVisibleBar;



   datetime StartVisibleTime = Time[StartVisibleBar], EndVisibleTime = Time[EndVisibleBar];

   subIndex = ArraySize(DrawHigh);



   if(subIndex == 0 || (!(subIndex== CountVisibleBars||subIndex== CountVisibleBars+1)))

      ReDrawFlage = true;

   else

     {

      subIndex = -1;

      for(ind = StartVisibleBar; ind >= EndVisibleBar; ind --)

        {

         int x,y;

         subIndex++;

         ChartTimePriceToXY(0,0,Time[ind],High[ind],y,x);

         if(DrawHigh[subIndex] != x)

           {

            ReDrawFlage = true;

            break;

           }

        }

     }



   if(ReDrawFlage)

     {

      ObjectsDeleteAll(0,"RectLabel"+ID_Chart);

      ArrayFree(DrawHigh);

      for(ind = StartVisibleBar; ind >= EndVisibleBar; ind --)

        {

         int x,y;

         ArrayResize(DrawHigh,ArraySize(DrawHigh)+1);

         ChartTimePriceToXY(0,0,Time[ind],High[ind],y,x);

         DrawHigh[ArraySize(DrawHigh)-1]=x;

        }

     }



   if(ReDrawFlage)

     {

      ArrayFree(NameBox);

      ArrayFree(dtStartBox);

      ArrayFree(dtEndBox);

      ArrayFree(doubleStartBox);

      ArrayFree(doubleEndBox);

      ArrayFree(Redraw);

      for(ind = 0; ind < intObjTotal; ind ++)

        {

         string stName = ObjectName(ind);

         if(ObjectGetInteger(0,stName,OBJPROP_TYPE)!=OBJ_RECTANGLE)

            continue;

         ArrayResize(NameBox,ArraySize(NameBox)+1);

         ArrayResize(dtStartBox,ArraySize(dtStartBox)+1);

         ArrayResize(dtEndBox,ArraySize(dtEndBox)+1);

         ArrayResize(doubleStartBox,ArraySize(doubleStartBox)+1);

         ArrayResize(doubleEndBox,ArraySize(doubleEndBox)+1);

         ArrayResize(Redraw,ArraySize(Redraw)+1);

         NameBox[ArraySize(NameBox)-1] = Redraw[ArraySize(Redraw)-1] = stName;

         dtStartBox[ArraySize(dtStartBox)-1]=(datetime)ObjectGetInteger(0,stName,OBJPROP_TIME1);

         dtEndBox[ArraySize(dtEndBox)-1]=(datetime)ObjectGetInteger(0,stName,OBJPROP_TIME2);

         doubleStartBox[ArraySize(doubleStartBox)-1]=ObjectGetDouble(0,stName,OBJPROP_PRICE1);

         doubleEndBox[ArraySize(doubleEndBox)-1]=ObjectGetDouble(0,stName,OBJPROP_PRICE2);

        }

     }



   if(ArraySize(Redraw)>0)

     {

      for(ind = ArraySize(Redraw) - 1; ind >=0 ; ind --)

        {

         if(!((ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME1) < EndVisibleTime && ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME1) > StartVisibleTime)||

              (ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME2) < EndVisibleTime && ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME2) > StartVisibleTime)||

              (MathMax(ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME2),ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME1)) > EndVisibleTime &&

               MathMin(ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME2),ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME1)) < StartVisibleTime

              )

             ))

           {

            if(ArraySize(Redraw)!=1)

              {

               if(ind != ArraySize(Redraw)-1)

                  Redraw[ind]=Redraw[ArraySize(Redraw)-1];

               ArrayResize(Redraw,ArraySize(Redraw)-1);

              }

            else

               ArrayFree(Redraw);

           }

        }

     }



   ulong  star = GetMicrosecondCount(), ulong_ = 200 * 800;

   subIndex = ArraySize(Redraw);



   for(ind = 0; ind < subIndex ; ind ++)

     {

      datetime Start = (datetime)MathMin(ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME1),ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME2)),

               End = (datetime)MathMax(ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME1),ObjectGetInteger(0, Redraw[ind], OBJPROP_TIME2));



      double  Min = MathMin(ObjectGetDouble(0, Redraw[ind], OBJPROP_PRICE1),ObjectGetDouble(0, Redraw[ind], OBJPROP_PRICE2));



      if(ulong_ < GetMicrosecondCount() - star)

         return;



      int x, y, w;



      if(CopyTickVolume(_Symbol, PERIOD_M1, Start, End + PeriodSeconds() - 1, V) <= 0 ||

         CopyHigh(_Symbol, PERIOD_M1, Start, End + PeriodSeconds() - 1, H) <= 0 ||

         CopyLow(_Symbol, PERIOD_M1, Start, End + PeriodSeconds() - 1, L) <= 0)

         continue;



      ArrayFree(F);

      ArrayResize(F,(int)((MathMax(ObjectGetDouble(0, Redraw[ind], OBJPROP_PRICE1),ObjectGetDouble(0, Redraw[ind], OBJPROP_PRICE2))-Min)/Point)+1);



      int iArraySize = ArraySize(V);

      int Size = ArraySize(F);

      for(int j = 0; j < iArraySize; j ++)

        {

         int E = (int)((L[j]-Min)/Point);



         if(H[j]!=L[j])

           {

            double tmp = (H[j]-L[j])/Point, g = V[j]/tmp;

            for(int k = 0; k < (int)tmp; k++)

               if(E + k < Size && E + k >= 0)

                  F[E + k] += g;

           }

         else

            if(E < Size && E >= 0)

               F[E] += (double)V[j];

        }



      ChartTimePriceToXY(0,0,Start,Min,x,y);

      ChartTimePriceToXY(0,0,End,Min,w,y);



      double MaxV =  F[ArrayMaximum(F)];



      if(MaxV<1)

         continue;



      MaxV = MathAbs(w-x) > 12? (MathAbs(w-x)-3) / MaxV: MathAbs(w-x) / MaxV;

      MaxV = (MaxV * Percent) / 100.0;

      iArraySize = ArraySize(F);

      double PrevSumm = 0, SummValue = 0;

      for(int j = 0; j < iArraySize; j ++)

         SummValue+=F[j];

      ObjectsDeleteAll(0,"RectLabel"+ID_Chart + "_" + Redraw[ind]);

      for(int j = 0; j < iArraySize; j ++)

        {

         int Horizontal= (int)(MaxV * F[j]);

         if(Horizontal==0)

            continue;

         ChartTimePriceToXY(0, 0, Start, Min + Point * j, x, y);

         string name = "RectLabel"+ID_Chart + "_" + Redraw[ind] + "_" + IntegerToString(j);

         ObjectCreate(name,OBJ_RECTANGLE_LABEL,0,0,0);

         ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);

         ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y - 3);

         ObjectSetInteger(0,name,OBJPROP_XSIZE,Horizontal);

         ObjectSetInteger(0,name,OBJPROP_YSIZE,3);

         ObjectSetInteger(0,name,OBJPROP_WIDTH,0);

         ObjectSetInteger(0,name,OBJPROP_BACK,bUp!=U2?true:false);



         switch(Color)

           {

            case C1:

               ObjectSetInteger(0,name,OBJPROP_BGCOLOR,MainClr);

               break;

            case C2:

               PrevSumm += F[j];

               if(PrevSumm / SummValue < 0.025f || PrevSumm / SummValue > 0.975f)

                  ObjectSetInteger(0,name,OBJPROP_BGCOLOR,Rate2);

               else

                 {

                  if(PrevSumm / SummValue < 0.16f  || PrevSumm / SummValue > 0.84f)

                     ObjectSetInteger(0,name,OBJPROP_BGCOLOR,Rate1);

                  else

                     ObjectSetInteger(0,name,OBJPROP_BGCOLOR,MainClr);

                 }

               break;

           }

        }

     }

  }

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

//|                                                                  |

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

void OnTimer()

  {

   Draw();

  }

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

//|                                                                  |

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

int OnInit()

  {

//--- indicator buffers mapping



   EventSetMillisecondTimer(200);

//---

   return(INIT_SUCCEEDED);

  }

  

void OnDeinit(const int reason)

  {

   EventKillTimer();

   ObjectsDeleteAll(0,"RectLabel"+ID_Chart);

  }

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

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

  {

//---

   Draw();

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

   return(rates_total);

  }

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

//| $C=:F8O ?>;CG05B =><5@ ?5@2>3> 2848<>3> 10@0 =0 3@0D8:5.         |

//| =45:A0F8O :0: 2 B09<A5@88, ?>A;54=85 10@K 8<5NB <5=LH85 8=45:AK.|

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

int ChartFirstVisibleBar(const long chart_ID=0)

  {

//--- ?>43>B>28< ?5@5<5==CN 4;O ?>;CG5=8O 7=0G5=8O A2>9AB20

   long result=-1;

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();

//--- ?>;CG8< 7=0G5=85 A2>9AB20

   if(!ChartGetInteger(chart_ID,CHART_FIRST_VISIBLE_BAR,0,result))

     {

      //--- 2K2545< A>>1I5=85 >1 >H81:5 2 6C@=0; "-:A?5@BK"

      Print(__FUNCTION__+", Error Code = ",GetLastError());

     }

//--- 25@=5< 7=0G5=85 A2>9AB20 3@0D8:0

   return((int)result);

  }

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

//| $C=:F8O ?>;CG05B :>;8G5AB2> 10@>2, :>B>@K5 >B>1@060NBAO (2848<K )|

//| 2 >:=5 3@0D8:0.                                                  |

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

int ChartVisibleBars(const long chart_ID=0)

  {

//--- ?>43>B>28< ?5@5<5==CN 4;O ?>;CG5=8O 7=0G5=8O A2>9AB20

   long result=-1;

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();

//--- ?>;CG8< 7=0G5=85 A2>9AB20

   if(!ChartGetInteger(chart_ID,CHART_VISIBLE_BARS,0,result))

     {

      //--- 2K2545< A>>1I5=85 >1 >H81:5 2 6C@=0; "-:A?5@BK"

      Print(__FUNCTION__+", Error Code = ",GetLastError());

     }

//--- 25@=5< 7=0G5=85 A2>9AB20 3@0D8:0

   return((int)result);

  }

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

bool ChartScaleFix11Get(bool &result,const long chart_ID=0)

  {

//--- ?>43>B>28< ?5@5<5==CN 4;O ?>;CG5=8O 7=0G5=8O A2>9AB20

   long value;

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();

//--- ?>;CG8< 7=0G5=85 A2>9AB20

   if(!ChartGetInteger(chart_ID,CHART_SCALEFIX,0,value))

     {

      //--- 2K2545< A>>1I5=85 >1 >H81:5 2 6C@=0; "-:A?5@BK"

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- 70?><=8< 2 ?5@5<5==CN 7=0G5=85 A2>9AB20 3@0D8:0

   result=value;

//--- CA?5H=>5 2K?>;=5=85

   return(true);

  }

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

//| $C=:F8O 2:;NG05B/2K:;NG05B @568< <0AHB010 "1:1"                  |

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

bool ChartScaleFix11Set(const bool value,const long chart_ID=0)

  {

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();

//--- CAB0=>28< 7=0G5=85 A2>9AB20

   if(!ChartSetInteger(chart_ID,CHART_SCALEFIX,0,value))

     {

      //--- 2K2545< A>>1I5=85 >1 >H81:5 2 6C@=0; "-:A?5@BK"

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- CA?5H=>5 2K?>;=5=85

   return(true);

  }

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id==CHARTEVENT_KEYDOWN)

     {

      if((int)lparam==StringGetChar(KeyRoll,0))

        {

         switch(Color)

           {

            case C1:

               Color = C2;

               break;

            case C2:

               Color = C1;

               break;

           };

         ArrayFree(NameBox);

         Draw();

        }

      if((int)lparam==StringGetChar(KeyTop,0))

        {

         bUp=bUp!=U1?U1:U2;

         ArrayFree(NameBox);

         Draw();

        }

      if((int)lparam==StringGetChar(KeyFix,0))

        {

         bool tmp;

         ChartScaleFix11Get(tmp);

         ChartScaleFix11Set(!tmp);

         ArrayFree(NameBox);

         Draw();

        }

     }

  }



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

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