//+------------------------------------------------------------------+
//|    TRO_AggregateTrend                        Aggregate Trend.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, S M Jones"
#property link      "sjcoinc2000@yahoo.com"

#property indicator_separate_window

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

extern int myCorner = 0 ;
extern int myChartY = 100 ;

extern int myChartPeriod = 0 ;
extern int myBars        = 24 ;

extern string myName     ="TRO AggTrend";
extern string myFont  = "Courier" ; // "Verdana"
extern int myFontSize = 12 ; //9

extern color ColorHeading    =  Gray ; 
extern color ColorValue      = LightGray ; 



extern int     ATRTimeFrame = 0; // In minutes
extern int     ATRShort = 20;    // Short term 
extern int     ATRLong = 250;   // Longer term
extern int     PeriodSize = 5; // Time Frame
extern int     BarCount = 48; // Look back period
extern double  Factor = 1.1;  // What is the beginning of a trend 1.1:1



string S01,S02,S03,S04,S05 ,S06 , S07 , S08, S09, S10, S11, S12, S13; 

string symbol, tChartPeriod ;  

int    digits, period, win ; 
string   shortName;
int      corner;
int      totalLabels;
int      window;   

double point, open, high, low, close ;  
double X01,X02,X03,X04,X05 ;
double X06 ;   
double X07 ;
double X08, X09, X10, X11, X12, X13; 

string dHead01, dHead02;
string dLabel01, dValue01;
string dLabel02, dValue02;
string dLabel03, dValue03;
string dLabel04, dValue04;
string dLabel05, dValue05;
string dLabel06, dValue06;
string dLabel07, dValue07;
string dLabel08, dValue08;
string dLabel09, dValue09;
string dLabel10, dValue10;
string dLabel11, dValue11;
string dLabel12, dValue12;
string dLabel13, dValue13;

string   labelNames;

    int i,Strength, brokerhour,brokermin, brokersecond;
    double up, down, o, h, l, c,Range,temp,ratio,ATR,ATRL;
    string Trend  ;
    int ATRi ;
    int ATRiL  ; 

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
 
   shortName  = MakeUniqueName("TRO AT ","");
   labelNames = shortName;
   IndicatorShortName(shortName);
             
   if(myChartPeriod == 0) { myChartPeriod = Period() ; } 
   if(ATRTimeFrame == 0)  { ATRTimeFrame = Period() ; }       
   tChartPeriod =  TimeFrameToString(myChartPeriod) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
 
   win          = 0 ;
    
 dHead01="tagh01"+ tChartPeriod ;  dHead02="tag202"+ tChartPeriod ;     
    
 dLabel01="tag01"+ tChartPeriod ;  dValue01="tagv01"+ tChartPeriod ; 
 dLabel02="tag02"+ tChartPeriod ;  dValue02="tagv02"+ tChartPeriod ; 
 dLabel03="tag03"+ tChartPeriod ;  dValue03="tagv03"+ tChartPeriod ; 
 dLabel04="tag04"+ tChartPeriod ;  dValue04="tagv04"+ tChartPeriod ; 
 dLabel05="tag05"+ tChartPeriod ;  dValue05="tagv05"+ tChartPeriod ; 
 dLabel06="tag06"+ tChartPeriod ;  dValue06="tagv06"+ tChartPeriod ; 
 dLabel07="tag07"+ tChartPeriod ;  dValue07="tagv07"+ tChartPeriod ; 
 dLabel08="tag08"+ tChartPeriod ;  dValue08="tagv08"+ tChartPeriod ; 
 dLabel09="tag09"+ tChartPeriod ;  dValue09="tagv09"+ tChartPeriod ; 
 dLabel10="tag10"+ tChartPeriod ;  dValue10="tagv10"+ tChartPeriod ; 
 dLabel11="tag11"+ tChartPeriod ;  dValue11="tagv11"+ tChartPeriod ;     
 dLabel12="tag12"+ tChartPeriod ;  dValue12="tagv12"+ tChartPeriod ;    
 dLabel13="tag13"+ tChartPeriod ;  dValue13="tagv13"+ tChartPeriod ;     
  
 
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   ObjectsDeleteAll(win);  
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
bool sFirst = true ;

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

     Trend = "No Trend";
     ATRi = ATR;
     ATRiL = ATRL; 
     
    brokerhour = Hour();
    brokermin = Minute();
    brokersecond = Seconds();
    
    ATR = iATR(NULL, ATRTimeFrame, ATRShort, 0)/ Point;
    ATRL = iATR(NULL,ATRTimeFrame, ATRLong,0) / Point;

     ATRi = ATR;
     ATRiL = ATRL; 
     
    for(i=1; i<=BarCount; i++)  
      {
         o = iOpen(NULL,PeriodSize,i);
         h = iHigh(NULL,PeriodSize,i);
         l = iLow(NULL,PeriodSize,i);
         c = iClose(NULL,PeriodSize,i);
         //Range = h-l;
                  
         if(o<c)
            {
               Range = c-o;
               temp = up;
               up = temp+Range;
            }
            
         if(o>c)
            {
               Range = o-c;
               temp = down;
               down = temp+Range;
            }
         
      }          

   if ( up > down )
      { 
         ratio = up/down;
         if(ratio>Factor) Trend = "Trend UP";
         else Trend = "NO Trend";
      }
   if ( down > up )
      {
         ratio = -1*down/up;
         if(ratio < -1*Factor) Trend = "Trend DOWN";
         else Trend = "NO Trend";
      }
   Strength = up/Point - down/Point;
   
/*   
   Comment( brokerhour, ":",brokermin,":",brokersecond, "      ", Trend,"    ATR (", ATRTimeFrame,",",ATRShort,", ",ATRLong,") = ", ATRi,",  ", ATRiL,"   Ratio = ", ATR/ATRL,":1\n",
               "Strength = ", Strength, "     Up = ", up/Point, "     Down = ", -1*down/Point,"   Ratio = ", ratio,
               " : 1\nTime Scale = ", PeriodSize, " Minutes", "      Length of Trend = ", BarCount, " Bars back");
*/        


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

void DoHead ()
{
int hSize = 9 ;

S03 = "ATR(" +  ATRTimeFrame + "," + ATRShort + "," + ATRLong + ")" ;  
              
setObject(next(),S03,100+price.x.offset, -10+price.y.offset,ColorHeading ,"Arial bold",hSize);

setObject(next(),"Ratio",225+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
                 
setObject(next(),"Strength",300+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);                

setObject(next(),"Up",375+price.x.offset, -10+price.y.offset,ColorHeading ,"Arial bold",hSize);

setObject(next(),"Down",425+price.x.offset, -10+price.y.offset,ColorHeading ,"Arial bold",hSize);

setObject(next(),"Ratio",500+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);
                             
setObject(next(),"Time Scale",550+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
           
setObject(next(),"Length of Trend " ,625+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);           


}
//+------------------------------------------------------------------+ 

void DoShowGauge()
{


if(Trend == "Trend UP")   {ColorValue = Lime ; } else {
if(Trend == "Trend DOWN") {ColorValue = Red ; }  else {ColorValue = Yellow ; } }


string G03 = ATRi+ ", " + ATRiL  ; 
string G04 = DoubleToStr(ATR / ATRL , 4 ) + ":1" ; 

setObject(next(),myName,5+price.x.offset, 5+price.y.offset,ColorHeading ,"Arial bold",9);

setObject(next(),G03,100+price.x.offset, 0+price.y.offset,ColorValue ,"Arial bold",15);

setObject(next(),G04,200+price.x.offset, 0+price.y.offset, ColorValue ,"Arial bold",15);               
                 
setObject(next(),DoubleToStr( Strength ,0),300+price.x.offset, 0+price.y.offset, ColorValue ,"Arial bold",15);               
  
setObject(next(),DoubleToStr(up/Point,0),375+price.x.offset, 0+price.y.offset,ColorValue ,"Arial bold",15);
setObject(next(),DoubleToStr((-1*down)/Point,0),425+price.x.offset, 0+price.y.offset,ColorValue ,"Arial bold",15);

setObject(next(), DoubleToStr(ratio,4) + ":1"  ,475+price.x.offset, 0+price.y.offset, ColorValue ,"Arial bold",15);
            
setObject(next(),PeriodSize,575+price.x.offset, 0+price.y.offset, ColorValue ,"Arial bold",15);               
           
setObject(next(),BarCount+" "+Trend,625+price.x.offset, 0+price.y.offset, ColorValue ,"Arial bold",15);               


   
   
}

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

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


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

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      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;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}

/*
http://www.forexfactory.com/showpost.php?p=1581116&postcount=2

I thought about what a trend is for some time. 
The way I define an up trend is that there are more ticks up then there are ticks down 
for the given time frame I am looking at. 

So How do I measure the strength of a trend? 
Well, if a sideways moving market = no trend for the given time frame, 
then that must mean that there is an equal number of buying to selling. 
There for the ratio of buying to selling should be the same , or it in fact should be 1:1.

So any thing that creates a number that is less than or more than 1:1 is in fact a trend. 
The larger the ratio (positive for up trend, negative for down trend) the stronger the trend is.

This indicator is my definition of a trend. I only did a text version, 
because I can keep the values in my head and I do not like to cluter up the chart with abunch of lines.

The first part is a comparison of a short term ATR for a given time frame to a longer term for the same time frame. 
This tells me if volatility is increasing or decreasing.

The second part tells me the trend and the ratio. This tells me the strength of the trend. a 1:1 ratio is No Trend.

*/