//+------------------------------------------------------------------+
//|                                     past regression deviated.mq4 |
//|                Copyright © 2006, tageiger, aka fxid10t@yahoo.com |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, tageiger, aka fxid10t@yahoo.com"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 5


//---- input parameters
double lsma[];
double lsmaua[];
double lsmaub[];
double lsmada[];
double lsmadb[];

int    LSMAPrice    =  0;

int period=0;

int Periode7=0;   
int Periode6=0;   
int Periode5=0;   
int Periode4=0;   
int Periode3=0;   
int Periode2=0;   
int Periode1=0;   

extern int Corner=1;
extern int font_size=8;
extern int X2=120;
extern int X3=125;
extern int Y_space=10;

extern color LSMA_Year_Color=BlueViolet;
extern color LSMA_4Month_Color=SandyBrown;
extern color LSMA_Month_Color=Aqua;
extern color LSMA_Week_Color=Magenta;
extern color LSMA_Day_Color=Lime;
extern color LSMA_8H_Color=Yellow;
extern color LSMA_4H_Color=Blue;

string ss1  = "KG LSMA nYear sop";
string ss2  = "KG LSMA n4Month sop";
string ss3  = "KG LSMA nMonth sop";
string ss4  = "KG LSMA nWeek sop";
string ss5  = "KG LSMA nDay sop";
string ss6  = "KG LSMA n8H sop";
string ss7  = "KG LSMA n4H sop";

string sss1  = "KG bband tnYear sop";
string sss2  = "KG bband tn4Month sop";
string sss3  = "KG bband tnMonth sop";
string sss4  = "KG bband tnWeek sop";
string sss5  = "KG bband tnDay sop";
string sss6  = "KG bband tn8H sop";
string sss7  = "KG bband tn4H sop";

      int start.bar=0, end.bar=0;
      
string sinyal;

int init()  {
   getPeriod4H();
   getPeriod8H();
   getPeriod1D();   
   getPeriod1W();   
   getPeriod1M();   
   getPeriod4M();   
   getPeriod1Y();  
   
   SetIndexBuffer(0,lsma);
   SetIndexBuffer(1,lsmaua);
   SetIndexBuffer(2,lsmaub);
   SetIndexBuffer(3,lsmada);
   SetIndexBuffer(4,lsmadb);   
   
   string label2 = "KG LSMA Year sop";
   ObjectDelete(label2);
   ObjectCreate( label2, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label2,"Yearly LSMA",font_size, "Arial Bold", LSMA_Year_Color);
   ObjectSet( label2, OBJPROP_CORNER, Corner );
   ObjectSet( label2, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label2, OBJPROP_YDISTANCE, Y_space*2 );
   
   string label3 = "KG LSMA 4Month sop";
   ObjectDelete(label3);
   ObjectCreate( label3, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label3,"4Monthly LSMA",font_size, "Arial Bold", LSMA_4Month_Color);
   ObjectSet( label3, OBJPROP_CORNER, Corner );
   ObjectSet( label3, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label3, OBJPROP_YDISTANCE, Y_space*3 );
   
   string label4 = "KG LSMA Month sop";
   ObjectDelete(label4);
   ObjectCreate( label4, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label4,"Monthly LSMA",font_size, "Arial Bold", LSMA_Month_Color);
   ObjectSet( label4, OBJPROP_CORNER, Corner );
   ObjectSet( label4, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label4, OBJPROP_YDISTANCE, Y_space*4 );
   
   string label5 = "KG LSMA Week sop";
   ObjectDelete(label5);
   ObjectCreate( label5, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label5,"Weekly LSMA",font_size, "Arial Bold", LSMA_Week_Color);
   ObjectSet( label5, OBJPROP_CORNER, Corner );
   ObjectSet( label5, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label5, OBJPROP_YDISTANCE, Y_space*5 );
   
   string label6 = "KG LSMA Day sop";
   ObjectDelete(label6);
   ObjectCreate( label6, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label6,"Daily LSMA",font_size, "Arial Bold", LSMA_Day_Color);
   ObjectSet( label6, OBJPROP_CORNER, Corner );
   ObjectSet( label6, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label6, OBJPROP_YDISTANCE, Y_space*6 );
   
   string label7 = "KG LSMA 8H sop";
   ObjectDelete(label7);
   ObjectCreate( label7, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label7,"8H LSMA",font_size, "Arial Bold", LSMA_8H_Color);
   ObjectSet( label7, OBJPROP_CORNER, Corner );
   ObjectSet( label7, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label7, OBJPROP_YDISTANCE, Y_space*7 );

   string label8 = "KG LSMA 4H sop";
   ObjectDelete(label8);
   ObjectCreate( label8, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label8,"4H LSMA",font_size, "Arial Bold", LSMA_4H_Color);
   ObjectSet( label8, OBJPROP_CORNER, Corner );
   ObjectSet( label8, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label8, OBJPROP_YDISTANCE, Y_space*8 );
   
/*   string label9 = "KG LSMA 15M";
   ObjectDelete(label9);
   ObjectCreate( label9, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(label9,"15M LSMA",font_size, "Arial Bold", LSMA_15M_Color);
   ObjectSet( label9, OBJPROP_CORNER, Corner );
   ObjectSet( label9, OBJPROP_XDISTANCE, 3 );
   ObjectSet( label9, OBJPROP_YDISTANCE, 90 );
*/
   return(0);
}

int deinit() 
{
ObjectsDeleteAll(0,OBJ_LABEL);
}

int start() {

      start.bar=Periode7;
                                                panah(ss7,X2,Y_space*8); bband(sss7, LSMA_4H_Color, X3, Y_space*8);
      start.bar=Periode6;
                                             panah(ss6,X2,Y_space*7); bband(sss6, LSMA_8H_Color, X3, Y_space*7);
      start.bar=Periode5;
                                          panah(ss5,X2,Y_space*6); bband(sss5, LSMA_Day_Color, X3, Y_space*6);
      start.bar=Periode4;
                                       panah(ss4,X2,Y_space*5); bband(sss4, LSMA_Week_Color, X3, Y_space*5);
      start.bar=Periode3;
                                    panah(ss3,X2,Y_space*4); bband(sss3, LSMA_Month_Color, X3, Y_space*4);
      start.bar=Periode2;
                                 panah(ss2,X2,Y_space*3); bband(sss2, LSMA_4Month_Color, X3, Y_space*3);
      start.bar=Periode1;
                              panah(ss1,X2,Y_space*2); bband(sss1, LSMA_Year_Color, X3, Y_space*2);





   return(0);
}
//+------------------------------------------------------------------+
void bband(string nama_label, color warna, int l, int m)
{
       double hi = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, start.bar, 0));
       double lo = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, start.bar, 0));                          
   string hasilnya;
   double band3=(hi+lo)/2; //Middle
   double band1=(hi+band3)/2;//Upper
   double band2=(lo+band3)/2;//Lower
   
   if (Bid>band1) hasilnya=">Upper";
   if (Bid<band1 && Bid>band3) hasilnya=">Middle";
   if (Bid<band3 && Bid>band2) hasilnya="<Middle";
   if (Bid<band2) hasilnya="<Lower";

   ObjectDelete(nama_label);
   ObjectCreate(nama_label, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(nama_label, hasilnya ,font_size, "Arial Bold", warna);
   ObjectSet(nama_label, OBJPROP_CORNER, Corner );
   ObjectSet(nama_label, OBJPROP_XDISTANCE, l+15 );
   ObjectSet(nama_label, OBJPROP_YDISTANCE, m );      
   
}
//+------------------------------------------------------------------+
void panah(string sign1, int xx,int yy)
{
   lsma();
   if (sinyal=="flat") { string Sign="ó"; color Col=Yellow; } //Sideway
   else 
      if (sinyal=="up") { Sign="ñ"; Col=Lime; } //Trend Up
      else if (sinyal=="dn") { Sign="ò"; Col=Red; } //Trend Down 
   ObjectDelete(sign1);
   ObjectCreate(sign1, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(sign1, Sign ,font_size+2, "Wingdings", Col);
   ObjectSet(sign1, OBJPROP_CORNER, Corner );
   ObjectSet(sign1, OBJPROP_XDISTANCE, xx );
   ObjectSet(sign1, OBJPROP_YDISTANCE, yy );
}

//+------------------------------------------------------------------+
void getPeriod4H()
{
   switch(Period()) 
      {
         case 1: 
            Periode7=240;
            break;
         case 5: 
            Periode7=48;
            break;
         case 15: 
            Periode7=16;
            break;
         case 30: 
            Periode7=8;
            break;
         case 60: 
            Periode7=4;
            break;
         case 240: 
            Periode7=1;
            break;
         case 1440: 
            Periode7=0;
            break;
         case 10080: 
            Periode7=0;
            break;
         case 43200: 
            Periode7=0;
            break;
         
      }
}
void getPeriod8H()
{
   switch(Period()) 
      {
         case 1: 
            Periode6=480;
            break;
         case 5: 
            Periode6=96;
            break;
         case 15: 
            Periode6=32;
            break;
         case 30: 
            Periode6=16;
            break;
         case 60: 
            Periode6=8;
            break;
         case 240: 
            Periode6=2;
            break;
         case 1440: 
            Periode6=0;
            break;
         case 10080: 
            Periode6=0;
            break;
         case 43200: 
            Periode6=0;
            break;
         
      }
 }
void getPeriod1D()
{
   switch(Period()) 
      {
         case 1: 
            Periode5=1440;
            break;
         case 5: 
            Periode5=288;
            break;
         case 15: 
            Periode5=96;
            break;
         case 30: 
            Periode5=48;
            break;
         case 60: 
            Periode5=24;
            break;
         case 240: 
            Periode5=6;
            break;
         case 1440: 
            Periode5=1;
            break;
         case 10080: 
            Periode5=0;
            break;
         case 43200: 
            Periode5=0;
            break;
         
      }
}
void getPeriod1W()
{
   switch(Period()) 
      {
         case 1: 
            Periode4=7200;
            break;
         case 5: 
            Periode4=1440;
            break;
         case 15: 
            Periode4=480;
            break;
         case 30: 
            Periode4=240;
            break;
         case 60: 
            Periode4=120;
            break;
         case 240: 
            Periode4=30;
            break;
         case 1440: 
            Periode4=5;
            break;
         case 10080: 
            Periode4=1;
            break;
         case 43200: 
            Periode4=0;
            break;
         
      }
 }
void getPeriod1M()
{
   switch(Period()) 
      {
         case 1: 
            Periode3=28800;
            break;
         case 5: 
            Periode3=5760;
            break;
         case 15: 
            Periode3=1920;
            break;
         case 30: 
            Periode3=960;
            break;
         case 60: 
            Periode3=480;
            break;
         case 240: 
            Periode3=120;
            break;
         case 1440: 
            Periode3=20;
            break;
         case 10080: 
            Periode3=4;
            break;
         case 43200: 
            Periode3=1;
            break;
         
      }
 }
void getPeriod4M()
{
   switch(Period()) 
      {
         case 1: 
            Periode2=115200;
            break;
         case 5: 
            Periode2=23040;
            break;
         case 15: 
            Periode2=7680;
            break;
         case 30: 
            Periode2=3840;
            break;
         case 60: 
            Periode2=1920;
            break;
         case 240: 
            Periode2=480;
            break;
         case 1440: 
            Periode2=80;
            break;
         case 10080: 
            Periode2=16;
            break;
         case 43200: 
            Periode2=4;
            break;
         
      }
 }
void getPeriod1Y()
{
   switch(Period()) 
      {
         case 1: 
            Periode1=345600;
            break;
         case 5: 
            Periode1=69120;
            break;
         case 15: 
            Periode1=23040;
            break;
         case 30: 
            Periode1=11520;
            break;
         case 60: 
            Periode1=5760;
            break;
         case 240: 
            Periode1=1440;
            break;
         case 1440: 
            Periode1=240;
            break;
         case 10080: 
            Periode1=48;
            break;
         case 43200: 
            Periode1=12;
            break;
         
      }
 }
void lsma()
{ 
   int      LSMAPeriod=start.bar;
   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,lsmaua,lsmaub);
   if (lsma[limit] < lsma[limit+1]) CleanPoint(limit,lsmada,lsmadb);
   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);
      lsmaua[i] = EMPTY_VALUE;
      lsmaub[i] = EMPTY_VALUE;
      lsmada[i] = EMPTY_VALUE;
      lsmadb[i] = EMPTY_VALUE;
         if (lsma[i] > lsma[i+1]) PlotPoint(i,lsmaua,lsmaub,lsma);
         if (lsma[i] < lsma[i+1]) PlotPoint(i,lsmada,lsmadb,lsma);
   }   
   
   double  l=lsma[0];
   double ll=lsma[1];
   double l1=lsmaua[0];
   double l2=lsmaub[0];
   double l3=lsmada[0];
   double l4=lsmadb[0];

   sinyal="flat";
   if ( l>ll && l1==l && (l3==EMPTY_VALUE && l4==EMPTY_VALUE))
   {sinyal="up";
   }
   if ( l<ll && l3==l && (l1==EMPTY_VALUE && l2==EMPTY_VALUE))
   {sinyal="dn";
   }
   if ( l1==l && (l3==EMPTY_VALUE && l4==EMPTY_VALUE))
   {sinyal="up";
   }
   if ( l3==l && (l1==EMPTY_VALUE && l2==EMPTY_VALUE))
   {sinyal="dn";
   }
}

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;
      }
}