OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I make faster this data frame iteration? [closed]

  • Thread starter Thread starter David Sebastin Otero Velsquez
  • Start date Start date
D

David Sebastin Otero Velsquez

Guest
I am trying to calculate the performance of a stock if you sell at certain level of RSI (a column in the dataFrame). For this I have to iterate every row with a function that searchs and get the performance of the invest. There are more than 5000 rows and it is a very slow process. Just as clarification, instead of dates I have integers that starts from 0. The RSI is an column of "data".

These are the functions to get the performance.

Code:
def getGain(init, end, index=4):
  aGain = data.iloc[end, index] - data.iloc[init, index]
  gain = (aGain*100)/data.iloc[init, index]
  return gain, abs(init - end)


def getGainRSI(start, RSIExpected, indexP=4):
  pSells = data[data["RSI"]>RSIExpected]
  pSells = pSells.filter(["RSI"])
  sIndex = 0

  for iIndexes in tuple(pSells["RSI"][pSells["RSI"] > RSIExpected]):
    for n in [a for a in pSells.index[pSells["RSI"] == iIndexes].tolist() if a > start]:
      if n > start:
        sIndex = n
        break
    if sIndex != 0:
      break
  if sIndex == 0:
    return np.nan, np.nan
  else:
    return getGain(start, sIndex, indexP)

The "performance" and "time" dataframes are empty dataframes with the same shape of "data"

Code:
for r in range(len(data.index)):
  for c in performance.columns:
    performance.iloc[r,c], time.iloc[r,c] = getGainRSI(r, c)
    print("Row: ",r," Column: ",c)

I have tried Using Tuples in the getGainRSI but it not enough. I don't know what else to do.
<p>I am trying to calculate the performance of a stock if you sell at certain level of RSI (a column in the dataFrame). For this I have to iterate every row with a function that searchs and get the performance of the invest. There are more than 5000 rows and it is a very slow process. Just as clarification, instead of dates I have integers that starts from 0. The RSI is an column of "data".</p>
<p>These are the functions to get the performance.</p>
<pre><code>def getGain(init, end, index=4):
aGain = data.iloc[end, index] - data.iloc[init, index]
gain = (aGain*100)/data.iloc[init, index]
return gain, abs(init - end)


def getGainRSI(start, RSIExpected, indexP=4):
pSells = data[data["RSI"]>RSIExpected]
pSells = pSells.filter(["RSI"])
sIndex = 0

for iIndexes in tuple(pSells["RSI"][pSells["RSI"] > RSIExpected]):
for n in [a for a in pSells.index[pSells["RSI"] == iIndexes].tolist() if a > start]:
if n > start:
sIndex = n
break
if sIndex != 0:
break
if sIndex == 0:
return np.nan, np.nan
else:
return getGain(start, sIndex, indexP)
</code></pre>
<p>The "performance" and "time" dataframes are empty dataframes with the same shape of "data"</p>
<pre><code>for r in range(len(data.index)):
for c in performance.columns:
performance.iloc[r,c], time.iloc[r,c] = getGainRSI(r, c)
print("Row: ",r," Column: ",c)
</code></pre>
<p>I have tried Using Tuples in the getGainRSI but it not enough. I don't know what else to do.</p>
 

Latest posts

S
Replies
0
Views
1
Sergey Bakaev Rettley
S
Top