//+------------------------------------------------------------------+
//|                                        AbsoluteStrength_v1.1.mq4 |
//|                           Copyright © 2006, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                       E-mail: igorad2004@list.ru |
//+------------------------------------------------------------------+
//mod.2008fxtsd 4c mtf    ki
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_separate_window
#property indicator_buffers   8
#property indicator_color5    CornflowerBlue
#property indicator_width5    2 
#property indicator_color6    OrangeRed
#property indicator_width6    2
#property indicator_color3    DeepSkyBlue
#property indicator_width3    1
#property indicator_style3    0 
#property indicator_color4    Orange
#property indicator_width4    1 
#property indicator_style4    0 
#property indicator_color1    Silver//Goldenrod 
#property indicator_width1    1 
#property indicator_style1    0 
#property indicator_color2    SlateGray//Red 
#property indicator_width2    1 
#property indicator_style2    0 

#property indicator_color7    DimGray  //Chocolate 
#property indicator_width7    1 
#property indicator_style7    0 
#property indicator_color8    DarkSlateGray  //Maroon
#property indicator_width8    1 
#property indicator_style8    0 



//---- input parameters


extern int        Length     = 10; // Period of evaluation
extern int        Smooth     =  5; // Period of smoothing
extern int        Signal     =  5; // Period of Signal Line
extern int        MA_method      =  3; // Mode of Moving Average
extern int        MA_Price       =  0; // Price mode : 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted
extern int        AbS_Mode       =  0; // 0-RSI method; 1-Stoch method; 2-ADX method

extern bool      UseOBOSLevels =  false;
extern double    OverBoughtL   =  80; // OverBought Level
extern double    OverSoldL     =  20; // OverSold Level 
//extern int       MaxBarsToCount = 1500;
extern double    HistoMultiplier =  0.6;

extern int        TimeFrame  = 0;
extern string     TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN";
extern string     note_Price = "0C 1O 2H 3L 4Md 5Tp 6WghC, Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
extern string     MA_Method_ = "SMA0 EMA1 SMMA2 LWMA3";
extern string     AbS_Mode__ = "0-RSI 1-Stoch 2-ADX method";

string  IndicatorFileName; 

//---- buffers
double Bulls[];
double Bears[];
double AvgBulls[];
double AvgBears[];
double SmthBulls[];
double SmthBears[];
double SigBulls[];
double SigBears[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(8);
   SetIndexStyle  (4,DRAW_LINE);
   SetIndexBuffer (4,SmthBulls);
   SetIndexStyle  (5,DRAW_LINE);
   SetIndexBuffer (5,SmthBears);
 
   SetIndexStyle  (2,DRAW_LINE);
   SetIndexBuffer (2,SigBulls);
   SetIndexStyle  (3,DRAW_LINE);
   SetIndexBuffer (3,SigBears);
   
    SetIndexStyle (0,DRAW_HISTOGRAM);
    SetIndexBuffer(0,Bulls);
    SetIndexStyle (1,DRAW_HISTOGRAM);
    SetIndexBuffer(1,Bears);


    SetIndexStyle  (6,DRAW_HISTOGRAM);
    SetIndexBuffer (6,AvgBulls);
    SetIndexStyle  (7,DRAW_HISTOGRAM);
    SetIndexBuffer (7,AvgBears);
   



//----
           switch(TimeFrame)
   {
      case 1: string TimeFrameStr = "M1";  break;
      case 5     :   TimeFrameStr = "M5";  break;
      case 15    :   TimeFrameStr = "M15"; break;
      case 30    :   TimeFrameStr = "M30"; break;
      case 60    :   TimeFrameStr = "H1";  break;
      case 240   :   TimeFrameStr = "H4";  break;
      case 1440  :   TimeFrameStr = "D1";  break;
      case 10080 :   TimeFrameStr = "W1";  break;
      case 43200 :   TimeFrameStr = "MN1"; break;
      default    :   TimeFrameStr = "TF 0";
   }


           switch(AbS_Mode)
     {
      case 1: string AbS_Mode_Str = "STOmode";  break;
      case 2     :   AbS_Mode_Str = "ADXmode";  break;
      default    :   AbS_Mode_Str = "RSImode";
     }

   //---- name for DataWindow and indicator subwindow label
   
   string short_name="AbsStr ["+TimeFrameStr+"] "+Length+","+Smooth+","+Signal+
                     " ("+MA_method+") "+AbS_Mode_Str+"|";
   
   IndicatorShortName(short_name);
            IndicatorFileName = WindowExpertName();
    
   SetIndexLabel(4,"Bulls"+short_name);
   SetIndexLabel(5,"Bears"+short_name);
   SetIndexLabel(2,"SignalBulls"+short_name);
   SetIndexLabel(3,"SignalBears"+short_name);      
   SetIndexLabel(0,"Bulls-Bears"+short_name);      
   
      SetIndexEmptyValue(0,EMPTY_VALUE);   
      SetIndexEmptyValue(1,EMPTY_VALUE);   
      SetIndexEmptyValue(2,EMPTY_VALUE);   
      SetIndexEmptyValue(3,EMPTY_VALUE);   
      SetIndexEmptyValue(4,EMPTY_VALUE);   
      SetIndexEmptyValue(5,EMPTY_VALUE);   
      SetIndexEmptyValue(6,EMPTY_VALUE);   
      SetIndexEmptyValue(7,EMPTY_VALUE);   
   
 
             if (TimeFrame<Period()) TimeFrame=Period();

   
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int      shift,  counted_bars=IndicatorCounted();
   double   Price1, Price2, smax, smin;

   int      limit,i;
   if(counted_bars < 0) return(-1);
   limit = Bars-counted_bars;
   if (TimeFrame != Period())
      {
         limit = MathMax(limit,TimeFrame/Period());

        datetime TimeArray[];
        ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,TimeFrame);
            for(i=0,int y=0; i<limit; i++)
           {
              if(Time[i]<TimeArray[y]) y++;
              
  SmthBulls[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,4,y);
  SmthBears[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,5,y);
  SigBulls[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,2,y);
  SigBears[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,3,y);
  Bulls[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,0,y);

  Bears[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,1,y);

  AvgBulls[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,6,y);

  AvgBears[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
                  Length,Smooth,Signal,MA_method,MA_Price,AbS_Mode,
                  UseOBOSLevels,OverBoughtL,OverSoldL,HistoMultiplier,7,y);


            }
         return(0);         
      }



//---- 
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
     limit = Bars-counted_bars;
     limit=Bars-Length+Smooth+Signal-1;
   
   
   
//   if(counted_bars>0) limit=Bars-counted_bars;
//   limit--;
   
   for( shift=limit; shift>=0; shift--)
      {
      Price1 = iMA(NULL,0,1,0,0,MA_Price,shift);
      Price2 = iMA(NULL,0,1,0,0,MA_Price,shift+1); 
      
     if (AbS_Mode>2) AbS_Mode=2;

        if (AbS_Mode==0)
         {
         Bulls[shift] = 0.5*(MathAbs(Price1-Price2)+(Price1-Price2));
         Bears[shift] = 0.5*(MathAbs(Price1-Price2)-(Price1-Price2));
         }
        
        if (AbS_Mode==1)
         {
         smax=High[Highest(NULL,0,MODE_HIGH,Length,shift)];
         smin=Low[Lowest(NULL,0,MODE_LOW,Length,shift)];
         
         Bulls[shift] = Price1 - smin;
         Bears[shift] = smax - Price1;
         }
         
         if (AbS_Mode==2)

         {
         Bulls[shift] = 0.5*(MathAbs(High[shift]-High[shift+1])+(High[shift]-High[shift+1]));
         Bears[shift] = 0.5*(MathAbs(Low[shift+1]-Low[shift])+(Low[shift+1]-Low[shift]));
         }
      }
      
      for( shift=limit; shift>=0; shift--)
      {
      AvgBulls[shift]=iMAOnArray(Bulls,0,Length,0,MA_method,shift);     
      AvgBears[shift]=iMAOnArray(Bears,0,Length,0,MA_method,shift);
      }
      
      for( shift=limit; shift>=0; shift--)
      {
      SmthBulls[shift]=iMAOnArray(AvgBulls,0,Smooth,0,MA_method,shift);     
      SmthBears[shift]=iMAOnArray(AvgBears,0,Smooth,0,MA_method,shift);
      }
                
      for( shift=limit; shift>=0; shift--)
      {
         if (UseOBOSLevels )
         {
         SigBulls[shift]=OverBoughtL/100*(SmthBulls[shift]+SmthBears[shift]);
         SigBears[shift]=OverSoldL/100*(SmthBulls[shift]+SmthBears[shift]);
         }
         else
         {
         SigBulls[shift]=iMAOnArray(SmthBulls,0,Signal,0,MA_method,shift);     
         SigBears[shift]=iMAOnArray(SmthBears,0,Signal,0,MA_method,shift);
         }


      }
//----

//   for (i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Bars-BarsToDraw*TimeFrame/Period());   


       for( shift=limit; shift>=0; shift--)
        {
  Bulls[shift]    = EMPTY_VALUE;   Bears[shift]     = EMPTY_VALUE; 
  AvgBulls[shift] = EMPTY_VALUE;   AvgBears[shift]  = EMPTY_VALUE; 


  double   delta =   (SmthBulls[shift]   -   SmthBears[shift])*HistoMultiplier ;  //  
  double   delta1 =  (SmthBulls[shift+1] -   SmthBears[shift+1])*HistoMultiplier ; //   
  
  if (delta>delta1)
  
  {    if (delta>0)   { Bulls[shift]   = delta;    Bears[shift]  =   EMPTY_VALUE; }
       else           { AvgBulls[shift]= delta; AvgBears[shift]  =   EMPTY_VALUE; }
  }
  else 
  {    if (delta>0)  { Bears[shift]    = delta;     Bulls[shift] =  EMPTY_VALUE; }
       else          { AvgBears[shift] = delta;  AvgBulls[shift] =  EMPTY_VALUE; }       
  }          
         }


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