//+------------------------------------------------------------------+ 
//|   _TRO_SAK_iIND_SW                                               | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpeldone@gmail.com                                        |  
//+------------------------------------------------------------------+ 
/*


TOP LEFT is the MAXIMUM Value

BOTTOM LEFT is the MINIMUM Value

TOP RIGHT is the AVERAGE Value

BOTTOM RIGHT is the NUMBER OF BARS CHECKED

BIG NUMBER IS THE CURRENT Value.

*/
#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

//---- indicator settings 
#property indicator_separate_window
#property indicator_buffers 0
#property indicator_minimum 0
#property indicator_maximum 1



//---- indicator parameters  
//extern int   Threshold       = 20; 

//extern int win = 1;

extern bool show.head = true ;
extern int price.x.offset= 0;//250
extern int price.y.offset= 10;//-160

extern string myFont  = "Arial bold" ; 
extern int myFontSize = 10 ; 

extern int   Chart_Period    = 0;

extern int   MaxBars         = 24;
extern string Ind_Name_1 ="iMACD";
extern string myName     ="MACD";
extern color  myIndColor = DarkGray ;

extern double iParm3 = 6;
extern double iParm4 = 240;
extern double iParm5 = 9;
extern double iParm6 = 0;
extern double iParm7 = 0;
extern double iParm8 = -100;
extern int    iMode  = 0;

extern color ColorHeading    =  Gray ; 
extern color ColorValue      = LightGray ; 
extern color ThresholdColor  = LimeGreen; 
extern color ColorABAvg      =  DarkViolet ;    
extern color ColorAvg        =  Orange ;  
extern color ColorABCur      =  Blue ; 
extern color ColorMinMax     =  Chocolate ; 
extern color ColorPosDiv     =  Green ; 
extern color ColorNegDiv     =  Red ; 

//---- indicator buffers 


double     MaxValue = 0 ; 
double     MinValue = 0 ; 
double     AvgValue = 0 ; 
double     xValue   = 0 ; 
double     tValue   = 0 ; 

double     zValue   = 0 ; 

//---- buffers



double cVal = 0, pVal = 0;


bool  show.info       = true;
bool  ShowChart       = true; 

double   AboveAvg, BelowAvg, AboveCur, BelowCur ;
string   labelNames;
string   shortName;
int      corner;
int      totalLabels;
int      window;  

string tNEGDivergence, tPOSDivergence ;
//+------------------------------------------------------------------+ 


int init() 
  { 
   corner     = 0;
   shortName  = MakeUniqueName("TW ","");
   labelNames = shortName;
      IndicatorShortName(shortName);
   return(0); 
  } 
//+------------------------------------------------------------------+ 
int deinit() 
{ 
//   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
   ObjectsDeleteAll( window);
   return(0);  
}  

//+------------------------------------------------------------------+ 
int start() 
  { 
  
bool sFirst = true ;

window      = WindowFind(shortName);
   
if ( sFirst ) {
sFirst = false ;

ObjectsDeleteAll( window);
DoHead() ;
}

if(Chart_Period == 0) Chart_Period = Period();

    MaxValue = 0 ; 
    MinValue = 999999 ; 

tNEGDivergence ="";
tPOSDivergence ="";

   double HighVal,LowVal; 
   int limit; 
   int counted_bars=IndicatorCounted(); 
    
//---- last counted bar will be recounted 
   if(counted_bars>0) counted_bars--; 
   if(counted_bars<0) return(-1); 
   

   AboveAvg = 0 ; 
   BelowAvg = 0 ; 
   AboveCur = 0 ; 
   BelowCur = 0 ;
  
   limit = MaxBars + 1;  

//---- Value counted in the 1-st buffer 


		double buf_tmp = 0 ;
		int z = 0 ;

DoWork(Symbol(),Chart_Period , 0 ) ;
zValue = cVal ;

   for(int i=1; i<limit; i++) 
   { 

     DoWork(Symbol(), Chart_Period , i ) ;
     DoDivergence( zValue, cVal, iClose(NULL,Chart_Period,0), iClose(NULL,Chart_Period,i), i ) ;
     

     tValue = cVal ; 
     
     buf_tmp =  buf_tmp + cVal ;
 
     
     if( tValue > MaxValue ) {MaxValue = tValue  ; }
     if( tValue < MinValue ) {MinValue = tValue  ; }  
    
     if( tValue > AvgValue ) {AboveAvg = AboveAvg + 1  ; }
     if( tValue < AvgValue ) {BelowAvg = BelowAvg + 1  ; }     

     if( tValue > zValue ) {AboveCur = AboveCur + 1  ; }
     if( tValue < zValue ) {BelowCur = BelowCur + 1  ; }     
                 
   } // for 

   AvgValue = buf_tmp / MaxBars ; // average 




string tPeriod = " "+TimeFrameToString( Chart_Period ) ;

if ( zValue > AvgValue ) { color ValueColor = ThresholdColor ; } else { ValueColor = ColorValue   ; }

setObject(next(),myName+"("+DoubleToStr(iParm3,0)+")",0+price.x.offset, 0+price.y.offset,ColorHeading ,myFont, myFontSize);

setObject(next(),tPeriod,150+price.x.offset, 0+price.y.offset,ColorHeading ,myFont, myFontSize);

setObject(next(),DoubleToStr(zValue,Digits),200+price.x.offset, 0+price.y.offset, ValueColor ,myFont, myFontSize);               
                 
setObject(next(),DoubleToStr( AvgValue ,Digits),250+price.x.offset, 0+price.y.offset, ColorAvg ,myFont, myFontSize);               
  

//string  XIN829 = DoubleToStr( MaxValue ,Digits) + " / " + DoubleToStr( MinValue ,Digits) ;    

setObject(next(),DoubleToStr( MaxValue ,Digits),300+price.x.offset, 0+price.y.offset,ColorMinMax ,myFont, myFontSize);
setObject(next(),DoubleToStr( MinValue ,Digits),350+price.x.offset, 0+price.y.offset,ColorMinMax ,myFont, myFontSize);


//string  XIN829b = DoubleToStr( AboveCur ,0) + " / " + DoubleToStr( BelowCur ,0) ; 

setObject(next(),DoubleToStr( AboveCur ,0),400+price.x.offset, 0+price.y.offset, ColorABCur ,myFont, myFontSize);
setObject(next(),DoubleToStr( BelowCur ,0),450+price.x.offset, 0+price.y.offset, ColorABCur ,myFont, myFontSize);     
                 
//string  XIN834 = DoubleToStr( AboveAvg ,0) + " / " + DoubleToStr( BelowAvg ,0) ;   
             
setObject(next(),DoubleToStr( AboveAvg ,0),500+price.x.offset, 0+price.y.offset, ColorABAvg ,myFont, myFontSize);               
setObject(next(),DoubleToStr( BelowAvg ,0),550+price.x.offset, 0+price.y.offset, ColorABAvg ,myFont, myFontSize);               
            

setObject(next(),tPOSDivergence,600+price.x.offset, 0+price.y.offset, ColorPosDiv ,"Arial bold",10);               
setObject(next(),tNEGDivergence,600+price.x.offset, 12+price.y.offset, ColorNegDiv ,"Arial bold",10);               
           
setObject(next(),MaxBars,800+price.x.offset, 0+price.y.offset, ColorAvg ,myFont, myFontSize);               
  
      

   WindowRedraw();    
 
//} 

   return(0); 
} 

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

 
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="MN1";
   }
   return(tfs);
}
 
 
 
//+------------------------------------------------------------------+

void DoDivergence( double curr, double prev, double zP, double zP2, int ii )
{
if( curr < prev && zP > zP2) {tNEGDivergence = tNEGDivergence +ii+" " ; }

if( curr > prev && zP < zP2) {tPOSDivergence = tPOSDivergence +ii+" " ; }

}

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


//+------------------------------------------------------------------+
void DoWork( string myPair, int myPeriod , int p )  
  
{
double cX, pX ;

while(true)
{

if ( Ind_Name_1=="tMid"  ) { cVal = (iHigh(myPair, myPeriod, p ) + iLow(myPair, myPeriod, p ) ) * 0.5  ; 
                             pVal = (iHigh(myPair, myPeriod, p+1 ) + iLow(myPair, myPeriod, p+1 ) ) * 0.5  ;  break;}

if ( Ind_Name_1=="tPrMid"  ) { cVal = (iHigh(myPair, myPeriod, p+1 ) + iLow(myPair, myPeriod, p+1 ) ) * 0.5  ; 
                               pVal = (iHigh(myPair, myPeriod, p+2 ) + iLow(myPair, myPeriod, p+2 ) ) * 0.5  ;  break;}


if ( Ind_Name_1=="iOpen"  ) { cVal = iOpen(myPair, myPeriod, iParm3 ); pVal = iOpen(myPair, myPeriod, iParm3+p+1 ); break;}

if ( Ind_Name_1=="iClose"  ) { cVal = iClose(myPair, myPeriod, iParm3 ); pVal = iClose(myPair, myPeriod, iParm3+p+1 ); break;}

if ( Ind_Name_1=="iHigh"  ) { cVal = iHigh(myPair, myPeriod, iParm3 ); pVal = iHigh(myPair, myPeriod, iParm3+p+1 ); break;}

if ( Ind_Name_1=="iLow"  )  { cVal = iLow(myPair, myPeriod, iParm3 );  pVal = iLow(myPair, myPeriod, iParm3+p+1 ); break;}

if ( Ind_Name_1=="iHighest"  ) { cX = iHighest(myPair, myPeriod, iParm3, iParm4, iParm5 );    cVal = iHigh(myPair, myPeriod, cX ) ;
                                 pX = iHighest(myPair, myPeriod, iParm3, iParm4, iParm5+1 );  pVal = iHigh(myPair, myPeriod, pX );  break;}
                                 
if ( Ind_Name_1=="iLowest"  )  { cX = iLowest(myPair, myPeriod, iParm3, iParm4, iParm5);    cVal = iLow(myPair, myPeriod, cX ) ;
                                 pX = iLowest(myPair, myPeriod, iParm3, iParm4, iParm5+1 ); pVal = iLow(myPair, myPeriod, pX ) ; break;}



if ( Ind_Name_1=="iAC"  ) { cVal = iAC(myPair, myPeriod, p ); pVal = iAC(myPair, myPeriod, p+1 ); break;}
                            
if ( Ind_Name_1=="iAD"  ) { cVal = iAD(myPair, myPeriod, p ); pVal = iAD(myPair, myPeriod, p+1 ); break;}
 
if ( Ind_Name_1=="iADX" ) { cVal = iADX(myPair,myPeriod, iParm3, iParm4, iParm5, p ) ;  
                            pVal = iADX(myPair,myPeriod, iParm3, iParm4, iParm5, p+1 ) ; break;}

if ( Ind_Name_1=="iATR" ) { cVal = iATR(myPair,myPeriod, iParm3,  p ) ;  
                            pVal = iATR(myPair,myPeriod, iParm3,  p+1 ) ; break;} 
                            
if ( Ind_Name_1=="iAO" )  { cVal = iAO(myPair,myPeriod, p ) ;  
                            pVal = iAO(myPair,myPeriod,  p+1 ) ; break;} 
                            
if ( Ind_Name_1=="iBearsPower" ) { cVal = iBearsPower(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                                   pVal = iBearsPower(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;}  

if ( Ind_Name_1=="iBullsPower" ) { cVal = iBullsPower(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                                   pVal = iBullsPower(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;}  

if ( Ind_Name_1=="iCCI" ) { cVal = iCCI(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                            pVal = iCCI(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;}  

if ( Ind_Name_1=="iDeMarker" ) { cVal = iDeMarker(myPair,myPeriod, iParm3,  p ) ;  
                                 pVal = iDeMarker(myPair,myPeriod, iParm3,  p+1 ) ; break;}  

if ( Ind_Name_1=="iRSI" ) { cVal = iRSI(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                            pVal = iRSI(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;}  

if ( Ind_Name_1=="iForce" ) { cVal = iForce(myPair,myPeriod, iParm3, iParm4, iParm5, p ) ;  
                              pVal = iForce(myPair,myPeriod, iParm3, iParm4, iParm5, p+1 ) ; break;}

if ( Ind_Name_1=="iFractals" ) { cVal = iFractals(myPair,myPeriod, iParm3,  p ) ;  
                                 pVal = iFractals(myPair,myPeriod, iParm3,  p+1 ) ; break;} 

if ( Ind_Name_1=="iBWMFI" )  { cVal = iBWMFI(myPair,myPeriod, p ) ;  
                               pVal = iBWMFI(myPair,myPeriod,  p+1 ) ; break;} 
                               
if ( Ind_Name_1=="iMomentum" ) { cVal = iMomentum(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                                 pVal = iMomentum(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;}                                 
                               

if ( Ind_Name_1=="iMFI" ) { cVal = iMFI(myPair,myPeriod, iParm3,  p ) ;  
                            pVal = iMFI(myPair,myPeriod, iParm3,  p+1 ) ; break;}                                
                               
if ( Ind_Name_1=="iMA" ) { cVal = iMA(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, p ) ;  
                           pVal = iMA(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, p+1 ) ; break;}
                              
                              
if ( Ind_Name_1=="iOsMA" ) { cVal = iOsMA(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, p ) ;  
                             pVal = iOsMA(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, p+1 ) ; break;}   
                             
                             
if ( Ind_Name_1=="iMACD" ) { cVal = iMACD(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, iParm7, p ) ;  
                             pVal = iMACD(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, iParm7, p+1 ) ; break;}                               
                                                             

if ( Ind_Name_1=="iOBV" ) { cVal = iOBV(myPair,myPeriod, iParm3,  p ) ;  
                            pVal = iOBV(myPair,myPeriod, iParm3,  p+1 ) ; break;} 


if ( Ind_Name_1=="iSAR" ) { cVal = iSAR(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                            pVal = iSAR(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;} 

if ( Ind_Name_1=="iRVI" ) { cVal = iRVI(myPair,myPeriod, iParm3, iParm4,  p ) ;  
                            pVal = iRVI(myPair,myPeriod, iParm3, iParm4,  p+1 ) ; break;} 

if ( Ind_Name_1=="iStdDev" ) { cVal = iStdDev(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, p ) ;  
                               pVal = iStdDev(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, p+1 ) ; break;} 
                                                              

if ( Ind_Name_1=="iStochastic" ) { cVal = iStochastic(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, iParm7, iParm8, p ) ;  
                                   pVal = iStochastic(myPair,myPeriod, iParm3, iParm4, iParm5, iParm6, iParm7, iParm8, p+1 ) ; break;}                               
                                                             
if ( Ind_Name_1=="iWPR" ) { cVal = iWPR(myPair,myPeriod, iParm3,  p ) ;  
                            pVal = iWPR(myPair,myPeriod, iParm3,  p+1 ) ; break;} 
                                
//IndHead = "ERROR! NO SUCH INDICATOR"; cVal = -999.99;  break;
}                                
 
} // DoWork  




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

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

//+------------------------------------------------------------------+
void DoHead ()
{
int hSize = 9 ;

setObject(next(),"Period ",150+price.x.offset, -10+price.y.offset,ColorHeading ,"Arial bold",hSize);
setObject(next(),"Value  ",200+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);                                
setObject(next(),"Avg Val",250+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
setObject(next(),"Max Val",300+price.x.offset, -10+price.y.offset,ColorHeading ,"Arial bold",hSize);
setObject(next(),"Min Val",350+price.x.offset, -10+price.y.offset,ColorHeading ,"Arial bold",hSize);
setObject(next(),"# > Val",400+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);
setObject(next(),"# < Val",450+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);                 
setObject(next(),"# > Avg",500+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
setObject(next(),"# < Avg",550+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);                                   
setObject(next(),"Pos/Neg Divergence",600+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);           
//setObject(next(),"Neg Div",700+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);           
setObject(next(),"# Bars ",800+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize); 

}

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