//+------------------------------------------------------------------+ 
//|   $_TRO_Trigger_Heat                                             | 
//|                                                                  | 
//|   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 string closetrue = "use.close = false, then use bid/ask" ;
extern bool use.close          = false ;
extern bool my.alert.sound     = true ;
extern bool my.alert.stop      = false ;
extern bool AlertOncePerBar    = true ;

extern bool use.HEAT        = true ;

extern string heat.close = "use.close = false, then prev midpoint" ;
extern bool use.Heat.close = false ;


extern string mySymbol = "" ;
extern int    myPeriod = 0 ;

extern string myHeading = "HEAT" ;

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


  double M5_Open, M5_Close, M5_Diff ;

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

  double Min5close;
  double Min5open;
  double Min5open1;


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


//
bool use.Heat.pMid = false ;
int   H_Heat, pM5_Heat ;
color H_color;
int M1_Heat,M5_Heat,M15_Heat,M30_Heat,H1_Heat,H4_Heat,D1_Heat,W1_Heat,MN1_Heat; 
int Trade_Heat, Total_Heat, Scalp_Heat ;
string Heat_String, Heat_Dir ;

//


double PriceHEAT ; 

string   pAlertHEAT, pHeat_Dir;
string   tAlertHEAT; 

color ColorValuesHEAT ;
 
bool   ShTriggerHEAT; 
bool   LgTriggerHEAT;
bool   OktoAlertHEAT = false;

string ArrowHEAT  ;  
   
color colorArrowHEAT  ; 

int pcounted_bars, counted_bars, bars ;

//


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

int fHeat( string myPair, int myPeriod, int myShift )  
{

color N_color  = Lime;
color NE_color = LimeGreen;
color E_color  = Gold;
color SE_color = Orange;
color S_color  = Red;



      double H_price = iClose(myPair,myPeriod,myShift);
      double H_close = iClose(myPair,myPeriod,myShift+1);
      double H_high  = iHigh(myPair,myPeriod,myShift+1);
      double H_low   = iLow(myPair,myPeriod,myShift+1);
      double H_pMid  = ( H_high + H_low ) * 0.50 ;    
     
if (use.Heat.close == false) use.Heat.pMid = true ;
      
while(true)
{
      
  if (H_price > H_high)                     { Heat_Dir = "+" ; H_Heat = 2 ;  H_color = N_color;  break; }
  if (use.Heat.close && H_price >  H_close) { Heat_Dir = "+" ; H_Heat = 1 ;  H_color = NE_color; break; }
  if (use.Heat.close && H_price==  H_close) { Heat_Dir = "=" ; H_Heat = 0 ;  H_color = E_color;  break; }
  if (use.Heat.pMid && H_price > H_pMid)    { Heat_Dir = "+" ; H_Heat = 1 ;  H_color = NE_color; break; }
  if (use.Heat.pMid && H_price== H_pMid)    { Heat_Dir = "=" ; H_Heat = 0 ;  H_color = E_color;  break; }  
  if (H_price < H_low)                      { Heat_Dir = "-" ; H_Heat = -2 ; H_color = S_color; break; }
                                              Heat_Dir = "-" ; H_Heat = -1 ; H_color = SE_color;  break; 
} // while
          
          
return( H_Heat ) ; 
}
//+------------------------------------------------------------------+
string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_MN1: tfs="MN1"; break;
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
   }
   return(tfs);
}

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   corner     = 0;
   shortName  = MakeUniqueName(myHeading,"");
   labelNames = shortName;
      IndicatorShortName(shortName);
 
 
 if( mySymbol == "" ) { mySymbol = Symbol() ; }
 
 if( myPeriod == 0 ) { myPeriod = Period() ; } 
 
//----
   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 ; 
 

   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);
 

   PrevMidH1      = (high1 + low1) * 0.50 ;
   PrevMidD1      = (pdayHigh + pdayLow) * 0.50 ;
   cdayMid        = (cdayHigh + cdayLow) * 0.50 ;   

   close       = iClose(mySymbol,myPeriod,0);
   open        = iOpen(mySymbol,myPeriod,0);     
   diff        = close - open ;     

int hSize = 9 ;

point =MarketInfo(mySymbol,MODE_POINT);  

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

counted_bars  = IndicatorCounted(); 
bars          = Bars - counted_bars ;
 

if( pcounted_bars != counted_bars
|| AlertOncePerBar == false ) { OktoAlertHEAT = true ;  } 

if(use.HEAT ) { DoHEAT (); }  

pcounted_bars = counted_bars ;

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

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) + " Heat:"+Heat_String); }
                          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, colorArrowcMid ,"Arial bold",hSize);               

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


if(use.HEAT )   { 
setObject(next(),tAlertHEAT   +" " + DoubleToStr(PriceHEAT,xPoint),400+price.x.offset, 5+price.y.offset, ColorValuesHEAT ,"Arial bold",hSize);               
//setObject(next(),ArrowHEAT ,475+price.x.offset, 5+price.y.offset, colorArrowHEAT ,"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.HEAT )   {   
setObject(next(),"HEAT",400+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
}

        
}


//+------------------------------------------------------------------+
// HEAT strategy
//+------------------------------------------------------------------+


void DoHEAT()
{

   close             = cdayClose ;
   pHeat_Dir         = Heat_Dir ;
   pAlertHEAT        = tAlertHEAT ;
   tAlertHEAT        = "WAIT";
//   PriceHEAT         = 0 ;
   ColorValuesHEAT   = myColorValues ; 

//   ArrowHEAT      = ArrowE ; 
//   colorArrowHEAT = Yellow ; 


Heat_String = "" ;

pM5_Heat = fHeat(mySymbol, PERIOD_M5, 1 ) ;
M5_Heat  = fHeat(mySymbol, PERIOD_M5, 0 ) ;

Heat_String = pHeat_Dir + Heat_Dir;
 

if(use.close) { bid = close ; ask = close ; } 
else          { bid = MarketInfo(mySymbol,MODE_BID); MarketInfo(mySymbol,MODE_ASK);  } 



   if(M5_Heat > 0
   && pM5_Heat < 0
   && diff >= 0) {
   
   tAlertHEAT       = "BUY";
   if (LgTriggerHEAT == false)   { 

      ArrowHEAT = ArrowN ; 
      colorArrowHEAT = Lime ;
      if (pAlertHEAT == "WAIT" && OktoAlertHEAT ) {
         PriceHEAT = close ; 
         OktoAlertHEAT = false ;
         if ( my.alert.sound ) {Alert(mySymbol," HEAT BUY "+ DoubleToStr(PriceHEAT,xPoint) + " Heat:"+Heat_String); }
         Print(mySymbol,"BUY");
      }
    }

   ColorValuesHEAT  = Lime ; 
   LgTriggerHEAT =  true ; 
   }
   
   if(M5_Heat < 0
   && pM5_Heat > 0
   && diff <= 0) {
      
   tAlertHEAT       = "SHORT" ;
   if (ShTriggerHEAT == false)    {

       ArrowHEAT = ArrowS ; 
       colorArrowHEAT = Red ; 
       if (pAlertHEAT == "WAIT" && OktoAlertHEAT ) {
          PriceHEAT = close ; 
          OktoAlertHEAT = false ;
          if ( my.alert.sound ) {Alert(mySymbol," HEAT SHORT "+ DoubleToStr(PriceHEAT,xPoint) + " Heat:"+Heat_String); }
          Print(mySymbol,"SHORT");       
       }
    }

   ColorValuesHEAT  = Red ; 
   ShTriggerHEAT =  true ;    
   }   

   if (pAlertHEAT == "WAIT") { colorArrowHEAT = Yellow ;  ArrowHEAT = ArrowE;  LgTriggerHEAT = false ; ShTriggerHEAT = false ; }
} // END HEAT


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


//+------------------------------------------------------------------+
// 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 
   && Trade_Heat > 0 ) {
   
   tAlertXYZ       = "BUY";
   if (LgTriggerXYZ == false)   { 

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

   ColorValuesXYZ  = Lime ; 
   LgTriggerXYZ =  true ; 
   }
   
   if(close == short
   && Trade_Heat > 0 ) {  

   tAlertXYZ       = "SHORT" ;
   if (ShTriggerXYZ == false)    {

       ArrowXYZ = ArrowS ; 
       colorArrowXYZ = Red ; 
       if (pAlertXYZ == "WAIT") {
          PriceXYZ = close ; 
          if ( my.alert.sound ) {Alert(mySymbol," XYZ SHORT "+ DoubleToStr(PriceXYZ,xPoint) + " Heat:"+Heat_String); }
          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);               
}

*/