Trading RSI divergence EA, code troubleshoot help needed!

 

Hi, I am trying to trade on RSI divergence, and coded as below. I borrowed some logic from RSI Divergence indicator codes, however, when I backtest it, this EA doesn't seem to function and the backtest ends in a second. Can someone take a look at my code and let me know where I got it wrong? I think it may have to do with my "shift" setting, but I am not sure. Thanks very much for any help!

input bool   OpenBUY=True;
input bool   OpenSELL=True;
input bool   CloseBySignal=True;
input double StopLoss=0;
input double TakeProfit=0;
input double TrailingStop=0;
input int    RSIperiod=14;

input bool   AutoLot=True;
input double Risk=1;
input double ManualLots=0.1;
input int    MagicNumber=123;
input string Koment="RSIea";
input int    Slippage=10;

input ENUM_APPLIED_PRICE    RSI_applied_price                = 0;

//---- buffers

double rsi[];

//----

//---
int OrderBuy,OrderSell;
int ticket;
int LotDigits;

double Trail,iTrailingStop;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {

   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   OrderBuy=0;
   OrderSell=0;
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment)
           {
            if(OrderType()==OP_BUY) OrderBuy++;
            if(OrderType()==OP_SELL) OrderSell++;
            if(TrailingStop>0)
              {
               iTrailingStop=TrailingStop;
               if(TrailingStop<stoplevel) iTrailingStop=stoplevel;
               Trail=iTrailingStop*Point;
               double tsbuy=NormalizeDouble(Bid-Trail,Digits);
               double tssell=NormalizeDouble(Ask+Trail,Digits);
               if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>Trail && Bid-OrderStopLoss()>Trail)
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tsbuy,OrderTakeProfit(),0,Blue);
                 }
               if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>Trail && (OrderStopLoss()-Ask>Trail || OrderStopLoss()==0))
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tssell,OrderTakeProfit(),0,Blue);
                 }
              }
           }
     }

   double rsim5=iRSI(Symbol(),5,RSIperiod,PRICE_CLOSE,1);
   double rsim15=iRSI(Symbol(),15,RSIperiod,PRICE_CLOSE,1);

// double HTb=iCustom(Symbol(),0,"HalfTrend-1.02",0,0); //buy
// double HTs=iCustom(Symbol(),0,"HalfTrend-1.02",1,0); //buy
//--- open position
   int shift=4;
   if(IsTrough(shift) == true) 
   {
    int currentTrough = shift;
    int lastTrough = GetLastTrough(shift);
    if(OpenBUY && OrderBuy<3 && rsi[currentTrough] > rsi[lastTrough] && Low[currentTrough] < Low[lastTrough])OPBUY(); 
   } 
   if(IsPeak(shift) == true)
   {
    int currentPeak = shift;
    int lastPeak = GetLastPeak(shift);
    if(OpenSELL  && OrderSell<3  && rsi[currentPeak] < rsi[lastPeak] && High[currentPeak] > High[lastPeak])OPSELL(); 
   }
//--- close position by signal
   if(CloseBySignal)
     {
      if(OrderSell>0  && rsim5>70 && rsim15>70) CloseSell();
      if(OrderBuy>0 && rsim5<30  && rsim15<30)  CloseBuy();
     }
//---
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPBUY()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPSELL()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0;
//---
   ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseSell()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseBuy()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LOT()
  {
   double lotsi;
   double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);
   double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);
   double tick=MarketInfo(Symbol(),MODE_TICKVALUE);
//---
   double  myAccount=AccountBalance();
//---
   if(ilot_min==0.01) LotDigits=2;
   if(ilot_min==0.1) LotDigits=1;
   if(ilot_min==1) LotDigits=0;
//---
   if(AutoLot)
     {
      lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits);
        } else { lotsi=ManualLots;
     }
//---
   if(lotsi>=ilot_max) { lotsi=ilot_max; }
//---
   return(lotsi);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CalculateRSI(int x1)
{
    rsi[x1] = iRSI(Symbol(),PERIOD_CURRENT,RSIperiod,RSI_applied_price,x1);             
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

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

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsPeak(int shift)
{
    if(High[shift]>=High[shift+1] && High[shift+1]>=High[shift+2] && High[shift+2]>=High[shift+3]
       && High[shift]>=High[shift-1] && High[shift-1]>=High[shift-2] && High[shift-2]>=High[shift-3])
        return(true);
    else 
        return(false);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsTrough(int shift)
{
    if(Low[shift]<=Low[shift+1] && Low[shift+1]<=Low[shift+2] && Low[shift+2]<=Low[shift+3]
       && Low[shift]<=Low[shift-1] && Low[shift-1]<=Low[shift-2] && Low[shift-2]<=Low[shift-3])
        return(true);
    else 
        return(false);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetLastPeak(int shift)
{
    for(int j = shift + 5; j < Bars; j++)
    {
        if(High[j] >= High[j+1] && High[j+1] >= High[j+2] && High[j+2]>=High[j+3] &&
           High[j] >= High[j-1] && High[j-1] >= High[j-2] && High[j-2]>=High[j-3])
            return(j);
    }
    return(-1);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetLastTrough(int shift)
{
    for(int j = shift + 5; j < Bars; j++)
    {
        if(Low[j] <= Low[j+1] && Low[j+1] <= Low[j+2] && Low[j+2]<=Low[j+3] &&
           Low[j] <= Low[j-1] && Low[j-1] <= Low[j-2] && Low[j-2]<=Low[j-3])
            return(j);
    }
    return(-1);
}
Reason: