//+------------------------------------------------------------------+
//|                                                          SOP.mq4 |
//|  Created by Patriot                                                                |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_maximum 5

extern int Bar = 1;
extern bool DisplayAlert = false;

double AngleOfLSMA, BBBand, BSBBand, SBBand, LSMADaily1, LSMADaily;
int PriceOnLSMA; // Possible Value 1 = top, 2 = equal, 3 = bottom
int PriceOnBand; // Possible Value 1 = >top, 2 = Top-Mid, 3 = Mid-Bottom, 4 = < Bottom
int LSMAAngle; // Possible Value 1 = up, 2 = flat, 3= down
int Trend,OldTrend; // Possible Value 1 = Trending Up, 2 = Wait, 3 = Trending Down;
string TextPriceOnLSMA, TextLSMA, TextBand, TextTrend;
int LSMAPeriod,LSMAPrice=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   getPeriod();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //int    counted_bars=IndicatorCounted();
//----
   
   
   
//---- Mencari posisi harga terhadap LSMA

   LSMADaily = 3.0*iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,Bar)-2.0*iMA(NULL,0,LSMAPeriod,0,MODE_SMA,LSMAPrice,Bar);
   LSMADaily1 = 3.0*iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,Bar+1)-2.0*iMA(NULL,0,LSMAPeriod,0,MODE_SMA,LSMAPrice,Bar+1);
   
   if (iClose(Symbol(),0,Bar) > LSMADaily)
      {
         PriceOnLSMA = 1;
         TextPriceOnLSMA = "Above";
      }
   if (iClose(Symbol(),0,Bar) < LSMADaily)
      {
         PriceOnLSMA = 3;
         TextPriceOnLSMA = "Below";       
      }
   if (iClose(Symbol(),0,Bar) == LSMADaily)
      {
         PriceOnLSMA = 2;
         TextPriceOnLSMA = "Equal";        
      }
   
   // LSMA Angle
   
   
   if (LSMADaily > LSMADaily1)
      {
         TextLSMA = "UP";
         LSMAAngle = 1;
      }
   if (LSMADaily< LSMADaily1)
      {
         TextLSMA = "DOWN";
         LSMAAngle = 3;
      }
   if ((LSMADaily == LSMADaily1 ))
      {
         TextLSMA = "Flat";
         LSMAAngle =2;
      }      
   
   
   // Balance Band
   
   BBBand = iCustom(Symbol(),0,"KG Daily BS Bands",0,Bar);
   BSBBand = iCustom(Symbol(),0,"KG Daily BS Bands",2,Bar);
   SBBand = iCustom(Symbol(),0,"KG Daily BS Bands",1,Bar);
   
   if (iClose(Symbol(),0,Bar) >= BBBand) // > top
      {
         TextBand = "Above BB";
         PriceOnBand = 1;
      }
   
   if ((iClose(Symbol(),0,Bar) < BBBand) && iClose(Symbol(),0,Bar) > BSBBand) // Top-Mid
      {
         TextBand = "BB - BSB";
         PriceOnBand = 2;
      }
   
   if ((iClose(Symbol(),0,Bar) < BSBBand) && iClose(Symbol(),0,Bar) > SBBand) // Mid-Bottom
      {
         TextBand = "BSB - SB";
         PriceOnBand = 3;
      }
   
   if (iClose(Symbol(),0,Bar) <= SBBand) // < bottom
   {
         TextBand = "Below SB";
         PriceOnBand = 4;
      }
   
         
  //--------- Analisa Trend ----------------//
  
  // Angle LSMA UP
  
  if (LSMAAngle ==1)
  {
      // Buy if Price above LSMA and Above SB
      
      if (PriceOnLSMA <=2 && PriceOnBand <=3)
         {
            Trend = 1;
            TextTrend = "Trending UP";
         }
      else 
         {
            TextTrend = "Wait";  
            Trend = 2;
         }
  }
  
  // Angle LSMA Down
  if (LSMAAngle ==3)
  {
      // Sell if Price Below LSMA and Below BB
      
      if (PriceOnLSMA >=2 && PriceOnBand >=2)
         {
            TextTrend = "Trending DOWN";
            Trend = 3;
         }
      else 
         {
            TextTrend = "Wait";  
            Trend = 2;
         }
  }
  
  if (LSMAAngle ==2)
  {
      // LSMA above BB      
      if (LSMADaily >= BBBand)
      {
         // If Price above BB Buy
         if (PriceOnLSMA <= 2)
            {
               TextTrend = "Trending UP";
               Trend = 1;
            }
            
         if (PriceOnLSMA == 3 && PriceOnBand == 1)
            {
               TextTrend = "Wait";
               Trend = 2;
            }
         else 
            {
               TextTrend = "Trending DOWN";
               Trend = 3;
            }
      
      }
      
      // LSMA between BB and SM
      if (LSMADaily < BBBand && LSMADaily > SBBand)
      {
         if (PriceOnLSMA == 1)
            {
               TextTrend = "Trending UP";
               Trend = 1;
            }
         if (PriceOnLSMA == 2)
            {
               TextTrend = "Wait";
               Trend = 2;
            }
         if (PriceOnLSMA == 3)
            {  
               TextTrend = "Trending Down";
               Trend = 3;
            }
      
      }
      
      // LSMA below BB
      if (LSMADaily < SBBand)
      {
         if (PriceOnLSMA >=2)
            {
               TextTrend = "Trending Down";
               Trend = 3;
            }
      
         if (PriceOnLSMA == 1 && PriceOnBand == 4)
            {
               TextTrend = "Wait";
               Trend = 2;
            }
         else 
            {
               TextTrend = "Buy";
               Trend = 1;
            }
      }
  }
  
  if (OldTrend != Trend && DisplayAlert == true)
  {
      Alert ( Symbol() + " is " + TextTrend);
      OldTrend = Trend;
  }
  

  
  //Print (LSMADaily);       
  //Print (PriceOnLSMA);
  ObjectCreate("POL1", OBJ_LABEL, WindowFind("SOP"), 0, 0);
  ObjectSetText("POL1","Price - LSMA:", 10, "Arial", CadetBlue);
  ObjectSet("POL1", OBJPROP_CORNER, 0);
  ObjectSet("POL1", OBJPROP_XDISTANCE, 25);
  ObjectSet("POL1", OBJPROP_YDISTANCE, 5);
        
            
  ObjectCreate("POL2", OBJ_LABEL, WindowFind("SOP"), 0, 0);
  ObjectSetText("POL2",TextPriceOnLSMA,10, "Arial", DarkOrange);
  ObjectSet("POL2", OBJPROP_CORNER, 0);
  ObjectSet("POL2", OBJPROP_XDISTANCE, 115);
  ObjectSet("POL2", OBJPROP_YDISTANCE, 5);
  
  ObjectCreate("LSMAA1", OBJ_LABEL, WindowFind("SOP"), 0, 0);
  ObjectSetText("LSMAA1","LSMA Angle:", 10, "Arial", CadetBlue);
  ObjectSet("LSMAA1", OBJPROP_CORNER, 0);
  ObjectSet("LSMAA1", OBJPROP_XDISTANCE, 200);
  ObjectSet("LSMAA1", OBJPROP_YDISTANCE, 5);
        
            
  ObjectCreate("LSMAA2", OBJ_LABEL, WindowFind("SOP"), 0, 0);
  ObjectSetText("LSMAA2",TextLSMA,10, "Arial", DarkOrange);
  ObjectSet("LSMAA2", OBJPROP_CORNER, 0);
  ObjectSet("LSMAA2", OBJPROP_XDISTANCE, 290);
  ObjectSet("LSMAA2", OBJPROP_YDISTANCE, 5);
  
  ObjectCreate("LSMA1", OBJ_LABEL, WindowFind("SOP"), 0, 0);
  ObjectSetText("LSMA1","Price - Daily Band:", 10, "Arial", CadetBlue);
  ObjectSet("LSMA1", OBJPROP_CORNER, 0);
  ObjectSet("LSMA1", OBJPROP_XDISTANCE, 410);
  ObjectSet("LSMA1", OBJPROP_YDISTANCE, 5);
        
            
  ObjectCreate("LSMA2", OBJ_LABEL, WindowFind("SOP"), 0, 0);
  ObjectSetText("LSMA2",TextBand,10, "Arial", DarkOrange);
  ObjectSet("LSMA2", OBJPROP_CORNER, 0);
  ObjectSet("LSMA2", OBJPROP_XDISTANCE, 530);
  ObjectSet("LSMA2", OBJPROP_YDISTANCE, 5);
  
  ObjectCreate("Trend", OBJ_LABEL, WindowFind("SOP"), 0, 0);
    if (Trend == 1)
      ObjectSetText("Trend",TextTrend,10, "Arial", Aqua);
  if (Trend == 2)
      ObjectSetText("Trend",TextTrend,10, "Arial", Yellow);
  if (Trend == 3)
      ObjectSetText("Trend",TextTrend,10, "Arial", Red);
  
  ObjectSet("Trend", OBJPROP_CORNER, 0);
  ObjectSet("Trend", OBJPROP_XDISTANCE, 650);
  ObjectSet("Trend", OBJPROP_YDISTANCE, 5);
//----
   return(0);
  }
//+------------------------------------------------------------------+


void getPeriod()
{
   switch(Period()) 
      {
         case 1: 
            LSMAPeriod=1440;
            break;
         case 5: 
            LSMAPeriod=288;
            break;
         case 15: 
            LSMAPeriod=96;
            break;
         case 30: 
            LSMAPeriod=48;
            break;
         case 60: 
            LSMAPeriod=24;
            break;
         case 240: 
            LSMAPeriod=6;
            break;
         case 1440: 
            LSMAPeriod=0;
            break;
         case 10080: 
            LSMAPeriod=0;
            break;
         case 43200:
            LSMAPeriod=0;
            break;
         
      }
 }

