//+------------------------------------------------------------------+ 
//|   tro_trend_stats                                                | 
//|                                                                  | 
//|   Copyright © 2008, AVERY T. HORTON, JR. AKA TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
 

#property indicator_chart_window

extern int myChartX   = 450 ;
extern int myChartY   = 0 ;
extern int myCorner   = 0 ;
extern string myFont  = "Courier" ;
extern int myFontSize = 12 ;

extern int       myBars       = 21 ;
//extern int       myShift      = 1 ;
extern int       myPeriod     = 0 ;



double Hi, Li, C;

double prev_high, prev_low, prev_close, D, p, value ;
int h  ;
string lbl[15], lbl2[15];
int n, j, i, shift, stop, nHH, nLL, gHH, gLL  ;

color pColor = Blue ;

string symbol, tChartPeriod, ShortName ;  

int    digits, period, win ; 
double    H1L0, L1H0 ;

double point,  open, high, low, close, mid, range, pClose, pOpen, pHigh, pLow, pMid, pRange, cRange  ;  
double xMax = - 1,  xMin = 9999999999999, xDiff, xClose, xHigh, xLow, xRunUp, xRunDn, xPain ;
double High0, Low0, Open0,  Close0, Open1,High1, Low1, Close1   ;  
double xMaxRange = - 1,  xMinRange = 9999999999999;

double     ind_bufferOp[];
double     xTrendOp[];
double     xCountOp;

double     ind_bufferHi[];
double     xTrendHi[];
double     xCountHi; 

double     ind_bufferLo[];
double     xTrendLo[];
double     xCountLo; 

double     ind_bufferCl[];
double     xTrendCl[];
double     xCountCl; 

double HH, LL, HL, LH ;
double rHH, rLL, rHL, rLH ;

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

int init()
  {
   if(myPeriod == 0) { period = Period() ; } else { period = myPeriod ; }    
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    = "tstats"+symbol+period ;
   win          = 0 ;
   
   shift         = myBars  - 1 ;  

   ArrayResize(ind_bufferOp, myBars) ;   
   ArrayResize(ind_bufferHi, myBars) ;
   ArrayResize(ind_bufferLo, myBars) ;
   ArrayResize(ind_bufferCl, myBars) ;      
   return(0);
  }
  
//+------------------------------------------------------------------+
int deinit()
  {
//----
ObjectsDeleteAll();
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
{

n        = 10;

open     = iOpen( symbol, period, 0 ) ;
close    = iClose( symbol, period, 0 ) ;
high     = iHigh( symbol, period, 0 ) ;
low      = iLow( symbol, period, 0 ) ;

pOpen    = iOpen( symbol, period, shift ) ;
pClose   = iClose( symbol, period, shift ) ;
pHigh    = iHigh( symbol, period, shift ) ;
pLow     = iLow( symbol, period, shift ) ;

xPain    = ((close-open)+(close-high)+(close-low)) * 0.5 ;

xMax     = -1 ;
xMin     = 999999999 ;

xMaxRange     = -1 ;
xMinRange     = 999999999 ;


for( i = 0 ; i <= shift ; i++ )
{
   xHigh     = iHigh( symbol, period, i ) ;
   xLow      = iLow( symbol, period, i ) ;
   xDiff     = xHigh - xLow ;
   
   if( xHigh > xMax ) { xMax = xHigh ; nHH = i ; }
   if( xLow  < xMin ) { xMin = xLow ;  nLL = i ; }

   if( xDiff > xMaxRange ) { xMaxRange = xDiff ; gHH = i ; }
   if( xDiff < xMinRange ) { xMinRange = xDiff ; gLL = i ; }   
   
}

xRunUp = ( xMax - pOpen ) / point ;
xRunDn = ( pOpen - xMin ) / point ;
xDiff  = ( close - pOpen ) / point ;


range    = (xMax - xMin ) / point ; 


xMaxRange = xMaxRange  / point ; 
xMinRange = xMinRange  / point ; 


HH = 0;
LL = 0;
HL = 0; 
LH = 0;

   for(int i=myBars; i >= 0; i--) // initialize buffers to 0
   {  
      ind_bufferOp[i] = 0 ; 
      ind_bufferHi[i] = 0 ; 
      ind_bufferLo[i] = 0 ;       
      ind_bufferCl[i] = 0 ;               
   } 

   for( i=myBars-1; i >= 0; i--)
   {
   
      Open0  = iOpen(symbol,period,i) ;
      Open1  = iOpen(symbol,period,i+1) ;
      High0  = iHigh(symbol,period,i) ;
      High1  = iHigh(symbol,period,i+1) ;
      Low0   = iLow(symbol,period,i) ;
      Low1   = iLow(symbol,period,i+1) ;
      Close0 = iClose(symbol,period,i) ;
      Close1 = iClose(symbol,period,i+1) ;

   
      if(Open0 > Open1) { if(ind_bufferOp[i+1] <= 0) {ind_bufferOp[i] = 1;  } else { ind_bufferOp[i] =  ind_bufferOp[i+1] + 1 ; } }
      if(Open0 < Open1) { if(ind_bufferOp[i+1] >= 0) {ind_bufferOp[i] = -1;  } else { ind_bufferOp[i] =  ind_bufferOp[i+1] - 1 ; } }

      if(High0 > High1) { HH = HH + 1 ; if(ind_bufferHi[i+1] <= 0) {ind_bufferHi[i] = 1;  } else { ind_bufferHi[i] =  ind_bufferHi[i+1] + 1 ; } }
      if(High0 < High1) { LH = LH + 1 ; if(ind_bufferHi[i+1] >= 0) {ind_bufferHi[i] = -1;  } else { ind_bufferHi[i] =  ind_bufferHi[i+1] - 1 ; } }

      if(Low0 > Low1) { HL = HL + 1 ; if(ind_bufferLo[i+1] <= 0) {ind_bufferLo[i] = 1;  } else { ind_bufferLo[i] = ind_bufferLo[i+1] + 1 ; } }
      if(Low0 < Low1) { LL = LL + 1 ; if(ind_bufferLo[i+1] >= 0) {ind_bufferLo[i] = -1; } else { ind_bufferLo[i] = ind_bufferLo[i+1] - 1 ; } }

      if(Close0 > Close1) { if(ind_bufferCl[i+1] <= 0) {ind_bufferCl[i] = 1;  } else { ind_bufferCl[i] = ind_bufferCl[i+1] + 1 ; } }
      if(Close0 < Close1) { if(ind_bufferCl[i+1] >= 0) {ind_bufferCl[i] = -1; } else { ind_bufferCl[i] = ind_bufferCl[i+1] - 1 ; } }
                   
   } 

L1H0 = ( High0 - Low1 ) / point ;
L1H0 = NormalizeDouble( L1H0 , point ) ; 

H1L0 = ( Low0 - High1 ) / point ;
H1L0 = NormalizeDouble( H1L0 , point ) ; 

rHH = 100 * ( HH / myBars ) ; 
rLH = 100 * ( LH / myBars ) ;
rHL = 100 * ( HL / myBars ) ; 
rLL = 100 * ( LL / myBars ) ;   
  

j       = 0 ;
lbl[j]  = ShortName+j;
lbl2[j] = tChartPeriod + " Trend Stats";
DoShow(j, SteelBlue);



j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Open(" + myBars + ") " + DoubleToStr(pOpen,digits);
//DoColor( range, pRange ) ; 
DoShow(j, Orange); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Close    " + DoubleToStr(close,digits);
DoColor( close, pOpen ) ; 
DoShow(j, pColor); 



j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Diff(" + myBars + ") "  + DoubleToStr(xDiff,0);
DoColor( close , open ) ; 
DoShow(j, pColor);  
 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Range(" + myBars + ")" + DoubleToStr(range,0);
//DoColor( range, pRange ) ; 
DoShow(j, Orange); 


j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Max[" + nHH +"]  " + DoubleToStr(xMax,digits);
//DoColor( xMax, open ) ; 
DoShow(j, Orange);
 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Min[" + nLL + "]  "  + DoubleToStr(xMin,digits);
//DoColor( high, pHigh ) ; 
DoShow(j, Orange);

 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Run Up   " + DoubleToStr(xRunUp,0);
//DoColor( low, pLow ) ; 
DoShow(j, Orange);  



j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Run Dn   " + DoubleToStr(xRunDn,0);
//DoColor( open, pOpen ) ; 
DoShow(j, Orange);  
 


j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Pain     " + DoubleToStr(xPain,digits);
DoColor( xPain, 0 ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "+/-OPEN  " + DoubleToStr(ind_bufferOp[0],0);
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 


j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "+/-HIGH  " + DoubleToStr(ind_bufferHi[0],0);
DoColor( ind_bufferHi[0], 0 ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "+/-LOW   " + DoubleToStr(ind_bufferLo[0],0);
DoColor( ind_bufferLo[0], 0 ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "+/-CLOSE " + DoubleToStr(ind_bufferOp[0],0);
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "L1H0     " + DoubleToStr(L1H0,0);
DoColor( High0 , Low1 ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "H1L0     " + DoubleToStr(H1L0,0);
DoColor( Low0 , High1) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "HH(" + myBars +")  " + DoubleToStr(rHH,0) + "%" ;
DoColor( rHH , rLH ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "LH(" + myBars +")  " + DoubleToStr(rLH,0) + "%" ;
DoColor( rLH , rHH ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "HL(" + myBars +")  " + DoubleToStr(rHL,0) + "%" ;
DoColor( rHL , rLL ) ; 
DoShow(j, pColor); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "LL(" + myBars +")  " + DoubleToStr(rLL,0) + "%" ;
DoColor( rLL , rHL ) ; 
DoShow(j, pColor); 


j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "MaxRange " + DoubleToStr(xMaxRange,0);
//DoColor( xPain, 0 ) ; 
DoShow(j, Orange); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Max Date " + TimeToStr( Time[gHH] ) ;
//DoColor( xPain, 0 ) ; 
DoShow(j, Orange); 




j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "MinRange " + DoubleToStr(xMinRange,0);
//DoColor( xPain, 0 ) ; 
DoShow(j, Orange); 

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Min Date " + TimeToStr( Time[gLL] ) ;
//DoColor( xPain, 0 ) ; 
DoShow(j, Orange); 

   return(0);
  }

//+------------------------------------------------------------------+
  
void DoShow( int u, color dsColor )
{
ObjectCreate(lbl[u],23,0,Time[0],PRICE_CLOSE);
ObjectSet(lbl[u], OBJPROP_CORNER, myCorner );
ObjectSet(lbl[u],OBJPROP_XDISTANCE, myChartX);
ObjectSet(lbl[u],OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(lbl[u],lbl2[u],myFontSize,myFont,dsColor);
n = n+20;

   return(0);
  }

//+------------------------------------------------------------------+
  
void DoColor( double c1, double c2 )
{

if(c1 > c2 ) { pColor = Lime ; } else {
if(c1 < c2 ) { pColor = Red ; } else { pColor = Yellow ; } } 

}  
  
//+------------------------------------------------------------------+
void drawLabel(string name,double lvl,color Color)
{
    if(ObjectFind(name) != 0)
    {
        ObjectCreate(name, OBJ_TEXT, 0, Time[10], lvl);
        ObjectSetText(name, name, myFontSize, myFont , EMPTY);
        ObjectSet(name, OBJPROP_COLOR, Color);
    }
    else
    {
        ObjectMove(name, 0, Time[10], lvl);
    }
}

//+------------------------------------------------------------------+
void drawLine(double lvl,string name, color Col,int type)
{
         if(ObjectFind(name) != 0)
         {
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            if(type == 1)
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            else
            ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
            
            ObjectSet(name, OBJPROP_COLOR, Col);
            ObjectSet(name,OBJPROP_WIDTH,1);
            
         }
         else
         {
            ObjectDelete(name);
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            if(type == 1)
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            else
            ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
            
            ObjectSet(name, OBJPROP_COLOR, Col);        
            ObjectSet(name,OBJPROP_WIDTH,1);
          
         }
}

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

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

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

/*
Comment(
"HH= ", DoubleToStr(HH,digits), "\n",
"myBars= ", DoubleToStr(myBars,digits), "\n",
"rHH= ", DoubleToStr(rHH,digits), "\n",

""); 


Comment(
"High0= ", DoubleToStr(High0,digits), "\n",
"Low1= ", DoubleToStr(Low1,digits), "\n",
"L1H0= ", DoubleToStr(L1H0,digits), "\n",
"");


http://www.forexrealm.com/technical-analysis/technical-indicators/price-action-indicator.html



The Price Action Indicator (PAIN) provides a lot of helpful information using only today's open, high, low and close. 
This pays a single value that is rather efficient, building ideal limit-up and limit-down scenarios in bond futures. 
The output has also demonstrated to be firm with the interpretation of "Japanese candlestick pattern". 
Using the formula:

[(C-O)+(C-H)+(C-L)] / 2

where:

(C-O) defines Intra-Day Momentum,

(C-L) defines Late Selling Pressure (LSP) and

(C-H) defines Late Buying Pressure (LBP)

This yields a single value that has proven itself by constructing ideal limit-up and 
limit-down scenarios in bond futures. 
The output has also shown to be consistent with the interpretation of Japanese candlestick patterns.

The stock's price is under selling pressure if the Close is near the Low. 
The stocks price is under purchasing pressure, or as they say the "Bulls are driving up the price" 
if the Close is near the High. 
And in the end, a high PAIN value with the Close near the High will be an excellent potential long 
if the overall market conditions stay fortunate.

*/