#property copyright "BLUR71"
#property link      "blrobertsjr@hotmail.com"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_width1 4
#property indicator_color2 Red
#property indicator_width2 4
#property indicator_color3 Yellow
#property indicator_width3 2
#property indicator_color4 Aqua
#property indicator_width4 0
#property indicator_style4 2
#property indicator_levelcolor DarkSlateGray
#property indicator_levelstyle 2
#define   indicator_minimum -50
#define   indicator_maximum  50
extern bool AlertOn=false, ColorDescendingBars=true,PlotMainLine=true, PlotSignalLine=true;
extern int  KPeriod=5,DPeriod=4,SPeriod=4, MAMode=0,MAPrice=0,BuyAlertLevel=-30,SellAlertLevel=30;
double UpBuffer[], DownBuffer[], StoMain[], StoSignal[];
int Positive=0, Negative=0;
//----
int init()
  {IndicatorBuffers(4);
   SetIndexStyle(0,2);
   SetIndexBuffer(0,UpBuffer);
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexStyle(1,2);
   SetIndexBuffer(1,DownBuffer);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   if(PlotMainLine) SetIndexStyle(2,0); else SetIndexStyle(2,12);
   SetIndexBuffer(2,StoMain);
   if (PlotSignalLine) SetIndexStyle(3,0); else SetIndexStyle(3,12);
   SetIndexBuffer(3,StoSignal);
   SetLevelValue(0,BuyAlertLevel);
   SetLevelValue(1,SellAlertLevel);
   return(0);}
//----
int deinit() {return(0);}
//----
int start()
{int counted_bars=IndicatorCounted(),I,limit;
 IndicatorShortName("Stoch Histo v2 ["+KPeriod+","+DPeriod+","+SPeriod+"]");
 if (counted_bars<0) return(-1);
 if (counted_bars>0) counted_bars--;
 limit=Bars-31;
 if(counted_bars>=31) limit=Bars-counted_bars-1;
//----
   for (I=limit;I>=0;I--)
   {StoMain[I]=iStochastic(0,0,KPeriod,DPeriod,SPeriod,MAMode,MAPrice,MODE_MAIN,I)-50;
    StoSignal[I]=iStochastic(0,0,KPeriod,DPeriod,SPeriod,MAMode,MAPrice,MODE_SIGNAL,I)-50;
      if(StoMain[I]>BuyAlertLevel)
      {if (Positive==0)
       {Positive=1;Negative=0;
        if (I == 1 && AlertOn ) Alert(Symbol()," M",Period()," Stoch Histo BUY @ ",Ask);}}
      else if (StoMain[I]<SellAlertLevel)
      {if(Negative==0)
       {Negative=1;Positive=0;
        if (I == 1 && AlertOn) Alert(Symbol()," M",Period()," Stoch Histo SELL @ ",Ask);}}
    if(ColorDescendingBars==true)
    {if (StoMain[I]>StoMain[I+1])
      {UpBuffer[I]=StoMain[I];
       DownBuffer[I]=EMPTY_VALUE;}
     else
      {DownBuffer[I]=StoMain[I];
       UpBuffer[I]=EMPTY_VALUE;}}
     else if(ColorDescendingBars==false && StoMain[I]>=0.00)
      {UpBuffer[I]=StoMain[I];}
     else
      {DownBuffer[I]=StoMain[I];}}
return(0);
}