//+------------------------------------------------------------------+
//|                                                                  |
//|                                                ///TD Pressure/// |
//|                                          ///Developed by 4rex/// |
//|                                        ///Soheil.4x@Gmail.com/// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009,4rex"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 RoyalBlue
#property indicator_color2 Red
#property indicator_color3 Green
#property indicator_width2 2
#property indicator_width3 2
#property indicator_level1 75
#property indicator_level2 25
//---- input parameters
extern int NoBars=1000;
extern int TDPressureperiod=5;
extern bool DurationAnalysis = true;
extern int DurationAnalysisPeriod = 6;
extern color  OverboughtNumberColor = DarkOrange;
extern color  OverSoldNumberColor = DeepSkyBlue;
extern double OverBought=75;
extern double OverSold=25;

//----Variables

int i,nCountedBars,E,font_size,shift,H,J,S,L;
string font;
double Delta,TrueRange,BuyingPressure,SellingPressure,denomTDPressure,numerator,denominator;


//---- buffers
double Pressure[];
double OB[];
double OS[];
double ExtMaxBuffer[];
double ExtMinBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  { 
  if(Bars<=NoBars-TDPressureperiod) {
   Alert("Not enough bars in ",Symbol(),Period()," chart.","\n","Please set No.Bars to ",Bars-TDPressureperiod-2," or less.");
   return(0);}                            
 
   string short_name;
//---- 
   IndicatorBuffers(5);
   SetIndexBuffer(0,Pressure);
   SetIndexBuffer(1,OB);
   SetIndexBuffer(2,OS);
   SetIndexBuffer(3,ExtMaxBuffer);
   SetIndexBuffer(4,ExtMinBuffer);
   
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   
//----   
font = "Arial Black";
font_size = 10;
//---- name for DataWindow and indicator subwindow label
   E=WindowOnDropped()+WindowsTotal();
   short_name="TD Pressure ("+TDPressureperiod+")  "+E+"  ";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   
//---- first values aren't drawn
   SetIndexDrawBegin(0,TDPressureperiod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
int deinit() {
   int windowNo=WindowFind("TD Pressure ("+TDPressureperiod+")  "+E+"  ");   
   for (int i=0;i<Bars;i++)
   ObjectDelete("TD-Pressure" +windowNo+ Time[shift+i]);
   
   return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| TD Pressure                                                      |
//+------------------------------------------------------------------+
int start()
  {
  int windowNo=WindowFind("TD Pressure ("+TDPressureperiod+")  "+E+"  ");

//---- insufficient data
   if(Bars<=TDPressureperiod) return(0);
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();
//----
if(nCountedBars>2) i=Bars-nCountedBars-1;
   else               i=Bars-2;
   while(i>=0)
     {
    
           Delta=Close[i]-Open[i];
         
           TrueRange=High[i]-Low[i];
           
           if(Delta>0) BuyingPressure=(Delta/TrueRange)*Volume[i];
            
           ExtMaxBuffer[i]=BuyingPressure; 
           
           if(Delta<0) SellingPressure=(Delta/TrueRange)*Volume[i]; 
           
           denomTDPressure=BuyingPressure+MathAbs(SellingPressure);
        
           ExtMinBuffer[i]=denomTDPressure;
    
        i--;
     }   
//---- initial zero
   if(nCountedBars<1)
      for(i=1; i<=TDPressureperiod; i++)
         Pressure[Bars-i]=0;   
//----
   i=NoBars;
        while(i>=0)
     {
     
     numerator=iMAOnArray(ExtMaxBuffer,0,TDPressureperiod,0,MODE_SMA,i);
     denominator=iMAOnArray(ExtMinBuffer,0,TDPressureperiod,0,MODE_SMA,i);
     
           
      if(denominator !=0)
         Pressure[i]=100*(numerator/denominator);
         
         else
            Pressure[i]=50;
      
         if (DurationAnalysis==true){
         if (Pressure[i] > OverBought ) { OB[i] = Pressure[i]; OB[i+1] = Pressure[i+1]; }
            else                           { OB[i] = EMPTY_VALUE;
                                           if (OB[i+2] == EMPTY_VALUE)
                                               OB[i+1]  = EMPTY_VALUE; }
               
               if (Pressure[i] < OverBought && Pressure[i+1] > OverBought){
               J=1;
               while(Pressure[i+J] > OverBought){H=J; J++;}
               if (H<=DurationAnalysisPeriod){
               
               Write("TD-Pressure" +windowNo+ Time[shift+i], Time[shift+i], Pressure[i+1], DoubleToStr(H,0), font_size, font, OverboughtNumberColor);
               }
               }
               
            if (Pressure[i] < OverSold)   { OS[i] = Pressure[i]; OS[i+1] = Pressure[i+1]; }
            else                           { OS[i] = EMPTY_VALUE;
                                           if (OS[i+2] == EMPTY_VALUE)
                                               OS[i+1]  = EMPTY_VALUE; }
            
             if (Pressure[i] > OverSold && Pressure[i+1] < OverSold){
              L=1;
               while(Pressure[i+L] < OverSold){S=L; L++;}
               if (S<=DurationAnalysisPeriod){
               
               Write("TD-Pressure" +windowNo+ Time[shift+i], Time[shift+i], Pressure[i+1], DoubleToStr(S,0), font_size, font, OverSoldNumberColor);
               }
               }                                                                                         
      }   
                                                     
      i--;
     }
     
   return(0);
  }


//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void Write(string name, datetime x, double y, string text, int font_size, string font, color text_color) {

   int windowNo=WindowFind("TD Pressure ("+TDPressureperiod+")  "+E+"  ");
    
   if(ObjectFind(name) == -1) 
      ObjectCreate(name, OBJ_TEXT,windowNo, x, y);
   else {
      ObjectDelete(name);
      ObjectCreate(name, OBJ_TEXT,windowNo, x, y);
   }
   ObjectSetText(name, text, font_size, font, text_color);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

//End of Program

