0

I would like to find out the row which meets the condition RSI < 25. However, the result is generated with one data frame. Is it possible to create separate dataframes for any single row?

Thanks.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as wb

stock='TSLA'

ck_df = wb.DataReader(stock,data_source='yahoo',start='2015-01-01')

rsi_period = 14

chg = ck_df['Close'].diff(1)

gain = chg.mask(chg<0,0)
ck_df['Gain'] = gain

loss = chg.mask(chg>0,0)
ck_df['Loss'] = loss

avg_gain = gain.ewm(com = rsi_period-1,min_periods=rsi_period).mean()
avg_loss = loss.ewm(com = rsi_period-1,min_periods=rsi_period).mean()

ck_df['Avg Gain'] = avg_gain
ck_df['Avg Loss'] = avg_loss

rs = abs(avg_gain/avg_loss)

rsi = 100-(100/(1+rs))

ck_df['RSI'] = rsi

RSIFactor = ck_df['RSI'] <25

ck_df[RSIFactor]
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • you might accept and upvote if the answer(s) are helpful: https://stackoverflow.com/help/someone-answers – Zanshin Dec 08 '18 at 21:07

2 Answers2

1

If you want to know at what index the RSI < 25 then just use:

ck_df[ck_df['RSI'] <25].index

The result will also be a dataframe. If you insist on making a new one then:

new_df = ck_df[ck_df['RSI'] <25].copy()

petezurich
  • 9,280
  • 9
  • 43
  • 57
Omkar Sabade
  • 765
  • 3
  • 12
  • Thanks for helping:) Is any choice to make it separate. – Jonathan Tong Dec 08 '18 at 05:55
  • I'm not sure I understand, what do you want to separate? – Omkar Sabade Dec 08 '18 at 05:56
  • I appreciate your prompt response. I mean one data frame for one sorted row Therefore, there should be new_df1, new_df2, new_df3, etc.. Thanks – Jonathan Tong Dec 08 '18 at 05:59
  • Each row is a dataframe in itself, so your new_df1 = new_df.iloc[0],new_df2 = new_df.iloc[1]. Although this seems counter intuitive, you can write something about why you want it that way , maybe there's a better solution. – Omkar Sabade Dec 08 '18 at 06:05
  • Actually, i want to find the data as below. Sort row that RSI <25, then find the highest RSI value of following 14 rows. Example, we sort row1's RSI is below 25, then i want to find the highest RSI value from row 2 to row 15. As we have different rows that RSI are below 15, we will have a few sets of data. Therefore, i find the way to store data as various sets Maybe my logic is worng, it should have a better way to do that. – Jonathan Tong Dec 08 '18 at 06:46
  • As I understand you want to find out the maximum RSI value among the rows where RSI is less than 25 right? Try this : new_df['RSI'].max() – Omkar Sabade Dec 08 '18 at 06:59
  • @JonathanTong, this poses a new question. You should post a new one separately – Zanshin Dec 08 '18 at 07:01
0

To split the rows found by @Omkar's solution into separate dataframes you might use this function taken from here: Pandas: split dataframe into multiple dataframes by number of rows;

def split_dataframe_to_chunks(df, n):
    df_len = len(df)
    count = 0
    dfs = []

    while True:
        if count > df_len-1:
            break

        start = count
        count += n
        dfs.append(df.iloc[start : count])
    return dfs

With this you get a list of dataframes.

Zanshin
  • 1,262
  • 1
  • 14
  • 30