LR_R2_Trend_Sqz





//+------------------------------------------------------------------+
//|                                                  LR R2 trend.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 Green
#property indicator_color2 DarkGreen
#property indicator_color3 Maroon
#property indicator_color4 Red
#property indicator_color5 SlateBlue
#property indicator_color6 DimGray
#property indicator_color7 Yellow

#property indicator_width1 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 1
#property indicator_width7 1

//
//
//
//
//

extern int    Length           = 34;
extern int    AppliedPrice     = PRICE_CLOSE;

extern int       BBSqzPrd = 20;
extern double    SqzSigLevel = 0.00004;//0;

int       bolPrd  = 20;  
double    bolDev  = 2.0;
int       keltPrd = 20;
double    keltFactor= 1.5;


extern string TimeFrame       = "current timeframe";
extern string note_TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-currentTF";





//
//
//
//
//

double Buffer[];
double LRSBuffer[];
double LRSBufferUa[];
double LRSBufferUb[];
double LRSBufferDa[];
double LRSBufferDb[];
double RSQBuffer[];
double BBSqBuffer[];

//
//
//
//
//

int    timeFrame;
double Trending;
string IndicatorFileName;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorDigits(5);
   IndicatorBuffers(8);
   SetIndexBuffer(0,LRSBufferUa); SetIndexLabel(0,"LR up");
   SetIndexBuffer(1,LRSBufferUb); SetIndexLabel(1,"LR weak up");
   SetIndexBuffer(2,LRSBufferDa); SetIndexLabel(2,"LR down");
   SetIndexBuffer(3,LRSBufferDb); SetIndexLabel(3,"LR weak down");
   SetIndexBuffer(4,Buffer);      SetIndexLabel(4,"no trend");
   SetIndexBuffer(5,LRSBuffer);   SetIndexLabel(5,"LR regression slope");
   SetIndexBuffer(6,BBSqBuffer);  SetIndexLabel(6,"BB Squeeze");
   SetIndexBuffer(7,RSQBuffer);

   for (int i = 0; i < 4; i++) SetIndexStyle(i,DRAW_HISTOGRAM);
            SetIndexStyle(4,DRAW_ARROW);
            SetIndexArrow(4,158);
            SetIndexStyle(6,DRAW_ARROW);
            SetIndexArrow(6,158);
   
   //
   //
   //
   //
   //
  
   timeFrame = stringToTimeFrame(TimeFrame);
   IndicatorShortName("LRR2Trend "+TimeFrameToString(timeFrame)+" ("+Length+")");
   IndicatorFileName = WindowExpertName();
   Trending = 3.858889/Length;
   return(0);
}

//
//
//
//
//

int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{

   int  counted_bars=IndicatorCounted();
   int  i,limit;

   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
       limit=Bars-counted_bars;

  // //

   //
   //
   //
   
   if (timeFrame != Period())
   {
      datetime TimeArray[];
         limit = MathMax(limit,timeFrame/Period());
         ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,timeFrame);
         
         //
         //
         //
         //
         //
         
         for(i=0,int y=0; i<limit; i++)
         {
            if(Time[i]<TimeArray[y]) y++;
            LRSBufferUa[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,0,y);
            LRSBufferUb[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,1,y);
            LRSBufferDa[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,2,y);
            LRSBufferDb[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,3,y);
            Buffer[i]      = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,4,y);
            LRSBuffer[i]   = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,5,y);
            BBSqBuffer[i]  = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,BBSqzPrd,SqzSigLevel,6,y);

          }
       return(0);         
     }
         
   //
   //
   //
   //
   //
 
   double diff,d,dPrev, std,bbs;

   for (i=limit;i>=0;i--)
   {
      
     
      bolPrd  =BBSqzPrd;
      keltPrd =  BBSqzPrd;    
      diff = iATR(NULL,0,keltPrd,i)*keltFactor;
		std = iStdDev(NULL,0,bolPrd,MODE_SMA,0,PRICE_CLOSE,i);
//  	bbs = bolDev * std / diff;   //!zero divide!
      if (diff != 0)
          bbs = bolDev * std / diff;
      else  bbs = 0;  
     
     
      if(bbs<1) 
        {

        if (LRSBuffer[i]>0)
            BBSqBuffer[i]= SqzSigLevel;
        else
            BBSqBuffer[i]= -SqzSigLevel;
         } 
      else 
            {
            BBSqBuffer[i]=EMPTY_VALUE;
            }
  
   //
   //
   //
 
      
      LRSBuffer[i] = LinearRegressionSlope(Length,i);
      RSQBuffer[i] = RSquared(Length,i);
      
      //
      //
      //
      //
      //

      Buffer[i]      = EMPTY_VALUE;
      LRSBufferUa[i] = EMPTY_VALUE;
      LRSBufferUb[i] = EMPTY_VALUE;
      LRSBufferDa[i] = EMPTY_VALUE;
      LRSBufferDb[i] = EMPTY_VALUE;
 
         if (RSQBuffer[i] < Trending) { Buffer[i] = 0.00; continue; }
         if (LRSBuffer[i] > 0)
         {
            if (LRSBuffer[i] > LRSBuffer[i+1]) { LRSBufferUa[i] = LRSBuffer[i]; LRSBufferUb[i] = EMPTY_VALUE; }
            if (LRSBuffer[i] < LRSBuffer[i+1]) { LRSBufferUb[i] = LRSBuffer[i]; LRSBufferUa[i] = EMPTY_VALUE; }                     
         }
         if (LRSBuffer[i] < 0)
         {
            if (LRSBuffer[i] < LRSBuffer[i+1]) { LRSBufferDa[i] = LRSBuffer[i]; LRSBufferDb[i] = EMPTY_VALUE; }
            if (LRSBuffer[i] > LRSBuffer[i+1]) { LRSBufferDb[i] = LRSBuffer[i]; LRSBufferDa[i] = EMPTY_VALUE; }                     
         }
 




        


  }

   //
   //
   //
   //
   //


 
   
   for(i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Length); 


   return(0);
}


//---------------------------------------------------------------------------------|
//                                                                                 |
//---------------------------------------------------------------------------------|
//
//
//
//
//

int    lrs.period=EMPTY_VALUE;
double lrs.sumX;
double lrs.sumXSqr;
double lrs.divisor;

//
//
//
//
//

double LinearRegressionSlope(int len,int shift)
{
   double LinearRegSlope;
   double SumXY = 0;
   double SumY  = 0;

   //
   //
   //
   //
   //

   if (lrs.period != len)
   {
      lrs.period  = len;
      lrs.sumX    = lrs.period * (lrs.period-1) / 2;
      lrs.sumXSqr = lrs.period * (lrs.period-1) * (2 * lrs.period - 1) / 6;
      lrs.divisor = MathPow(lrs.sumX,2) - lrs.period * lrs.sumXSqr;
   }

   //
   //
   //
   //
   //

   for (int i=0; i<lrs.period; i++)
   {
      double price = iMA(NULL,0,1,0,MODE_EMA,AppliedPrice,i+shift);
            SumXY += i*price;
            SumY  +=   price;
   }
   if( lrs.divisor != 0 ) 
         	LinearRegSlope = (lrs.period * SumXY - lrs.sumX * SumY)/lrs.divisor;
   else     LinearRegSlope = 0; 
   return  (LinearRegSlope);
}


//---------------------------------------------------------------------------------|
//                                                                                 |
//---------------------------------------------------------------------------------|
//
//
//
//
//

double rs.len = EMPTY_VALUE;
double rs.SumX;
double rs.SumY;
double rs.SumX2;
double rs.SumY2;
double rs.SumXY;
double rs.SumR;

//
//
//
//
//

double RSquared(int Len,int shift)
{
   double RSquared;
   double price;
   double Q1;
   double Q2;
   double Q3;
   int    i;


   if (rs.len != Len)
      {
         rs.len = Len;
         rs.SumX  = 0; for(i=0;i<=Len-1;i++) rs.SumX  += i+1;
         rs.SumX2 = 0; for(i=0;i<=Len-1;i++) rs.SumX2 += (i+1)*(i+1);
      }         
   
      rs.SumY  = 0;
      rs.SumY2 = 0;
      rs.SumXY = 0;
      
      //
      //
      //
      //
      //
      
      for(i=0;i<Len;i++)
         {
            price = iMA(NULL,0,1,0,MODE_EMA,AppliedPrice,i+shift);
                rs.SumY  +=         price;
                rs.SumY2 += MathPow(price,2);
                rs.SumXY +=   (i+1)*price;
         }             
         Q1 = rs.SumXY - rs.SumX*rs.SumY/Len;
         Q2 = rs.SumX2 - rs.SumX*rs.SumX/Len;
         Q3 = rs.SumY2 - rs.SumY*rs.SumY/Len;
         
         //
         //
         //
         //
         //
         
         if( Q2*Q3 != 0 ) RSquared = Q1*Q1/(Q2*Q3);
         else             RSquared = 0; 







   return(RSquared);
}


//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringTrimLeft(StringTrimRight(StringUpperCase(tfs)));
         if (tfs=="M1" || tfs=="1M" ||tfs=="1" )              tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5M" ||tfs=="5" )              tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15M"||tfs=="15")              tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30M"||tfs=="30")              tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="1H" ||tfs=="60" ||  tfs=="H" )  tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="4H" ||tfs=="240")               tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1D" ||tfs=="1440" || tfs=="D")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="1W" ||tfs=="10080"|| tfs=="W")  tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="MN1"||tfs=="43200"|| tfs=="1MN")tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}
string TimeFrameToString(int tf)
{
   string tfs="";
   
   if (tf!=Period())
      switch(tf) {
         case PERIOD_M1:  tfs="M1"  ; break;
         case PERIOD_M5:  tfs="M5"  ; break;
         case PERIOD_M15: tfs="M15" ; break;
         case PERIOD_M30: tfs="M30" ; break;
         case PERIOD_H1:  tfs="H1"  ; break;
         case PERIOD_H4:  tfs="H4"  ; break;
         case PERIOD_D1:  tfs="D1"  ; break;
         case PERIOD_W1:  tfs="W1"  ; break;
         case PERIOD_MN1: tfs="MN1";
      }
   return(tfs);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
         char = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
         else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:


Implements a curve of type DRAW_HISTOGRAM
Implements a curve of type DRAW_ARROW

Indicators Used:


Indicator of the average true range
Standard Deviation indicator
Moving average indicator


Custom Indicators Used:
IndicatorFileName

Order Management characteristics:

Other Features: