
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
double sprev[];

string SignalText;

extern bool RepeatSignals        = false;
extern int CountBars            = 1000;
extern bool AlertON              = true;
int Alert_Count = 0;
datetime    Prior_Time;

int init()
{
  SetIndexBuffer(0, CrossUp); SetIndexStyle(0, DRAW_ARROW, 3); SetIndexArrow(0, 233);
  SetIndexBuffer(1, CrossDown); SetIndexStyle(1, DRAW_ARROW, 3); SetIndexArrow(1, 234);
  SetIndexBuffer(2, sprev);   
	IndicatorBuffers(3);
	return (0);
}

int deinit() {
   return (0);
}

int g_last_bar_time = 0;

int start() {
  if (g_last_bar_time == Time[0]) {return(0);} else {g_last_bar_time = Time[0];} //

   int counted_bars=IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
 
   if (iTime(Symbol(),Period(),1) != Prior_Time)
    {
     Prior_Time = iTime(Symbol(),Period(),1);
     Alert_Count = 0;
    }
   else return(0);
   
   int limit = MathMin(Bars-counted_bars,CountBars); limit=MathMin(Bars-1, limit);
   for (int i=limit; i>=0; i--)
   {
	   	sprev[i]=0; if (i<Bars-1) sprev[i]=sprev[i+1];
      double ma84 = iMA(NULL, 0, 84, 0, MODE_EMA, PRICE_CLOSE, i);
      double kelt00 = iCustom(NULL,0,"KeltnerChannels",10,0,i);  
      double kelt01 = iCustom(NULL,0,"KeltnerChannels",10,0,i+1);   
      double kelt20 = iCustom(NULL,0,"KeltnerChannels",10,2,i);  
      double kelt21 = iCustom(NULL,0,"KeltnerChannels",10,2,i+1); 
      
                                   
      if(Close[i]>ma84 && Close[i+1]<kelt01 && Close[i]>kelt00)
      { // long conditions
      	sprev[i]=1;
	   		if (i<Bars-1) if (sprev[i]!=sprev[i+1])
	   		{
         if(RepeatSignals) { CrossUp[i]=Low[i]-50 * Point; if (i<=1) { if (AlertON == 1 && Alert_Count == 0) Alert("BUY Signal: " + Symbol());} }
         else if(CrossUp[i+1]==EMPTY_VALUE) { CrossUp[i]=Low[i]-50 * Point; if (i<=1) { if (AlertON == 1 && Alert_Count == 0) Alert("BUY Signal: " + Symbol());} }
        }
      }
      else if(Close[i]<ma84 && Close[i+1]>kelt21 && Close[i]<kelt20)
      { // short conditions
      	sprev[i]=-1;
	   		if (sprev[i]!=sprev[i+1])
	   		{
         if(RepeatSignals){ CrossDown[i]=High[i]+50 * Point; if (i<=1) { if (AlertON == 1 && Alert_Count == 0) Alert("SELL Signal: " + Symbol());} }
         else if(CrossUp[i+1] == EMPTY_VALUE){ CrossDown[i]=High[i]+50 * Point; if (i<=1) { if (AlertON == 1 && Alert_Count == 0) Alert("SELL Signal: " + Symbol());} }
        }
      }
   }
   return (0);
}

