//+------------------------------------------------------------------+
//|                              ARROW_RSI_CROSS50.mq4                                                             
//+------------------------------------------------------------------+
#property copyright "AHGduP"
#property link      "ARROW_RSI_CROSS-70"


#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrMagenta 
#property indicator_width1 1
#property indicator_color2 clrDodgerBlue
#property indicator_width2 1
#property indicator_color3 Aqua 
#property indicator_width3 1


//====================================================

extern int        RSIPeriod= 8;

extern int        RSI_OB = 75;
extern int        RSI_OS = 25;
extern int        RSI_BUY = 55;
extern int        RSI_SELL = 45;
extern int        NEXT_BARS = 10;

//====================================================
extern bool   Alarm_On= False;
extern int    Arrow_Size= 1;
extern int    Move_Arrow = 1;
extern int    CountBars = 288;

double bBuffer1[];
double sBuffer1[];
double rsi_buff[];

int period;
string TimeFrameStr;
//+------------------------------------------------------------------+
int init()
  {

   SetIndexStyle(0,DRAW_ARROW,0,Arrow_Size);
   SetIndexBuffer(0,bBuffer1);
   SetIndexEmptyValue(0,0);
   SetIndexArrow(0,234);

   SetIndexStyle(1,DRAW_ARROW,0,Arrow_Size);
   SetIndexBuffer(1,sBuffer1);
   SetIndexEmptyValue(1,0);
   SetIndexArrow(1,233);

   SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(2,rsi_buff);

   period=Period();
   TimeFrameStr=TimeFrameToString(period);


   string  ThisName="RSI";// Rsi_246 ??? 
   string Text=ThisName;
   Text=Text+"  ("+TimeFrameStr;
   Text=Text+")";
   Text=Text+"( ";
   Text=Text+" ";
   Text=Text+")  ";
   IndicatorShortName(Text);
   return(0);

  }
//+--------------------------------------------------------------------------------------+

int deinit()
  {

   for(int i=ObjectsTotal()-1; i>=0; i--)
      ObjectDelete(ObjectName(i));

   return(0);
  }
//+--------------------------------------------------------------------------------------+

int start()
  {


   int limit,counted_bars,y=0;

   counted_bars=IndicatorCounted();

   //if(counted_bars>0) counted_bars--;
   //limit=Bars-counted_bars;
   //or
   int lookback = 2;
       limit = MathMax(Bars-1-lookback-IndicatorCounted(),0);
       
   //IndicatorCounted() and counted_bars are older syntax, and used incorrectly here
   //Printing the relevant values will show what's happening, comparing the previous version, and using the new lines above
   Print("bars=",Bars," indcount=",IndicatorCounted()," cb=",counted_bars," lim=",limit);
   
   for(int i=limit; i>=0; i--){
      rsi_buff[i]=iRSI(_Symbol,_Period,RSIPeriod,PRICE_CLOSE,i);
   }
   
   static bool rsi_xob;//keep a flag to know that it's happened in the past
   
   for(int i=limit; i>=0; i--){//run through chart left to right
      if(!rsi_xob && rsi_buff[i+2]<RSI_OB && rsi_buff[i+1]>RSI_OB){//if it hasn't crossed up yet
         rsi_xob = true;//log that it has crossed up
         bBuffer1[i+1] = High[i+1];//put an arrow
      }
      if(rsi_xob && rsi_buff[i+1]<45){//if it has crossed up, but has now cross down
         rsi_xob = false;//reset the cross up
         sBuffer1[i+1] = Low[i+1];//put an arrow
      }
   }
   

//   for(int i=0; i<limit; i++)
//     {
//      Buffer[i]=iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,i);
//     }
//
//   for(int i=0; i<limit; i++)
//     {
//      //RSI_OB = 75; RSI_SELL = 45;
//      if(Buffer[i]<RSI_OB && Buffer[i+1]>RSI_OB && Buffer[i]<RSI_SELL)
//        {
//         bBuffer1[i]=iHigh(NULL,0,i);
//        }
//
//      if(Buffer[i]>RSI_OS && Buffer[i+1]<RSI_OS && Buffer[i]>RSI_BUY)
//        {
//         sBuffer1[i]=iLow(NULL,0,i);
//        }
//
//     }
     
     
     
   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);
  }
//+------------------------------------------------------------------+
