//+------------------------------------------------------------------+
//|                                                   SweetSpots.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//|                         Modifications made by unknown            |
//|                                                                  |
//|  see this thread:                                                |
//|       http://www.forexfactory.com/showthread.php?t=246430        |
//|                                                                  |
//+------------------------------------------------------------------+
//|                                                 31st July 2013   |
//|                                                                  |
//|       Modifications made by RaptorUK,  note:  all mods marked    |
//|                        RaptorUK                                  |
//|                                                                  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright "Copyright Shimodax"
#property link      "http://www.strategybuilderfx.com"

#property indicator_chart_window

/* Introduction:

   This indicator shows lines at sweet spots (50 and 100 
   pips levels). It is recommended to turn off the grid.
   
   Enjoy!

   Markus
*/

extern int NumLinesAboveBelow= 100;
extern int SweetSpotMainLevels= 1000;
extern color LineColorMain= Blue;
extern int LineStyleMain= STYLE_SOLID;
extern bool ShowSubLevels= true;
extern int sublevels= 500;
extern color LineColorSub= Blue;
extern int LineStyleSub= STYLE_DOT;
extern int ShiftLine = 15;
extern color TextColor = Orange;




//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}

int deinit()
{
   int obj_total= ObjectsTotal();
   
   for (int i= obj_total; i>=0; i--) {
      string name= ObjectName(i);
    
      if (StringSubstr(name,0,11)=="[SweetSpot]") 
         ObjectDelete(name);
   }
   
   return(0);
}
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   static datetime timelastupdate= 0;
   static datetime lasttimeframe= 0;
   static datetime Bar1Time;          // added by RaptorUK
    
   // no need to update these buggers too often   
//   if (CurTime()-timelastupdate < 6 && Period()==lasttimeframe)  // removed by RaptorUK

   // added by RaptorUK
   //  update once per bar
   if(Bar1Time == Time[1])
      return (0);
   
   Bar1Time = Time[1];
   // end of  added by RaptorUK
   
   
//   deinit();  // delete all previous lines    // removed by RaptorUK
      
   int i, ssp1, style, ssp, thickness; //sublevels= 50;
   double ds1;
   color linecolor;
   
   if (!ShowSubLevels)
      sublevels*= 2;
   
   ssp1= Bid / Point;
   ssp1= ssp1 - ssp1%sublevels;

   for (i= -NumLinesAboveBelow; i<NumLinesAboveBelow; i++) {
   
      ssp= ssp1+(i*sublevels); 
      
      if (ssp%SweetSpotMainLevels==0) {
         style= LineStyleMain;
         linecolor= LineColorMain;
      }
      else {
         style= LineStyleSub;
         linecolor= LineColorSub;
      }
      
      thickness= 1;
      
      if (ssp%(SweetSpotMainLevels*10)==0) {
         thickness= 2;      
      }

      if (ssp%(SweetSpotMainLevels*100)==0) {
         thickness= 3;      
      }
      
      ds1= ssp*Point;
      SetLevel(DoubleToStr(ds1,Digits), ds1,  linecolor, style, thickness, Time[10]);
   }

   return(0);
}


//+------------------------------------------------------------------+
//| Helper                                                           |
//+------------------------------------------------------------------+
void SetLevel(string text, double level, color col1, int linestyle, int thickness, datetime startofday)
{
   int digits= Digits;
   string linename= "[SweetSpot] " + text + " Line",
          pricelabel; 

   // create or move the horizontal line   
   if (ObjectFind(linename) != 0) {

      ObjectCreate(linename+"Text", OBJ_TEXT, 0, Time[0]+(ShiftLine+5)*Period()*60, level);
      ObjectSetText(linename+"Text", DoubleToStr(level, Digits), 8, "Arial", TextColor);

      ObjectCreate(linename, OBJ_TREND, 0, Time[0]+ShiftLine*Period()*60, level, Time[0]+(ShiftLine+1)*Period()*60, level);
      ObjectSet(linename, OBJPROP_STYLE, linestyle);
      ObjectSet(linename, OBJPROP_COLOR, col1);
      ObjectSet(linename, OBJPROP_WIDTH, thickness);
      
      ObjectSet(linename, OBJPROP_BACK, True);
   }
   else {
      ObjectMove(linename, 0, Time[0]+ShiftLine*Period()*60, level);
      ObjectMove(linename, 1, Time[0]+(ShiftLine+1)*Period()*60, level);
      ObjectMove(linename+"Text", 0, Time[0]+(ShiftLine+5)*Period()*60, level);
      
//      ObjectMove(linename, 0, Time[1], level);   // removed by RaptorUK
//      ObjectMove(linename, 1, Time[0], level);   // removed by RaptorUK
   }
}
      