//+-------------------------------------------------------\!/--------------------------------------------------------+
//+ [Knowledge of the ancients]                          (ò ó)                                      [XU CANDLES.mq4] +
//+-------------------------------------------------o0o---(_)---o0o--------------------------------------------------+
#property link        "https://forex-station.com/viewtopic.php?p=1295442917#p1295442917"
#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 21
#define Version "XU"
string ID = "Xard>";

string Name=WindowExpertName(),MyName="XU CANDLES BT",indicatorFileName;

//+------------------------------------------------------------------------------------------------------------------+
 input string Indicator                      = Version;
          int getChartScale                  = WRONG_VALUE;  int Type;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR01                          = "<<<==== [01] 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                       = clrDarkGray,       // Candle Wait Color
              CandleDn                       = clrViolet;         // Candle Down Color
       double candle0[],candle1[],candle2[],candle3[],candle4[],candle5[],candle6[],candle7[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR02                          = "<<<==== [02] S1MA Trend Settings ====>>>";
   input bool showS1MA                       = true;              // Show S1MA?
    input int S1MAper                        = 36;                // 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                      =  5;                // S1MA line width
  input color S1MAwtclr                      = clrNONE;           // S1MA Wait Color
  input color S1MAupclr                      = clrDodgerBlue;     // S1MA Up Color
  input color S1MAdnclr                      = clrCrimson;        // S1MA Down Color
         bool returnBars,calculateS1TREND;  double S1MAbgd[],S1MA[],S1MAdna[],S1MAdnb[],S1trend[];
//+------------------------------------------------------------------------------------------------------------------+
extern string STR03                          = "<<<==== [03] S2MA Trend Settings ====>>>";
   input bool showS2MA                       = true;              // Show S2MA?
    input int S2MAper                        =  9;                // 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                      =  4;                // S2MA line width
  input color S2MAwtclr                      = clrNONE;           // S2MA Wait Color
  input color S2MAupclr                      = clrDodgerBlue;     // S2MA Up Color
  input color S2MAdnclr                      = clrCrimson;        // S2MA Down Color
         bool calculateS2TREND;  double S2MAbgd[],S2MA[],S2MAdna[],S2MAdnb[],S2trend[];   
//+------------------------------------------------------------------------------------------------------------------+
extern string STR04                          = "<<<==== [04] BOXtxt Settings ====>>>";
   input bool showBOXtxt                     = true;
       string BOXtxt,BOXspc; color BOXclr;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR05                          = "<<<==== [05] Symbol Header Settings ====>>>";
   input bool showSymbolHeader               = true;  
    input int LR                             = 55,
              UD                             = 10,
              HDRsize                        = 40,
              HDRsize2                       = 14;  
        color Sbg=clrBlack,SymClr;
       double OpenToday;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR06                          = "<<<==== [06] Daily Open level Settings ====>>>";
    input int GmtShift,MA_Period=2,MA_Method=MODE_SMMA,PriceMode=PRICE_WEIGHTED; 
    input int maxBars                        = 5000;
  input color UpCol=clrLime,DnCol=clrGold; double BUFdn[],BUFup[],OPening,Bufo;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR07                          = "<<<==== [07] Alert Settings ====>>>";
   input bool inpAlertsOn                    = false;             // Turn alerts on?
   input bool inpAlertsOnCurrent             = true;              // 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?
extern string STR08                          = "<<<==== [08] Button Settings ====>>>";   
extern int                btn_Subwindow = 0;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;
extern string             btn_text              = "xu.";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 8;
extern color              btn_text_ON_color     = clrLime;
extern color              btn_text_OFF_color    = clrRed;
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 1100;
extern int                button_y              = 0;
extern int                btn_Width             = 60;
//extern int                btn_Height            = 20;
//+------------------------------------------------------------------------------------------------------------------+
       double TRX[],data1;  string TimeFrame="Current time frame";  int timeFrame;
       string FontType                       = "Arial Black";
    input int moveTxtLR                      = 0,moveTxtUD=0,FontSize=12;
    
bool show_data = true;
bool recalc    = true;

string IndicatorName, IndicatorObjPrefix,buttonId;

string GenerateIndicatorName(const string target) 
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}

//+----OnInit Function-----------------------------------------------------------------------------------------------+
   int OnInit()
   {
IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(IndicatorName);
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
      show_data = val != 0;
         
   int Buffers=21,Buf=-1;  IndicatorBuffers(Buffers);
   if(TimeFrame==0 || TimeFrame<Period()) TimeFrame=Period();  timeFrame=stringToTimeFrame(TimeFrame);
   IndicatorDigits(Digits); IndicatorShortName(MyName); 
//+------------------------------------------------------------------------------------------------------------------+
   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);//4
   Buf+=1; SetIndexBuffer(Buf,candle5);    SetIndexStyle(Buf,DRAW_NONE);
   Buf+=1; SetIndexBuffer(Buf,candle6);//6
   Buf+=1; SetIndexBuffer(Buf,candle7);  //  fSetBuffers();//7
//+------------------------------------------------------------------------------------------------------------------+
   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);//12
//+------------------------------------------------------------------------------------------------------------------+
   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);//17
//+------------------------------------------------------------------------------------------------------------------+
   if(Period()<=PERIOD_H4){Type=DRAW_ARROW;} else Type=DRAW_NONE;
   Buf+=1; SetIndexBuffer(Buf,BUFdn); SetIndexStyle(Buf,Type,0,0,DnCol); SetIndexArrow(Buf,110);
   Buf+=1; SetIndexBuffer(Buf,BUFup); SetIndexStyle(Buf,Type,0,0,UpCol); SetIndexArrow(Buf,110);//29
//+------------------------------------------------------------------------------------------------------------------+
   Buf+=1; SetIndexBuffer(Buf,TRX);        SetIndexStyle(Buf,DRAW_NONE);//20
//+------------------------------------------------------------------------------------------------------------------+
   EventSetMillisecondTimer(300);  indicatorFileName = WindowExpertName(); 
   
   
    ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + (btn_text);
   createButton(buttonId, btn_text, btn_Width, 20, 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);   
   
    return(INIT_SUCCEEDED);}//End OnInit
//+------------------------------------------------------------------------------------------------------------------+
   void OnDeinit(const int reason){if(reason ==REASON_REMOVE) ObjectsDeleteAll(0,MyName+":");
   EventKillTimer( ); CleanUpOnAisle16();
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
   }
   
   
void createButton(string buttonID,string buttonText,int width,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,width);
   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);
  }
//+------------------------------------------------------------------+
void handleButtonClicks()
  {
   if(ObjectGetInteger(0, buttonId, OBJPROP_STATE))
     {
      ObjectSetInteger(0, buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      // start();
     }
  }
//+------------------------------------------------------------------------------------------------------------------+
   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[]){
   if(Name==MyName){ //fSetBuffers();
   int i,limit=fmin(rates_total-prev_calculated+1,rates_total-1);
//+---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_HIGH,i+0);
                else  S1MA[i]=iMA(Symbol(),S1MAtf,S1MAper,S1MAshft,S1MAmode,PRICE_LOW, 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_CLOSE,i+0);
                else  S2MA[i]=iMA(Symbol(),S2MAtf,S2MAper,S2MAshft,S2MAmode,PRICE_CLOSE,i+0); S2MAbgd[i]=S2MA[i];
   if(!calculateS2TREND && S2trend[i]==-1) PlotPoint(i,S2MAdna,S2MAdnb,S2MA);}}//EOF S2MA
//+----Directional Trading-------------------------------------------------------------------------------------------+
   for(i=limit;i>=0;i--)
   {TRX[i]= 0; candle(2,i); 
   BOXtxt="  NO TRADES - WAITING . . ."; BOXclr=clrGray;  BOXspc="gggggggggggggggggg"; SymClr=CandleWt;
   
   if(S2trend[i]== 1 && S1trend[i]== 1){
   TRX[i]= 1; 
   candle(1,i); 
   BOXtxt="  UP TREND - BUYS ONLY";
   BOXclr=clrBlue;
   BOXspc="ggggggggggggggggg";
   SymClr=CandleUp;}
   
   if(S2trend[i]==-1 && S1trend[i]==-1){
   TRX[i]=-1; 
   candle(3,i);
   BOXtxt="  DOWN TREND - SELLS ONLY";
   BOXclr=clrMagenta;
   BOXspc="ggggggggggggggggggg";
   SymClr=clrViolet;}
   
   }//End Directional Trading
//+---Symbol Header Function-----------------------------------------------------------------------------------------+
   if(showSymbolHeader){
   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+269+145,UD+55,C'30,160,30',HDRsize2);}//End Symbol Header
//+---Box Label------------------------------------------------------------------------------------------------------+
   if(showBOXtxt){
   Panels(ID+"X070",2,-1,BOXspc,FontSize+2,"Webdings",BOXclr);
   Labels(ID+"X071",7,-3); ObjectSetText(ID+"X071",timeFrameToString(timeFrame)+BOXtxt,FontSize+2,FontType,clrSnow);}
//+---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;}}
//+------------------------------------------------------------------------------------------------------------------+

   manageAlerts();
   
//+---ex4 Protection Function----------------------------------------------------------------------------------------+
   }else{Comment("\n                The name of this indicator cannot be changed\n     ''XU CANDLES''");}

   return(rates_total);}
               
//+------------------------------------------------------------------------------------------------------------------+
   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,0,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(showCandles)        Type=DRAW_HISTOGRAM; else Type=DRAW_NONE;
	SetIndexStyle(4,Type,0,iW,CandleUp);
   SetIndexStyle(6,Type,0,iW,CandleWt);
   SetIndexStyle(7,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
//+---OnChartEvent Function------------------------------------------------------------------------------------------+
   void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
   {
   if(IsStopped()) return; fSetBuffers();
   handleButtonClicks();
   if(id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   
     SetIndexStyle(0,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);         
     SetIndexStyle(1,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);          
     SetIndexStyle(2,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);  
     SetIndexStyle(3,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);     
     SetIndexStyle(4,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);         
     SetIndexStyle(5,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);          
     SetIndexStyle(6,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);  
     SetIndexStyle(7,  showCandles ? DRAW_HISTOGRAM :  DRAW_NONE);     
  
    SetIndexStyle(8,showS1MA ? DRAW_LINE :  DRAW_NONE);
    SetIndexStyle(9,showS1MA ? DRAW_LINE :  DRAW_NONE);
    SetIndexStyle(10,showS1MA ? DRAW_LINE :  DRAW_NONE);
    SetIndexStyle(11,showS1MA ? DRAW_LINE :  DRAW_NONE);
    
    SetIndexStyle(13,showS2MA ? DRAW_LINE :  DRAW_NONE);
    SetIndexStyle(14,showS2MA ? DRAW_LINE :  DRAW_NONE);
    SetIndexStyle(15,showS2MA ? DRAW_LINE :  DRAW_NONE);
    SetIndexStyle(16,showS2MA ? DRAW_LINE :  DRAW_NONE);
   
     SetIndexStyle(18, DRAW_ARROW ); 
     SetIndexStyle(19, DRAW_ARROW ); 
   

   if(show_data)
     {
      handleButtonClicks();
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
        
        
     }
   else
     {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      
      for (int tp=0; tp<indicator_buffers; tp++)
            SetIndexStyle(tp,DRAW_NONE);
     
     } 
	//if(id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON){
 //  if(StringFind(sparam,MyName+":back:"  ,0)==0) ObjectSet(sparam,OBJPROP_STATE,false);
	
	//} 
	
	// OnInit();
	 }
//+----END OF FILE---------------------------------------------------------------------------------------------------+