//+------------------------------------------------------------------+
//|       TRO MACD 2 Colour HISTOGRAM                                |
//|                                                                  |
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND CONTRIBUTIONS ACCEPTED                               | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 

#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 
 
#property  indicator_separate_window
#property  indicator_buffers 8
#property  indicator_color1  Lime
#property  indicator_color2  Coral
//#property  indicator_color3  Black //Aqua
#property  indicator_color4  Red
#property  indicator_color7  Green
#property  indicator_color8  Maroon


#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width7 3
#property indicator_width8 3



//---- indicator parameters

extern bool sound.alert = false; 


extern string note1 = "----------------------";
extern string note2 = "Fast Moving Average";
extern int FastMA=6;
extern string note3 = "0=Simple,1=Exponential";
extern string note4 = "2=Smooth,3=Linear Weighted";
extern int FastMA_Method=1;
extern string note5 = "----------------------";
extern string note6 = "Slow Moving Average";
extern int SlowMA=240;
extern string note7 = "0=Simple,1=Exponential";
extern string note8 = "2=Smooth,3=Linear Weighted";
extern int SlowMA_Method=3;
extern string note9 = "----------------------";
extern string note10 = "Signal Moving Average";
extern int SignalMA=0;
extern string note11 = "0=Simple,1=Exponential";
extern string note12 = "2=Smooth,3=Linear Weighted";
extern int SignalMA_Method=1;


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

extern string myIndName = "TRO_MACD" ; 
extern bool   show.parms = false ;
extern string myDayOpen = "05:00" ; 

extern int    myTradeRisk      = 2 ;
extern int    myTrendRisk      = 2 ;
extern int    myBPBRisk        = 1 ;
extern int    myH1Risk         = 1 ;
extern int    myD1Risk         = 1 ;
extern int    myTunnelRisk     = 1 ;


extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 20 ;
extern int    xStart       = 0 ;
extern int    xOffSet      = 0 ;


//---- indicator buffers
double     ind_Buffer1[];
double     ind_Buffer2[];
double     ind_buffer3a[];
double     ind_buffer3b[];
double     ind_buffer3c[];
double     ind_buffer3d[];

double     ind_buffer4[];
double     ind_buffer5[];

string   ToBuy = "", ToSell = "" ;
string   tObjName01 , tObjName02 , tObjName03, tObjName04 ;
string   tTrade = "" ,  tLastTrade = "" ;   
string   symbol, period, shortName, ArrowTrend ;

color    tColor    = White, ColorToBuy, ColorToSell;


int      TrendCount = 0 , Trend[] , OldTrend = 0, OldBars, LongRisk, ShortRisk ;
int      corner;
int      window; 
int      win ;  

string Arrow12 = "á";  // 12 oclock 225
string Arrow2  = "ä";  //  2 oclock 228
string Arrow3  = "à";  //  3 oclock 224
string Arrow5  = "æ";  //  5 oclock 230
string Arrow6  = "â";  //  6 oclock 226
string ArrowUp = "";
string ArrowDn = "";

double point  ;
double H1_Open, D1_Open ;


datetime PreviousBarTime ;

//+------------------------------------------------------------------+
bool NewBar()
{
if(PreviousBarTime<Time[0])
{
PreviousBarTime = Time[0];
return(true);
}

return(false); // end NewBar() 
}


 
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  string FastMAname, SlowMAname, SignalMAname;
//---- 2 additional buffers are used for counting.
//   IndicatorBuffers(8);
//---- drawing settings

   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexDrawBegin(0,SignalMA);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexDrawBegin(1,SignalMA);
   
//   SetIndexStyle(4,DRAW_LINE);
//   SetIndexBuffer (4,ind_Buffer1);
//   SetIndexStyle(5,DRAW_LINE);
 //  SetIndexBuffer(5,ind_Buffer2);
   
   SetIndexStyle(6,DRAW_HISTOGRAM);
   SetIndexDrawBegin(6,SignalMA);
   SetIndexStyle(7,DRAW_HISTOGRAM);
   SetIndexDrawBegin(7,SignalMA);
     
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
   
//---- indicator buffers mapping
   if(!SetIndexBuffer(0,ind_buffer3a) &&
      !SetIndexBuffer(1,ind_buffer3b) &&
      !SetIndexBuffer(2,ind_buffer4)  &&      
      !SetIndexBuffer(3,ind_buffer5)  &&
      !SetIndexBuffer(4,ind_Buffer1)  &&      
      !SetIndexBuffer(5,ind_Buffer2)  &&
      !SetIndexBuffer(6,ind_buffer3c) &&      
      !SetIndexBuffer(7,ind_buffer3d)  
      )
      
      Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   switch(FastMA_Method)
     {
      case 1 : FastMAname="EMA";  break;
      case 2 : FastMAname="SMMA"; break;
      case 3 : FastMAname="LWMA"; break;
      default :
         FastMA_Method=0;
         FastMAname="SMA";
     }
   switch(SlowMA_Method)
     {
      case 1 : SlowMAname="EMA";  break;
      case 2 : SlowMAname="SMMA"; break;
      case 3 : SlowMAname="LWMA"; break;
      default :
         SlowMA_Method=0;
         SlowMAname="SMA";
     }
   switch(SignalMA_Method)
     {
      case 1 : SignalMAname="EMA";  break;
      case 2 : SignalMAname="SMMA"; break;
      case 3 : SignalMAname="LWMA"; break;
      default :
         SignalMAname=0;
         SignalMAname="SMA";
     }
//   IndicatorShortName("MACD ["+FastMAname+"("+FastMA+"),"+SlowMAname+"("+SlowMA+"),"+SignalMAname+"("+SignalMA+")]");
   
   symbol = Symbol();
   period = Period(); 
   point  = MarketInfo(symbol,MODE_POINT);  
   
   if(show.parms) { shortName = myIndName+ "["+FastMAname+"("+FastMA+"),"+SlowMAname+"("+SlowMA+"),"+SignalMAname+"("+SignalMA+")]" ; }
   else {shortName = myIndName ;}
   IndicatorShortName(shortName);   

   tObjName01 = myIndName+ symbol + period +FastMA+SlowMA + "01";
   tObjName02 = myIndName+ symbol + period +FastMA+SlowMA + "02";
   tObjName03 = myIndName+ symbol + period +FastMA+SlowMA + "03";
   tObjName04 = myIndName+ symbol + period +FastMA+SlowMA + "04";
         
   return(0);
  }

//+------------------------------------------------------------------+  
int deinit()
  {
   ObjectsDeleteAll(win);   
   return(0);
  }  
  
//+------------------------------------------------------------------+

int start()
  {
  
   win =    WindowFind(shortName);
   
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//---- macd counted in the 1-st additional buffer

   for(int i=0; i<limit; i++)
      ind_buffer4[i]=iMA(NULL,0,FastMA,0,FastMA_Method,PRICE_CLOSE,i)
                        -iMA(NULL,0,SlowMA,0,SlowMA_Method,PRICE_CLOSE,i);
                        
//---- signal line counted in the 2-nd additional buffer

   for(i=0; i<limit; i++)
      ind_buffer5[i]=iMAOnArray(ind_buffer4,Bars,SignalMA,0,SignalMA_Method,i);

//---- main loop

   double value=0, pvalue=0;
   for(i=limit; i>=0; i--)
      {
         ind_buffer3a[i]=0.0;
         ind_buffer3b[i]=0.0;      
         ind_buffer3c[i]=0.0;
         ind_buffer3d[i]=0.0;             
         
         value=ind_buffer4[i]-ind_buffer5[i];
         pvalue=ind_buffer4[i+1]-ind_buffer5[i+1];
           
         if (value > 0 && value >= pvalue ) { ind_buffer3a[i]=value; Trend[i] = 2 ; }
         if (value > 0 && value < pvalue )  { ind_buffer3c[i]=value; Trend[i] = 1 ; }         
         if (value < 0 && value < pvalue )  { ind_buffer3b[i]=value; Trend[i] = -2 ; }
         if (value < 0 && value >= pvalue ) { ind_buffer3d[i]=value; Trend[i] = -1 ; }        
        
      }   

LongRisk  = 0 ;  
ShortRisk = 0 ; 
             
if(  ind_buffer3a[0] != 0 && ind_buffer3a[1] == 0 ) { ShortRisk = myTradeRisk  ; ArrowTrend = ArrowUp ; tTrade = "BUY! " ; tColor = indicator_color1 ; } else {
if(  ind_buffer3a[0] != 0 ) { ShortRisk = myTrendRisk  ; ArrowTrend = ArrowUp ; tTrade = "UpTrend " ; tColor = indicator_color1 ; } else {
if(  ind_buffer3b[0] != 0 && ind_buffer3b[1] == 0 ) {LongRisk  = myTradeRisk  ; ArrowTrend = ArrowDn ; tTrade = "SHORT! " ; tColor = indicator_color4 ; } else {
if(  ind_buffer3b[0] != 0 ) { LongRisk  = myTrendRisk  ; ArrowTrend = ArrowDn ; tTrade = "DnTrend " ; tColor = indicator_color4 ; } else {
if(  ind_buffer3c[0] != 0 ) { LongRisk  = myBPBRisk  ; ShortRisk = myBPBRisk  ; ArrowTrend = ArrowDn ; tTrade = "PullBack" ; tColor = indicator_color7 ; } else {
if(  ind_buffer3d[0] != 0 ) { ShortRisk = myBPBRisk  ; LongRisk  = myBPBRisk  ;  ArrowTrend = ArrowUp ; tTrade = "Bounce  " ; tColor = indicator_color8 ; } }}}}}

   int   yy = TimeYear(Time[0]);
   int   mm = TimeMonth(Time[0]);
   int   dd = TimeDay(Time[0]);
  
   string yymmddms = yy+"."+mm+"."+dd+ myDayOpen ;
         
   datetime var1 = StrToTime(yymmddms);
     
   int shift=iBarShift(NULL,0,var1,false);   


D1_Open = iOpen(NULL,PERIOD_D1,shift) ;

H1_Open = iOpen(NULL,PERIOD_H1,0) ;

if( Close[0] >= H1_Open ) { ShortRisk = ShortRisk + myH1Risk  ; } else { LongRisk  = LongRisk + myH1Risk  ; }
if( Close[0] >= D1_Open ) { ShortRisk = ShortRisk + myD1Risk ; }  else { LongRisk  = LongRisk + myD1Risk ; }


int BarShift = iBarShift(NULL,60,Time[0],true); 
   
double tunnel1 = iMA(NULL,60,144,0,1,0,BarShift);       
double tunnel2 = iMA(NULL,60,169,0,1,0,BarShift); 

if( Close[0] >= tunnel1 && Close[0] >= tunnel2 ) { ShortRisk = ShortRisk + myTunnelRisk ; }   
if( Close[0] < tunnel1  && Close[0] <  tunnel2 ) { LongRisk  = LongRisk  + myTunnelRisk ; }  

int H1_SR_Risk = 0 ;
int D1_SR_Risk = 0 ;

int hh, ll, fRisk;

double cc, xRES, xSUP, xRANGE, xRESpips, xSUPpips, xRet, xRetPct ;


if( H1_SR_Risk == 1  ) { LongRisk  = LongRisk  + myH1Risk  ; }
if( H1_SR_Risk == -1 ) { ShortRisk = ShortRisk + myH1Risk  ; }
 
if( D1_SR_Risk == 1  ) { LongRisk  = LongRisk  + myD1Risk  ; }
if( D1_SR_Risk == -1 ) { ShortRisk = ShortRisk + myD1Risk  ; }



if( LongRisk <= ShortRisk ) { ColorToBuy = Lime; ColorToSell = Red; ToBuy = "Long Risk" ; ToSell = "Short Risk"; ArrowUp = Arrow12 ; ArrowDn = Arrow5 ; } 
else {  ColorToBuy = Red; ColorToSell = Lime; ToBuy = "Long Risk" ; ToSell = "Short Risk"; ArrowUp = Arrow2 ; ArrowDn = Arrow6 ; } 

TrendCount = 1 ;
ArrayResize(Trend,Bars) ;
for(i=0; i<=Bars; i++)
{   
if( Trend[i] == Trend[i+1] ) { TrendCount = TrendCount + 1 ; } else { break ; }  
}

if(sound.alert)
{ 
   if( tTrade == "BUY! "   && tTrade != tLastTrade ) { Alert("MACD " + tTrade + " " + symbol ) ; }
   if( tTrade == "SHORT! " && tTrade != tLastTrade ) { Alert("MACD " + tTrade + " " + symbol ) ; }  
}

 tLastTrade = tTrade; 


   ObjectCreate(tObjName02, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName02, ArrowTrend , myFontSize, "Wingdings", tColor ); 
   ObjectSet(tObjName02, OBJPROP_CORNER, 0);
   ObjectSet(tObjName02, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName02, OBJPROP_YDISTANCE, 0+y.offset); //  

   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName01, tTrade + " " + TrendCount , myFontSize, myFont, tColor ); // + TrendCount
   ObjectSet(tObjName01, OBJPROP_CORNER, 0);
   ObjectSet(tObjName01, OBJPROP_XDISTANCE, myFontSize+x.offset); // 
   ObjectSet(tObjName01, OBJPROP_YDISTANCE, 0+y.offset); //  




   ObjectCreate(tObjName03, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName03, ToBuy + " " + LongRisk , 8, myFont, ColorToBuy );  
   ObjectSet(tObjName03, OBJPROP_CORNER, 0);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 10+myFontSize+y.offset); //  

   ObjectCreate(tObjName04, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName04, ToSell  + " " + ShortRisk , 8, myFont, ColorToSell );  
   ObjectSet(tObjName04, OBJPROP_CORNER, 0);
   ObjectSet(tObjName04, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName04, OBJPROP_YDISTANCE, (5+myFontSize)*2+y.offset); // 
        
   return(0);
  }
  
 
//+------------------------------------------------------------------+
/*


if( NewBar() == true ) 
{
   if( Trend[0] == Trend[1] ) { TrendCount = TrendCount + 1 ; } else { TrendCount = 1 ;  } 
} // if( NewBar() == true ) 



TrendCount = 1 ;

for(i=0; i<=limit; i++)
{   
if( Trend[i] == Trend[i+1] ) { TrendCount = TrendCount + 1 ; } else { break ; }  
}




if( Bars != OldBars ) 
{    if( Trend == OldTrend ) { TrendCount = TrendCount + 1 ; } else { TrendCount = 1 ;  } }

OldBars = Bars ;
*/


//   for(i=limit; i>=0; i--){ ind_buffer4[i] = 0; ind_buffer5[i] = 0; }
/*
i=0 ;
Comment( "ind_buffer3a[i]=" , DoubleToStr(ind_buffer3a[i],Digits)  , "\n" ,
         "ind_buffer3b[i]=" ,DoubleToStr(ind_buffer3b[i],Digits)   ,  "\n" ,
         "ind_buffer3c[i]=" ,DoubleToStr(ind_buffer3c[i],Digits)   ,  "\n" ,         
         "ind_buffer3d[i]=" ,DoubleToStr(ind_buffer3d[i] ,Digits)  ,  "\n" ,
         "value=" ,DoubleToStr(value ,Digits)  ,  "\n" ,
         "pvalue=" ,DoubleToStr(pvalue ,Digits)  ,  "\n" ,
         "i=" , i         
                           
           ) ;  
           
Comment( "tunnel1=" , DoubleToStr(tunnel1,Digits)  , "\n" ,
         "tunnel2=" ,DoubleToStr(tunnel2,Digits)   ,  "\n" ) ;
       
*/

