//+-------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-station.com"
//+-------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  OrangeRed

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern int Price         = PRICE_CLOSE;
extern int PriceShift    = 0;
extern int EmaPeriod     = 10;
extern int HighLowPeriod = 100;
extern int LwmaPeriod    = 6;
extern int LsmaPeriod    = 15;

double bhi[];
double blo[];
double beh[];
double sig[];
double trend[];
string indicatorFileName;
bool   returnBars;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,bhi); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,blo); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,beh);
   SetIndexBuffer(3,sig);
   SetIndexBuffer(4,trend);
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
   IndicatorShortName(timeFrameToString(timeFrame)+" Behgozin Strength Finder ("+PriceShift+","+EmaPeriod+","+HighLowPeriod+","+LwmaPeriod+","+LsmaPeriod+")");
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double work[];
int start()
{
   int i,r,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            int limit=Bars-counted_bars;
            if (returnBars) { bhi[0] = limit+1; return(0); }
            if (timeFrame!=Period())
            {
               limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
               for (i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,timeFrame,Time[i]);               
                     trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Price,PriceShift,EmaPeriod,HighLowPeriod,LwmaPeriod,LsmaPeriod,4,y);
                     if (trend[i] ==  1) { bhi[i] = High[i]; blo[i] = Low[i]; }
                     if (trend[i] == -1) { blo[i] = High[i]; bhi[i] = Low[i]; }
               }
               return(0);                  
            }
            if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);

   //
   //
   //
   //
   //
           
   for(i=limit, r=Bars-i-1; i>=0; i--,r++)
   {
      if ((i+PriceShift)>=0)
      {
         double price = iMA(NULL,0,1,0,MODE_SMA,Price,i+PriceShift);
         double ema   = iEma(price,EmaPeriod,r);
            if (ema != 0)
                  work[r] = 100.0*(price-ema)/ema;
            else  work[r] = 0;    
         double max = work[r];
         double min = work[r];
         for (int k=1; k<HighLowPeriod && (r-k)>=0; k++)
         {
            max = MathMax(max,work[r-k]);
            min = MathMin(min,work[r-k]);
         }
         beh[i] = iLwma(work[r]*(max-min),LwmaPeriod,r);
         sig[i] = iLinr(beh[i],LsmaPeriod,r);
         trend[i] = trend[i+1];
            if (beh[i]>sig[i]) trend[i] =  1;
            if (beh[i]<sig[i]) trend[i] = -1;
            if (trend[i] ==  1) { bhi[i] = High[i]; blo[i] = Low[i]; }
            if (trend[i] == -1) { blo[i] = High[i]; bhi[i] = Low[i]; }
      }
   }
   return(0);         
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workEma[][1];
double iEma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workEma,0)!= Bars) ArrayResize(workEma,Bars);

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workLwma[][1];
double iLwma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workLwma,0)!= Bars) ArrayResize(workLwma,Bars);
   
   //
   //
   //
   //
   //
   
   workLwma[r][instanceNo] = price;
      double sumw = period;
      double sum  = period*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workLinr[][1];
double iLinr(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workLinr,0)!= Bars) ArrayResize(workLinr,Bars);

   //
   //
   //
   //
   //
   
      period = MathMax(period,1);
      workLinr[r][instanceNo] = price;
         double lwmw = period; double lwma = lwmw*price;
         double sma  = price;
         for(int k=1; k<period && (r-k)>=0; k++)
         {
            double weight = period-k;
                   lwmw  += weight;
                   lwma  += weight*workLinr[r-k][instanceNo];  
                   sma   +=        workLinr[r-k][instanceNo];
         }             
   
   return(3.0*lwma/lwmw-2.0*sma/period);
}


//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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);
}