//+------------------------------------------------------------------+
//|                             One Side Gaussian Support Resistance |
//|                                                           mladen |
//|                                                                  |
//| original idea and first implementation by : Tinytjan             |
//| (http://codebase.mql4.com/ru/2735)                               |
//+------------------------------------------------------------------+
//| MODIFIED_VERSION_OS Gaussian Support Resistance-1                |
//| MODIFIED BY AVERY T. HORTON, JR. AKA THERUMPLEDONE@GMAIL.COM     |
//| I am NOT the ORIGINAL author 
//  and I am not claiming authorship of this indicator. 
//  All I did was modify it. I hope you find my modifications useful.|
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfxgmail.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 DimGray
#property indicator_style3 STYLE_DOT

//
//

extern bool   Sound.Alert    = false ;
extern bool   Show.PriceBox  = true ;
extern bool   Show.MidBand   = true ;
extern int       myBoxWidth  = 3;


//

extern int SRPeriod   = 40;
extern int SmoothType = 3;

//
//
//
//
//

double Resistance[];
double Support[];
double Average[];
double SmoothHigh[];
double SmoothLow[];


string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 

bool Trigger ;

int OldBars = -1 ;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

#import "OneSideGaussianLibrary.ex4"
   void   BuffersInit();
   double Smooth(int version,int price,int i);
#import

//
//
//
//
//

int init()
{

   period       = Period() ;     
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   

   tShortName = "gsr"+ symbol + tChartPeriod  ;


   BuffersInit();
   IndicatorBuffers(5);
      SetIndexBuffer(0, Resistance); SetIndexLabel(0,"OS Gaussian resistance");
      SetIndexBuffer(1, Support);    SetIndexLabel(1,"OS Gaussian support");
      SetIndexBuffer(2, Average);    SetIndexLabel(2,"OS Gaussian average");
      SetIndexBuffer(3, SmoothHigh);
      SetIndexBuffer(4, SmoothLow);
         SmoothType  = MathMax(MathMin(SmoothType,7),0);
      IndicatorShortName("One side Gaussian ("+SRPeriod+","+SmoothType+")");
   return(0);
}

//
int deinit()
{
   ObjectDelete(tShortName+"U"); 
   ObjectDelete(tShortName+"L"); 
   ObjectDelete(tShortName+"M"); 
   return(0);
}

//
//

int start()
{

      
   if( Bars != OldBars ) { Trigger = True ; }

   int counted_bars = IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
   
   //
   //
   //
   //
   //
   
   for(i = limit; i >= 0; i--)
   {
      SmoothLow[i]  = Smooth(SmoothType,PRICE_LOW, i);
      SmoothHigh[i] = Smooth(SmoothType,PRICE_HIGH,i);
      Support[i]    = SmoothLow [ArrayMinimum(SmoothLow,SRPeriod,i)];
      Resistance[i] = SmoothHigh[ArrayMaximum(SmoothHigh,SRPeriod,i)];
      Average[i]    = (Support[i] + Resistance[i])/2;
   }
   
   
      if ( Trigger &&  Sound.Alert ) 
      {
        if( Close[0] > Resistance[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price above Resistance "+ DoubleToStr(Resistance[0] ,digits)); }
        if( Close[0] < Support[0] )    { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price below Support "+ DoubleToStr(Support[0] ,digits)); }     
      }


    if(Show.PriceBox)
    {


       if (ObjectFind(tShortName+"U") != 0)
      {
          ObjectCreate(tShortName+"U",OBJ_ARROW,0,Time[0],Resistance[0]);
          ObjectSet(tShortName+"U",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"U",OBJPROP_COLOR,indicator_color1);  
          ObjectSet(tShortName+"U",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"U",0,Time[0],Resistance[0]);
         ObjectSet(tShortName+"U",OBJPROP_COLOR,indicator_color1);  
      }

       if (ObjectFind(tShortName+"L") != 0)
      {
          ObjectCreate(tShortName+"L",OBJ_ARROW,0,Time[0],Support[0]);
          ObjectSet(tShortName+"L",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"L",OBJPROP_COLOR,indicator_color2);  
          ObjectSet(tShortName+"L",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"L",0,Time[0],Support[0]);
         ObjectSet(tShortName+"L",OBJPROP_COLOR,indicator_color2);  
      } 

       if(Show.MidBand) {
       if (ObjectFind(tShortName+"M") != 0)
      {
          ObjectCreate(tShortName+"M",OBJ_ARROW,0,Time[0],Average[0]);
          ObjectSet(tShortName+"M",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"M",OBJPROP_COLOR,indicator_color3);  
          ObjectSet(tShortName+"M",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"M",0,Time[0],Average[0]);
         ObjectSet(tShortName+"M",OBJPROP_COLOR,indicator_color3);  
      }
      } //if(Show.MidBand) {

   } // if(Show.PriceBox)

   
   return(0);
}


//+------------------------------------------------------------------+ 

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}


//+------------------------------------------------------------------+ 