//+------------------------------------------------------------------+
//|                                                       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 2
#property indicator_color1  Aqua
#property indicator_color2  Crimson
#property indicator_width1  2
#property indicator_width2  2

//
//
//
//
//

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 bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = false;
extern bool   alertsNotify     = true;
extern bool   alertsEmail      = true;
extern string soundFile        = "alert2.wav"; 

extern bool   ShowArrows       = false;
extern string arrowsIdentifier = "iTrend Arrows1";
extern double arrowsUpperGap   = 1.0;
extern double arrowsLowerGap   = 1.0;
extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 241;
extern int    arrowsDnCode     = 242;


extern bool   ShowText         = true;
extern string TextDir          = "iTrend Text";
extern int    textXPosition    = 5;
extern int    textYPosition    = 30;
extern color  upTextColor      = Blue;
extern color  dnTextColor      = Red;
extern color  nuTextColor      = Gray;
extern int    upFontSize       = 12;
extern int    dnFontSize       = 12;
extern int    nuFontSize       = 12;

//
//
//
//
//

double val1[];
double val2[];
double trend[];


int Power_Price;
int Bands_Mode;

//
//
//
//
//

int init()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0,val1); 
   SetIndexBuffer(1,val2); 
   SetIndexBuffer(2,trend);

   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);
}

int deinit() 
{  
   deleteArrows(); 
   ObjectDelete(TextDir);
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);
         val1[i]  =  CurrentPrice-iBands(NULL,0,Bands_Period,Bands_Deviation,0,Bands_Mode,Power_Price,i);
         val2[i]  = -(iBearsPower(NULL,0,Power_Period,Power_Price,i)+iBullsPower(NULL,0,Power_Period,Power_Price,i)); 
         trend[i] = trend[i+1]; 
            if (val1[i]>val2[i])  { trend[i] = 1; Commenter(1);  }
            if (val1[i]<val2[i])  { trend[i] =-1; Commenter(-1); }
            
            //
            //
            //
            //
            //
       
            if (ShowArrows)
            {
               deleteArrow(Time[i]);
               if (trend[i] != trend[i+1])
               {
                 if (trend[i] == 1)  drawArrow(i,arrowsUpColor,arrowsUpCode,false);
                 if (trend[i] ==-1)  drawArrow(i,arrowsDnColor,arrowsDnCode, true);
               }
            }
   }  
   
   //
   //
   //
   //
   //
   
   if (alertsOn)
   {
      if (alertsOnCurrent)
            int whichBar = 0;
      else      whichBar = 1;
      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert("up");
      else  doAlert("down");       
   }  
return(0);
}

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" iTrend changed trend to "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," iTrend " +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," iTrend "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//
//
//
//
//

void Commenter(int Dir) 
{
    if (ShowText)
    {
      ObjectCreate(TextDir,OBJ_LABEL,0,0,0);
         ObjectSet(TextDir,OBJPROP_XDISTANCE,textXPosition);
         ObjectSet(TextDir,OBJPROP_YDISTANCE,textYPosition);
         if (Dir == 1) ObjectSetText(TextDir, "Buy", upFontSize,"Arial Black",upTextColor);
         if (Dir ==-1) ObjectSetText(TextDir, "Sell",dnFontSize,"Arial Black",dnTextColor);
    }
   
return; 
}



