//+------------------------------------------------------------------+
//|       TRO_MA_COUNT                                               |
//|                                                                  |
//|   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_chart_window
#property indicator_buffers 1
#property indicator_color1 Lime  
//---- indicator parameters
extern string myMessage = "Crossed MA bars ago = ";
extern string note1 = "----------------------";
extern string note2 = "Fast Moving Average";
extern int FastMA=50;
extern string note3 = "0=Simple,1=Exponential";
extern string note4 = "2=Smooth,3=Linear Weighted";
extern int FastMA_Method=1;
 


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

extern string myIndName = "TRO_MACD" ; 
 
extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 20 ;
extern int win = 0;
extern int corner = 0 ;

//---- indicator buffers

double     ind_buffer4[];

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      window; 
int        i;  

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 close, close1 ;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  string FastMAname, SlowMAname, SignalMAname; 
  
   switch(FastMA_Method)
     {
      case 1 : FastMAname="EMA";  break;
      case 2 : FastMAname="SMMA"; break;
      case 3 : FastMAname="LWMA"; break;
      default :
         FastMA_Method=0;
         FastMAname="SMA";
     } 
       
   symbol = Symbol();
   period = Period(); 
   
   shortName = myIndName+ "["+FastMAname+"("+FastMA+")"+"]" ; 
 
   IndicatorShortName(shortName);   

   tObjName01 = myIndName+ symbol + period +FastMA  + "01";
   tObjName02 = myIndName+ symbol + period +FastMA  + "02";
   tObjName03 = myIndName+ symbol + period +FastMA  + "03";
   tObjName04 = myIndName+ symbol + period +FastMA  + "04";

   SetIndexBuffer(0,ind_buffer4);
 //  ArraySetAsSeries(Trend,true) ;        
 //  ArraySetAsSeries(ind_buffer4,true) ;  
   
   return(0);
  }

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

int start()
  {
  

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

//---- main loop
ArrayResize(Trend,Bars) ;   

   for(i=0; i<limit; i++)
      {
      close  = iClose(NULL,0,i) ;
      close1 = iClose(NULL,0,i+1) ;   
         
      if(( High[i] >= ind_buffer4[i]) && ( Low[i+1]  <= ind_buffer4[i+1] ) ) {Trend[i] = 0 ; } else {
      if(( Low[i]  <= ind_buffer4[i]) && ( High[i+1] >= ind_buffer4[i+1]) )  {Trend[i] = 0 ; } else {       
           Trend[i] = 1 ;  }}  
                    
      }   
      
   
      
TrendCount = 1 ;


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

if(close >= ind_buffer4[i]) { ArrowTrend = Arrow2 ; tColor = Lime ;} 
else { ArrowTrend = Arrow5 ;tColor = Red ; }

 tLastTrade = tTrade; 

   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName01, myMessage+TrendCount , myFontSize, myFont, tColor ); // + TrendCount
   ObjectSet(tObjName01, OBJPROP_CORNER, corner);
   ObjectSet(tObjName01, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName01, OBJPROP_YDISTANCE, 0+y.offset); //  
 
   ObjectCreate(tObjName02, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName02, ArrowTrend , myFontSize, "Wingdings", tColor ); 
   ObjectSet(tObjName02, OBJPROP_CORNER, corner);
   ObjectSet(tObjName02, OBJPROP_XDISTANCE, -22+x.offset); // 
   ObjectSet(tObjName02, OBJPROP_YDISTANCE, 0+y.offset); //  

/*
Comment(Trend[0],Trend[1],Trend[2],Trend[3],Trend[4],Trend[5],Trend[6],"\n",
       " ind_buffer4[0]", DoubleToStr(ind_buffer4[0],Digits),"\n", 
       " ind_buffer4[1]", DoubleToStr(ind_buffer4[1],Digits),"\n", 
       " ");
*/        
   return(0);
}
//+------------------------------------------------------------------+