//+------------------------------------------------------------------+
//|                                       SAS_v2007.11.indicator.mq4 |
//|         Copyright 2007 , SmartFX Auto-trading System by smartfx99|
//|                                         Released Date:2007/11/11 |
//|                                              smartfx99@gmail.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2007, SmartFX Auto-trading System by smartfx99"
#property link      "smartfx99@gmail.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 Brown
#property indicator_color3 Yellow
#property indicator_color4 Red
#property indicator_color5 Lime

//----------------------- USER INPUT OPTION
extern int sas_ma_length = 10;
extern double sas_percent = 0.2;
//-----------------------
double sas_ma_high[];
double sas_ma_org[];
double sas_ma_low[];
double sas_sell_arrow[];
double sas_buy_arrow[];

int    sas_RsiPeriods          =       2;
int    sas_Rsi_Applied_Price   =       0;
int    sas_BuyIfDay1RsiBelow   =      65; // 1st day of tracking must be < this setting
int    sas_BuyIfDay3RsiBelow   =      65; // 3rd day must be < this setting
int    sas_SellIfDay1RsiAbove  =      35; // 1st day of tracking must be > this setting
int    sas_SellIfDay3RsiAbove  =      35; // 3rd day must be > this setting
int    sas_CloseBuyIfRsiAbove  =      75;
int    sas_CloseSellIfRsiBelow =      25;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 5 indicator buffers mapping
   SetIndexBuffer(0,sas_ma_high);
   SetIndexBuffer(1,sas_ma_org);
   SetIndexBuffer(2,sas_ma_low);
   SetIndexBuffer(3,sas_sell_arrow); 
   SetIndexBuffer(4,sas_buy_arrow);         
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,0);
   SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID,0);
//---- index labels
   SetIndexLabel(0,"MA_high");
   SetIndexLabel(1,"MA_org");   
   SetIndexLabel(2,"MA_low");
   SetIndexLabel(3,"Sell_Arrow");
   SetIndexLabel(4,"Buy_Arrow");
//---- index arrows   
   SetIndexArrow(3,234);
   SetIndexArrow(4,233);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                                        |
//+------------------------------------------------------------------+
int start()
  {
//----  
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
   double sas_ma;
   for(int i=0; i<limit; i++)
     {
      //---- ma
      sas_ma=iMA(NULL,0,sas_ma_length,0,MODE_SMA,PRICE_MEDIAN,i);
      sas_ma_high[i]=sas_ma*(1+sas_percent/100);
      sas_ma_org[i] =sas_ma;
      sas_ma_low[i] =sas_ma*(1-sas_percent/100);
      //---- signal
      double sas_rsi_Day1 = iRSI(Symbol(),0,sas_RsiPeriods,sas_Rsi_Applied_Price,i+3);
      double sas_rsi_Day2 = iRSI(Symbol(),0,sas_RsiPeriods,sas_Rsi_Applied_Price,i+2);
      double sas_rsi_Day3 = iRSI(Symbol(),0,sas_RsiPeriods,sas_Rsi_Applied_Price,i+1);
      double sas_rsi_Today = iRSI(Symbol(),0,sas_RsiPeriods,sas_Rsi_Applied_Price,i);      
      if ((iHigh(Symbol(),0,i) > sas_ma_high[i])&& (sas_rsi_Day1 > sas_SellIfDay1RsiAbove) && (sas_rsi_Day2 > sas_rsi_Day1) && (sas_rsi_Day3 > sas_rsi_Day2) && (sas_rsi_Day3 > sas_SellIfDay3RsiAbove))
        sas_sell_arrow[i] = iHigh(Symbol(),0,i);
      if ((iLow(Symbol(),0,i) < sas_ma_low[i])  && (sas_rsi_Day1 < sas_BuyIfDay1RsiBelow)  && (sas_rsi_Day2 < sas_rsi_Day1) && (sas_rsi_Day3 < sas_rsi_Day2) && (sas_rsi_Day3 < sas_BuyIfDay3RsiBelow))
        sas_buy_arrow[i] = iLow(Symbol(),0,i);           
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+  
  