LRS_line_mtf





//+------------------------------------------------------------------+
//|                                                     LR trend.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
//mod2008 LRs mtf ki  based on LR trend
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Green

#property indicator_width1 2


//
//
//
//
//

extern int    Length           = 14;
extern int    AppliedPrice     = PRICE_CLOSE;
extern string TimeFrame        = "current time frame";
extern string     TimeFrames = "M1-MN as in MT4 periodicity toolbar or: M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-currentTF";
extern string     note_Price = "0C 1O 2H 3L 4Md 5Tp 6WghC, Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";

//
//
//
//
//

double LRSBuffer[];


//
//
//
//
//

int    timeFrame;
double Trending;
string IndicatorFileName;

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

int init()
{
   IndicatorDigits(5);
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE);   
   SetIndexBuffer(0,LRSBuffer);   SetIndexLabel(0,"LR  slope");
   
   //
   //
   //
   //
   //
  
   timeFrame = stringToTimeFrame(TimeFrame);
   IndicatorShortName("LRS ("+Length+") "+TimeFrameToString(timeFrame)+" ");
   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++;
            LRSBuffer[i]   = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,0,y);
         }
      return(0);         
   }
         
   //
   //
   //
   //
   //
   
   for (i=limit;i>=0;i--)
   {
      LRSBuffer[i] = LinearRegressionSlope(Length,i);
   }   
      //
      //
      //
      //
      //

   

   
   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;

//
//
//
//
//



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

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringTrimLeft(StringTrimRight(StringUpperCase(tfs)));
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") 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_LINE


Indicators Used:


Moving average indicator


Custom Indicators Used:
IndicatorFileName

Order Management characteristics:

Other Features: