//+-------------------------------+
//|KG Price Action BS LSMA.mq4    |
//|Opreked by wiedstone@yahoo.com |
//+-------------------------------+
//---- indicator settings
#property  indicator_separate_window
#property  indicator_minimum 0
#property  indicator_maximum 1
#property  indicator_buffers 6
#property  indicator_color1  Blue
#property  indicator_color2  Red
#property  indicator_color3  White
#property  indicator_color4  DarkSlateGray
#property  indicator_width1 2
#property  indicator_width2 2
#property  indicator_width3 2
#property  indicator_width4 2

int    LSMA_Period;
extern int Applied_Price   =0;

double     TU[];
double     TD[];
double     SR[];
double     SC[];
double     lsma[];
double     bs[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,2);
   SetIndexStyle(2,DRAW_HISTOGRAM,EMPTY,2);
   SetIndexStyle(3,DRAW_HISTOGRAM,EMPTY,2);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   
   SetIndexBuffer(0,TU);
   SetIndexBuffer(1,TD);
   SetIndexBuffer(2,SR);
   SetIndexBuffer(3,SC);
   SetIndexBuffer(4,lsma);
   SetIndexBuffer(5,bs);
   getPeriod();
   return(0);
  }
//+------------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator                               |
//+------------------------------------------------------------------+
int start()
  {
   int    i,limit;
   int    counted_bars=IndicatorCounted();
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   for(i=0; i<limit; i++)
      lsma[i]=3.0*iMA(NULL,0,LSMA_Period,0,MODE_LWMA,Applied_Price,i)-2.0*iMA(NULL,0,LSMA_Period,0,MODE_SMA,Applied_Price,i);
   for(i=0; i<limit; i++)
      bs[i]=( iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, LSMA_Period, i)) + iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, LSMA_Period, i))  
             )/2;
   for(i=limit-1; i>=0; i--)
     {
      if(lsma[i] > bs[i] || lsma[i] < bs[i] && Close[i] > lsma[i] && Close[i] > bs[i]) { TU[i] = 1; TD[i] =0; SR[i] = 0; SC[i] = 0; }//TU
      if(lsma[i] < bs[i] || lsma[i] > bs[i] && Close[i] < lsma[i] && Close[i] < bs[i]) { TU[i] = 0; TD[i] =1; SR[i] = 0; SC[i] = 0; }//TD
      if(lsma[i] > bs[i] && Close[i] < lsma[i] && Close[i] > bs[i] && lsma[i]  < lsma[i+1]) { TU[i] = 0; TD[i] =0; SR[i] = 1; SC[i] = 0; }//SR-D
      if(lsma[i] < bs[i] && Close[i] > lsma[i] && Close[i] < bs[i] && lsma[i]  > lsma[i+1]) { TU[i] = 0; TD[i] =0; SR[i] = 1; SC[i] = 0; }//SR-U
      if(lsma[i] > bs[i] && Close[i] < lsma[i] && Close[i] > bs[i] && lsma[i]  > lsma[i+1]) { TU[i] = 0; TD[i] =0; SR[i] = 0; SC[i] = 1; }//SC-U
      if(lsma[i] < bs[i] && Close[i] > lsma[i] && Close[i] < bs[i] && lsma[i]  < lsma[i+1]) { TU[i] = 0; TD[i] =0; SR[i] = 0; SC[i] = 1; }//SC-D
      string text;
      if( TU[0] == 1 && TD[0] == 0 && SR[0] == 0 && SC[0] == 0 )text="Trend Up";
      if( TU[0] == 0 && TD[0] == 1 && SR[0] == 0 && SC[0] == 0 )text="Trend Down";
      if( TU[i] == 0 && TD[i] == 0 && SR[0] == 1 && SC[0] == 0 )text="Sideway Retracement";
      if( TU[i] == 0 && TD[i] == 0 && SR[0] == 0 && SC[0] == 1 )text="Sideway Continuation";
      IndicatorShortName("Price Condition BS LSMA Weekly ("+text+")");
     }
   //---- done
   return(0);
  }
//+------------------------------------------------------------------+

void getPeriod()
{
   switch(Period()) 
      {
         case 1: 
            LSMA_Period=7200;
            break;
         case 5: 
            LSMA_Period=1440;
            break;
         case 15: 
            LSMA_Period=480;
            break;
         case 30: 
            LSMA_Period=240;
            break;
         case 60: 
            LSMA_Period=120;
            break;
         case 240: 
            LSMA_Period=30;
            break;
         case 1440: 
            LSMA_Period=5;
            break;
         case 10080: 
            LSMA_Period=1;
            break;
         case 43200:
            LSMA_Period=0;
            break;
         
      }
 }