//+------------------------------------------------------------------+
//|                                     FXR_TripleHybridSRMidBar.mq4e|
//|                      Copyright © 2012, MetaQuotes Software Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
//Recent Update working on LIVE Bar through counters X2 i>=0
#property copyright "Copyright 2016 © UM/ForexRomeo Production"
#property link      ""
#property   description "Version 5.2 20/04/2016"
#property description "Alert Limited avbove M5 TimeFrame"
#property description "Key BAR for REVERSAL is Suspended for now until revision subject"
#property description " Indicator Previous Name is FXR_LimitLessCashVariation"
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 17
#property indicator_chart_window
#property indicator_color1 clrRed
#property indicator_color2 clrLimeGreen //LimeGreen
#property indicator_color3 clrCrimson //Red
#property indicator_color4 clrForestGreen //clrLimeGreen
#property  indicator_color5  clrDarkOliveGreen //Upper BBAND
#property  indicator_color6  clrDarkOliveGreen //Lower BBAND
#property  indicator_color7  clrDarkGray        //Main BBAND   clrDarkGreen
#property  indicator_color8  clrLime             //BuyTag off BBAND
#property  indicator_color9  clrOrange           //BuyTag off BBAND
#property  indicator_color10 clrYellow           //RSILS Yellow Dot
#property  indicator_color11 clrYellow           //RSILS Yellow FadeX
#property  indicator_color12 clrSnow             //CXR
#property  indicator_color13 clrDeepPink         //Reversal Arrow Down
#property  indicator_color14 clrLimeGreen        //Reversal Arrow Up
#property indicator_color15 clrLightGreen        //MidBar Up clrLime
#property indicator_color16 clrBlack             //MidBar Ranging Bar
#property indicator_color17 clrLightPink         //MidBar Down clrPaleVioletRed
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 4
#property indicator_width4 4
#property indicator_width5 1 
#property indicator_width6 1
#property indicator_width7 3 
#property indicator_width8 1 
#property indicator_width9 1 
#property indicator_width10 2 
#property indicator_width11 2 
#property indicator_width12 2 
#property indicator_width13 2 
#property indicator_width14 2 
#property indicator_width15 2 
#property indicator_width16 2 
#property indicator_width17 2 


enum  HowToColorBars{         // By ENUM nothing of the text is relevant only Position as numbers 
   No_Color_Method=0,
   RSils_ColoredBars=1,      //Colored Bars Based MomentumRSILS
   xSuTrend_ColoredBars=2,   //Colored Bars Based xSuperTrend
   CTRx_ColoredBars=3,       //Colored Bars Based CTR
   Stoch_ColoredBars=4,      //Colored Bars Based Stochastics
   
  };
  
  enum WhatIsMidBAr{
   DoNot_ShowMidBarInfo=0,   //Do Not Show MidBar Info
   Show_MidHighLow=1,        //MidBar of High & Low 
   Show_MidOpenClose=2,      //MidBar of Open & Close
   
  };

//-----------------------Input Parameters
extern HowToColorBars   ColoredBarMethod =CTRx_ColoredBars; 
extern WhatIsMidBAr DrawEMA3MidBars=DoNot_ShowMidBarInfo; 
extern int  MidBarsMarker=167; //159,108,91
extern int     SuperTrend_Period=7;      // SuperTrend ATR Period
extern double  SuperTrend_Multiplier=0.886; // SuperTrend Multiplier 1.7
extern int     CTRxBarsNumber = 13;
extern int     MaxHistoryBars = 2000;
extern int     KPeriod       = 5;
extern int     Slowing       = 5;
extern int     DPeriod       = 3;
extern ENUM_MA_METHOD StoMaMethod=0;
extern ENUM_STO_PRICE  PriceField= STO_CLOSECLOSE;
extern bool    PrintTriggers=True;
extern int     PTriggerX=20;
extern int     PTriggerY=550;
extern bool    TrainingFibo=True; 
//extern int     FibLcorner=2;
extern int     FibLabelX=400;
extern int     FibLabelY=12;
extern int     RSILS_Period = 5;//7
extern int     RSILSSmoothPeriod = 3;//13
extern int     RSILSSignal=5;
extern int     UD_levels=24;
//extern int     LocalBarsToLookBack=7;
extern bool    CxRAlerts=False;
extern bool    TagLevelsAlert=False;
extern ENUM_TIMEFRAMES MinTFAlert=PERIOD_M5;
extern int     BBandPeriod=30;
extern double  BBandDev=2.0;
extern int     BBandShift=2;
extern ENUM_APPLIED_PRICE BBPrice= PRICE_OPEN;
extern int     FlexMidBand=10;
extern ENUM_MA_METHOD MidBBMethod= MODE_SMA;
extern int HLMinSequence=5;
extern double BalanceRatio=0.5;

//---- indicator buffers
double BBandUPBuff[],  BBandDnBuff[], BBandMainBuff[],  BuyTagBuffer[],   SellTagBuffer[];
double   RsiBuf[],      smRSIBuffer[], SignaLineBuffer[],RsilsTP[],Fade[];
double   gdaBearHL[],   gdaBullHL[],gdaBearOC[],gdaBullOC[];
double   Asist1Buff[],  Asist2Buff[], Asist3Buff[];
double CxrBuff[],  RawBuff[];
double PeakLBuff[],PeakHBuff[];
double EMA3RangeBuff[],EMA3UpBuff[],EMA3DownBuff[],SMMA3Op[],EMA3Cl[],MidBar;
int WhichBar=0,SignalGap;
static datetime PrevTime = Time[0];
//int Vertical_i=0;
string lTWheel,TypeColoredBar;
color clTWheel;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorDigits(Digits);
   IndicatorBuffers(26);
   //if(CTRxColoredBars+StochColoredBars+xSuTrendColoredBars+RSilsColoredBars>1) {CTRxColoredBars=False;StochColoredBars=False; xSuTrendColoredBars=False; RSilsColoredBars=False;}
   SetIndexBuffer(0, gdaBearHL);      SetIndexStyle(0, DRAW_HISTOGRAM);    SetIndexLabel(0,NULL/*"GreenUP"*/);       SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexBuffer(1, gdaBullHL);      SetIndexStyle(1, DRAW_HISTOGRAM);    SetIndexLabel(1,NULL/*"RedDown"*/);       SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexBuffer(2, gdaBearOC);    SetIndexStyle(2, DRAW_HISTOGRAM);            SetIndexLabel(2,NULL/*"GreenUP"*/);                 SetIndexEmptyValue(2,EMPTY_VALUE);
   SetIndexBuffer(3, gdaBullOC);    SetIndexStyle(3, DRAW_HISTOGRAM);            SetIndexLabel(3,NULL/*"RedDown"*/);                 SetIndexEmptyValue(3,EMPTY_VALUE);
   //SetIndexBuffer(11,CxrBuff);         SetIndexStyle(11,DRAW_ARROW);         SetIndexLabel(11,NULL);                    SetIndexArrow(11,82);  
   SetIndexBuffer(4,BBandUPBuff);     SetIndexStyle(4,DRAW_LINE,STYLE_DOT);        SetIndexLabel(4,"UpperB"+BBandPeriod);  SetIndexShift(4,BBandShift);
   SetIndexBuffer(5,BBandDnBuff);     SetIndexStyle(5,DRAW_LINE,STYLE_DOT);        SetIndexLabel(5,"LowerB"+BBandPeriod);  SetIndexShift(5,BBandShift);
   SetIndexBuffer(6,BBandMainBuff);   SetIndexStyle(6,DRAW_LINE,STYLE_DOT);          SetIndexLabel(6,"MainB"+FlexMidBand); SetIndexShift(6,BBandShift);//(BBandPeriod-4
   SetIndexBuffer(7,BuyTagBuffer);    SetIndexStyle(7,DRAW_ARROW);         SetIndexLabel(7,"BuyTag");                SetIndexArrow(7,59);
   SetIndexBuffer(8,SellTagBuffer);   SetIndexStyle(8,DRAW_ARROW);         SetIndexLabel(8,"SellTag");               SetIndexArrow(8,59);
   SetIndexBuffer(9,RsilsTP);         SetIndexStyle(9,DRAW_ARROW);         SetIndexLabel(9,NULL);                    SetIndexArrow(9,159); 
   SetIndexBuffer(10,Fade);            SetIndexStyle(10,DRAW_ARROW);         SetIndexLabel(10,NULL);                    SetIndexArrow(10,251); 
   SetIndexBuffer(11,CxrBuff);         SetIndexStyle(11,DRAW_ARROW);         SetIndexLabel(11,NULL);                    SetIndexArrow(11,82);  
   SetIndexBuffer(12, PeakLBuff);     SetIndexStyle(12,DRAW_ARROW);        SetIndexLabel(12,NULL);                   SetIndexArrow(12,242);   SetIndexEmptyValue(12,EMPTY_VALUE);//"PeakL"
   SetIndexBuffer(13, PeakHBuff);     SetIndexStyle(13,DRAW_ARROW);        SetIndexLabel(13,NULL);                   SetIndexArrow(13,241);   SetIndexEmptyValue(13,EMPTY_VALUE);//"PeakH"
   if(DrawEMA3MidBars!=0){
   SetIndexBuffer(14, EMA3UpBuff);     SetIndexStyle(14,DRAW_ARROW);         SetIndexArrow(14,MidBarsMarker);  SetIndexLabel(14,"MidHLU");      SetIndexEmptyValue(14,EMPTY_VALUE);
   SetIndexBuffer(15, EMA3RangeBuff);  SetIndexStyle(15,DRAW_ARROW);         SetIndexArrow(15,MidBarsMarker);  SetIndexLabel(15,"MidHL~");      SetIndexEmptyValue(15,EMPTY_VALUE);
   SetIndexBuffer(16, EMA3DownBuff);   SetIndexStyle(16,DRAW_ARROW);         SetIndexArrow(16,MidBarsMarker);  SetIndexLabel(16,"MidHLD");      SetIndexEmptyValue(16,EMPTY_VALUE);}
   else {SetIndexLabel(14,NULL);    SetIndexLabel(15,NULL); SetIndexLabel(16,NULL); }
//---------------------Assistant Buffers Only--------------------------------------------------------   
   SetIndexBuffer(17,Asist1Buff);     SetIndexStyle(17,DRAW_NONE);         SetIndexLabel(17,NULL);              
   SetIndexBuffer(18,Asist2Buff);     SetIndexStyle(18,DRAW_NONE);         SetIndexLabel(18,NULL);
   SetIndexBuffer(19,Asist3Buff);     SetIndexStyle(19,DRAW_NONE);         SetIndexLabel(19,NULL);
   SetIndexBuffer(20,RawBuff);        SetIndexStyle(20,DRAW_NONE);         SetIndexLabel(20,NULL);
   SetIndexBuffer(21,SMMA3Op);        SetIndexStyle(21,DRAW_NONE);         SetIndexLabel(21,NULL);
   SetIndexBuffer(22,EMA3Cl);         SetIndexStyle(22,DRAW_NONE);         SetIndexLabel(22,NULL);
   SetIndexBuffer(23,smRSIBuffer);    SetIndexStyle(23,DRAW_NONE);         SetIndexLabel(23,NULL);
   SetIndexBuffer(24,SignaLineBuffer);SetIndexStyle(24,DRAW_NONE);         SetIndexLabel(24,NULL);
   SetIndexBuffer(25,RsiBuf);         SetIndexStyle(25,DRAW_NONE);         SetIndexLabel(25,NULL);
   SetIndexDrawBegin(0,CTRxBarsNumber);   SetIndexDrawBegin(1,CTRxBarsNumber);   SetIndexDrawBegin(2,CTRxBarsNumber);   SetIndexDrawBegin(3,CTRxBarsNumber);   IndicatorShortName("FXR_TripleHybridSR");//FXR_LimitLessCashVariation
   
   if       (ColoredBarMethod==1)   TypeColoredBar="BarCol-RSils"+" "+RSILS_Period+","+ RSILSSmoothPeriod+","+RSILSSignal; 
   else if  (ColoredBarMethod==2)   TypeColoredBar="BarCol-xST"+" "+SuperTrend_Period+"x"+SuperTrend_Multiplier;
   else if  (ColoredBarMethod==3)   TypeColoredBar="BarCol-CTR"+" "+CTRxBarsNumber;
   else if  (ColoredBarMethod==4)   TypeColoredBar="BarCol-Stoch"+" "+KPeriod+Slowing+DPeriod;
   
   //DefineGap();
 
  //---- initialization done

   return(0);
  }
//+------------------------------------------------------------------+
//|Remove Graphic Objects                                               |
//+------------------------------------------------------------------+
int deinit() {
 string name;
  for(int i=ObjectsTotal()-1;i>=0;i--)
    {
     name=ObjectName(i);
     if (StringSubstr(name, 0,4)=="RsiB") ObjectDelete(name);
     }
     ObjectDelete("RSILSBias");
     ObjectDelete("FiboBias");
     ObjectDelete("HybSR1");
     ObjectDelete("HybSR2");
return (0);
}
//+------------------------------------------------------------------+
//| Application of RSILS                                             |
//+------------------------------------------------------------------+
int start()
  {
  //----------------------------------
   double CurrBarStd = 0.0;
   double PrevBarStd = 0.0;
   double PreRawBuf = 0.0;
   double BarsNumLow = 0.0;
   double BarsNumHigh = 0.0;
   double CurrBarMid;
   int    limit,i;
   int    counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;   // last counted bar will be recounted
   limit = MathMin(MaxHistoryBars,Bars - counted_bars);   
     if (limit==1 ) {limit+=5; }//added to refresh chart
   for ( i = 0; i <=limit; i++) {
      BarsNumHigh = High[iHighest(NULL, 0, MODE_HIGH, CTRxBarsNumber, i)];
      BarsNumLow = Low[iLowest(NULL, 0, MODE_LOW, CTRxBarsNumber, i)];
      CurrBarMid = (High[i] + Low[i]) / 2.0;
      CurrBarStd = 0.66 * ((CurrBarMid - BarsNumLow) / (MathMax(BarsNumHigh - BarsNumLow,Point*0.1)) - 0.5) + 0.67 * PrevBarStd;
      CurrBarStd = MathMin(MathMax(CurrBarStd, -0.999), 0.999);//Normalizingg Variance before Natural Log
      RawBuff[i] = MathLog((CurrBarStd + 1.0) / (1 - CurrBarStd)) / 2.0 + /*RawBuff[i+1]/2;*/PreRawBuf / 2.0;
      PrevBarStd = CurrBarStd;
      PreRawBuf = RawBuff[i];
      RsiBuf[i]=iRSI(NULL,0,RSILS_Period,PRICE_CLOSE,i)-50;
   //RawBuff[i]=iCustom(Symbol(),0,"FXR_CTR",21,False,False,False,CtrMaxHistoryBars,6,i);
   //if(RawBuff[i]!=EMPTY_VALUE && RawBuff[i]>0) {CxrBuff[i]=EMPTY_VALUE; CxrDnBuffer[i]=High[i];}
   //else if(RawBuff[i]!=EMPTY_VALUE && RawBuff[i]<0) {CxrBuff[i]=Low[i]; CxrDnBuffer[i]=EMPTY_VALUE;}
   //else      {CxrBuff[i]=EMPTY_VALUE; CxrDnBuffer[i]=EMPTY_VALUE;}
   BBandUPBuff[i]=iBands(NULL,0,BBandPeriod,BBandDev,BBandShift,BBPrice,MODE_UPPER,i);
   BBandDnBuff[i]=iBands(NULL,0,BBandPeriod,BBandDev,BBandShift,BBPrice,MODE_LOWER,i);
   //BBandMainBuff[i]=iBands(NULL,0,BBandPeriod,BBandDev-4,BBandShift,BBPrice,MODE_MAIN,i);   
   
   BBandMainBuff[i]=iMA(NULL,0,FlexMidBand,BBandShift,MidBBMethod,BBPrice,i);   
   SMMA3Op[i] =iMA(NULL,0,3,0,MODE_SMMA,PRICE_OPEN,i+1); 
   EMA3Cl[i] = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,i+1);
   //if      (ColoredBarMethod!=0){
        //if     (Close[i]>Open[i]) {gdaBullHL[i]=High[i]; gdaBearHL[i]=Low[i]; }
        //else if (Close[i]<Open[i]) {gdaBullHL[i]=Low[i]; gdaBearHL[i]=High[i]; }    }
   if (ColoredBarMethod==3)     makeCTRxColoredBar(i);
   else if (ColoredBarMethod==4)    makeStochasticsColoredBar(i);
   else if (ColoredBarMethod==2) makeXsupertrendColoredBar(i,limit); 
   }
// ----- Draw RSILSSignal
   for( i=MathMin(limit+2, Bars - RSILSSmoothPeriod); i>=0; i--){
   //for( i=0; i<=limit+2; i++){  
         smRSIBuffer[i]=iMAOnArray(RsiBuf,Bars,RSILSSmoothPeriod,0,MODE_EMA,i);
         SignaLineBuffer[i]=iMAOnArray(RsiBuf,Bars,RSILSSignal,0,MODE_EMA,i);//}
         if      (ColoredBarMethod==1)   makeRSilsColoredBar(i);  
    if (iRSI(NULL,0,2,PRICE_CLOSE,i)>94 &&SignaLineBuffer[i]>UD_levels) RsilsTP[i]=High[i];
    else if (iRSI(NULL,0,2,PRICE_CLOSE,i)<6 && SignaLineBuffer[i]<-UD_levels) RsilsTP[i]=Low[i];
    else RsilsTP[i]=EMPTY_VALUE;
    if (smRSIBuffer[i]*RsiBuf[i]<0.0 &&RsiBuf[i]<0) Fade[i]=Low[i];
    else if (smRSIBuffer[i]*RsiBuf[i]<0.0 &&RsiBuf[i]>0) Fade[i]=High[i];
    else Fade[i]=EMPTY_VALUE;
     BuyTagBuffer[i]=BuyTagBuffer[i+1];
     SellTagBuffer[i]=SellTagBuffer[i+1];
     if(MathAbs(RsiBuf[i])<UD_levels-1.5){ 
     if(RsiBuf[i+1]>UD_levels-1.5 && High[i+1]>=BBandUPBuff[i+1])  BuyTagBuffer[i]=MathMax(High[i+1],High[i])/*+20*Point*/;
     else if(RsiBuf[i+1]<-UD_levels+1.5&& Low[i+1]<=BBandDnBuff[i+1]) SellTagBuffer[i]=MathMin(Low[i+1],Low[i])/*-20*Point*/;   }               
//if (ColoredBarMethod==1)   makeRSilsColoredBar(i);         
   if      (DrawEMA3MidBars==Show_MidHighLow) MidBar =NormalizeDouble((High[i]+Low[i])/2.0,Digits-1);
   else if (DrawEMA3MidBars==Show_MidOpenClose)   MidBar =NormalizeDouble((Open[i]+Close[i])/2.0,Digits-1);
   //============================================================================================================
   //All  the changes made by // are change the MidNar signal arrow instead of CTR ARROWS
            //double KFull0=iCustom(Symbol(),0,"Colored Stochastic",KPeriod,Slowing,DPeriod,0,1,80,20,"Current time frame",False,1,i);             
            //double KFull1=iCustom(Symbol(),0,"Colored Stochastic",KPeriod,Slowing,DPeriod,0,1,80,20,"Current time frame",False,1,i+1);             
            //double KFull2=iCustom(Symbol(),0,"Colored Stochastic",KPeriod,Slowing,DPeriod,0,1,80,20,"Current time frame",False,1,i+2);             
            //double DFull0=iCustom(Symbol(),0,"Colored Stochastic",KPeriod,Slowing,DPeriod,0,1,80,20,"Current time frame",False,0,i);     
      //Stoc Filter added not in CTR_Trigger yet
      if (EMA3Cl[i+WhichBar]> SMMA3Op[i+WhichBar] && Close[i]>SMMA3Op[i+WhichBar]){
            EMA3UpBuff[i] =MidBar; EMA3DownBuff[i] =EMPTY_VALUE;EMA3RangeBuff[i]=EMPTY_VALUE;
      //if (RawBuff[i]<0 &&EMA3UpBuff[i] !=EMPTY_VALUE && ((KFull0-KFull1)>(KFull1-KFull2)||(KFull0<DFull0))&& KFull0>50) {PeakLBuff[i]=High[i]+CalcArrowsGapDisplay(Period())*10*Point();PeakHBuff[i]=EMPTY_VALUE;}
         }
      else if (EMA3Cl[i+WhichBar]< SMMA3Op[i+WhichBar]&& Close[i]<SMMA3Op[i+WhichBar]){
         EMA3DownBuff[i] =MidBar;EMA3UpBuff[i]=EMPTY_VALUE;EMA3RangeBuff[i]=EMPTY_VALUE;
         //if (RawBuff[i]>0 && EMA3DownBuff[i] !=EMPTY_VALUE&& ((KFull0-KFull1)>(KFull1-KFull2)||(KFull0>DFull0))&& KFull0<50) {PeakHBuff[i]=Low[i]-CalcArrowsGapDisplay(Period())*10*Point();PeakLBuff[i]=EMPTY_VALUE;}
         }       
      else {EMA3RangeBuff[i]=MidBar;  EMA3UpBuff[i]=EMPTY_VALUE;EMA3DownBuff[i]=EMPTY_VALUE; }
            
   //===========ALternative Arrows Made By Midbar Arrows special condition============================================START==============
 double MidLineH=(High[i+1]+Low[i+1])*BalanceRatio*1.00001;
    if (High[i+1]>High[iHighest(NULL,0,MODE_HIGH,HLMinSequence,i+2)]){
    if (Close[i+1]<MidLineH && Open[i+1]<MidLineH)    PeakLBuff[i]=High[i+1]+CalcArrowsGapDisplay(Period())*10*Point();
    }   
    double MidLineL=(High[i+1]+Low[i+1])*BalanceRatio*0.99999;
    if (Low[i+1]<Low[iLowest(NULL,0,MODE_LOW,HLMinSequence,i+2)]){
    if (Close[i+1]>MidLineL && Open[i+1]>MidLineL)    PeakHBuff[i]=Low[i+1]-CalcArrowsGapDisplay(Period())*10*Point();
    }
//===========ALternative Arrows Made By Midbar Arrows special condition================================================END==========
    
     CxrBuff[i]=EMPTY_VALUE;
         if ((Close[i]>=Close[i+1] || Close[i+1]>= Close[i+2])&& (High[i]>=High[i+1] && High[i+1]>= High[i+2])){
            if (RawBuff[i+1]>0 && RawBuff[i+1]<RawBuff[i+2]   && RawBuff[i]<RawBuff[ArrayMinimum(RawBuff,5,i+1)]) CxrBuff[i]=High[i];//+CalcArrowsGapDisplay(Period())*10*Point();
            if (RawBuff[i]<0 && RawBuff[i+1]>0  && RawBuff[i+1]<RawBuff[i+2]) CxrBuff[i]=High[i]+70*Point();
            }
      else if ((Close[i]<=Close[i+1] || Close[i+1]<= Close[i+2])&& (Low[i]<=Low[i+1] && Low[i+1]<= Low[i+2] )){
            if (RawBuff[i+1]<0 && RawBuff[i+1]>RawBuff[i+2] &&RawBuff[i]>RawBuff[ArrayMaximum(RawBuff,5,i+1)])  CxrBuff[i]=Low[i];//-CalcArrowsGapDisplay(Period())*10*Point();
            if (RawBuff[i]>0 && RawBuff[i+1]<0 && RawBuff[i+1]>RawBuff[i+2]) CxrBuff[i]=Low[i]-70*Point();
            }
     
///----------------------------------------                  
    if(TrainingFibo) {
    string tag="Fiber",lFWheel;
    color clFWheel;
   for(int k=0; k<ObjectsTotal();k++)
     {
      string name=ObjectName(k);
      if(ObjectType(name)!=OBJ_FIBO)               { continue; }
      if(StringSubstr(name,0,StringLen(tag))!=tag) { continue; }
      double price0   = ObjectGetDouble(0,name,OBJPROP_PRICE,0),
             price1   = ObjectGetDouble(0,name,OBJPROP_PRICE,1),
             fiblevel70 = ObjectGetDouble(0,name,OBJPROP_LEVELVALUE,6),
             fiblevel88 = ObjectGetDouble(0,name,OBJPROP_LEVELVALUE,7),
             fibprice70 = price1-((price1-price0)*fiblevel70),
             fibprice88 = price1-((price1-price0)*fiblevel88);
             ObjectSet("Fiber",OBJPROP_TIMEFRAMES,OBJ_PERIOD_M1|OBJ_PERIOD_M5|OBJ_PERIOD_M15|OBJ_PERIOD_M30);
      //Comment(DoubleToStr(fibprice70,Digits-1)+" / "+DoubleToStr(fibprice88,Digits-1));
      if (price1>price0) { lFWheel="MarketMakers BUY 76.4%@ "+DoubleToStr(fibprice70,Digits-1)+", 88.6%@ "+DoubleToStr(fibprice88,Digits-1)+", "+DoubleToStr((Close[0]-fibprice70)/(10*Point),0)+" Pips"; clFWheel=clrAquamarine;}
      else { lFWheel="MarketMakers SELL 76.4%@ "+DoubleToStr(fibprice70,Digits-1)+", 88.6%@ "+DoubleToStr(fibprice88,Digits-1)+", "+DoubleToStr((-Close[0]+fibprice70)/(10*Point),0)+" Pips"; clFWheel=clrOrange;}
      ObjectCreate("FiboBias", OBJ_LABEL, 0, 0, 0);
      ObjectSet("FiboBias", OBJPROP_CORNER, 2); //FibLcorner=2;
      ObjectSet("FiboBias", OBJPROP_XDISTANCE, FibLabelX);
      ObjectSet("FiboBias", OBJPROP_YDISTANCE, FibLabelY);
      ObjectSet("FiboBias", OBJPROP_BACK, true);
      ObjectSetText("FiboBias", lFWheel, 35, "Impact", clFWheel);
     }}
 if(CxRAlerts&&Period()>=MinTFAlert)   { 
      //----Update 02/2016 changed the alert to live bar [0] instead of [1]
      
       /*if(PeakLBuff[1]!=EMPTY_VALUE &&CxrDnBuffer[1]!=EMPTY_VALUE &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" CXR & KeyReversal DN High:"+DoubleToStr(High[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
       }
       
       if(PeakLBuff[1]!=EMPTY_VALUE &&CxrBuff[1]!=EMPTY_VALUE &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+TF2Str(Period())+" CXR & KeyReversal UP Low:"+DoubleToStr(Low[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMin(Low[1],Low[0]),Digits-1));
         PrevTime = Time[0];
       } */
      //----Update 02/2016 added to Swquenced CXR 
       if(CxrBuff[2]!=EMPTY_VALUE&&CxrBuff[1]!=EMPTY_VALUE&& CxrBuff[2]>=High[2] &&  CxrBuff[1]>=High[1] &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR CXR 2Sequenced Turn DN High:"+DoubleToStr(High[0],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
       }
      
       if(CxrBuff[2]!=EMPTY_VALUE&&CxrBuff[1]!=EMPTY_VALUE&&CxrBuff[2]<=Low[2] && CxrBuff[1]<=Low[1] &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+TF2Str(Period())+" HybSR CXR 2Sequenced Turn UP Low:"+DoubleToStr(Low[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMin(Low[1],Low[0]),Digits-1));
         PrevTime = Time[0];
       } 
       if(RsilsTP[1]!=EMPTY_VALUE && CxrBuff[1]!=EMPTY_VALUE&&CxrBuff[1]>=High[1] &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR CXR&RSILS-Dot TakeOff LOW!: "+DoubleToStr(Low[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
       }
       
       if(Asist1Buff[1]!=EMPTY_VALUE && CxrBuff[1]!=EMPTY_VALUE&&CxrBuff[1]<=Low[1] &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR CXR&RSILS-Dot LandOff HIGH!: "+DoubleToStr(High[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
      }    
      
    }         
    if(TagLevelsAlert && Period()>=MinTFAlert)   { 
      //----Update 02/2016 changed the alert to live bar [0] instead of [1]
     
       if(High[0]>BuyTagBuffer[1] &&High[1]<=BuyTagBuffer[1] &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR Buy Breakout: "+DoubleToStr(BuyTagBuffer[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
       }
       
       if(Low[0]<SellTagBuffer[1] &&Low[1]>=SellTagBuffer[1] &&PrevTime <  Time[0])
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR Sell Breakout: "+DoubleToStr(SellTagBuffer[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
      }    
       /*if(BuyTagBuffer[1]!=BuyTagBuffer[2] &&PrevTime <  Time[0] && MathAbs(Bid-BuyTagBuffer[1])<200*Point)//New filter to avoid empty value alert
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR New Level Buy: "+DoubleToStr(BuyTagBuffer[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
       }
       
       if(SellTagBuffer[1]!=SellTagBuffer[2] &&PrevTime <  Time[0]&& MathAbs(Bid-BuyTagBuffer[1])<200*Point)//New filter to avoid empty value alert
       {
         Alert(Symbol()+" "+ TF2Str(Period())+" HybSR New Level Sell: "+DoubleToStr(SellTagBuffer[1],Digits-1)+"/"+DoubleToStr(Bid,Digits-1));//DoubleToStr(MathMax(High[0],High[1]),Digits-1));
         PrevTime = Time[0];
      }  */ 
      
    }         
                  string txt1=DoubleToStr(BuyTagBuffer[1],Digits-1)+" HSR "+DoubleToStr(SellTagBuffer[1],Digits-1);
                  string txt2=DoubleToStr((Bid-BuyTagBuffer[1])/(10*Point),0)+"   HSR   "+DoubleToStr((SellTagBuffer[1]-Bid)/(10*Point),0)+"  "+TypeColoredBar;
               
                  if (PrintTriggers)   ShowTrigges(txt1,txt2);
                  }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
string TF2Str(int iPeriod) {
  if (iPeriod==0 || iPeriod==PERIOD_CURRENT) iPeriod=Period();
  switch(iPeriod) {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN1");
    default: return("M"+iPeriod);
  }
}


   
  int CalcArrowsGapDisplay (int a) {
   switch (a) {
   case 1:     return (3);
   case 5:     return (6);
   case 15:    return (8);
   case 30:    return (9);
   case 60:    return (11);
   case 240:   return (13);
   case 1440:  return (16);
   case 10080: return (20);
   case 43200: return (23);
   }
   return (0);
}
//------------------------------------------
bool ShowTrigges(string a,string b)
      {
      color TrigerColor;
      if (Bid>BuyTagBuffer[1] && Bid>SellTagBuffer[1]) {TrigerColor=clrLime;}
      else if (Bid<SellTagBuffer[1] && Bid<BuyTagBuffer[1]) {TrigerColor=clrOrange;}
      else {TrigerColor=clrGray;}//clrYellow
      a="Buy "+a+" Sell";
//      double yWindow =300;// WindowPriceMin()+(WindowPriceMax() - WindowPriceMin()) / 2; 
      ObjectCreate("HybSR1", OBJ_LABEL, 0, 0, 0);
      ObjectSet("HybSR1", OBJPROP_CORNER, 1);
      ObjectSet("HybSR1", OBJPROP_XDISTANCE, PTriggerX);
      ObjectSet("HybSR1", OBJPROP_YDISTANCE, PTriggerY); //300
      ObjectSet("HybSR1", OBJPROP_BACK, true);
      ObjectSetText("HybSR1", a, 10, "Arial Bold", TrigerColor);
      ObjectCreate("HybSR2", OBJ_LABEL, 0, 0, 0);
      ObjectSet("HybSR2", OBJPROP_CORNER, 1);
      ObjectSet("HybSR2", OBJPROP_XDISTANCE, StringLen(a)/2+StringLen(b)-25);
      ObjectSet("HybSR2", OBJPROP_YDISTANCE, PTriggerY+20);//320
      ObjectSet("HybSR2", OBJPROP_BACK, true);
      ObjectSetText("HybSR2", b, 10, "Arial Bold", TrigerColor);
      return (True);
      }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////
void makeXsupertrendColoredBar(int i,int limit){
     
   for( i=0; i<=limit; i++) {
   if (i>=Bars-1) continue;
    double dAtr = iATR(NULL, 0, SuperTrend_Period, i);
    double dUpperLevel=(High[i]+Low[i])/2+SuperTrend_Multiplier*dAtr;
    double dLowerLevel=(High[i]+Low[i])/2-SuperTrend_Multiplier*dAtr;
    
    // Set supertrend levels
    if (Close[i]>Asist3Buff[i+1] && Close[i+1]<=Asist3Buff[i+1]) {
      Asist3Buff[i]=dLowerLevel;
    }
    else if (Close[i]<Asist3Buff[i+1] && Close[i+1]>=Asist3Buff[i+1]) {
      Asist3Buff[i]=dUpperLevel;
    }
    else if (Asist3Buff[i+1]<dLowerLevel)
        Asist3Buff[i]=dLowerLevel;
    else if (Asist3Buff[i+1]>dUpperLevel)
        Asist3Buff[i]=dUpperLevel;
    else
      Asist3Buff[i]=Asist3Buff[i+1];
    
    // Draw Candles
    if (Close[i]>Asist3Buff[i] || (Close[i]==Asist3Buff[i] && Close[i+1]>Asist3Buff[i+1])) {
      gdaBullHL[i]=High[i];
      gdaBearHL[i]=Low[i]; 
      gdaBullOC[i]=MathMax(Open[i],Close[i]);
      gdaBearOC[i]=MathMin(Open[i],Close[i]);
      /*if (Close[i]>Open[i]) {
        Asist1Buff[i]=Open[i];
        Asist2Buff[i]=Close[i];
      }
      else {
        Asist1Buff[i]=Close[i];
        Asist2Buff[i]=Open[i];
      }*/
    }
    else if (Close[i]<Asist3Buff[i] || (Close[i]==Asist3Buff[i] && Close[i+1]<Asist3Buff[i+1])) {
      gdaBearHL[i]=High[i];   
      gdaBullHL[i]=Low[i];    
      gdaBearOC[i]=MathMax(Open[i],Close[i]);
      gdaBullOC[i]=MathMin(Open[i],Close[i]);
      /*if (Close[i]>Open[i]) {
        Asist1Buff[i]=Close[i];
        Asist2Buff[i]=Open[i];
      }
      else {
        Asist1Buff[i]=Open[i];
        Asist2Buff[i]=Close[i];
      }*/
    }
  }}
////////////////////////////////////////////////////////////////////////////////////
void makeStochasticsColoredBar(int i){
  //---------------------------------- if (StocColoredBars && !RsiLSColoredBars){
      Asist1Buff[i]=iCustom(Symbol(),0,"Colored Stochastic",KPeriod,Slowing,DPeriod,StoMaMethod,PriceField,80,20,"Current time frame",False,1,i);             
      Asist2Buff[i]=iCustom(Symbol(),0,"Colored Stochastic",KPeriod,Slowing,DPeriod,StoMaMethod,PriceField,80,20,"Current time frame",False,0,i);           
    if      ( (Asist2Buff[i]>80 && Asist1Buff[i]>80) || (Asist1Buff[i]>Asist2Buff[i] && Asist1Buff[i]>20))     {
            gdaBearHL[i]=Low[i]; gdaBullHL[i]=High[i];
            gdaBullOC[i]=MathMax(Open[i],Close[i]);
            gdaBearOC[i]=MathMin(Open[i],Close[i]);}
    else if ( (Asist2Buff[i]<20 && Asist1Buff[i]<20) || (Asist1Buff[i]<Asist2Buff[i] && Asist1Buff[i]<80))     {
            gdaBearHL[i]=High[i]; gdaBullHL[i]=Low[i];
            gdaBearOC[i]=MathMax(Open[i],Close[i]);
            gdaBullOC[i]=MathMin(Open[i],Close[i]);}
    else {  gdaBearHL[i]=EMPTY_VALUE;gdaBullHL[i]=EMPTY_VALUE;
            gdaBearOC[i]=EMPTY_VALUE; gdaBullOC[i]=EMPTY_VALUE;}
    }
////////////////////////////////////////////////////////////////////////////////////
void makeCTRxColoredBar(int i){            
            if (RawBuff[i]>0)          {
               gdaBearHL[i]=Low[i];    gdaBullHL[i]=High[i];
               gdaBullOC[i]=MathMax(Open[i],Close[i]);
               gdaBearOC[i]=MathMin(Open[i],Close[i]);}
            else if(RawBuff[i]<0)   {
            gdaBullHL[i]=Low[i]; gdaBearHL[i]=High[i];   
             gdaBearOC[i]=MathMax(Open[i],Close[i]);
            gdaBullOC[i]=MathMin(Open[i],Close[i]); }
            else   {gdaBearHL[i]=EMPTY_VALUE;gdaBullHL[i]=EMPTY_VALUE;
             gdaBearOC[i]=EMPTY_VALUE; gdaBullOC[i]=EMPTY_VALUE;}
    }   
/////NEW VERSION///////////////////////////////////////////////////////////////////////////////
void makeRSilsColoredBar(int i){            
     if( RsiBuf[i]>smRSIBuffer[i] && SignaLineBuffer[i]>0.67*smRSIBuffer[i] /*&&SignaLineBuffer[i]>=SignaLineBuffer[i+1]*/ ){
               gdaBearHL[i]=Low[i];gdaBullHL[i]=High[i];      
               gdaBullOC[i]=MathMax(Open[i],Close[i]);
               gdaBearOC[i]=MathMin(Open[i],Close[i]);}  
     else if( RsiBuf[i]<smRSIBuffer[i] && SignaLineBuffer[i]<0.67*smRSIBuffer[i] /*&&SignaLineBuffer[i]<=SignaLineBuffer[i+1]*/ ){
               gdaBearHL[i]=High[i];gdaBullHL[i]=Low[i]; 
               gdaBearOC[i]=MathMax(Open[i],Close[i]);
               gdaBullOC[i]=MathMin(Open[i],Close[i]); }
     else      {gdaBearHL[i]=EMPTY_VALUE;gdaBullHL[i]=EMPTY_VALUE;
               gdaBearOC[i]=EMPTY_VALUE; gdaBullOC[i]=EMPTY_VALUE;}
     }
     
     ////////////////////////////////////////////////////////////////////////////////////
/*void makeRSilsColoredBar(int i){            
     if( RsiBuf[i]>smRSIBuffer[i] && SignaLineBuffer[i]>0){
               gdaBearHL[i]=Low[i];gdaBullHL[i]=High[i];      
               gdaBullOC[i]=MathMax(Open[i],Close[i]);
               gdaBearOC[i]=MathMin(Open[i],Close[i]);}  
     else if( RsiBuf[i]<smRSIBuffer[i] && SignaLineBuffer[i]<0)
               {
               gdaBearHL[i]=High[i];gdaBullHL[i]=Low[i]; 
               gdaBearOC[i]=MathMax(Open[i],Close[i]);
               gdaBullOC[i]=MathMin(Open[i],Close[i]); }
     else      {gdaBearHL[i]=EMPTY_VALUE;gdaBullHL[i]=EMPTY_VALUE;
               gdaBearOC[i]=EMPTY_VALUE; gdaBullOC[i]=EMPTY_VALUE;}
     }*/
