//+------------------------------------------------------------------+ 
//|   $_TRO_Trigger_001                                              | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+ 


#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpldone.com/" 

#property indicator_separate_window
#property indicator_buffers 0
#property indicator_minimum 0
#property indicator_maximum 1

extern int price.x.offset= 0;//250
extern int price.y.offset= 10;//-160

extern bool my.alert.sound     = true ;
extern bool my.alert.stop      = false ;

extern bool use.Tunnel     = true ;
extern bool use.Ind2       = true ;
extern bool use.Filter     = true ;

extern string mySymbol = "" ;
extern double myRange = 19 ;

extern color myColorHeading   =  Gray ; 
extern color myColorValues    =  Yellow ; 

extern string _Ind2_inputs = "MA Period, MA type, MA Mode, chart period" ;
extern int       Ind2MAPeriod = 5;
extern int       Ind2MAType   = 1;
extern int       Ind2MAMode   = MODE_EMA;
extern int       Ind2Period   = 60;


  double PrevMidD1, PrevMidH1;
  double close1;
  double high1;
  double low1;
  double high;
  double low;
  double close;
  double open;
  double dayMACD;
  double dayClose;
  double dayOpen;
  double M30Close;
  double M30Open;
  double median;
  double H1range;

  double Min5close;
  double Min5open;



double pdayHigh;
double pdayLow;
double cdayHigh;
double cdayLow;
double pdayClose ;
double pdayOpen;
double cdayClose;
double cdayOpen;
double pdayRange;
double cdayRange;
double cdayMid;

double value1;
double value2;  
  
double point ;
int xPoint ;

string   labelNames;
string   shortName;

int      corner;
int      totalLabels;
int      window;    

color ColorHeading  ; 
color ColorValues ; 
color ColorPrice ; 

color colorArrowcMid  ; 
string ArrowcMid ;



string ArrowN   = "ñ"; // north
string ArrowNE  = "ö";  // northeast
string ArrowE   = "ð";  // east
string ArrowSE  = "ø";  // southeast
string ArrowS   = "ò";  // south

 
  

double Tunnel1, Tunnel2, TunnelTop, TunnelBot ;
double TunnelTgtU1, TunnelTgtU2, TunnelTgtU3 ;
double TunnelTgtL1, TunnelTgtL2, TunnelTgtL3 ;
double PriceTunnel ; 

string   pAlertTunnel;
string   tAlertTunnel; 

color ColorValuesTunnel ;
 
bool   ShTriggerTunnel; 
bool   LgTriggerTunnel;

string ArrowTunnel  ;  
   
color colorArrowTunnel  ; 

//
double PriceInd2 ; 
double Ind2MA_Cur, Ind2MA_Prev;

string   pAlertInd2;
string   tAlertInd2; 

color ColorValuesInd2 ;
 
bool   ShTriggerInd2; 
bool   LgTriggerInd2;

string ArrowInd2  ;  
   
color colorArrowInd2  ; 

//
double PriceFilter ; 
double FilterMA_Cur, FilterMA_Prev;

string   pAlertFilter;
string   tAlertFilter; 

color ColorValuesFilter ;
 
bool   ShTriggerFilter; 
bool   LgTriggerFilter;

string ArrowFilter  ;  
   
color colorArrowFilter  ; 


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   corner     = 0;
   shortName  = MakeUniqueName("$TRO 001 ","");
   labelNames = shortName;
      IndicatorShortName(shortName);
 
 if( mySymbol == "" ) { mySymbol = Symbol() ; }
 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
bool sFirst = true ;

window      = WindowFind(shortName);
   
if ( sFirst ) {
sFirst = false ;
ObjectsDeleteAll( window, 21);
ObjectsDeleteAll( window, 22);
ObjectsDeleteAll( window, 23);
DoHead() ;
}
//----


ColorHeading      = myColorHeading ; 
ColorValues       = myColorValues ; 

  


   dayMACD       = iMACD(mySymbol,PERIOD_D1,4,5,13,PRICE_CLOSE,MODE_MAIN,0);
   dayClose      = iClose(mySymbol,PERIOD_D1,0);
   dayOpen       = iOpen(mySymbol,PERIOD_D1,0);
   M30Close      = iClose(mySymbol,PERIOD_M30,0);
   M30Open       = iOpen(mySymbol,PERIOD_M30,0);
   
   median        = iMA(mySymbol,PERIOD_M30,1,0,MODE_CLOSE,PRICE_MEDIAN,1);
   
   Min5close     = iClose(mySymbol,PERIOD_M5,0);
   Min5open      = iOpen(mySymbol,PERIOD_M5,0);     
   
   close         = iClose(mySymbol,PERIOD_H1,0);
   open          = iOpen(mySymbol,PERIOD_H1,0);   
   high          = iHigh(mySymbol,PERIOD_H1,0);
   low           = iLow(mySymbol,PERIOD_H1,0);   
 
   high1         = iHigh(mySymbol,PERIOD_H1,1);
   low1          = iLow(mySymbol,PERIOD_H1,1); 
   close1         = iClose(mySymbol,PERIOD_H1,1);

   //H1range         = (high - low) / point ;


 

   pdayHigh       = iHigh(mySymbol,PERIOD_D1,1);
   pdayLow        = iLow(mySymbol,PERIOD_D1,1);
   cdayHigh       = iHigh(mySymbol,PERIOD_D1,0);
   cdayLow        = iLow(mySymbol,PERIOD_D1,0);
   pdayRange      = pdayHigh - pdayLow;
   cdayRange      = cdayHigh - cdayLow;
   pdayClose      = iClose(mySymbol,PERIOD_D1,1);
   pdayOpen       = iOpen(mySymbol,PERIOD_D1,1);
   cdayClose      = iClose(mySymbol,PERIOD_D1,0);
   cdayOpen       = iOpen(mySymbol,PERIOD_D1,0);
   value1         = pdayHigh + 30*point;
   value2         = pdayLow - 30*point;

   PrevMidH1      = (high1 + low1) * 0.50 ;
   PrevMidD1      = (pdayHigh + pdayLow) * 0.50 ;
   cdayMid        = (cdayHigh + cdayLow) * 0.50 ;   
   
   Tunnel1        = iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,0);
   Tunnel2        = iMA(NULL,0,169,0,MODE_EMA,PRICE_CLOSE,0);
  
   TunnelTop      = MathMax(Tunnel1,Tunnel2);
   TunnelBot      = MathMin(Tunnel1,Tunnel2);        



int hSize = 9 ;


point =MarketInfo(mySymbol,MODE_POINT);  

if ( point == 0.01 )  {xPoint = 2 ; } else { xPoint = 4 ;  } 
   
//------------- 

if(use.Tunnel )   { DoTunnel (); }  

if(use.Ind2 )   { DoInd2 (); } 

if(use.Filter )   { DoFilter (); } 

//-------------    

if(cdayClose > cdayMid) { colorArrowcMid = Lime ; ArrowcMid = ArrowN ; } else {
if(cdayClose < cdayMid) { colorArrowcMid = Red ;  ArrowcMid = ArrowS ; } else {
                          colorArrowcMid = Yellow ; ArrowcMid = ArrowE ; 
                          if ( my.alert.stop ) {Alert(mySymbol,", cdayMid - SL "+ DoubleToStr(close,xPoint)); }
                          Print(mySymbol,", - !!!SL");} 
}

if(cdayClose > cdayOpen) { ColorPrice = Lime ; } else {
if(cdayClose < cdayOpen) { ColorPrice = Red ; } else {ColorPrice = Yellow ; } }

setObject(next(),mySymbol,10+price.x.offset, 5+price.y.offset,ColorHeading ,"Arial bold",hSize);

setObject(next(), DoubleToStr(close,xPoint),75+price.x.offset, 5+price.y.offset, ColorPrice ,"Arial bold",hSize);               

setObject(next(), DoubleToStr(cdayMid,xPoint),125+price.x.offset, 5+price.y.offset, ColorPrice ,"Arial bold",hSize);               

setObject(next(),ArrowcMid ,175+price.x.offset, 5+price.y.offset, colorArrowcMid  ,"Wingdings",hSize);               


if(use.Tunnel )   {  
setObject(next(),tAlertTunnel   +" " + DoubleToStr(PriceTunnel,xPoint),800+price.x.offset, 5+price.y.offset, ColorValuesTunnel ,"Arial bold",hSize);               
//setObject(next(),ArrowTunnel ,885+price.x.offset, 5+price.y.offset, colorArrowTunnel ,"Wingdings",hSize);               
}    
 
if(use.Ind2 )   { 
setObject(next(),tAlertInd2   +" " + DoubleToStr(PriceInd2,xPoint),700+price.x.offset, 5+price.y.offset, ColorValuesInd2 ,"Arial bold",hSize);               
//setObject(next(),ArrowInd2 ,785+price.x.offset, 5+price.y.offset, colorArrowInd2 ,"Wingdings",hSize);               
} 

if(use.Filter )   { 
setObject(next(),tAlertFilter   +" " + DoubleToStr(PriceFilter,xPoint),600+price.x.offset, 5+price.y.offset, ColorValuesFilter ,"Arial bold",hSize);               
//setObject(next(),ArrowFilter ,675+price.x.offset, 5+price.y.offset, colorArrowFilter ,"Wingdings",hSize);               
}

//setObject(next(),DoubleToStr(H1range,4),400+price.x.offset, 5+price.y.offset, ColorValues ,"Arial bold",hSize);               
//setObject(next(),DoubleToStr(close,4),400+price.x.offset, 5+price.y.offset, ColorValues ,"Arial bold",hSize);               
       

    

   WindowRedraw();     
//----
   return(0);
  }
//+------------------------------------------------------------------+

string next() { totalLabels++; return(totalLabels); }  

string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}

//+------------------------------------------------------------------+
void setObject(string name,string text,int x,int y,color theColor, string font = "Arial",int size=10,int angle=0)
{
   string labelName = StringConcatenate(labelNames,name);

      if (ObjectFind(labelName) == -1)
          {
             ObjectCreate(labelName,OBJ_LABEL,window,0,0);
             ObjectSet(labelName,OBJPROP_CORNER,corner);
             if (angle != 0)
                  ObjectSet(labelName,OBJPROP_ANGLE,angle);
          }               
       ObjectSet(labelName,OBJPROP_XDISTANCE,x);
       ObjectSet(labelName,OBJPROP_YDISTANCE,y);
       ObjectSetText(labelName,text,size,font,theColor);
}

//+------------------------------------------------------------------+
void DoHead ()
{
int hSize = 9 ;

setObject(next(),"Price",75+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
setObject(next(),"CurDayMid",125+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               



if(use.Tunnel )   {
setObject(next(),"Tunnel",800+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
}

if(use.Ind2 )   {   
setObject(next(),"Ind2",700+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
}

if(use.Filter )   {   
setObject(next(),"Filter",600+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
}
        
}


//+------------------------------------------------------------------+
// 1 HOUR VEGAS TUNNEL 
//+------------------------------------------------------------------+
 
void DoTunnel()
{


   pAlertTunnel        = tAlertTunnel ;
   tAlertTunnel        = "WAIT";
   ColorValuesTunnel   = myColorValues ; 


   if(close > TunnelTop) { ArrowTunnel = ArrowN ; colorArrowTunnel = Lime ; } else {
   if(close < TunnelBot) { ArrowTunnel = ArrowS ; colorArrowTunnel = Red  ; } else { 
                           ArrowTunnel = ArrowE ; colorArrowTunnel = Orange ;  } }
 
   if( close <= TunnelTop 
   &&  close >= TunnelBot ) {
  
     TunnelTgtU1 = TunnelTop + 34 * point;  
     TunnelTgtU2 = TunnelTop + 55 * point; 
     TunnelTgtU3 = TunnelTop + 89 * point; 
     TunnelTgtL1 = TunnelBot - 34 * point;  
     TunnelTgtL2 = TunnelBot - 55 * point; 
     TunnelTgtL3 = TunnelBot - 89 * point;      
    }

   if( close > TunnelTop 
   &&  low   < TunnelTop ) {

   tAlertTunnel       = "BUY";
   if (LgTriggerTunnel == false)   { 
      PriceTunnel = TunnelTop ;
      if (pAlertTunnel == "WAIT") { 
         if ( my.alert.sound ) {Alert(mySymbol," Tunnel BUY "+ DoubleToStr(PriceTunnel,xPoint)); }
         Print(mySymbol,"BUY");
      }
    }

   ColorValuesTunnel  = Lime ; 
   LgTriggerTunnel =  true ; 
   }
   
   if( close < TunnelBot 
   &&  high  > TunnelBot ) {

   tAlertTunnel       = "SHORT" ;
   if (ShTriggerTunnel == false)    {
       PriceTunnel = TunnelBot ; 
       if (pAlertTunnel == "WAIT") { 
          if ( my.alert.sound ) {Alert(mySymbol," Tunnel SHORT "+ DoubleToStr(PriceTunnel,xPoint)); }
          Print(mySymbol,"SHORT");       
       }
    }

   ColorValuesTunnel  = Red ; 
   ShTriggerTunnel =  true ;    
   }   

   if (pAlertTunnel == "WAIT") {  LgTriggerTunnel = false ; ShTriggerTunnel = false ; }


if ( close == TunnelTgtU1 
||   close == TunnelTgtU2
||   close == TunnelTgtU3) {
    tAlertTunnel       = "SELL" ;
    if ( my.alert.sound ) {Alert(mySymbol," Tunnel SELL "+ DoubleToStr( close,xPoint)); }
    Print(mySymbol,"SELL");   
    ColorValuesTunnel  = Red ;  
}

if ( close == TunnelTgtL1 
||   close == TunnelTgtL2
||   close == TunnelTgtL3) {
    tAlertTunnel       = "COVER" ;
    if ( my.alert.sound ) {Alert(mySymbol," Tunnel COVER "+ DoubleToStr( close,xPoint)); }
    Print(mySymbol,"COVER");   
    ColorValuesTunnel  = Lime ;  
}

} // END TUNNEL 


//+------------------------------------------------------------------+
//INDICATOR 2 
//+------------------------------------------------------------------+


void DoInd2()
{

   pAlertInd2        = tAlertInd2 ;
   tAlertInd2        = "WAIT";
   PriceInd2         = 0 ;
   ColorValuesInd2   = myColorValues ; 

   Ind2MA_Cur  = iMA(mySymbol,Ind2Period,Ind2MAPeriod,0,Ind2MAMode,PRICE_CLOSE,0);
   Ind2MA_Prev = iMA(mySymbol,Ind2Period,Ind2MAPeriod,0,Ind2MAMode,PRICE_CLOSE,1);

   ArrowInd2 = ArrowE ; colorArrowInd2 = Yellow ; 
 

   if(close > Ind2MA_Cur
   && Ind2MA_Cur > Ind2MA_Prev) {

   tAlertInd2       = "BUY";
   if (LgTriggerInd2 == false)   { 
      PriceInd2 = close ;
      ArrowInd2 = ArrowN ; colorArrowInd2 = Lime ;
      if (pAlertInd2 == "WAIT") { 
         if ( my.alert.sound ) {Alert(mySymbol," Ind2 BUY "+ DoubleToStr(PriceInd2,xPoint)); }
         Print(mySymbol,"BUY");
      }
    }

   ColorValuesInd2  = Lime ; 
   LgTriggerInd2 =  true ; 
   }
   
   if(close < Ind2MA_Cur
   && Ind2MA_Cur < Ind2MA_Prev) {  

   tAlertInd2       = "SHORT" ;
   if (ShTriggerInd2 == false)    {
       PriceInd2 = close ; 
       ArrowInd2 = ArrowS ; colorArrowInd2 = Red ;
       if (pAlertInd2 == "WAIT") { 
          if ( my.alert.sound ) {Alert(mySymbol," Ind2 SHORT "+ DoubleToStr(PriceInd2,xPoint)); }
          Print(mySymbol,"SHORT");       
       }
    }

   ColorValuesInd2  = Red ; 
   ShTriggerInd2 =  true ;    
   }   

   if (pAlertInd2 == "WAIT") {  LgTriggerInd2 = false ; ShTriggerInd2 = false ; }
} // END INDICATOR 2


//+------------------------------------------------------------------+
//INDICATOR 2 
//+------------------------------------------------------------------+


void DoFilter()
{

   pAlertFilter        = tAlertFilter ;
   tAlertFilter        = "WAIT";
   PriceFilter         = 0 ;
   ColorValuesFilter   = myColorValues ; 

   FilterMA_Cur  = iMA(mySymbol,PERIOD_H1,12,0,MODE_EMA,PRICE_CLOSE,0);
   FilterMA_Prev = iMA(mySymbol,PERIOD_H1,12,0,MODE_EMA,PRICE_CLOSE,1);

   ArrowFilter = ArrowE ; colorArrowFilter = Yellow ; 
 

   if(close > FilterMA_Cur
   && FilterMA_Cur > FilterMA_Prev) {

   tAlertFilter       = "BUY";
   if (LgTriggerFilter == false)   { 
      PriceFilter = close ;
      ArrowFilter = ArrowN ; colorArrowFilter = Lime ;
      if (pAlertFilter == "WAIT") { 
         if ( my.alert.sound ) {Alert(mySymbol," Filter BUY "+ DoubleToStr(PriceFilter,xPoint)); }
         Print(mySymbol,"BUY");
      }
    }

   ColorValuesFilter  = Lime ; 
   LgTriggerFilter =  true ; 
   }
   
   if(close < FilterMA_Cur
   && FilterMA_Cur < FilterMA_Prev) {  

   tAlertFilter       = "SHORT" ;
   if (ShTriggerFilter == false)    {
       PriceFilter = close ; 
       ArrowFilter = ArrowS ; colorArrowFilter = Red ;
       if (pAlertFilter == "WAIT") { 
          if ( my.alert.sound ) {Alert(mySymbol," Filter SHORT "+ DoubleToStr(PriceFilter,xPoint)); }
          Print(mySymbol,"SHORT");       
       }
    }

   ColorValuesFilter  = Red ; 
   ShTriggerFilter =  true ;    
   }   

   if (pAlertFilter == "WAIT") {  LgTriggerFilter = false ; ShTriggerFilter = false ; }
} // END Filter


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


//+------------------------------------------------------------------+
// code needed to add strategy
//+------------------------------------------------------------------+

/* 

extern bool use.XYZ     = true ;

double PriceXYZ ; 

string   pAlertXYZ;
string   tAlertXYZ; 

color ColorValuesXYZ ;
 
bool   ShTriggerXYZ; 
bool   LgTriggerXYZ;

string ArrowXYZ  ;  
   
color colorArrowXYZ  ; 


if(use.XYZ )   { DoXYZ (); } 


void DoXYZ()
{

   pAlertXYZ        = tAlertXYZ ;
   tAlertXYZ        = "WAIT";
   PriceXYZ         = 0 ;
   ColorValuesXYZ   = myColorValues ; 

   ArrowXYZ      = ArrowE ; 
   colorArrowXYZ = Yellow ; 

   if(close == long) {

   tAlertXYZ       = "BUY";
   if (LgTriggerXYZ == false)   { 
      PriceXYZ = close ;
      ArrowXYZ = ArrowN ; 
      colorArrowXYZ = Lime ;
      if (pAlertXYZ == "WAIT") { 
         if ( my.alert.sound ) {Alert(mySymbol," XYZ BUY "+ DoubleToStr(PriceXYZ,xPoint)); }
         Print(mySymbol,"BUY");
      }
    }

   ColorValuesXYZ  = Lime ; 
   LgTriggerXYZ =  true ; 
   }
   
   if(close == short)  
   {

   tAlertXYZ       = "SHORT" ;
   if (ShTriggerXYZ == false)    {
       PriceXYZ = close ;
       ArrowXYZ = ArrowS ; 
       colorArrowXYZ = Red ; 
       if (pAlertXYZ == "WAIT") { 
          if ( my.alert.sound ) {Alert(mySymbol," XYZ SHORT "+ DoubleToStr(PriceXYZ,xPoint)); }
          Print(mySymbol,"SHORT");       
       }
    }

   ColorValuesXYZ  = Red ; 
   ShTriggerXYZ =  true ;    
   }   

   if (pAlertXYZ == "WAIT") {  LgTriggerXYZ = false ; ShTriggerXYZ = false ; }
} // END XYZ


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

if(use.XYZ )   { 
setObject(next(),tAlertXYZ   +" " + DoubleToStr(PriceXYZ,xPoint),700+price.x.offset, 5+price.y.offset, ColorValuesXYZ ,"Arial bold",hSize);               
setObject(next(),ArrowXYZ ,1175+price.x.offset, 5+price.y.offset, colorArrowXYZ ,"Wingdings",hSize);               

}  

if(use.XYZ )   {   
setObject(next(),"XYZ",700+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
}

*/