//+-------------------------------------------------------\!/--------------------------------------------------------+
//+ [Knowledge of the ancients]                          (ò ó)                           [XU-v65m-XARD-UNIVERSE-V1.1.mq4] +
//+-------------------------------------------------o0o---(_)---o0o--------------------------------------------------+
#property link        "https://forex-station.com/post1294848283.html#p1294848283"
#property description "THIS IS A FREE INDICATOR WITH NO TIME RESTRICTIONS"
#property description "                                                      "
#property description "Welcome to the XARD UNIVERSE"
#property description "                                                      "
#property description "Let the light shine and illuminate your trading world"
#property description "and with it secure your financial prosperity"
#property  indicator_chart_window
#property  indicator_buffers 41
#define Version "XU-v65m-XARD UNIVERSE_semabgd V1.4"
string ID = "Xard>";

string MyName="XU v65m-XARD UNIVERSE_semabgd",indicatorFileName;
bool show_data = true;
bool bgd_recalc = false;
string IndicatorObjPrefix, buttonId;

//+------------------------------------------------------------------------------------------------------------------+
 input string Indicator                      = Version;
   input bool showVersion                    = true;
   input bool showBGDzones                   = true;
   input bool showTEXTx                      = true;
   input bool showTextBOX                    = true;
   input bool showOpenBias                   = true;
   input bool showSymbolHeader               = true;
          int getChartScale                  = WRONG_VALUE;  int Type;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR00                          = "<<<==== [00] Lightweight Settings ====>>>";
    input int maxBars                        = 1500;               // Max bars to calculate (lightweight limit)
extern string STR01                          = "<<<==== [01] Ribbon Settings ====>>>";
   input bool showRib1                       = true;              // Show Ribbon?
  input color Rib1ClrUP                      = C'44,104,51';      // Ribbon Up Color
  input color Rib1ClrDN                      = C'114,44,51';      // Ribbon Down Color
       double Rib1a[],Rib1b[];
       
   input bool showRib2                       = true;              // Show Ribbon?
  input color Rib2ClrUP                      = C'33,55,42';       // Ribbon Up Color
  input color Rib2ClrDN                      = C'65,33,42';       // Ribbon Down Color
       double Rib2a[],Rib2b[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR02                          = "<<<==== [02] SEMA Settings ====>>>";
  extern bool showSEMA1=true,showSEMA2=true,showSEMA3=true,showSEMA4=true,showSEMA5=true;
   extern int DotPer1=9,DotPer2=36,DotPer3=144,DotPer4=144,DotPer5=576,deltazz;
       string Dev_Step_1="0,5",Dev_Step_2="0,5",Dev_Step_3="0,5",Dev_Step_4="0,5",Dev_Step_5="0,5";    
   extern int S1size=4,S2size=8,S3size=14,S4size=8,S5size=16,S1kod=108,S2kod=108,S3kod=108,S4kod=221,S5kod=110,S6kod=222;
 extern color cU1=clrSpringGreen,cD1=clrOrangeRed,cU2=clrDodgerBlue,cD2=clrOrchid,cU3=clrLimeGreen,cD3=clrCrimson,
              cU4=clrSnow,cD4=clrSnow,cU5=clrDarkGray,cD5=clrDarkGray;
          int APer,BPer,CPer,DPer,EPer,Dev1,Stp1,Dev2,Stp2,Dev3,Stp3,Dev4,Stp4,Dev5,Stp5;
       double ABufUp[],ABufDn[],BBufUp[],BBufDn[],CBufUp[],CBufDn[],DBufUp[],DBufDn[],EBufUp[],EBufDn[];
       double myPoint,mPoint;
       double SetPoint(){if(Digits<4) mPoint=0.01; else mPoint=0.0001; return(mPoint);} 
//+------------------------------------------------------------------------------------------------------------------+
extern string STR03                          = "<<<==== [03] Candle Settings ====>>>";
   input bool showCandles                    = true;              // Show Candles?
    input int cWick                          = 2;                 // Candle Wick Size
  input color CandleUp                       = C'96,170,204',     // Candle Up Color
              CandleWt                       = C'95,105,115',     // Candle Wait Color//clrDarkGray
              CandleDn                       = clrViolet;         // Candle Down Color
       double candle0[],candle1[],candle2[],candle3[],candle4[],candle5[],candle6[],candle7[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR04                          = "<<<==== [04] S1MA Trend Settings ====>>>";
   input bool showS1MA                       = false;             // Show S1MA?
    input int S1MAper                        =  1;                // S1MA high low period
        input ENUM_MA_METHOD  S1MAmode       = MODE_EMA;          // S1MA moving average method
    input int S1MAtf                         =  0;                // S1MA tf
    input int S1MAshft                       =  0;                // S1MA shift
    input int S1MAwidth                      =  3;                // S1MA line width
  input color S1MAwtclr                      = C'36,62,62';       // S1MA Wait Color
  input color S1MAupclr                      = C'65,145,71';      // S1MA Up Color
  input color S1MAdnclr                      = C'195,67,70';      // S1MA Down Color
         bool returnBars,calculateS1TREND;  double S1MAbgd[],S1MA[],S1MAdna[],S1MAdnb[],S1trend[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR05                          = "<<<==== [05] S2MA Trend Settings ====>>>";
   input bool showS2MA                       = true;              // Show S2MA?
    input int S2MAper                        = 13;                // S2MA high low period
        input ENUM_MA_METHOD  S2MAmode       = MODE_EMA;          // S2MA moving average method
    input int S2MAtf                         =  0;                // S2MA tf
    input int S2MAshft                       =  0;                // S2MA shift
    input int S2MAwidth                      =  2;                // S2MA line width
  input color S2MAwtclr                      = C'36,62,62';       // S2MA Wait Color
  input color S2MAupclr                      = C'65,145,71';      // S2MA Up Color
  input color S2MAdnclr                      = C'195,67,70';      // S2MA Down Color
         bool calculateS2TREND;  double S2MAbgd[],S2MA[],S2MAdna[],S2MAdnb[],S2trend[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR06                          = "<<<==== [06] S3MA Trend Settings ====>>>";
   input bool showS3MA                       = true;              // Show S3MA?
    input int S3MAper                        = 55;                // S3MA high low period
        input ENUM_MA_METHOD  S3MAmode       = MODE_EMA;          // S3MA moving average method
    input int S3MAtf                         =  0;                // S3MA tf
    input int S3MAshft                       =  0;                // S3MA shift
    input int S3MAwidth                      =  3;                // S3MA line width
  input color S3MAwtclr                      = C'36,62,62';       // S3MA Wait Color
  input color S3MAupclr                      = clrLime;           // S3MA Up Color
  input color S3MAdnclr                      = clrRed;            // S3MA Down Color
         bool calculateS3TREND;  double S3MAbgd[],S3MA[],S3MAdna[],S3MAdnb[],S3trend[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR07                          = "<<<==== [07] BGDzones Settings ====>>>";
  input color ColorUp                        = C'23,52,80',
              ColorDown                      = C'80,43,62',
              ColorWait                      = C'30,47,62';
       double Dummy=-1,colors1[],colors2[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR08                          = "<<<==== [08] BOXtxt Settings ====>>>";
       string BOXtxt,BOXtxt2;  
        color BOXclr,BOXclr2;
    input int FontSize3                      = 15,
              move_text_sideways             = 0,
              move_text_updown               = 0;
    input int moveTxtLR                      = 0,
              moveTxtUD                      = 0;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR09                          = "<<<==== [09] Symbol Header Settings ====>>>";
    input int LR                             = 55,
              UD                             = 10,
              HDRsize                        = 40,
              HDRsize2                       = 14;  
        color Sbg                            = clrBlack,SymClr;
        color colCarlsberg;
       double OpenToday;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR10                          = "<<<==== [10] Daily Open level Settings ====>>>";
    input int GmtShift,
              MA_Period                      = 2,
              MA_Method                      = MODE_SMMA,
              PriceMode                      = PRICE_WEIGHTED; 
   color UpCol=clrLime,DnCol=clrOrangeRed; double BUFdn[],BUFup[],OPening,Bufo;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR11                          = "<<<==== [11] Alert Settings ====>>>";
   input bool inpAlertsOn                    = false;             // Turn alerts on?
   input bool inpAlertsOnCurrent             = false;             // Alerts on current (still opened) bar?
   input bool inpAlertsMessage               = true;              // Alerts should show pop-up message?
   input bool inpAlertsSound                 = false;             // Alerts should play alert sound?
   input bool inpAlertsPushNotif             = false;             // Alerts should send push notification?
   input bool inpAlertsEmail                 = false;             // Alerts should send email?
//+------------------------------------------------------------------------------------------------------------------+
       double TRX[],data1;  string TimeFrame="Current time frame";  int timeFrame;
       string FontType                       = "Arial Black";
//+------------------------------------------------------------------------------------------------------------------+
extern string STR12                          = "<<<==== [12] Background Button Settings ====>>>";
extern string             button_note1_         = "------------------------------";
extern int                btn_Subwindow         = 0;                               // What window to put the button on.  If <0, the button will use the same sub-window as the indicator.
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;               // button corner on chart for anchoring
extern string             btn_text              = "BGD";                            // a button name
extern string             btn_Font              = "Arial";                         // button font name
extern int                btn_FontSize          = 9;                               // button font size               
extern color              btn_text_ON_color     = clrLime;                         // ON color when the button is turned on
extern color              btn_text_OFF_color    = clrRed;                          // OFF color when the button is turned off
extern color              btn_background_color  = clrDimGray;                      // background color of the button
extern color              btn_border_color      = clrBlack;                        // border color the button
extern int                button_x              = 12;                              // x coordinate of the button     
extern int                button_y              = 25;                              // y coordinate of the button     
extern int                btn_Width             = 80;                              // button width
extern int                btn_Height            = 20;                              // button height
extern string             UniqueButtonID        = "XU_BGD_1";                      // Unique ID for each button        
extern string             button_note2          = "------------------------------";
    input int FontSize                       = 12;
//+----OnInit Function-----------------------------------------------------------------------------------------------+
   int OnInit(){int Buffers=41,Buf=-1;  IndicatorBuffers(Buffers);
   if(TimeFrame==0 || TimeFrame<Period()) TimeFrame=Period();  timeFrame=stringToTimeFrame(TimeFrame);
   IndicatorDigits(Digits); IndicatorShortName(MyName);
//+------------------------------------------------------------------------------------------------------------------+
   if(showRib1)           Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
   Buf+=1; SetIndexBuffer(Buf,Rib1a);        
   Buf+=1; SetIndexBuffer(Buf,Rib1b);       fSetBuffers();//01
   if(showRib2)           Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
   Buf+=1; SetIndexBuffer(Buf,Rib2a);        
   Buf+=1; SetIndexBuffer(Buf,Rib2b);       fSetBuffers();//03
//+------------------------------------------------------------------------------------------------------------------+
   if(DotPer1>0) APer=MathCeil(DotPer1*Period()); else APer=0;
   if(DotPer2>0) BPer=MathCeil(DotPer2*Period()); else BPer=0;
   if(DotPer3>0) CPer=MathCeil(DotPer3*Period()); else CPer=0;
   if(DotPer4>0) DPer=MathCeil(DotPer4*Period()); else DPer=0;
   if(DotPer5>0) EPer=MathCeil(DotPer5*Period()); else EPer=0;
//+---SML RED/GRN DOT (9)--------------------------------------------------------------------------------------------+
   if(showSEMA1)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer1>0){
   Buf+=1; SetIndexBuffer(Buf,ABufUp); SetIndexStyle(Buf,Type,0,S1size,cU1);  SetIndexArrow(Buf,S1kod);
   Buf+=1; SetIndexBuffer(Buf,ABufDn); SetIndexStyle(Buf,Type,0,S1size,cD1);  SetIndexArrow(Buf,S1kod);}//05
//+---GOLD SQUARE (576)----------------------------------------------------------------------------------------------+
   if(showSEMA5)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer5>0){
   Buf+=1; SetIndexBuffer(Buf,EBufUp); SetIndexStyle(Buf,Type,0,S5size,cU5);  SetIndexArrow(Buf,S5kod);
   Buf+=1; SetIndexBuffer(Buf,EBufDn); SetIndexStyle(Buf,Type,0,S5size,cD5);  SetIndexArrow(Buf,S5kod);}//07
//+---MED DODGERBLUE/ORCHID DOT (36)---------------------------------------------------------------------------------+
   if(showSEMA2)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer2>0){
   Buf+=1; SetIndexBuffer(Buf,BBufUp); SetIndexStyle(Buf,Type,0,S2size,cU2);  SetIndexArrow(Buf,S2kod);
   Buf+=1; SetIndexBuffer(Buf,BBufDn); SetIndexStyle(Buf,Type,0,S2size,cD2);  SetIndexArrow(Buf,S2kod);}//09
//+---DODGERBLUE/PINK PERIMETER DOT (144)----------------------------------------------------------------------------+
   if(showSEMA3)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer3>0){
   Buf+=1; SetIndexBuffer(Buf,CBufUp); SetIndexStyle(Buf,Type,0,S3size,cU3);  SetIndexArrow(Buf,S3kod);
   Buf+=1; SetIndexBuffer(Buf,CBufDn); SetIndexStyle(Buf,Type,0,S3size,cD3);  SetIndexArrow(Buf,S3kod);}//11
//+---ARROW DOT (144)------------------------------------------------------------------------------------------------+
   if(showSEMA4)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer4>0){
   Buf+=1; SetIndexBuffer(Buf,DBufUp); SetIndexStyle(Buf,Type,0,S4size,cU4);  SetIndexArrow(Buf,S4kod);
   Buf+=1; SetIndexBuffer(Buf,DBufDn); SetIndexStyle(Buf,Type,0,S4size,cD4);  SetIndexArrow(Buf,S6kod);}//13
//+------------------------------------------------------------------------------------------------------------------+
   int CDev=0,CSt=0,Mass[],C=0;
   if(IntFromStr(Dev_Step_1,C,Mass)==1){Stp1=Mass[1]; Dev1=Mass[0];}
   if(IntFromStr(Dev_Step_2,C,Mass)==1){Stp2=Mass[1]; Dev2=Mass[0];}
   if(IntFromStr(Dev_Step_3,C,Mass)==1){Stp3=Mass[1]; Dev3=Mass[0];}
   if(IntFromStr(Dev_Step_4,C,Mass)==1){Stp4=Mass[1]; Dev4=Mass[0];}
   if(IntFromStr(Dev_Step_5,C,Mass)==1){Stp5=Mass[1]; Dev5=Mass[0];}   
//+------------------------------------------------------------------------------------------------------------------+
   if(showCandles)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
   Buf+=1; SetIndexBuffer(Buf,candle0);    SetIndexStyle(Buf,Type,EMPTY,cWick,CandleUp);
   Buf+=1; SetIndexBuffer(Buf,candle1);    SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,candle2);    SetIndexStyle(Buf,Type,EMPTY,cWick,CandleWt);
   Buf+=1; SetIndexBuffer(Buf,candle3);    SetIndexStyle(Buf,Type,EMPTY,cWick,CandleDn);
   Buf+=1; SetIndexBuffer(Buf,candle4);//18
   Buf+=1; SetIndexBuffer(Buf,candle5);    SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,candle6);//20
   Buf+=1; SetIndexBuffer(Buf,candle7);    fSetBuffers();//21
//+------------------------------------------------------------------------------------------------------------------+
   if(showS2MA)           Type=DRAW_LINE; else Type=DRAW_NONE; int S2W=S2MAwidth,S2B=S2W+4; color S2Wc=S2MAwtclr;
   Buf+=1; SetIndexBuffer(Buf,S2MAbgd);   SetIndexStyle(Buf,Type,0,S2B,S2Wc);       SetIndexDrawBegin(Buf,S2MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S2MA);      SetIndexStyle(Buf,Type,0,S2W,S2MAupclr);  SetIndexDrawBegin(Buf,S2MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S2MAdna);   SetIndexStyle(Buf,Type,0,S2W,S2MAdnclr);  SetIndexDrawBegin(Buf,S2MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S2MAdnb);   SetIndexStyle(Buf,Type,0,S2W,S2MAdnclr);  SetIndexDrawBegin(Buf,S2MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S2trend);   SetIndexStyle(Buf,DRAW_NONE);//26
//+------------------------------------------------------------------------------------------------------------------+
   if(showS3MA)           Type=DRAW_LINE; else Type=DRAW_NONE; int S3W=S3MAwidth,S3B=S3W+4; color S3Wc=S3MAwtclr;
   Buf+=1; SetIndexBuffer(Buf,S3MAbgd);   SetIndexStyle(Buf,Type,0,S3B,S3Wc);       SetIndexDrawBegin(Buf,S3MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S3MA);      SetIndexStyle(Buf,Type,0,S3W,S3MAupclr);  SetIndexDrawBegin(Buf,S3MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S3MAdna);   SetIndexStyle(Buf,Type,0,S3W,S3MAdnclr);  SetIndexDrawBegin(Buf,S3MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S3MAdnb);   SetIndexStyle(Buf,Type,0,S3W,S3MAdnclr);  SetIndexDrawBegin(Buf,S3MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S3trend);   SetIndexStyle(Buf,DRAW_NONE);//31
//+------------------------------------------------------------------------------------------------------------------+
   if(showS1MA)           Type=DRAW_LINE; else Type=DRAW_NONE; int S1W=S1MAwidth,S1B=S1W+4; color S1Wc=S1MAwtclr;
   Buf+=1; SetIndexBuffer(Buf,S1MAbgd);   SetIndexStyle(Buf,Type,0,S1B,S1Wc);       SetIndexDrawBegin(Buf,S1MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S1MA);      SetIndexStyle(Buf,Type,0,S1W,S1MAupclr);  SetIndexDrawBegin(Buf,S1MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S1MAdna);   SetIndexStyle(Buf,Type,0,S1W,S1MAdnclr);  SetIndexDrawBegin(Buf,S1MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S1MAdnb);   SetIndexStyle(Buf,Type,0,S1W,S1MAdnclr);  SetIndexDrawBegin(Buf,S1MAper+1);
   Buf+=1; SetIndexBuffer(Buf,S1trend);   SetIndexStyle(Buf,DRAW_NONE);//36
//+------------------------------------------------------------------------------------------------------------------+
   if(Period()<=PERIOD_H4){Type=DRAW_ARROW;} else Type=DRAW_NONE;
   Buf+=1; SetIndexBuffer(Buf,BUFdn); SetIndexStyle(Buf,Type,0,0,DnCol); SetIndexArrow(Buf,168);
   Buf+=1; SetIndexBuffer(Buf,BUFup); SetIndexStyle(Buf,Type,0,0,UpCol); SetIndexArrow(Buf,168);//38
//+------------------------------------------------------------------------------------------------------------------+
   Buf+=1; SetIndexBuffer(Buf,colors1);    SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,TRX);        SetIndexStyle(Buf,DRAW_NONE);//40
//+------------------------------------------------------------------------------------------------------------------+
   IndicatorObjPrefix = "__" + btn_text + "__";
   buttonId = "_" + UniqueButtonID + IndicatorObjPrefix + "_BT_";
   if (ObjectFind(buttonId)<0)
      createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, button_x);
   show_data = ObjectGetInteger(0, buttonId, OBJPROP_STATE);
   if (show_data) ObjectSetInteger(0, buttonId, OBJPROP_COLOR, btn_text_ON_color);
   else           ObjectSetInteger(0, buttonId, OBJPROP_COLOR, btn_text_OFF_color);
   EventSetMillisecondTimer(300);  indicatorFileName = WindowExpertName();  return(INIT_SUCCEEDED);}//End OnInit
//+------------------------------------------------------------------------------------------------------------------+
   void OnDeinit(const int reason){if(reason ==REASON_REMOVE) ObjectsDeleteAll(0,MyName+":");
   EventKillTimer( ); CleanUpOnAisle16();
   if(reason != REASON_CHARTCHANGE) ObjectDelete(buttonId);}
//+------------------------------------------------------------------------------------------------------------------+
void createButton(string buttonID,string buttonText,int width2,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
   ObjectDelete    (0,buttonID);
   ObjectCreate    (0,buttonID,OBJ_BUTTON,btn_Subwindow,0,0);
   ObjectSetInteger(0,buttonID,OBJPROP_COLOR,txtColor);
   ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,bgColor);
   ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR,borderColor);
   ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
   ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,width2);
   ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,height);
   ObjectSetString (0,buttonID,OBJPROP_FONT,font);
   ObjectSetString (0,buttonID,OBJPROP_TEXT,buttonText);
   ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,fontSize);
   ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID,OBJPROP_CORNER,btn_corner);
   ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,1);
   ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,9999);
   ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,9999);
   // Upon creation, set the initial state to "true" which is "on", so background is shown by default
   ObjectSetInteger(0, buttonId, OBJPROP_STATE, true);
}
//+------------------------------------------------------------------------------------------------------------------+
   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[]){
   fSetBuffers();
   int i,limit=(int)fmin(fmin(rates_total-prev_calculated+1,rates_total-1),maxBars-1);
//+---Semafor Function-----------------------------------------------------------------------------------------------+
   if(DotPer1>0) CountZZ(ABufUp,ABufDn,DotPer1,Dev1,Stp1);
   if(DotPer2>0) CountZZ(BBufUp,BBufDn,DotPer2,Dev2,Stp2);
   if(DotPer3>0) CountZZ(CBufUp,CBufDn,DotPer3,Dev3,Stp3);
   if(DotPer4>0) CountZZ(DBufUp,DBufDn,DotPer4,Dev4,Stp4);
   if(DotPer5>0) CountZZ(EBufUp,EBufDn,DotPer5,Dev5,Stp5);
//+---S1MA Function--------------------------------------------------------------------------------------------------+
   if(returnBars){S1MA[0]=limit+1; return(0);} if(calculateS1TREND || timeFrame == Period()){
   if(!calculateS1TREND && S1trend[limit]==-1) CleanPoint(limit,S1MAdna,S1MAdnb); 
   for(i=limit;i>=0;i--){S1MAdna[i]=EMPTY_VALUE; S1MAdnb[i]=EMPTY_VALUE; S1trend[i]=S1trend[i+1]; 
                  if(Close[i]>iMA(Symbol(),S1MAtf,S1MAper,S1MAshft,S1MAmode,PRICE_HIGH,i+1)) S1trend[i]= 1;
                  if(Close[i]<iMA(Symbol(),S1MAtf,S1MAper,S1MAshft,S1MAmode,PRICE_LOW, i+1)) S1trend[i]=-1;
   if(S1trend[i]==-1) S1MA[i]=iMA(Symbol(),S1MAtf,S1MAper,S1MAshft,S1MAmode,PRICE_WEIGHTED,i+0);
                else  S1MA[i]=iMA(Symbol(),S1MAtf,S1MAper,S1MAshft,S1MAmode,PRICE_WEIGHTED,i+0); S1MAbgd[i]=S1MA[i];
   if(!calculateS1TREND && S1trend[i]==-1) PlotPoint(i,S1MAdna,S1MAdnb,S1MA);}}//EOF S1MA
//+---S2MA Function--------------------------------------------------------------------------------------------------+
   if(returnBars){S2MA[0]=limit+1; return(0);} if(calculateS2TREND || timeFrame == Period()){
   if(!calculateS2TREND && S2trend[limit]==-1) CleanPoint(limit,S2MAdna,S2MAdnb); 
   for(i=limit;i>=0;i--){S2MAdna[i]=EMPTY_VALUE; S2MAdnb[i]=EMPTY_VALUE; S2trend[i]=S2trend[i+1]; 
                  if(Close[i]>iMA(Symbol(),S2MAtf,S2MAper,S2MAshft,S2MAmode,PRICE_HIGH,i+1)) S2trend[i]= 1;
                  if(Close[i]<iMA(Symbol(),S2MAtf,S2MAper,S2MAshft,S2MAmode,PRICE_LOW, i+1)) S2trend[i]=-1;
   if(S2trend[i]==-1) S2MA[i]=iMA(Symbol(),S2MAtf,S2MAper,S2MAshft,S2MAmode,PRICE_WEIGHTED,i+0);
                else  S2MA[i]=iMA(Symbol(),S2MAtf,S2MAper,S2MAshft,S2MAmode,PRICE_WEIGHTED,i+0); S2MAbgd[i]=S2MA[i];
   if(!calculateS2TREND && S2trend[i]==-1) PlotPoint(i,S2MAdna,S2MAdnb,S2MA);}}//EOF S2MA
//+---S3MA Function--------------------------------------------------------------------------------------------------+
   if(returnBars){S3MA[0]=limit+1; return(0);} if(calculateS3TREND || timeFrame == Period()){
   if(!calculateS3TREND && S3trend[limit]==-1) CleanPoint(limit,S3MAdna,S3MAdnb); 
   for(i=limit;i>=0;i--){S3MAdna[i]=EMPTY_VALUE; S3MAdnb[i]=EMPTY_VALUE; S3trend[i]=S3trend[i+1]; 
                  if(Close[i]>iMA(Symbol(),S3MAtf,S3MAper,S3MAshft,S3MAmode,PRICE_HIGH,i+1)) S3trend[i]= 1;
                  if(Close[i]<iMA(Symbol(),S3MAtf,S3MAper,S3MAshft,S3MAmode,PRICE_LOW, i+1)) S3trend[i]=-1;
   if(S3trend[i]==-1) S3MA[i]=iMA(Symbol(),S3MAtf,S3MAper,S3MAshft,S3MAmode,PRICE_WEIGHTED,i+0);
                else  S3MA[i]=iMA(Symbol(),S3MAtf,S3MAper,S3MAshft,S3MAmode,PRICE_WEIGHTED,i+0); S3MAbgd[i]=S3MA[i];
   if(!calculateS3TREND && S3trend[i]==-1) PlotPoint(i,S3MAdna,S3MAdnb,S3MA);}}//EOF S3MA
//+---Rib Function---------------------------------------------------------------------------------------------------+
   for(i=0;i<limit;i++){Rib1a[i]=S1MA[i]; Rib1b[i]=S2MA[i];}
   for(i=0;i<limit;i++){Rib2a[i]=S2MA[i]; Rib2b[i]=S3MA[i];}
//+---BGD Zones Function---------------------------------------------------------------------------------------------+
   if(showBGDzones){
      datetime tDummy=Dummy; if(Dummy<=Time[0]) tDummy=Time[0];
      double MaxValue=High[0]*10.0;

      // Determine loop range: full redraw when bgd_recalc flagged or first run
      int bgdLimit = (bgd_recalc || prev_calculated<=1)
                     ? (int)fmin(rates_total-1, maxBars-1)
                     : limit;

      // If OFF→ON transition: clear all old BGD rectangles first
      if(bgd_recalc){
         string zn;
         for(int z=ObjectsTotal()-1; z>=0; z--){
            zn=ObjectName(z);
            if(StringSubstr(zn,0,StringLen(ID+"BGD:"))== ID+"BGD:") ObjectDelete(zn);
         }
         bgd_recalc=false;
      }

      if(show_data){
         for(i=bgdLimit; i>=0; i--){
            if(i>Bars-2) continue;
            // propagate state
            colors1[i]=colors1[i+1];
            colors2[i]=colors2[i+1];
            // update state from SEMA dots
            if(BBufUp[i]) colors1[i]= 1;
            if(BBufDn[i]) colors1[i]=-1;
            if(ABufUp[i]) colors2[i]= 1;
            if(ABufDn[i]) colors2[i]=-1;
            // find zone start
            int index;
            for(index=i; index<Bars-1; index++) if(colors1[index]!=colors1[index+1]) break;
            string name=ID+"BGD:"+Time[index+1];
            ObjectDelete(name);
            ObjectDelete(ID+"BGD:"+Time[1]);
            datetime lastTime=Time[i]; if(i==0) lastTime=tDummy;
            ObjectCreate(name,OBJ_RECTANGLE,0,Time[index+1],0,lastTime,MaxValue);
            ObjectSet(name,OBJPROP_BACK,true);
            switch((int)colors1[i]){
               case -1: ObjectSet(name,OBJPROP_COLOR,ColorDown); break;
               case  1: ObjectSet(name,OBJPROP_COLOR,ColorUp);   break;
               default: ObjectSet(name,OBJPROP_COLOR,ColorWait); break;
            }
         }
         RefreshRates();
      }
   }
//+---Daily Open Function--------------------------------------------------------------------------------------------+
   for(i=limit;i>=0;i--){if(TimeDayOfWeek(Time[i+1])!=0){ 
   if(TimeDay(Time[i]) != TimeDay(Time[i+1])){OPening=Open[i-GmtShift];}}
   Bufo=iMA(Symbol(),0,MA_Period,0,MA_Method,PriceMode,i);   BUFdn[i-GmtShift]=OPening; BUFup[i-GmtShift]=OPening;
   if((OPening>Bufo)){BUFup[i]=EMPTY_VALUE;}}
//+----Directional Trading-------------------------------------------------------------------------------------------+
   for(i=limit;i>=0;i--)
   {candle(2,i); TRX[i]= 0; SymClr=CandleWt;
   
   if(S2trend[i]== 1 && S3trend[i]== 1){
   TRX[i]= 1; 
   candle(1,i);
   SymClr=CandleUp;}
   
   if(S2trend[i]==-1 && S3trend[i]==-1){
   TRX[i]=-1; 
   candle(3,i);
   SymClr=clrOrangeRed;}
//+---Select type of BIAS Square or Arrow in current TF--------------------------------------------------------------+
   color BXup=clrGreen,BXdn=clrMaroon;
   if(EBufUp[i]){BOXtxt="  UP SQUARE"; BOXclr=BXup;}
	if(EBufDn[i]){BOXtxt="  DN SQUARE"; BOXclr=BXdn;}
   if(CBufUp[i]){BOXtxt2=" UP ARROW";  BOXclr2=BXup;}   
	if(CBufDn[i]){BOXtxt2=" DN ARROW";  BOXclr2=BXdn;}
   }//End Directional Trading
//+---Power Label----------------------------------------------------------------------------------------------------+
   if(showOpenBias){string BOX2txt=" "; color TXT2clr=C'30,47,62',BOX2clr;
   if(Close[0]>OPening){BOX2txt="Price is Above the Daily Open Line"; BOX2clr=CandleUp;}
                  else {BOX2txt="Price is Below the Daily Open Line"; BOX2clr=CandleDn;}
   PanelsX(ID+"XB01",530-2,-4,"gggggggggggggggggggggg",FontSize+1,"Webdings",BOX2clr);
   LabelsX(ID+"XB02",536-1,-8); ObjectSetText(ID+"XB02",BOX2txt,FontSize+2,FontType,TXT2clr);}	
//+---Symbol Header Function-----------------------------------------------------------------------------------------+
   if(showSymbolHeader){for(i=limit;i>=0;i--){
   if(S3trend[i]== 1){colCarlsberg=clrDodgerBlue;} if(S3trend[i]==-1){colCarlsberg=clrOrangeRed;}
   SetTextRight(ID+"X001",StringSubstr(Symbol(),0,8)+" "+timeFrameToString(timeFrame),LR+280+134,UD+1,Sbg,HDRsize);
   SetTextRight(ID+"X002",StringSubstr(Symbol(),0,8)+" "+timeFrameToString(timeFrame),LR+279+134,UD+0,SymClr,HDRsize);
   SetTextRight(ID+"X003","Carlsberg Don't Do Charts ",LR+413,UD+54,clrBlack,HDRsize2);
   SetTextRight(ID+"X004","Carlsberg Don't Do Charts ",LR+414,UD+55,colCarlsberg,HDRsize2);}}//End Symbol Header
//+---Version Label--------------------------------------------------------------------------------------------------+
   if(showVersion){
   xLabels(ID+"Xard98",53,29); ObjectSetText(ID+"Xard98",Version,FontSize3,FontType,clrBlack);
   xLabels(ID+"Xard99",54,30); ObjectSetText(ID+"Xard99",Version,FontSize3,FontType,SymClr);}
   //+---TextBOX Display------------------------------------------------------------------------------------------------+
   if(showTextBOX){xPanels(ID+"Xard1",4,3,"ggggggg",FontSize3+2,"Webdings",BOXclr);
                   xLabels(ID+"Xard2",3,2); ObjectSetText(ID+"Xard2",BOXtxt,FontSize3,FontType,Snow);
                   xPanels(ID+"Xard3",4+166,3,"ggggggg",FontSize3+2,"Webdings",BOXclr2);
                   xLabels(ID+"Xard4",3+177,2); ObjectSetText(ID+"Xard4",BOXtxt2,FontSize3,FontType,Snow);}//End TextBOX
//+------------------------------------------------------------------------------------------------------------------+
   if(showTEXTx){
   printTEXT(ID+"Xard77",0,"COMPOUND INTEREST",14,"Arial Bold",C'145,125,88',2,4,340);
   printTEXT(ID+"Xard78",0,"'He who understands it, earns it... He who does not, pays it'.",14,"Arial Italic",clrSnow,2,4,564);}
//+------------------------------------------------------------------------------------------------------------------+
   manageAlerts();
   
   return(rates_total);}
//+------------------------------------------------------------------------------------------------------------------+
   void PanelsX(string aa3,int x,int y,string Text,int fontSize,string Font,color Color){
         ObjectDelete(aa3);
         ObjectCreate(aa3,OBJ_LABEL,0,0,0);
            ObjectSet(aa3,OBJPROP_CORNER,0);
            ObjectSet(aa3,OBJPROP_XDISTANCE,x+moveTxtLR+4);
            ObjectSet(aa3,OBJPROP_YDISTANCE,y+moveTxtUD+4);
            ObjectSet(aa3,OBJPROP_BACK,false);
        ObjectSetText(aa3,Text,fontSize,Font,Color);}
//+------------------------------------------------------------------------------------------------------------------+
   void LabelsX(string aa4,int x,int y){ 
         ObjectDelete(aa4);
         ObjectCreate(aa4,OBJ_LABEL,0,0,0);
            ObjectSet(aa4,OBJPROP_CORNER,0);
            ObjectSet(aa4,OBJPROP_XDISTANCE,x+moveTxtLR+4);
            ObjectSet(aa4,OBJPROP_YDISTANCE,y+moveTxtUD+4);
            ObjectSet(aa4,OBJPROP_BACK,false);}
//+------------------------------------------------------------------------------------------------------------------+
   void printTEXT(string xar,int Win,string Label,int FontS,string FontN,color FontC,int Corner,int yPos,int xPos){
   if(ObjectFind(xar)!=0) ObjectCreate(xar,OBJ_LABEL,0,0,0);
   ObjectSetText(xar,Label,FontS,FontN,FontC);
       ObjectSet(xar,OBJPROP_CORNER,Corner);
       ObjectSet(xar,OBJPROP_XDISTANCE,xPos);
       ObjectSet(xar,OBJPROP_YDISTANCE,yPos);}                 
//+------------------------------------------------------------------------------------------------------------------+
   void Panels(string aa3,int x,int y,string Text,int fontSize,string Font,color Color){
         ObjectDelete(aa3);
         ObjectCreate(aa3,OBJ_LABEL,0,0,0);
            ObjectSet(aa3,OBJPROP_CORNER,2);
            ObjectSet(aa3,OBJPROP_XDISTANCE,x+moveTxtLR+4);
            ObjectSet(aa3,OBJPROP_YDISTANCE,y+moveTxtUD+4);
            ObjectSet(aa3,OBJPROP_BACK,false);
        ObjectSetText(aa3,Text,fontSize,Font,Color);}
//+------------------------------------------------------------------------------------------------------------------+
   void Labels(string aa4,int x,int y){ 
         ObjectDelete(aa4);
         ObjectCreate(aa4,OBJ_LABEL,0,0,0);
            ObjectSet(aa4,OBJPROP_CORNER,2);
            ObjectSet(aa4,OBJPROP_XDISTANCE,x+moveTxtLR+4);
            ObjectSet(aa4,OBJPROP_YDISTANCE,y+moveTxtUD+4);
            ObjectSet(aa4,OBJPROP_BACK,false);}
//+------------------------------------------------------------------------------------------------------------------+
   void SetTextRight(string name,string text,int x,int y,color colour,int fontsize=10, string font="Arial Black"){
       ObjectDelete(name);
    if(ObjectCreate(0,name,OBJ_LABEL,0,0,0)) {
   ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);}
   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
   ObjectSetText(name,text,fontsize,font,colour);}
//+------------------------------------------------------------------------------------------------------------------+
   void label1(string name1,int corner,int xOffset,int yOffset,string text,string fontName,int fontSize,color txtColor){
	     ObjectCreate(name1,OBJ_LABEL,1,0,0);
	        ObjectSet(name1,OBJPROP_CORNER,3);
	        ObjectSet(name1,OBJPROP_XDISTANCE,xOffset); 
	        ObjectSet(name1,OBJPROP_YDISTANCE,yOffset);
	        ObjectSet(name1,OBJPROP_BACK,false);
	ObjectSetInteger(0,name1,OBJPROP_SELECTABLE,false);
	    ObjectSetText(name1,text,fontSize,fontName,txtColor); 
	ObjectSetString(0,name1,OBJPROP_TOOLTIP,"\n");}
//+------------------------------------------------------------------------------------------------------------------+
   void CleanPoint(int i,double& first[],double& second[]){
   if (i>=Bars-3) return;
   if ((second[i  ] != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1]  = EMPTY_VALUE; else
   if (( first[i  ] != EMPTY_VALUE) && ( first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
         first[i+1]  = EMPTY_VALUE;}
//+------------------------------------------------------------------------------------------------------------------+
   void PlotPoint(int i,double& first[],double& second[],double& from[]){
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
   if (first[i+2] == EMPTY_VALUE){ 
              first[i] = from[i];  first[i+1] = from[i+1]; second[i] = EMPTY_VALUE;}
      else  {second[i] = from[i]; second[i+1] = from[i+1];  first[i] = EMPTY_VALUE;}
      else  { first[i] = from[i];                          second[i] = EMPTY_VALUE;}}
//+------------------------------------------------------------------------------------------------------------------+
   string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
   int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};
//+------------------------------------------------------------------------------------------------------------------+
   int stringToTimeFrame(string tfs){tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
   if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));  return(Period());}
//+------------------------------------------------------------------------------------------------------------------+
   string timeFrameToString(int tf){
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
   if (tf==iTfTable[i]) return(sTfTable[i]);  return("");}
   string stringUpperCase(string str){ string s = str;
   for (int length=StringLen(str)-1; length>=0; length--){int tchar = StringGetChar(s, length);
   if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256)) s = StringSetChar(s, length, tchar - 32); else
   if(tchar > -33 && tchar < 0) s = StringSetChar(s, length, tchar + 224);}  return(s);}
//+------------------------------------------------------------------------------------------------------------------+
   void manageAlerts(){
   if(inpAlertsOn){int whichBar = (inpAlertsOnCurrent) ? 0 : 1;
   if(TRX[whichBar] != TRX[whichBar+1]){
   if(TRX[whichBar] ==  1) doAlert(whichBar,"BUY BUY BUY");
   if(TRX[whichBar] == -1) doAlert(whichBar,"SELL SELL SELL"); }}}
//+------------------------------------------------------------------------------------------------------------------+
   void doAlert(int forBar, string doWhat){
   static string   previousAlert="nothing";
   static datetime previousTime;  string msg;
   if(previousAlert != doWhat || previousTime != iTime(_Symbol,_Period,forBar)){
      previousAlert  = doWhat;   previousTime  = iTime(_Symbol,_Period,forBar);
   msg=_Symbol+" "+timeFrameToString(_Period)+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" Direction changed to "+doWhat;
   if(inpAlertsMessage)   Alert(msg);
   if(inpAlertsEmail)     SendMail(_Symbol+" Trading ",msg);
   if(inpAlertsPushNotif) SendNotification(msg);
   if(inpAlertsSound)     PlaySound("alert2.wav"); }}
//+----Candle Function-----------------------------------------------------------------------------------------------+
   void candle(int direction,int i){double high,low,bodyHigh,bodyLow;
     bodyHigh = MathMax(Open[i],Close[i]);
     bodyLow  = MathMin(Open[i],Close[i]);
     high     = High[i];
     low      = Low[i];
     candle0[i] = low;     candle1[i] = low;     candle2[i] = low;     candle3[i] = low;
     candle4[i] = bodyLow; candle5[i] = bodyLow; candle6[i] = bodyLow; candle7[i] = bodyLow;
     switch (direction){
           case 1:candle0[i] = high; candle4[i] = bodyHigh; break;
           case 2:candle2[i] = high; candle6[i] = bodyHigh; break;
           case 3:candle3[i] = high; candle7[i] = bodyHigh; break; }}
//+---fSetBuffers Function-------------------------------------------------------------------------------------------+
   void fSetBuffers(){int iChartScale = int(ChartGetInteger(0,CHART_SCALE));
   if(getChartScale == iChartScale) return;
	   getChartScale = iChartScale; int iW=0,rW=0; switch(iChartScale){
		case 0: iW=1; rW=1; break; case 1: iW=1; rW=2; break; case 2: iW= 2; rW= 3; break;
		case 3: iW=4; rW=5; break; case 4: iW=7; rW=9; break; case 5: iW=14; rW=17; break;}
   if(showRib1)           Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
	SetIndexStyle(0,Type,0,rW,Rib1ClrUP);
	SetIndexStyle(1,Type,0,rW,Rib1ClrDN);
	if(showRib2)           Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
	SetIndexStyle(2,Type,0,rW,Rib2ClrUP);
	SetIndexStyle(3,Type,0,rW,Rib2ClrDN);
	if(showCandles)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
	SetIndexStyle(18,Type,0,iW,CandleUp);
   SetIndexStyle(20,Type,0,iW,CandleWt);
   SetIndexStyle(21,Type,0,iW,CandleDn);  ChartRedraw();}
//+----Clean Chart Function------------------------------------------------------------------------------------------+
   void CleanUpOnAisle16(){string name; for(int s=ObjectsTotal()-1; s>=0; s--){ name=ObjectName(s);
         if(StringSubstr(name,0,StringLen(ID))==ID){ ObjectDelete(name); }}}//EOF
//+----Semafor-------------------------------------------------------------------------------------------------------+
   int CountZZ(double& ExtMapBufferUp[],double& ExtMapBufferDn[],int Depth,int ExtDeviation,int ExtBackstep){
   int shiftZZ,back,lasthighpos,lastlowpos; double val,res,curlow,curhigh,lasthigh,lastlow;
   for(shiftZZ=Bars-Depth; shiftZZ>=0; shiftZZ--){val=Low[iLowest(NULL,0,MODE_LOW,Depth,shiftZZ)];
   if(val==lastlow) val=0.0;  else { lastlow=val;
   if((Low[shiftZZ]-val)>(ExtDeviation+.5*myPoint)) val=0.0;  else {
   for(back=1; back<=ExtBackstep+1; back++){res=ExtMapBufferUp[shiftZZ+back];
   if (res!=0.0) res = res+deltazz;
   if((res!=0.0)&&(res>val)) ExtMapBufferUp[shiftZZ+back]=0.0;}}}
   if(val==0.0) ExtMapBufferUp[shiftZZ]=0.0; else ExtMapBufferUp[shiftZZ]=val-deltazz;
//--- high
   val=High[iHighest(NULL,0,MODE_HIGH,Depth,shiftZZ)];
   if(val==lasthigh) val=0.0;  else { lasthigh=val;
   if((val-High[shiftZZ])>(ExtDeviation+.5*myPoint)) val=0.0; else {
   for(back=1; back<=ExtBackstep+1; back++){ res=ExtMapBufferDn[shiftZZ+back];
   if(res!=0.0) res = res-deltazz;
   if((res!=0.0)&&(res<val)) ExtMapBufferDn[shiftZZ+back]=0.0;}}}
   if(val==0.0) ExtMapBufferDn[shiftZZ]=0.0; else ExtMapBufferDn[shiftZZ]=val+deltazz;}
//--- final cutting
   lasthigh=-1;  lasthighpos=-1;  lastlow=-1;  lastlowpos=-1;
   for(shiftZZ=Bars-Depth; shiftZZ>=0; shiftZZ--){ curlow=ExtMapBufferUp[shiftZZ];
   if(curlow != 0.0) curlow = curlow+deltazz;    curhigh=ExtMapBufferDn[shiftZZ];
   if(curhigh != 0.0) curhigh = curhigh-deltazz;
   if((curlow==0.0)&&(curhigh==0.0)) continue;
//---
   if(curhigh!=0.0){if(lasthigh>0.0){
   if(lasthigh<curhigh) ExtMapBufferDn[lasthighpos]=0.0; else ExtMapBufferDn[shiftZZ]=0.0;}
//---
   if(lasthigh<curhigh || lasthigh<0.0){lasthigh=curhigh; lasthighpos=shiftZZ;} lastlow=-1;}
   if(curlow!=0.0){if(lastlow>0.0){
   if(lastlow>curlow) ExtMapBufferUp[lastlowpos]=0.0; else ExtMapBufferUp[shiftZZ]=0.0;}
//---
   if((curlow<lastlow)||(lastlow<0.0)){lastlow=curlow; lastlowpos=shiftZZ;} lasthigh=-1;}}
   for(shiftZZ=Bars-1; shiftZZ>=0; shiftZZ--){
   if(shiftZZ>=Bars-Depth) ExtMapBufferUp[shiftZZ]=0.0; else {break;}} return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   int Str2Massive(string VStr,int& M_Count,int& VMass[]){int val=StrToInteger(VStr); if(val>0){M_Count++;
   int mc=ArrayResize(VMass,M_Count); if(mc==0)return(-1); VMass[M_Count-1]=val; return(1);} else return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   int IntFromStr(string ValStr,int& M_Count, int& VMass[]){if(StringLen(ValStr)==0) return(-1);
   string SS=ValStr; int NP=0; string CS; M_Count=0; ArrayResize(VMass,M_Count);
   while(StringLen(SS)>0){NP=StringFind(SS,",");
   if(NP>0){CS=StringSubstr(SS,0,NP); SS=StringSubstr(SS,NP+1,StringLen(SS));}  else {
   if(StringLen(SS)>0){CS=SS; SS="";}} if(Str2Massive(CS,M_Count,VMass)==0){return(-2);}} return(1);}
//+---OnChartEvent Function------------------------------------------------------------------------------------------+
   void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam){
   if(id==CHARTEVENT_OBJECT_CREATE || id==CHARTEVENT_OBJECT_DELETE) return;
   if(id==CHARTEVENT_MOUSE_MOVE    || id==CHARTEVENT_MOUSE_WHEEL)   return;
   if(id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON){
      if(StringFind(sparam,MyName+":back:",0)==0) ObjectSet(sparam,OBJPROP_STATE,false);
      // Handle BGD background button toggle
      if(sparam == buttonId){
         show_data = ObjectGetInteger(0, buttonId, OBJPROP_STATE);
         if(show_data){
            ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_ON_color);
            bgd_recalc = true; // force full BGD redraw on next calculate
         } else {
            ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_OFF_color);
            // clear BGD rectangles only
         string delName;
         for(int d=ObjectsTotal()-1; d>=0; d--){
            delName=ObjectName(d);
            if(StringSubstr(delName,0,StringLen(ID+"BGD:"))== ID+"BGD:") ObjectDelete(delName);
         }
         }
         ChartRedraw();
         WindowRedraw();
      }
      if(IsStopped()) return; fSetBuffers();}  OnInit();}
//+------------------------------------------------------------------------------------------------------------------+
   void xPanels(string M5,int x,int y,string Text,int fontSize,string Font,color Color){
   static int window=0; if(window<0) window=WindowFind(ID); 
          ObjectCreate(M5,OBJ_LABEL,window,0,0);
             ObjectSet(M5,OBJPROP_CORNER,2);
             ObjectSet(M5,OBJPROP_XDISTANCE,x+move_text_sideways);
             ObjectSet(M5,OBJPROP_YDISTANCE,y+move_text_updown);
             ObjectSet(M5,OBJPROP_BACK,false);
         ObjectSetText(M5,Text,fontSize,Font,Color);}
//+------------------------------------------------------------------------------------------------------------------+
   void xLabels(string M6,int x,int y){
   static int window=0; if(window<0) window=WindowFind(ID);
          ObjectCreate(M6,OBJ_LABEL,window,0,0);
             ObjectSet(M6,OBJPROP_CORNER,2);
             ObjectSet(M6,OBJPROP_XDISTANCE,x+move_text_sideways);
             ObjectSet(M6,OBJPROP_YDISTANCE,y+move_text_updown);
             ObjectSet(M6,OBJPROP_BACK,false);}
//+----END OF FILE---------------------------------------------------------------------------------------------------+