//+------------------------------------------------------------------+
//|                                      DIBS_TriggerLines_v1.2a.mq4 |
//|                                  Copyright © 2008, ForexArea.com |
//|                                         http://www.forexarea.com |
//|                                  		  Modified by AAD... |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, ForexArea.com"
#property link      "http://www.forexarea.com"

// For more DIBS information go to http://www.forexfactory.com/showthread.php?t=86766 
// or http://www.forexfactory.com/showthread.php?t=56907

/*
Updated: 8/11/2008 - I added full customizable options (color, size, width, and style) to 
this indicator. It now also has the option to be used on any time frame when the "Show DIBS 
On All Time Frames" option is set to True. DIBS open hours are now more specifically 
specified...
---



*/ 

#property indicator_chart_window

extern string  sComment0		= "(( Mods By AAD ))";
extern string  sComment1		= "--- DIBS Open Hour Options ---";
extern string  sComment2		= "> Your broker GMT start = DIBS open hour/6AM London";
extern string  sComment3		= "GMT-8 = 22 Standard, 21 Daylight Savings";
extern string  sComment4		= "GMT-6 = 1 Standard, 23 Daylight Savings";
extern string  sComment5		= "GMT-3 = 3 Standard, 2 Daylight Savings";
extern string  sComment6		= "GMT-1 = 5 Standard, 4 Daylight Savings";
extern string  sComment7		= "GMT 00:00 = 6 Standard, 5 Daylight Savings";
extern string  sComment8		= "GMT+1 = 7 Standard, 6 Daylight Savings";
extern string  sComment9		= "GMT+3 = 9 Standard, 8 Daylight Savings";
extern string  sComment10		= "GMT+6 = 12 Standard, 11 Daylight Savings";
extern string  sComment11		= "GMT+8 = 14 Standard, 13 Daylight Savings";
extern string  sComment12		= "> London 6AM = Standard/Daylight Savings";
extern string  sComment13		= "============== 11PM PST/10PM PST";
extern string  sComment14		= "============== 12AM MST/11PM MST";
extern string  sComment15		= "============== 1AM CST/12AM CST";
extern string  sComment16		= "============== 2AM EST/1AM EST";
extern int     iOpenHour		= 5;
extern string  sComment17		= "> Total hours to monitor (9-10).";
extern int     iBarsToTrade		= 10;
extern string  sComment18		= "--- Length/Width/Size Options ---";
extern string  sComment19		= "> Length of trigger lines (2-20).";
extern int     iTriggerLineLength	= 5;
extern string  sComment20		= "> Width of trigger lines (1-5).";
extern int     iTriggerLineWidth	= 2;
extern string  sComment21		= "> Weekly line width (1-5).";
extern int     iWeeklyLineWidth	= 1;
extern string  sComment22		= "> Hourly line width (1-5).";
extern int     iHourlyLineWidth	= 1;
extern string  sComment23		= "> Price label size (0-15).";
extern int     iPriceLabelSize	= 2;
extern string  sComment24		= "--- Line Style Options ---";
extern string  sComment25		= "> 0=Solid, 1=Dash, 2=Dot";
extern string  sComment26		= "> 3=DashDot, 4=DashDotDot";
extern string  sComment27		= "> Styles 1-4 only work if you set the";
extern string  sComment28		= "> line width to 1.";
extern int     iTriggerLineStyle	= 0;
extern int     iWeeklyLineStyle	= 4;
extern int     iHourlyLineStyle	= 2;
extern string  sComment29		= "--- Line Color Options ---";
extern color   cBuyTriggerLineClr	= DodgerBlue;
extern color   cSellTriggerLineClr	= DarkOrange;
extern color   cWeeklyOpenLineClr	= DimGray;
extern color   cDayOpenLineClr	= DimGray;
extern color   cDayCloseLineClr	= DimGray;
extern string  sComment30		= "--- Display Options ---";
extern bool    bShowDayCloseLine	= true;
extern bool    bShowWeeklyOpenLine	= true;
extern bool    bShowStoploss		= false;
extern string  sComment31		= "--- Alert Options ---";
extern string  sComment32		= "> Only set 1 alert option to True.";
extern string  sComment33		= "> Do not set both alerts to True!";
extern bool    bPopUpAlert		= false;
extern bool    bSoundAlert		= false;
extern string  sComment34		= "> Alert sound file name.";
extern string  sSoundFile		= "Alert2.wav";
extern string  sComment35		= "> Minute delay between alerts.";
extern int     iDelayBetweenAlarm	= 60;
extern string  sComment36		= "--- BackTesting Options ---";
extern string  sComment37		= "> BackTest False = Only shows todays DIBS.";
extern bool    bBackTest		= false;
extern string  sComment38		= "> Amount of bars to backtest for DIBS (200-50000).";
extern int     iBarsToCount		= 2000;
extern string  sComment39		= "--- Show DIBS On All Time Frames ---";
extern string  sComment40		= "> To view DIBS on D1/higher time frames the open";
extern string  sComment41		= "> hour must be set to 0. On the H4 time frame";
extern string  sComment42		= "> the open hour can be set to 4, 8, or 12.";
extern bool    bDIBSOnAllCharts	= false;


double dBuyTriggerLines[], dSellTriggerLines[];
bool bDrawAll;
bool bDrawLines;
int iTriggerObjects = 10000;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   //NewBar();  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   DeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void DeleteAll(){

   ObjectDelete("DIBS_WeeklyOpen");   

   for(int j = 0; j <= 10000; j++){
      ObjectDelete("DIBS_Trigger_B" + j);
      ObjectDelete("DIBS_Trigger_S" + j);
      ObjectDelete("DIBS_DayOpen" + j);
      ObjectDelete("DIBS_DayClose" + j);
      ObjectDelete("DIBS_Stoploss_B" + j);
      ObjectDelete("DIBS_Stoploss_S" + j);
   } 
}

int start()
  {
  
     if(Period() != PERIOD_H1 && bDIBSOnAllCharts == false){Comment("DIBS is currently set to only work on the H1 time frame, set 'bDIBSOnAllCharts' option to True to display. Mods By AAD..."); return(0);}
  
     if(NewBar()){DeleteAll(); bDrawLines = true; iTriggerObjects = 10000;}

     int limit;
     int counted_bars=IndicatorCounted();
     int iBarHour;
     int iExtension;
     double dDayOpenPrice;
     bool bDrawToday;
     int iUniqueID;
     int i;

     
  //---- check for possible errors
     if(counted_bars<0) return(-1);

     if(iBarsToCount == 0){
         limit=Bars;
      }else{
         limit=iBarsToCount;
      }
      
      
  //---- main loop
  
while(bDrawLines==true){

   if(bShowWeeklyOpenLine)DrawWeeklyOpen();

   // MUST be left at -1
   for(i=limit; i>-1; i--){
      
      bDrawToday = false;
	   // only draw objects for today
      if(bBackTest == false && TimeDay(Time[i]) == TimeDay(TimeCurrent()) && TimeMonth(Time[i]) == TimeMonth(TimeCurrent())){
            bDrawToday = true;
         }else{
           bDrawToday = false;
         }
	     
     if(bBackTest == true){
            bDrawToday = true;
        }
        
     if(bDrawToday){
		   // only trade set hours
		   iBarHour = TimeHour(Time[i]);
		   if(iBarHour >= iOpenHour && iBarHour <= iOpenHour + iBarsToTrade){
			   // get day open price
			   
			   if(iBarHour == iOpenHour){
			      dDayOpenPrice = iOpen(NULL, 0, i);
			      // draw vertical bar to show open of day 
			      ObjectCreate("DIBS_DayOpen" + i, OBJ_VLINE, 0, Time[i], 0);
			      ObjectSet("DIBS_DayOpen" + i, OBJPROP_COLOR, cDayOpenLineClr);
			      ObjectSet("DIBS_DayOpen" + i, OBJPROP_STYLE, iHourlyLineStyle);
				ObjectSet("DIBS_DayOpen" + i, OBJPROP_WIDTH, iHourlyLineWidth);
			   }
			   
			   if(iBarHour == iOpenHour + iBarsToTrade && bShowDayCloseLine){
			      // draw vertical bar to show close of day 
			      ObjectCreate("DIBS_DayClose" + i, OBJ_VLINE, 0, Time[i], 0);
			      ObjectSet("DIBS_DayClose" + i, OBJPROP_COLOR, cDayCloseLineClr);
			      ObjectSet("DIBS_DayClose" + i, OBJPROP_STYLE, iHourlyLineStyle);
				ObjectSet("DIBS_DayClose" + i, OBJPROP_WIDTH, iHourlyLineWidth);
			   }
			   
			   // see if we have an inside bar 
			   if(iHigh(NULL, 0, i) <= iHigh(NULL, 0, i+1) && iLow(NULL, 0,i) >= iLow(NULL, 0, i+1)){
			      // IB found so draw triggers
			      if(iHigh(NULL, 0, i) > dDayOpenPrice){
                  // draw buy trigger object 
                  iTriggerObjects--;
                  if(i<iBarsToTrade){
                        iExtension = i;
                        //iUniqueID = 25000;
                        iUniqueID = iTriggerObjects;
                    }else{
                        iExtension = iTriggerLineLength-1;
                        iUniqueID = iTriggerObjects;
                     }
                  if(bSoundAlert && i == 1)Alerts(1);
                  if(bPopUpAlert && i == 1)Alert("DIBS " + Symbol() + " - Buy Trigger");
                     if(ObjectFind("DIBS_Trigger_B" + iUniqueID) != 0){
                           ObjectCreate("DIBS_Trigger_B" + iUniqueID, OBJ_TREND, 0, Time[i], iHigh(NULL, 0, i) , Time[i-iExtension], iHigh(NULL, 0, i));
                           ObjectSet("DIBS_Trigger_B" + iUniqueID, OBJPROP_RAY, false);
			         ObjectSet("DIBS_Trigger_B" + iUniqueID, OBJPROP_STYLE, iTriggerLineStyle);
                           ObjectSet("DIBS_Trigger_B" + iUniqueID, OBJPROP_WIDTH, iTriggerLineWidth);
                           ObjectSet("DIBS_Trigger_B" + iUniqueID, OBJPROP_COLOR, cBuyTriggerLineClr);
                        }else{
                           ObjectMove("DIBS_Trigger_B" + iUniqueID, 2, Time[i], iHigh(NULL, 0, i));  
                        }

                     if(bShowStoploss){
                        ObjectCreate("DIBS_Stoploss_B" + iUniqueID, OBJ_ARROW, 0, Time[i], iLow(NULL, 0, i));
                        ObjectSet("DIBS_Stoploss_B" + iUniqueID, OBJPROP_ARROWCODE, 5);
                        ObjectSet("DIBS_Stoploss_B" + iUniqueID, OBJPROP_COLOR, cBuyTriggerLineClr);
                        ObjectSet("DIBS_Stoploss_B" + iUniqueID, OBJPROP_WIDTH, iPriceLabelSize);
                    }
                    
               }
               if(iLow(NULL, 0, i) < dDayOpenPrice){
                  // draw sell trigger object 
                  iTriggerObjects--;
                  if(i<iBarsToTrade){
                        iExtension = i;
                        //iUniqueID = 25000;
                        iUniqueID = iTriggerObjects;
                    }else{
                        iExtension = iTriggerLineLength-1;
                        iUniqueID = iTriggerObjects;
                     }
                  if(bSoundAlert && i == 1)Alerts(1);
                  if(bPopUpAlert && i == 1)Alert("DIBS " + Symbol() + " - Sell Trigger");
                  if(ObjectFind("DIBS_Trigger_S" + iUniqueID) != 0){
                        ObjectCreate("DIBS_Trigger_S" + iUniqueID, OBJ_TREND, 0, Time[i], iLow(NULL, 0, i) , Time[i-iExtension], iLow(NULL, 0, i));
                        ObjectSet("DIBS_Trigger_S" + iUniqueID, OBJPROP_RAY, false);
			      ObjectSet("DIBS_Trigger_S" + iUniqueID, OBJPROP_STYLE, iTriggerLineStyle);
                        ObjectSet("DIBS_Trigger_S" + iUniqueID, OBJPROP_WIDTH, iTriggerLineWidth);
                        ObjectSet("DIBS_Trigger_S" + iUniqueID, OBJPROP_COLOR, cSellTriggerLineClr);
                     }else{
                        ObjectMove("DIBS_Trigger_S" + iUniqueID, 2, Time[i], iLow(NULL, 0, i)); 
                     }
                  if(bShowStoploss){
                     ObjectCreate("DIBS_Stoploss_S" + iUniqueID, OBJ_ARROW, 0, Time[i], iHigh(NULL, 0, i));
                     ObjectSet("DIBS_Stoploss_S" + iUniqueID, OBJPROP_ARROWCODE, 5);
                     ObjectSet("DIBS_Stoploss_S" + iUniqueID, OBJPROP_WIDTH, iPriceLabelSize);
                     ObjectSet("DIBS_Stoploss_S" + iUniqueID, OBJPROP_COLOR, cSellTriggerLineClr);
                 }
               
		        } 
			   }// if 3
		    }// if 2
	   }// if 1    
   }// for
  bDrawLines = false;
}
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void DrawWeeklyOpen(){

   double WkOpen = iOpen(NULL, PERIOD_W1, 0);
   
   ObjectCreate("DIBS_WeeklyOpen", OBJ_HLINE, 0, 0, WkOpen);
   ObjectSet("DIBS_WeeklyOpen" , OBJPROP_COLOR, cWeeklyOpenLineClr);
   ObjectSet("DIBS_WeeklyOpen" , OBJPROP_WIDTH, iWeeklyLineWidth);
   ObjectSet("DIBS_WeeklyOpen" , OBJPROP_STYLE, iWeeklyLineStyle);
			      
}

bool NewBar() {

	static datetime LastTime = 0;
   
	if (Time[0] != LastTime) {
		LastTime = Time[0];
		bDrawLines = true;		
		return (true);
	} else
		return (false);
}

void Alerts(int Direction)
{
   static datetime AlertLastTime = 0;

   if(TimeCurrent() > AlertLastTime ) {
      AlertLastTime = TimeCurrent()+iDelayBetweenAlarm;
      PlaySound(sSoundFile);
   
   }
}

