//+------------------------------------------------------------------+
//|                                                       iTrend.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Crimson
#property indicator_color3 Magenta
#property indicator_width1 4
#property indicator_width2 4
#property indicator_style3 STYLE_DASH



extern int    Bands_Mode_0_2  = 0;  // =0-2 MODE_MAIN, MODE_LOW, MODE_HIGH
extern int    Power_Price_0_6 = 0; // =0-6 PRICE_CLOSE,PRICE_OPEN,PRICE_HIGH,PRICE_LOW,PRICE_MEDIAN,PRICE_TYPICAL,PRICE_WEIGHTED
extern int    Price_Type_0_3  = 0;  // =0-3 PRICE_CLOSE,PRICE_OPEN,PRICE_HIGH,PRICE_LOW
extern int    Bands_Period    = 20;
extern int    Bands_Deviation = 2;
extern int    Power_Period    = 13;
extern int    LevelBars       = 300;
extern double LevelFactor     = 0.283;


double ItrendLevel[];
double value[];
double value2[];



int Power_Price;
int Bands_Mode;
int init()
{
   SetIndexBuffer(0,value);  SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,value2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,ItrendLevel);

   if (Bands_Mode_0_2==1) Bands_Mode=MODE_LOW;
   if (Bands_Mode_0_2==2) Bands_Mode=MODE_HIGH;
   if (Bands_Mode_0_2==0) Bands_Mode=MODE_MAIN;

   if (Power_Price_0_6==1) Power_Price=PRICE_OPEN;
   if (Power_Price_0_6==2) Power_Price=PRICE_HIGH;
   if (Power_Price_0_6==3) Power_Price=PRICE_LOW;
   if (Power_Price_0_6==4) Power_Price=PRICE_MEDIAN;
   if (Power_Price_0_6==5) Power_Price=PRICE_TYPICAL;
   if (Power_Price_0_6==6) Power_Price=PRICE_WEIGHTED;
   if (Power_Price_0_6==6) Power_Price=PRICE_CLOSE;

   return(0);
}

//+------------------------------------------------------------------+
//| Trend                                                         |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(Bars<=Bands_Period) return(0);
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);

     

   for (i=limit-1; i>=0; i--)
   {  
      double CurrentPrice = iMA(NULL,0,1,0,MODE_SMA,Price_Type_0_3,i);
         value[i]    =  CurrentPrice-iBands(NULL,0,Bands_Period,Bands_Deviation,0,Bands_Mode,Power_Price,i);
         value2[i]   = -(iBearsPower(NULL,0,Power_Period,Power_Price,i)+iBullsPower(NULL,0,Power_Period,Power_Price,i)); 
      double high    = MathMax(value[ArrayMaximum(value,LevelBars,i)],value2[ArrayMaximum(value2,LevelBars,i)]);
      ItrendLevel[i] = high*LevelFactor; 
   }  
   SetLevelValue(0, ItrendLevel[0]);
   return(0);
}