//+------------------------------------------------------------------+
//|       TRO_MACD_TRADE                                             |
//|                                                                  |
//|   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 1
#property indicator_color1 Lime   

//---- indicator parameters




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



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

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


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


//---- indicator buffers
double ExtMapBuffer3[];
 double    ArrowTrend[];
 
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  ;

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 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,EMPTY, 2, Lime);
   SetIndexBuffer(0,ArrowTrend);
/*  
   SetIndexStyle(1,DRAW_LINE, EMPTY, 2, Yellow);
   SetIndexLabel (1, "Smooth Moving Average");
   SetIndexBuffer(1,Trend); 
*/     
//---- indicator buffers mapping

//---- 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(); 
   
   if(show.parms) { shortName = myIndName+ "["+FastMAname+"("+FastMA+"),"+SlowMAname+"("+SlowMA+"),"+SignalMAname+"("+SignalMA+")]" ; }
   else {shortName = myIndName ;}
   IndicatorShortName(shortName);   

  ArraySetAsSeries(ind_buffer4,true) ;
           
   return(0);
  }

//+------------------------------------------------------------------+  
int deinit()
  {

   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

ArrayResize(ArrowTrend,Bars) ;
ArrayResize(ExtMapBuffer3, Bars ) ;
ArrayResize(ind_buffer3a,Bars) ;
ArrayResize(ind_buffer3c, Bars ) ;
ArrayResize(ind_buffer3b,Bars) ;
ArrayResize(ind_buffer3d, Bars ) ;
ArrayResize(ind_buffer4,Bars) ;
ArrayResize(ind_buffer5, Bars ) ;
 
   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 ; }        

if(  ind_buffer3a[i] != 0 && ind_buffer3a[i+1] == 0 ) { ArrowTrend[i] = 3 ; tTrade = "BUY! " ;   } else {
if(  ind_buffer3a[i] != 0 ) { ArrowTrend[i] = 2 ; tTrade = "UpTrend " ;  } else {
if(  ind_buffer3b[i] != 0 && ind_buffer3b[i+1] == 0 ) { ArrowTrend[i] = -3 ; tTrade = "SHORT! " ;   } else {
if(  ind_buffer3b[i] != 0 ) { ArrowTrend[i] = -2 ; tTrade = "DnTrend " ;  } else {
if(  ind_buffer3c[i] != 0 ) { ArrowTrend[i] = 1 ; tTrade = "PullBack" ;   } else {
if(  ind_buffer3d[i] != 0 ) { ArrowTrend[i] = -1 ; tTrade = "Bounce  " ;  } }}}}}
 
      }   
 
            
//  for(i=limit; i>=0; i--) { ExtMapBuffer3[i] = ArrowTrend[i] ;}

Comment(  ArrowTrend[0] , "--", Symbol() ) ;

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

/*

if(  ind_buffer3a[0] != 0 && ind_buffer3a[1] == 0 ) { ShortRisk = myTradeRisk  ; ArrowTrend[0] = 3 ; tTrade = "BUY! " ;   } else {
if(  ind_buffer3a[0] != 0 ) { ShortRisk = myTrendRisk  ; ArrowTrend[0] = 2 ; tTrade = "UpTrend " ;  } else {
if(  ind_buffer3b[0] != 0 && ind_buffer3b[1] == 0 ) {LongRisk  = myTradeRisk  ; ArrowTrend[0] = -3 ; tTrade = "SHORT! " ;   } else {
if(  ind_buffer3b[0] != 0 ) { LongRisk  = myTrendRisk  ; ArrowTrend[0] = -2 ; tTrade = "DnTrend " ;  } else {
if(  ind_buffer3c[0] != 0 ) { LongRisk  = myBPBRisk  ; ShortRisk = myBPBRisk  ; ArrowTrend[0] = 1 ; tTrade = "PullBack" ;   } else {
if(  ind_buffer3d[0] != 0 ) { ShortRisk = myBPBRisk  ; LongRisk  = myBPBRisk  ;  ArrowTrend[0] = -1 ; tTrade = "Bounce  " ;  } }}}}}




   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,0,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,0,Time[0],true); 
   
double tunnel1 = iMA(NULL,0,144,0,1,0,BarShift);       
double tunnel2 = iMA(NULL,0,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 ; }  


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

 tLastTrade = tTrade; 
*/