//+------------------------------------------------------------------+
//| sto x ma rsi                                                     |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//mod 2008 fxtsd   ki

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Orange
#property indicator_color4 Aqua

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1

#property indicator_level1  80
#property indicator_level2  70
#property indicator_level3  50
#property indicator_level4  30
#property indicator_level5  20
#property indicator_levelstyle 2
#property indicator_levelcolor DarkOliveGreen

extern int Kperiod= 5;
extern int Dperiod= 3;
extern int slowing= 3;
extern int MAmethod= 1;
extern int price_field  = 1;
extern int line_mode    = 0;

extern string note_method_ = "MAmethod: SMA0 EMA1 SMMA2 LWMA3"; 
extern string price_field_ = "0- Low/High  1- Close/Close" ;
extern string line_mode___ = "0- main 1- signal line" ;

extern int RSI_period      = 5;
extern int RSI_price       = 0;
extern int MA_RSI_period   = 1;
extern int MA_RSI_method   = 1;

extern int  SignalOfset = 5;
extern bool HideSignals = false;
//---- input parameters

double UpBuffer[];
double DnBuffer[];
double RSIBuffer[];
double MARSIBuffer[];
double StoBuffer[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
  {
//---- indicators

   IndicatorBuffers(5);
   
   if (HideSignals)
   {
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_NONE);
   }
   else
   {
   SetIndexStyle(0,DRAW_ARROW,EMPTY);
   SetIndexStyle(1,DRAW_ARROW,EMPTY);
   }  
     
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
  
   SetIndexArrow(0,233);
   SetIndexArrow(1,234);

   SetIndexLabel(2,"MA_RSI");
   SetIndexLabel(3,"Sto");

   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DnBuffer);
   SetIndexBuffer(2,MARSIBuffer);
   SetIndexBuffer(3,StoBuffer);
   SetIndexBuffer(4,RSIBuffer);
   
  

   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) counted_bars=0;
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
 for(int i=0; i<limit;i++)
     {       
 
      
   StoBuffer[i]= iStochastic(NULL,0,Kperiod,Dperiod,slowing,MAmethod,price_field,line_mode,i) ;

     }

   for(i=0; i<limit;i++)
      {
      RSIBuffer[i]     = iRSI(NULL,0,RSI_period,RSI_price,i);
      } 
   for( i=0; i<limit;i++)
      {
      MARSIBuffer[i]   = iMAOnArray(RSIBuffer,0,MA_RSI_period,0,MA_RSI_method,i); 
      }

   for( i=0; i<limit;i++)
      {


         double sto  =StoBuffer[i];
         double sto1 =StoBuffer[i+1];

         double rsi  =MARSIBuffer[i];
         double rsi1 =MARSIBuffer[i+1];
         
         

      if( sto1<rsi1 && sto>rsi)
  
         {  
         UpBuffer[i] = sto1-SignalOfset;
         DnBuffer[i] = EMPTY_VALUE;
         }
      
      else 
      
      if( sto1>rsi1 && sto<rsi)
         {
         UpBuffer[i] = EMPTY_VALUE;
         DnBuffer[i] = sto1+SignalOfset;
         }
     
      else
         {
         DnBuffer[i] = EMPTY_VALUE;
         UpBuffer[i] = EMPTY_VALUE;
         }
 
      }

   return(0);
  }



