//+------------------------------------------------------------------+
//|                                                     LSMA nrp.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property  copyright "copyleft mladen (2012)"
#property  link      "mladenfx@gmail.com"

// Mods for User-friendliness Jan 2016, May 2017 by Big Be

#property indicator_chart_window
#property indicator_buffers 5
//#property strict

/*
#property indicator_color1 LightGray
#property indicator_color2 DodgerBlue
#property indicator_color3 DodgerBlue
#property indicator_color4 DeepPink
#property indicator_color5 DeepPink
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
*/

//
//
//
//
//

extern int    LSMAPeriod   = 9;
extern int    LSMAPrice    =  0;
//
//
extern color UpColor = BlanchedAlmond;
//White;
extern color DnColor = Indigo;
 //Black;
extern int   LineWidth = 2;
//
//
extern string  p = "-- LSMAPrice Types--   0 = Close";
extern string  p1 = " 1 = Open,  2 = High,  3 = Low";
extern string  p4 = " 4 = Median (High+Low)/2";
extern string  p5 = " 5 = Typical (High+Low+Close)/3";
extern string  p6 = " 6 = Weighted (H+L+Close+Close)/4";
extern string  nrp = "This is non-re-painting.";
extern string  note1 = "Mods by Big Be 2016, 2017";
//
//
//
//
//
//

double lsma[];
double lsmaUPa[];
double lsmaUPb[];
double lsmaDNa[];
double lsmaDNb[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   IndicatorDigits(Digits+1);
    
   SetIndexBuffer(0,lsma);     /* SetIndexLabel(0, NULL);*/ SetIndexStyle(0, DRAW_NONE);
   SetIndexLabel(0, "LSMA_2_Colors ("+LSMAPeriod+")");
   SetIndexBuffer(1,lsmaUPa);  SetIndexStyle(1,DRAW_LINE,EMPTY,LineWidth, UpColor); //SetIndexDrawBegin(1,HalfLength);
      SetIndexLabel(1,"UP");
         
   SetIndexBuffer(2,lsmaUPb);  SetIndexStyle(2,DRAW_LINE,EMPTY,LineWidth, UpColor); //SetIndexDrawBegin(2,HalfLength);  
      SetIndexLabel(2,NULL);
         
   SetIndexBuffer(3,lsmaDNa); SetIndexStyle(3,DRAW_LINE,EMPTY,LineWidth, DnColor); //SetIndexDrawBegin(1,HalfLength);
      SetIndexLabel(3,"DOWN");
   
   SetIndexBuffer(4,lsmaDNb); SetIndexStyle(4,DRAW_LINE,EMPTY,LineWidth, DnColor); //SetIndexDrawBegin(2,HalfLength); 
      SetIndexLabel(4,NULL);
   
   IndicatorShortName("LSMA_2_Colors ("+LSMAPeriod+")");
   return(0);
}

//
//
//
//
//

int start()
{ 
   int      counted_bars=IndicatorCounted();
   int      limit,i;

   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

   if (lsma[limit] > lsma[limit+1]) CleanPoint(limit,lsmaUPa,lsmaUPb);
   if (lsma[limit] < lsma[limit+1]) CleanPoint(limit,lsmaDNa,lsmaDNb);
   
   for(i = limit; i >= 0; i--)
   {
      lsma[i]   = 3.0*iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,i)-2.0*iMA(NULL,0,LSMAPeriod,0,MODE_SMA,LSMAPrice,i);
      lsmaUPa[i] = EMPTY_VALUE;
      lsmaUPb[i] = EMPTY_VALUE;
      lsmaDNa[i] = EMPTY_VALUE;
      lsmaDNb[i] = EMPTY_VALUE;
      
      if (lsma[i] >= lsma[i+1]) PlotPoint(i,lsmaUPa,lsmaUPb,lsma);
      if (lsma[i] < lsma[i+1]) PlotPoint(i,lsmaDNa,lsmaDNb,lsma);
   }   
   return(0);
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}