#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#property indicator_color3 Lime
#property indicator_color4 Red
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3


//---- input parameters
extern ENUM_APPLIED_PRICE     MA_Price   =0; //Applied Price: 0-C,1-O,2-H,3-L,4-Median,5-Typical,6-Weighted
extern int     MA_Length  =5; //MA's Period 
extern ENUM_MA_METHOD     MA_Mode    =0; //MA's Method:0-SMA,1-EMA,2-SMMA,3-LWMA  
extern int     ATR_Length =11;//ATR's Period
extern double  Kv         =1.3; //Volatility's Factor or Multiplier
extern double  MoneyRisk  =0.15; //Offset Factor 
extern string note             = "turn on Alert = true; turn off = false";
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsEmail      = false;
extern string soundFile        = "alert2.wav";
extern bool   arrowsVisible    = false;
extern string arrowsIdentifier = "fjtrend arrows1";
extern double arrowsUpperGap   = 0.5;
extern double arrowsLowerGap   = 0.5;
extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 241;
extern int    arrowsDnCode     = 242;

 bool    usePrice_HiLoBreak    =true;
 bool    useMA_HiLoEnvelope    =false;
 int     VisualMode =0; //0-lines,1-dots 
 string Applied_Price_ ="0-C,1-O,2-H,3-L,4-Median(H+L)/2,5-Typical(H+L+C)/3,6-Weighted(H+L+C+C)/4";
 string MAmethod_mode_ ="0-SMA,1-EMA,2-SMMA,3-LWMA";
 string Visual_Mode___ ="0-lines,1-dots ";
//---- indicator buffers
double UpBuffer[];
double DnBuffer[];
double UpSignal[];
double DnSignal[];
double smin[];
double smax[];
double trend[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(7);
   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DnBuffer);
   SetIndexBuffer(2,UpSignal);
   SetIndexBuffer(3,DnSignal);
   SetIndexBuffer(4,smin);
   SetIndexBuffer(5,smax);
   SetIndexBuffer(6,trend);
//----
   if(VisualMode==0)
     {
      SetIndexStyle(0,DRAW_ARROW);
      SetIndexStyle(1,DRAW_ARROW);
      SetIndexArrow(0,246);
      SetIndexArrow(1,248);
     }
   else
     {
      SetIndexStyle(0,DRAW_ARROW);
      SetIndexStyle(1,DRAW_ARROW);
      SetIndexArrow(0,246);
      SetIndexArrow(1,248);
     }
//----
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexArrow(3,234);
   IndicatorShortName("Fiji Trend");
   SetIndexLabel(0,"UpTrend");
   SetIndexLabel(1,"DnTrend");
   SetIndexLabel(2,"UpSignal");
   SetIndexLabel(3,"DnSignal");
   return(0);
  }
  
int deinit()
{
   deleteArrows();

return(0);
}


int start()
{
   int counted_bars=IndicatorCounted();
   int shift,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
   
   //
   //
   //
   //
   //
   
   for(shift=limit;shift>=0;shift--)
     {
      if(useMA_HiLoEnvelope)
        {
         double bprice=iMA(NULL,0,MA_Length,0,MA_Mode,2,shift);
         double sprice=iMA(NULL,0,MA_Length,0,MA_Mode,3,shift);
        }
      else
        {
         bprice=iMA(NULL,0,MA_Length,0,MA_Mode,MA_Price,shift);
         sprice=iMA(NULL,0,MA_Length,0,MA_Mode,MA_Price,shift);
        }
        smax[shift]   = bprice + Kv*iATR(NULL,0,ATR_Length,shift);
        smin[shift]   = sprice - Kv*iATR(NULL,0,ATR_Length,shift);
        
        DnBuffer[shift]=EMPTY_VALUE;
        DnSignal[shift]=EMPTY_VALUE;
        UpBuffer[shift]=EMPTY_VALUE;
        UpSignal[shift]=EMPTY_VALUE;
        trend[shift]=trend[shift+1];
      
        //
        //
        //
        //
        //
      
        if (High[shift] > smax[shift+1]) trend[shift] =  1;
        if (Low[shift]  < smin[shift+1]) trend[shift] = -1;
	     if (trend[shift] > 0 && smin[shift] < smin[shift+1]) smin[shift] = smin[shift+1];
	     if (trend[shift] < 0 && smax[shift] > smax[shift+1]) smax[shift] = smax[shift+1];
        if (trend[shift] == 1)
        {  
           UpBuffer[shift] = smin[shift] - (MoneyRisk - 1) * iATR(NULL,0,ATR_Length,shift); if (trend[shift+1]!= 1) UpSignal[shift] = UpBuffer[shift];
        }
        if (trend[shift] == -1)
        {  
            DnBuffer[shift] = smax[shift] + (MoneyRisk - 1) * iATR(NULL,0,ATR_Length,shift); if (trend[shift+1]!=-1)  DnSignal[shift] = DnBuffer[shift];
        }
           //
           //
           //
           //
           //
                 
           if (arrowsVisible)
           {
              deleteArrow(Time[shift]);
              if (trend[shift] != trend[shift+1])
              {
                 if (trend[shift] == 1) drawArrow(shift,arrowsUpColor,arrowsUpCode,false);
                 if (trend[shift] ==-1) drawArrow(shift,arrowsDnColor,arrowsDnCode, true);
              }
           }
     }
     
     //
     //
     //
     //
     //
     
     if (alertsOn)
     {
       if (alertsOnCurrent)
            int whichBar = 0;
       else     whichBar = 1;
       if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("Buy");
       else  doAlert("Sell");       
     }
   return(0);
  }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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 =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Fiji Trend ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Fiji Trend "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//
//
//
//
//

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);
}






