//+------------------------------------------------------------------+
//|                                                  Small Hours.mq4 |
//|                                      Copyright © 2011, Ido Kasse |
//|              http://www.forexfactory.com/showthread.php?t=280540 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Ido Kasse"
#property link      "http://www.forexfactory.com/showthread.php?t=280540"

extern int corner=1;
extern int y=15;
extern string slevel = "Show Entry, SL and TP2 levels";
extern bool showlevels = true;
extern string lecolor = "Textcolor";
extern color lcolor = Silver;
extern string dbox = "Draw box around the 5 bars range";
extern bool box = true;
extern string hmessage = "Show rules onscreen";
extern bool help = true;
extern string alertm = "Show Alert message on valid signal";
extern bool usealert = true;
extern string useold = "Use old Entry, SL and TP calculations";
extern bool oldcalculations = false;
extern string smail = "Send e-mail on valid signal";
extern bool mail = false;
extern string devtest = "Enable development testing part";
extern bool testing = false;
double atrvalue, atrcalc, tp1;
double hhll5, hhll5calc;
double hh5, ll5, hh3, ll3;
double setuphigh, setuplow;
double setupclose;
double sma20, sma40;
static datetime barStart;
double spread, spreadDiv;

//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
 
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   ObjectCreate("rule1", OBJ_LABEL, 0, 0, 0);
   ObjectSet("rule1", OBJPROP_CORNER, corner);
   ObjectSet("rule1", OBJPROP_XDISTANCE, 15);
   ObjectSet("rule1", OBJPROP_YDISTANCE, y); 
   ObjectSetText("rule1", "O", 15, "Arial Bold", lcolor);   

   ObjectCreate("rule2", OBJ_LABEL, 0, 0, 0);
   ObjectSet("rule2", OBJPROP_CORNER, corner);
   ObjectSet("rule2", OBJPROP_XDISTANCE, 15);
   ObjectSet("rule2", OBJPROP_YDISTANCE, y+25);
   ObjectSetText("rule2", "O", 15, "Arial Bold", lcolor);

   if (help==true)
   {
    ObjectCreate("rule1help", OBJ_LABEL, 0, 0, 0);
    ObjectSet("rule1help", OBJPROP_CORNER, corner);
    ObjectSet("rule1help", OBJPROP_XDISTANCE, 40);
    ObjectSet("rule1help", OBJPROP_YDISTANCE, y+2);   

    ObjectCreate("rule3help1", OBJ_LABEL, 0, 0, 0);
    ObjectSet("rule3help1", OBJPROP_CORNER, corner);
    ObjectSet("rule3help1", OBJPROP_XDISTANCE, 40);
    ObjectSet("rule3help1", OBJPROP_YDISTANCE, y+25);
   
    ObjectCreate("rule3help2", OBJ_LABEL, 0, 0, 0);
    ObjectSet("rule3help2", OBJPROP_CORNER, corner);
    ObjectSet("rule3help2", OBJPROP_XDISTANCE, 40);
    ObjectSet("rule3help2", OBJPROP_YDISTANCE, y+35);
   }

   if (showlevels==true)
   {
    ObjectCreate("stats1", OBJ_LABEL, 0, 0, 0);
    ObjectSet("stats1", OBJPROP_CORNER, corner);
    ObjectSet("stats1", OBJPROP_XDISTANCE, 15);
    ObjectSet("stats1", OBJPROP_YDISTANCE, y+55);

    ObjectCreate("stats2", OBJ_LABEL, 0, 0, 0);
    ObjectSet("stats2", OBJPROP_CORNER, corner);
    ObjectSet("stats2", OBJPROP_XDISTANCE, 15);
    ObjectSet("stats2", OBJPROP_YDISTANCE, y+70);

    ObjectCreate("label", OBJ_LABEL, 0, 0, 0);
    ObjectSet("label", OBJPROP_CORNER, corner);
    ObjectSet("label", OBJPROP_XDISTANCE, 15);
    ObjectSet("label", OBJPROP_YDISTANCE, y+100);

    ObjectCreate("entry", OBJ_LABEL, 0, 0, 0);
    ObjectSet("entry", OBJPROP_CORNER, corner);
    ObjectSet("entry", OBJPROP_XDISTANCE, 15);
    ObjectSet("entry", OBJPROP_YDISTANCE, y+120);

    ObjectCreate("tp1", OBJ_LABEL, 0, 0, 0);
    ObjectSet("tp1", OBJPROP_CORNER, corner);
    ObjectSet("tp1", OBJPROP_XDISTANCE, 15);
    ObjectSet("tp1", OBJPROP_YDISTANCE, y+140);

    ObjectCreate("tp2", OBJ_LABEL, 0, 0, 0);
    ObjectSet("tp2", OBJPROP_CORNER, corner);
    ObjectSet("tp2", OBJPROP_XDISTANCE, 15);
    ObjectSet("tp2", OBJPROP_YDISTANCE, y+160);

    ObjectCreate("sl", OBJ_LABEL, 0, 0, 0);
    ObjectSet("sl", OBJPROP_CORNER, corner);
    ObjectSet("sl", OBJPROP_XDISTANCE, 15);
    ObjectSet("sl", OBJPROP_YDISTANCE, y+180);
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("rule1");
   ObjectDelete("rule2");

   if (box==true) ObjectDelete("box");

   if (showlevels==true)
   {
    ObjectDelete("stats1");
    ObjectDelete("stats2");
    ObjectDelete("label");
    ObjectDelete("entry");
    ObjectDelete("tp1");
    ObjectDelete("tp2");
    ObjectDelete("sl");
   }

   if (help==true)
   {
    ObjectDelete("rule1help");
    ObjectDelete("rule3help1");
    ObjectDelete("rule3help2");
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----

//DIGIT FIX
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
               pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
   } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }

//PLACE TEXT
   ObjectSetText("rule1", CharToStr(129), 15, "Wingdings", lcolor);
   ObjectSetText("rule2", CharToStr(131), 15, "Wingdings", lcolor);
   if (help==true)
   {
    ObjectSetText("rule1help","HH5-LL5 < ATRx1.5 from setup bar", 8, "Arial", lcolor);
    ObjectSetText("rule3help1","BUY: Close of previous bar > 20SMA or 40SMA", 8, "Arial Bold", lcolor);
    ObjectSetText("rule3help2","SELL: Close of previous bar < 20SMA or 40SMA", 8, "Arial", lcolor);
   }

//OLD CALCULATIONS
   if (oldcalculations==true)
   {  
    atrvalue=iATR(NULL,0,20,1)/pips2dbl;
    atrcalc=atrvalue*1.5;
    tp1=atrvalue*0.75;
    hh5=High[iHighest(NULL,0,MODE_HIGH,5,1)];
    ll5=Low[iLowest(NULL,0,MODE_LOW,5,1)];
    hhll5=(hh5-ll5)/pips2dbl;
   }

//NEW CALCULATIONS
   if (oldcalculations==false)
   {  
    atrvalue=iATR(NULL,0,20,1)/pips2dbl;
    atrcalc=atrvalue*1.5;
    tp1=atrvalue*0.75;
    hh5=High[iHighest(NULL,0,MODE_HIGH,5,1)];
    ll5=Low[iLowest(NULL,0,MODE_LOW,5,1)];
    hhll5=(hh5-ll5)/pips2dbl;
    hh3=High[iHighest(NULL,0,MODE_HIGH,3,1)];
    ll3=Low[iLowest(NULL,0,MODE_LOW,5,1)];
   }
   
//RULE 1
   if (hhll5<atrcalc) ObjectSetText("rule1", CharToStr(129), 15, "Wingdings", Orange);

//RULE 3
   setupclose=iClose(NULL,0,1);
   sma20=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1);
   sma40=iMA(NULL,0,40,0,MODE_SMA,PRICE_CLOSE,1);
   if (setupclose>sma20||setupclose>sma40) ObjectSetText("rule2", CharToStr(131), 15, "Wingdings", Lime);
   if (setupclose<sma20||setupclose<sma40) ObjectSetText("rule2", CharToStr(131), 15, "Wingdings", Red);

//SET RULE 1 LIGHT SAME AS RULE 3 LIGHT
   if (hhll5<atrcalc&&setupclose>sma20||setupclose>sma40) ObjectSetText("rule1", CharToStr(129), 15, "Wingdings", Lime);
   if (hhll5<atrcalc&&setupclose<sma20||setupclose<sma40) ObjectSetText("rule1", CharToStr(129), 15, "Wingdings", Red);

//RUN ONCE PER BAR
if (barStart < Time[0]) //start of new bar 
{
barStart = Time[0];

//SHOW LEVELS
   if (showlevels==true)
   {
    ObjectSetText("label", "BUY / SELL", 13, "Arial Bold", lcolor);
    ObjectSetText("stats1", "ATRx1.5 / HH5-LL5 Range: "+DoubleToStr(atrcalc,1)+" / "+DoubleToStr(hhll5,1), 9, "Arial Bold", lcolor);
    ObjectSetText("stats2", "Highest / Lowest: "+DoubleToStr(hh5,Digits)+" / "+DoubleToStr(ll5,Digits), 9, "Arial Bold", lcolor);
    ObjectSetText("entry", "Entry: "+DoubleToStr(hh5+3*pips2dbl,Digits)+" / "+DoubleToStr(ll5-3*pips2dbl,Digits), 9, "Arial Bold", lcolor);
    ObjectSetText("tp1", "TP1: "+DoubleToStr(hh5+tp1*pips2dbl,Digits)+" / "+DoubleToStr(ll5-tp1*pips2dbl,Digits), 9, "Arial Bold", Green);
    
    if (oldcalculations==true) ObjectSetText("tp2", "TP2: "+DoubleToStr(hh5+atrcalc*pips2dbl,Digits)+" / "+DoubleToStr(ll5-atrcalc*pips2dbl,Digits), 9, "Arial Bold", Lime);
    if (oldcalculations==false) ObjectSetText("tp2", "TP2: "+DoubleToStr(hh5+(atrvalue*0.95)*pips2dbl,Digits)+" / "+DoubleToStr(ll5-(atrvalue*0.95)*pips2dbl,Digits), 9, "Arial Bold", Lime);
    
    if (oldcalculations==true) ObjectSetText("sl", "SL: "+DoubleToStr(ll5-3*pips2dbl,Digits)+" / "+DoubleToStr(hh5+3*pips2dbl,Digits), 9, "Arial Bold", Red);
    if (oldcalculations==false) ObjectSetText("sl", "SL: "+DoubleToStr(hh5-(hh3-ll3)-3*0.764*pips2dbl,Digits)+" / "+DoubleToStr(ll5+(hh3-ll3)-3*0.764*pips2dbl,Digits), 9, "Arial Bold", Red);
   }
   
//BUY ALERT 
   if (hhll5<atrcalc&&setupclose>sma20||setupclose>sma40) 
   {
     if (usealert==true) Alert(""+Symbol()+" BUY Alert! Place BUY stop orders now");
     if (mail==true) SendMail("Small Hours indicator", "BUY signal for "+Symbol()+" @ "+Period());
     ObjectSetText("label", "BUY", 15, "Arial Bold", White);
   }

//SELL ALERT
   if (hhll5<atrcalc&&setupclose<sma20||setupclose<sma40) 
   {
    if (usealert==true) Alert(""+Symbol()+" SELL Alert! Place SELL stop orders now");
    if (mail==true) SendMail("Small Hours indicator", "SELL signal for "+Symbol()+" @ "+Period());
    ObjectSetText("label", "SELL", 15, "Arial Bold", White);
   }

//TESTING AREA
  if (testing==true)
  {
//ARROW UP
   if (hhll5<atrcalc&&setupclose>sma20||setupclose>sma40) 
   {
    ObjectDelete("uparrow"); 
    ObjectCreate("uparrow", OBJ_ARROW, 0, Time[0], Low[1]-20/pips2dbl);
    ObjectSet("uparrow", OBJPROP_STYLE, STYLE_SOLID);
    ObjectSet("uparrow", OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
    ObjectSet("uparrow", OBJPROP_COLOR,Lime);   
   }

//ARROW DOWN
   if (hhll5<atrcalc&&setupclose<sma20||setupclose<sma40) 
   {
     ObjectDelete("downarrow");
     ObjectCreate("downarrow",OBJ_ARROW, 0, Time[0], High[1]+20/pips2dbl);
     ObjectSet("downarrow", OBJPROP_STYLE, STYLE_SOLID);
     ObjectSet("downarrow", OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
     ObjectSet("downarrow", OBJPROP_COLOR,Red);   
   }
  }

//DRAW BOX
   if(box==true)
   {
    ObjectDelete("box");
    ObjectCreate("box", OBJ_RECTANGLE, 0, Time[5], High[iHighest(NULL,0,MODE_HIGH,5,1)], Time[1], Low[iLowest(NULL,0,MODE_LOW,5,1)]);
    ObjectSet("box", OBJPROP_STYLE, STYLE_DASHDOTDOT);
    ObjectSet("box", OBJPROP_COLOR, MidnightBlue);
    ObjectSet("box", OBJPROP_WIDTH, 1);
   }

} //END BARSTART

//----
   return(0);
  }
//+------------------------------------------------------------------+