//+------------------------------------------------------------------+
//|                                                   15minHiLow.mq4 |
//|                                           Inthe Name of Almighty |
//|                                          jain.ramzamma@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Inthe Name of Almighty"
#property link      "jain.ramzamma@gmail.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

const string short_name = "HiLo_Trade Levels";
const int NOSHIFT     = 0;
const int CHARTWINDOW = 0;
extern int Left_Right  = 10;
extern int Up_Down     = 10;
 extern bool Daily     = True;
 extern bool bslevels = True;
 string BLabel  = "Buy:    ";
 string SLabel  = "Sell:   ";

extern ENUM_LINE_STYLE E_line_type = STYLE_DASH;
extern ENUM_LINE_STYLE TP_line_type = STYLE_DOT;

        
  double Day_Price[][6];
  datetime dopen;
  double   DH=0 ,DL =0,TdayOpen,TdayHigh,TdayLow,YdayClose,x05,x1,x105,x4,x7,x13,x_05,x_1,x_105,x_4,x_7,x_13; 
   int timeshift = 0, timeshifts = 0, beginner = 0;
   int periods ;

   
enum ANCHOR_POINTS {ANCHOR_POINT_ZERO, ANCHOR_POINT_ONE};
enum PIVOT_IDX { DAY_PIVOT, END_PIVOT_IDX};
ENUM_TIMEFRAMES pivotTF[END_PIVOT_IDX] = {PERIOD_D1};
//-------------------------------------------------------- 
int OnInit()
 {
   IndicatorShortName(short_name);
   return(INIT_SUCCEEDED);
 }
  
//-------------------------------------------------------- 
void OnDeinit(const int reason)
 {
//----
   for(int i = ObjectsTotal() - 1; i >= 0; i--)
      ObjectDelete(ObjectName(i));
      
   Comment(" ");
 }

//--------------------------------------------------------- 
bool NewBar(PIVOT_IDX indexToCheck)
{
   static datetime ary_BarZeroValues[END_PIVOT_IDX];
   bool isNewBar = false;
   datetime barZeroTime = iTime(Symbol(),pivotTF[indexToCheck],NOSHIFT);
   datetime lastBarZeroTime = ary_BarZeroValues[indexToCheck];
   if(lastBarZeroTime!=barZeroTime)
   {
      ary_BarZeroValues[indexToCheck] = barZeroTime;
      isNewBar = true;
   }
   return(isNewBar);
}    
//

int OnCalculate(const int rates_total, 
               const int prev_calculated, 
               const datetime &time[], 
               const double &open[], 
               const double &high[],
               const double &low[], 
               const double &close[], 
               const long& tick_volume[], 
               const long& volume[], 
               const int& spread[] )
{
   MainRun(rates_total - prev_calculated);
   return(rates_total);
}
void MainRun(int lastbar)
{   
   if(NewBar(DAY_PIVOT))
   {
      DoDailyRates();
   }
   DoDailyLines();
 
}
//=============================GOOD LUCK========================

//==============================================================
void DoDailyRates(void)
{
   dopen=iTime(Symbol(),PERIOD_D1,NOSHIFT);
   ArrayCopyRates(Day_Price,(Symbol()), PERIOD_D1);
     
 //  TdayHigh = iHigh(Symbol(), PERIOD_D1, 0); //Day_Price[0][3];
//   TdayLow =  iLow(Symbol(), PERIOD_D1, 0); //Day_Price[0][2];
   TdayOpen = iOpen(Symbol(), PERIOD_D1,0);

   string date=TimeToString(Time[Bars-1],TIME_DATE); 
   for(int i=Bars-1;i>=0;i--){
      
      if(date!=TimeToString(Time[i],TIME_DATE))
      {
         date=TimeToString(Time[i],TIME_DATE);
         DH = High[i];
         DL  = Low[i];
      }
   
    
   
      //do not know how to take first candle of H4 for the week /month 
      // and the first candle of the D1 for the month and week first candle for the year
      // for the first candle of jan month for the year.. 
    }
   x05 = DH+(DH- DL)*2.618;
   x_05= DL -(DH- DL)*2.618; 
   
   x1  = DH+(DH- DL)*4.236;  
   x_1 = DL -(DH- DL)*4.236;
   
   x105= DH +(DH- DL )*6.18;
   x_105=DL -(DH- DL)*6.18;
   
   x4  = DH+(DH- DL)*13;
   x_4 = DL -(DH- DL)*13;
   
   x7  = DH+(DH- DL)*26.18;
   x_7 = DL -(DH- DL)*26.18;
    
   x13 =DH+(DH- DL)*33;
   x_13=DL -(DH- DL)*33;
  
}
void ManageLine(string lName, datetime startTime, double lPrice, datetime endTime,color lColor, ENUM_LINE_STYLE lStyle, int lWidth)
{
   if (ObjectFind(lName) < 0 )
   {
         ObjectCreate(lName, OBJ_TREND, CHARTWINDOW,startTime,lPrice, endTime,lPrice);
         ObjectSet(lName, OBJPROP_COLOR, lColor);
         ObjectSet(lName, OBJPROP_STYLE, lStyle);
         ObjectSet(lName, OBJPROP_WIDTH, lWidth);
         ObjectSet(lName, OBJPROP_RAY,false);
    } else
    {
      ObjectMove(lName,ANCHOR_POINT_ZERO,startTime,lPrice);
      ObjectMove(lName,ANCHOR_POINT_ONE,endTime,lPrice); 
    }
}
void ManageLabel(string lName, string lText, datetime lTime, double lPrice, color lColor)
{
   if(ObjectFind(lName) < 0)
   {
      ObjectCreate(lName, OBJ_TEXT,CHARTWINDOW, lTime, lPrice);
   } else
   {
      ObjectMove(lName, ANCHOR_POINT_ZERO, lTime, lPrice);
   }
   ObjectSetText(lName, lText, 7, "Century Gothic", lColor);
}
void DoDailyLines(void)
{
   if (Daily==true)
   {
      dopen=iTime(Symbol(),PERIOD_D1,0);
      datetime timeTime       = (Time[0] + 5 * PeriodSeconds()) ;  //Time[0]+(Time[0]-Time[7]);
      datetime labelTime      = (Time[0] + 3 * PeriodSeconds()) ;  // - (60*60*4) );   
                 
         ManageLine("DH_Line", dopen, DH,timeTime ,clrLime,TP_line_type,1);
         ManageLabel("DHLabel",("Buy: ")+ DoubleToStr(DH,2),labelTime, DH,clrLime); 
         ManageLine("DL_Line", dopen, DL,timeTime ,clrOrangeRed,TP_line_type,1);
         ManageLabel("DLLabel",(" Sell: ")+ DoubleToStr(DL,2),labelTime, DL, clrOrangeRed);

         ManageLine("x05_Line", dopen, x05,timeTime ,C'0,185,0',TP_line_type,1);
         ManageLabel("x05Label",("2x: ")+ DoubleToStr(x05,2),labelTime, x05,C'0,185,0'); 
         ManageLine("x_05_Line", dopen, x_05,timeTime ,C'221,0,0',TP_line_type,1);
         ManageLabel("x_05Label",("-2x: ")+ DoubleToStr(x_05,2),labelTime, x_05,C'221,0,0'); 
        
         ManageLine("x1_Line", dopen, x1,timeTime ,C'0,185,0',TP_line_type,1);
         ManageLabel("x1Label",("4x: ")+ DoubleToStr(x1,2),labelTime, x1,C'0,185,0'); 
         ManageLine("x_1_Line", dopen, x_1,timeTime ,C'221,0,0',TP_line_type,1);
         ManageLabel("x_1Label",("-4x: ")+ DoubleToStr(x_1,2),labelTime, x_1,C'221,0,0'); 
        
         ManageLine("x105_Line", dopen, x105,timeTime ,C'0,185,0',TP_line_type,1);
         ManageLabel("x105Label",("7x: ")+ DoubleToStr(x105,2),labelTime, x105,C'0,185,0'); 
         ManageLine("x_105_Line", dopen, x_105,timeTime ,C'221,0,0',TP_line_type,1);
         ManageLabel("x_105Label",("-7x: ")+ DoubleToStr(x_105,2),labelTime, x_105,C'221,0,0'); 
        
         ManageLine("x4_Line", dopen, x4,timeTime ,C'0,185,0',TP_line_type,1);
         ManageLabel("x4Label",("13x: ")+ DoubleToStr(x4,2),labelTime, x4,C'0,185,0'); 
         ManageLine("x_4_Line", dopen, x_4,timeTime ,C'221,0,0',TP_line_type,1);
         ManageLabel("x_4Label",("-13x: ")+ DoubleToStr(x_4,2),labelTime, x_4,C'221,0,0'); 
        
         ManageLine("x7_Line", dopen, x7,timeTime ,C'0,185,0',TP_line_type,1);
         ManageLabel("x7Label",("21x :  ")+ DoubleToStr(x7,2),labelTime, x7,C'0,185,0'); 
         ManageLine("x_7_Line", dopen, x_7,timeTime ,C'221,0,0',TP_line_type,1);
         ManageLabel("x_7Label",("-21x: ")+ DoubleToStr(x_7,2),labelTime, x_7,C'221,0,0'); 
        
         ManageLine("x13_Line", dopen, x13,timeTime ,C'0,185,0',TP_line_type,1);
         ManageLabel("x13Label",("33x :  ")+ DoubleToStr(x13,2),labelTime, x13,C'0,185,0'); 
         ManageLine("x_13_Line", dopen, x_13,timeTime ,C'221,0,0',TP_line_type,1);
         ManageLabel("x_13Label",("-33x: ")+ DoubleToStr(x_13,2),labelTime, x_13,C'221,0,0'); 
              
   if (bslevels ==True )
      {       
        if (TdayOpen > YdayClose ) 
          {
        
            

            ObjectCreate("Sell",OBJ_LABEL,0,0,0);
            ObjectSet("Sell",OBJPROP_CORNER,1);
            ObjectSet("Sell",OBJPROP_COLOR, clrOrangeRed);
            ObjectSet("Sell",OBJPROP_XDISTANCE,Left_Right);
            ObjectSet("Sell",OBJPROP_YDISTANCE,Up_Down);
        //    SLabel = MarketInfo(Symbol(), MODE_BID);
  
     ObjectSetText("Sell",SLabel+DoubleToStr(TdayOpen,2),20,"Century Gothic",clrOrangeRed);
             } //Digits(NormalizeDouble(TdayOpen,Digits),0)
  
        if (TdayOpen < YdayClose ) 
        {
     
       
   
            ObjectCreate("Buy",OBJ_LABEL,0,0,0);
            ObjectSet("Buy",OBJPROP_CORNER,1);
            ObjectSet("Buy",OBJPROP_COLOR,clrLimeGreen);
            ObjectSet("Buy",OBJPROP_XDISTANCE,Left_Right);
            ObjectSet("Buy",OBJPROP_YDISTANCE,Up_Down);
         //   BLabel = MarketInfo(Symbol(), MODE_ASK);
     ObjectSetText("Buy",BLabel+DoubleToStr(TdayOpen,2),20,"Century Gothic",clrLimeGreen);
             } //Digits(NormalizeDouble(TdayOpen,2),0)
      }  
        
        



}

}



