Converting RSI indicator to Python

 

I'm having trouble converting the RSI indicator to Python. The main difference between my function and the RSI indicator is that my function is called everytime a new line of a history data file is appended to totalArray (nested list with values [datetime,o,h,l,c,v]). I can't seem to pinpoint the problem and would gladly appreciate help.

//RSI INDICATOR

   int    i,counted_bars=IndicatorCounted();
   double rel,negative,positive;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
//----
   i=Bars-RSIPeriod-1;
   if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=Close[k]-Close[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=Close[i]-Close[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
        }
      PosBuffer[i]=positive;
      NegBuffer[i]=negative;
      if(negative==0.0) RSIBuffer[i]=0.0;
      else RSIBuffer[i]=100.0-100.0/(1+positive/negative);
      i--;
     }

#MY FUNCTION

def GetRSI(totalArray,RSHolder = []):
    period = 11
    preCalculationPeriod = 11
    x = len(totalArray) - 1
    y = 0    
    gainVal = 0
    lossVal = 0

    if len(totalArray) <= period:
        RSHolder.append([0,0])
        return 0

    while y <= period:
        closeVal = totalArray[x-y][4]
        preCloseVal = totalArray[x-y-1][4]
        value = closeVal - preCloseVal 
        if value > 0:
            gainVal += value
        else:
            lossVal += abs(value)
        y += 1
    if len(totalArray) == preCalculationPeriod:
        avgGain = gainVal/period
        avgLoss = lossVal/period
    else:
        avgGain = (RSHolder[x-y-1][0]*(period - 1) + gainVal)/period
        avgLoss = (RSHolder[x-y-1][1]*(period - 1) + lossVal)/period
    RSHolder.append([avgGain,avgLoss])
    if avgLoss == 0:
        RSI = 100
    else:
        RSI = 100 - 100/(1 + avgGain/avgLoss)

    return RSI
Reason: