RSI calculations give wrong values (EA)

 

Guys,

i give up. I've been trying to code EA placing orders depending on price peak and certain RSI value. Thing is, my EA works bad like hell but what concerns me more is that RSI values it put on price chart are different than RSI indicator values. Why?

Picture attached shows my problem: robot draws the text near the peak bar with RSI values he thinks it is, i added manualy the real RSI values from indicator underneath.

RSI values wrong 

 

 Here is the code. Please help, what went wrong?

//+------------------------------------------------------------------+
//|                          Copyright 2014,Attitude Adam Grzybowski |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, Attitude"

#define MAGICMA  20140818

extern double Lots               = 1;
extern double SL;
extern double TP;
int res;
double rsiCurrent;

//buffers
double price[30];

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  { 
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForShort();
        else                                   // CheckForClose();
        return;
//----
  }

//+------------------------------------+
//|      1 position at a time          |
//+------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----

for(int i=0;i<OrdersTotal();i++)
        {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
                {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
                }
        }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+-----------------------------------------+
//| dont trade after stop loss hit          |
//+-----------------------------------------+ 
int Losses()
  {
    int Count = 0;
    for(int i=OrdersHistoryTotal()-1; i>=0; i--)
    {
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderProfit()>0) break;
      Count++;
    }
    return(Count);
  }


void CalculateIndicator()//(int countedBars)
{
    int BarsNotToCheck = Bars - 30;
        for(int i = Bars - BarsNotToCheck; i >= 0; i--)
        {
        price[i]=iClose(NULL,0,i);                                                                                      
        }     
}

int GetLastPeak()
 {
 int p=ArrayMaximum(price,WHOLE_ARRAY,5);                                                               
  return(p);
 }

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+

void CheckForShort()
 {
 CalculateIndicator();
    int lastPeak = GetLastPeak();
 bool bNewTrade=True;
 if(Losses()>=1)
  {
  if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
   {
   if(TimeCurrent()-OrderCloseTime() < 60)
   bNewTrade = False;
   }
  }
 if(bNewTrade==False)
  {
  return;
  }

double rsiPeak = iRSI(NULL, 0, 14, PRICE_CLOSE, lastPeak);
//---- sell conditions
  if(Close[3]<Close[2] && Close[1]<Close[2] && Close[lastPeak]<Close[1])  
     {
                SL = Bid + 10*Point;
                TP = Ask - 30*Point;
                res=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,SL,TP,"",MAGICMA,0,Red);
   
                                string s = DoubleToStr(rsiPeak, 1);
                        ObjectCreate(s, OBJ_TEXT, 0, Time[lastPeak], Close[lastPeak]);
                                ObjectSetText(s, s, 10, "Tahoma", White);
                                ObjectSet(s, OBJPROP_ANGLE, 90);  
                                ObjectSet(s, OBJPROP_BACK, true);
                        return;
     }
  }

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

Are the settings for the iRSI call the same as the settings for the RSI on the chart?

Please note that using this could result in an attempt to create an object with the same name as an existing object as it's quite possible for the value to be the same as a previous one. If this happens, the object will not be created.

                                string s = DoubleToStr(rsiPeak, 1);
                        ObjectCreate(s, OBJ_TEXT, 0, Time[lastPeak], Close[lastPeak]);
 
    int BarsNotToCheck = Bars - 30;
        for(int i = Bars - BarsNotToCheck; i >= 0; i--)
        {
        price[i]=iClose(NULL,0,i);
Same as "for(i = 30" but price only has elements 0 .. 29
 
Gadam123:

Guys,

re is the code. Please help, what went wrong?
Looks like you have the wrong indicator attached to the chart, it is not the standard RSI.
 

thanks for your inputs Gents,

1. Regarding the buffer size there is something i dont get. It supposed to collect 30 closing prices but if i code:

double price[30];
for(int i = 1; i <= 30; i++)

it returns "array out of range". When i change:

for(int i = 1; i < 30; i++)

everything goes well.

2. Regarding the RSI settings - this is for sure RSI indicator, i checked with different broker and it draws the same line with the same data.

I didnt mention i use different platform tham MT4 (its so called AlgoStudio but using MQL4). Funny thing - copying my robot to MT4 it returns correct RSI value and works perfectly (still shitty regarding the income;))

Gues i will have to get in touch with this AlgoStudio folks...

Reason: