//+------------------------------------------------------------------+
//|                                  Bands Envelopes Know Sure Thing |
//|                                      Copyright 2023 @ Shumer3000 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2023"
#property link      "https://www.mql5.com/ru/users/shumer3000"
#property description "BEKST"
#property strict
//-----
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1  Blue 
#property indicator_color2  Red  
#property indicator_color3  Blue 
#property indicator_color4  Red   
#property indicator_color5  Blue 
#property indicator_color6  Red  
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1 
//---- input parameters
extern int f1=9;          //fast period ROC1
extern int f2=12;         //fast period ROC2
extern int f3=18;         //fast period ROC3
extern int f4=24;         //fast period ROC4
extern int s1=26;         //slow period ROC1..3
extern int s2=39;         //slow period ROC4
extern int fp=13;         //fast period filter
extern int sp=5;          //slow period filter
extern int mp=13;         //period MA
extern double dev1=0.6;   //KST deviation
extern double dev2=2.6;   //BAND deviation
extern bool al=false;     //Alert ON/OFF
//---- buffers
double KST[];
double UKST[];
double DKST[];
double HKST[];
double LKST[];
double SHKST[];
double SLKST[];
double MA1[];
double MA2[];
double MA3[];
double MA4[];
double EMA1[];
double EMA2[];
double EMA3[];
double EMA4[];
double SU[];
double SD[];
//----
double MyPoint,hdev,ldev,d11,d20,d21,d30,d31,ma0,ma1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
   IndicatorShortName("BEKST("+(string)dev1+","+(string)dev2+")");
   IndicatorBuffers(17);
   SetIndexBuffer(0,UKST);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexLabel(0,"UKST");
   SetIndexBuffer(1,DKST);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexLabel(1,"DKST");
   SetIndexBuffer(2,SHKST);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexLabel(2,"SHKST");
   SetIndexBuffer(3,SLKST);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexLabel(3,"SLKST");
   SetIndexBuffer(4,SU);
   SetIndexStyle(4,DRAW_ARROW,0,1);
   SetIndexArrow(4,233);
   SetIndexBuffer(5,SD);
   SetIndexStyle(5,DRAW_ARROW,0,1);
   SetIndexArrow(5,234);
   SetIndexBuffer(6,KST);
   SetIndexBuffer(7,MA1);
   SetIndexBuffer(8,MA2);
   SetIndexBuffer(9,MA3);
   SetIndexBuffer(10,MA4);
   SetIndexBuffer(11,EMA1);
   SetIndexBuffer(12,EMA2);
   SetIndexBuffer(13,EMA3);
   SetIndexBuffer(14,EMA4);
   SetIndexBuffer(15,HKST);
   SetIndexBuffer(16,LKST);
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()  {
   return(0);
}
//+------------------------------------------------------------------+
//| Ticks Flow Index                                                 |
//+------------------------------------------------------------------+
double TFI(int tftf, int lperiod, int speriod, int shift, double& stf)  
  {
   double tu,td,ltf;
   stf=0;
   for(int j=speriod-1; j>=0; j--) {
     double ltu=0,ltd=0,wsum=0;
     for(int y=lperiod+shift; y>shift; y--) {
        tu=(iVolume(NULL,tftf,y+j)+(iClose(NULL,tftf,y+j)-iOpen(NULL,tftf,y+j))/Point)/2;
        td=iVolume(NULL,tftf,y+j)-tu;
        ltu+=tu*(y-shift); 
        ltd+=td*(y-shift);
        wsum+=y-shift; 
     }
     ltf=(ltu-ltd)/wsum; 
     stf+=ltf;
   }
   stf/=speriod; 
   return(ltf);
  }
//+------------------------------------------------------------------+
//| Bands Envelopes Know Sure Thing                                  |
//+------------------------------------------------------------------+
int start() {
   int i,counted_bars=IndicatorCounted(); 
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   int limit=Bars-counted_bars-1;
   //----
   for(i=limit; i>=0; i--) {
      MA1[i]=iMA(NULL,0,f1,0,MODE_EMA,PRICE_CLOSE,i);
      MA2[i]=iMA(NULL,0,f2,0,MODE_EMA,PRICE_CLOSE,i);
      MA3[i]=iMA(NULL,0,f3,0,MODE_EMA,PRICE_CLOSE,i);
      MA4[i]=iMA(NULL,0,f4,0,MODE_EMA,PRICE_CLOSE,i);
   }   
   //----
   for(i=limit; i>=0; i--) {
      EMA1[i]=iMAOnArray(MA1,0,s1,0,MODE_EMA,i);
      EMA2[i]=iMAOnArray(MA2,0,s1,0,MODE_EMA,i);
      EMA3[i]=iMAOnArray(MA3,0,s1,0,MODE_EMA,i);
      EMA4[i]=iMAOnArray(MA4,0,s2,0,MODE_EMA,i);
      KST[i]=(EMA1[i]+2*EMA2[i]+3*EMA3[i]+4*EMA4[i])/10; 
   }
   //----
   for(i=limit; i>=0; i--) {
      if(i>=Bars-2*s2) continue;
      int pb=0;
      double rh=0.0, rl=0.0;
      for(int j=s2;j>0;j--) 
         if(Volume[i+j]<Volume[i+j+1])  {
            hdev=High[i+j]-KST[i+j];
            ldev=KST[i+j]-Low[i+j]; 
            rh+=MathPow(hdev,2);
            rl+=MathPow(ldev,2);
            pb++;
         } 
      rh=MathSqrt(rh/pb);
      rl=MathSqrt(rl/pb);
      HKST[i]=KST[i]+rh*dev2; 
      LKST[i]=KST[i]-rl*dev2; 
   } 
   //----
   for(i=limit; i>=0; i--) {
      if(i>=Bars-2*s2) continue;
      UKST[i]=KST[i]*(1+dev1/1000); 
      DKST[i]=KST[i]*(1-dev1/1000); 
      SHKST[i]=iMAOnArray(HKST,0,s2,0,MODE_SMMA,i);
      SLKST[i]=iMAOnArray(LKST,0,s2,0,MODE_SMMA,i);
   }
   //----
   ObjectDelete("Price_Up2");
   ObjectCreate("Price_Up2",OBJ_ARROW_RIGHT_PRICE,0,TimeCurrent(),SHKST[0]);
   ObjectSet("Price_Up2",OBJPROP_COLOR,clrBlue);
   ObjectDelete("Price_Up1");
   ObjectCreate("Price_Up1",OBJ_ARROW_RIGHT_PRICE,0,TimeCurrent(),UKST[0]);
   ObjectSet("Price_Up1",OBJPROP_COLOR,clrBlue);
   ObjectDelete("Price_Dn1");
   ObjectCreate("Price_Dn1",OBJ_ARROW_RIGHT_PRICE,0,TimeCurrent(),DKST[0]);
   ObjectDelete("Price_Dn2");
   ObjectCreate("Price_Dn2",OBJ_ARROW_RIGHT_PRICE,0,TimeCurrent(),SLKST[0]);
   //----
   if(iBars(NULL,5)<fp+sp) return(0);
   for(i=limit; i>=0; i--) {
      if(i>=Bars-2*s2) continue;
      d30=TFI(5,fp,sp,i,d31);
      d20=TFI(1,fp,sp,i,d21);
      TFI(1,fp,sp,i+2,d11);
      ma0=iMA(NULL,0,mp,0,MODE_LWMA,PRICE_CLOSE,i);
      ma1=iMA(NULL,0,mp,0,MODE_LWMA,PRICE_CLOSE,i+2);
      //----
      if(d30>d31&&d20>d21&&d21>d11) 
         if((SLKST[i+1]<=SLKST[i]&&Close[i+1]<UKST[i+1]&&Close[i]>UKST[i]&&ma1<ma0)||                        //Up High line Up Envelopes
            (SLKST[i+2]>SLKST[i]&&ma1<SLKST[i+1]&&ma0>SLKST[i])||                                            //Up Low line Down Bands
            (SHKST[i+2]>SHKST[i]&&DKST[i+2]>DKST[i]&&Close[i+1]<DKST[i+1]&&Close[i]>DKST[i]&&ma0<DKST[i])) { //Up Low line Down Envelopes
            SU[i]=Low[i]-5*Point; 
            if(al) Alert("Buy Order or close the sale");
         }
      if(d30<d31&&d20<d21&&d21<d11)  
         if((SHKST[i+1]>=SHKST[i]&&Close[i+1]>DKST[i+1]&&Close[i]<DKST[i]&&ma1>ma0)||                        //Down Low line Down Envelopes
            (SHKST[i+2]<SHKST[i]&&ma1>SHKST[i+1]&&ma0<SHKST[i])||                                            //Down High line Up Bands
            (SLKST[i+2]<SLKST[i]&&UKST[i+2]<UKST[i]&&Close[i+1]>UKST[i+1]&&Close[i]<UKST[i]&&ma0>UKST[i])) { //Down High line Up Envelopes
            SD[i]=High[i]+5*Point; 
            if(al) Alert("Sell Order or close your purchases");
         }
   }
   //----
   return(0);
}
//+------------------------------------------------------------------+
             
            
            
            
Comments