//+------------------------------------------------------------------+
//|  
//| 
//+------------------------------------------------------------------+
#property  copyright ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Magenta


//---- input parameters
extern string RSI="RSI Paramenters";
extern int RSIPeriod4=3;
extern ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE;
extern int RSILevel=80;
extern int RSILevel2=20;
extern string Filter="Filter Last candle";
extern int LastCandleLevel=60;
extern int LastCandleLevel2=40;
extern int barsback=1;
extern bool Alert_On=true;


extern int CountBars=1350;

//---- buffers
double val1[];
double val2[];



#define SIGNAL_BAR 1
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- indicator line
   IndicatorBuffers(2);
   SetIndexStyle(0,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(0,233);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(1,234);
   SetIndexBuffer(0,val1);
   SetIndexBuffer(1,val2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| AltrTrend_Signal_v2_2                                            |
//+------------------------------------------------------------------+
int start()
  {
   static datetime bar_time=0;
   if(Time[0]==bar_time)
      return(0);
   bar_time=Time[0];
   if(CountBars>=Bars) CountBars=Bars;
   SetIndexDrawBegin(0,Bars-CountBars);
   SetIndexDrawBegin(1,Bars-CountBars);
   int i,shift,counted_bars=IndicatorCounted();

//---- check for possible errors
   if(counted_bars<0) return(-1);

//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=CountBars;i++) val1[CountBars-i]=0.0;
      for(i=1;i<=CountBars;i++) val2[CountBars-i]=0.0;
     }

   for(shift=CountBars; shift>0; shift--)
     {
      double RSI4current=iRSI(Symbol(),0,RSIPeriod4,RSIPrice,shift);
      double RSI4prev=iRSI(Symbol(),0,RSIPeriod4,RSIPrice,shift+barsback);
      if(RSI4current>RSILevel && RSI4prev<=LastCandleLevel2)
        {
         val2[shift]=High[shift]+8*Point;
         if(shift==1 && Alert_On)
         Alert("BUY!!! (",Symbol(),", ",Period(),")  -  BUY!!!");
        }
      if(RSI4current<RSILevel2 && RSI4prev>=LastCandleLevel)
        {
        val1[shift]=Low[shift]-8*Point;
         if(shift==1 && Alert_On)
         Alert("SELL!!! (",Symbol(),", ",Period(),")  -  SELL!!!");
        }
     }


   return(0);
  }
//+------------------------------------------------------------------+
