//+------------------------------------------------------------------+
//|                                                   SweetSpots.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//| SweetSpotsGOLD_TRO_MODIFIED_VERSION                              |
//| MODIFIED BY AVERY T. HORTON, JR. AKA THERUMPLEDONE@GMAIL.COM     |
//| I am NOT the ORIGINAL author 
//  and I am not claiming authorship of this indicator. 
//  All I did was modify it. I hope you find my modifications useful.|
//|                                                                  |
//+------------------------------------------------------------------+
 

#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 bool   TURN_OFF    = false ;
extern bool AutoAdjust  = true;
extern bool Show_Labels = true ;
extern int  ShiftLabel =  7 ;

extern int NumLinesAboveBelow  = 100;
extern int SweetSpotMainLevels = 100;

extern color LineColorMain= Yellow;
extern int LineStyleMain= STYLE_DOT;

extern bool ShowSubLevels= true;
extern int sublevels= 5;
extern bool OutputSubLevelsToGV = true ;
extern string GVVarName = "EntryPrice" ; // Symbol name and "_" will become prefix and "_L" or "_S" will be suffix
extern color LineColorSub= DimGray;
extern int LineStyleSub= STYLE_DOT;
extern int RefreshSeconds = 10 ;


string symbol, tChartPeriod,  tShortName, pricelabel ;  
int    digits, period, digits2, mult = 1  ; 
double point ;
string GVName ;
double PrevLevel ;
 
//+------------------------------------------------------------------+
int init()
{
   period       =  Period() ;    
   symbol       =  Symbol() ;
   digits       =  Digits ;   
   point        =  Point ;
 
   if(digits == 5 || digits == 3) { mult = 10; digits = digits - 1 ; point = point * 10 ; }   
    
   if(AutoAdjust && period > PERIOD_H1 && period < PERIOD_MN1) { mult = mult * 10; }
   if(AutoAdjust && period == PERIOD_MN1) { mult = mult * 100; }
   
   SweetSpotMainLevels = SweetSpotMainLevels * mult;
 
   sublevels           = sublevels * mult;
   
   GVName = Symbol() + "_" + GVVarName ;
   if(OutputSubLevelsToGV)
   {
      if(!GlobalVariableCheck(GVName + "_L")) 
      {
         GlobalVariableSet(GVName + "_L",0) ;
      }
      if(!GlobalVariableCheck(GVName + "_S")) 
      {
         GlobalVariableSet(GVName + "_S",0) ;
      }
   }         
    
   deinit();
   
   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);
}
   
//+------------------------------------------------------------------+
int start()
{
   if( TURN_OFF ) { return(0) ; }

   static datetime timelastupdate= 0;
   static datetime lasttimeframe= 0;
   
    
   // no need to update these buggers too often   
   if (CurTime()-timelastupdate < RefreshSeconds && Period()==lasttimeframe)
      return (0);
   
   deinit();  // delete all previous lines
      
   int i, ssp1, style, ssp, thickness; //sublevels= 50;
   double ds1;
   color linecolor;
   
   if (!ShowSubLevels)
      sublevels*= 2;
   
   ssp1= Bid / Point;
   ssp1= ssp1 - ssp1%sublevels;
   PrevLevel = 999999 ;

   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]);
      
      if(Close[0] > PrevLevel && Close[0] < ds1 )
      {
          GlobalVariableSet(GVName + "_L",ds1) ;
          GlobalVariableSet(GVName + "_S",PrevLevel) ;
      }
      
      PrevLevel = ds1 ;
   }

   return(0);
}


//+------------------------------------------------------------------+
//| Helper                                                           |
//+------------------------------------------------------------------+
void SetLevel(string text, double level, color col1, int linestyle, int thickness, datetime startofday)
{

   string linename= "[SweetSpot] " + text + " Line" + pricelabel; 

   // create or move the horizontal line   
   if (ObjectFind(linename) != 0) {
      ObjectCreate(linename, OBJ_TREND, 0, Time[0], level, Time[1], level, 0, 0);

//      ObjectCreate(linename, OBJ_HLINE, 0, 0, level);
      
      ObjectSet(linename, OBJPROP_STYLE, linestyle);
      ObjectSet(linename, OBJPROP_COLOR, col1);
      ObjectSet(linename, OBJPROP_WIDTH, thickness);

      ObjectSet(linename, OBJPROP_RAY, true);      
      ObjectSet(linename, OBJPROP_BACK, True);
   }
   else {
      ObjectMove(linename, 0, Time[0], level);
   }
   

   if(Show_Labels)
   {         
    
   
      string Obj0002 = linename+"linelbl" ;
      
      ObjectDelete(Obj0002);
      
      if(ObjectFind(Obj0002) != 0)
      {
          ObjectCreate(Obj0002,OBJ_ARROW,0,Time[0],level);
          ObjectSet(Obj0002,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(Obj0002,OBJPROP_COLOR,col1);  
      } 
      else
      {
         ObjectMove(Obj0002,0,Time[0],level);
      }
 
                 
   } // if     
    
}     


//+------------------------------------------------------------------+  

//+------------------------------------------------------------------+
  