ChillarAnand / stocktrends

A python package to calculate trends in stocks, derivates(Futures & Options) using Renko, PnF, LineBreak etc
GNU General Public License v3.0
247 stars 74 forks source link

ValueError:cannot convert float NaN to integer #18

Open tan-yong-sheng opened 3 years ago

tan-yong-sheng commented 3 years ago

Below Renko works for AAPL stock tickers but not for Malaysia Exchange such as 0200.KL. It comes out ValueError:cannot convert float NaN to integer although I find no NaN on my OHLC stock price data.

import datetime
import dateutil
import pandas_datareader.data as wb
from stocktrends import Renko

year=5
tickers ="0200.KL" #Comment it and uncomment the below code later
#tickers="AAPL" #uncomment this to find out whether this ticker works
ohlc = wb.get_data_yahoo(tickers,start=datetime.date.today()-dateutil.relativedelta.relativedelta(years=year),end=datetime.date.today())

def ATR(DF,n):
    "function to calculate True Range and Average True Range"
    df = DF.copy()
    df['H-L']=abs(df['High']-df['Low'])
    df['H-PC']=abs(df['High']-df['Adj Close'].shift(1))
    df['L-PC']=abs(df['Low']-df['Adj Close'].shift(1))
    df['True Range']=df[['H-L','H-PC','L-PC']].max(axis=1,skipna=False)
    df["Average True Range"] = df['True Range'].rolling(n).mean()
    #df2 = df.drop(['H-L','H-PC','L-PC'],axis=1)
    return df["Average True Range"]

def renko_DF(DF):
    "function to convert ohlc data into renko bricks"
    df = DF.copy()
    df.reset_index(inplace=True)
    df = df.iloc[:,[0,1,2,3,5,6]]
    df.rename(columns = {"Date" : "date", "High" : "high","Low" : "low", "Open" : "open","Adj Close" : "close", "Volume" : "volume"}, inplace = True)
    print(df.isnull().values.any())
    #df.fillna(method="bfill")
    df2 = Renko(df)
    df2.brick_size = round(ATR(DF,120)[-1],0)
    renko_df = df2.get_ohlc_data()
    return renko_df

renko_DF(ohlc)
ChillarAnand commented 3 years ago

Thanks, @tys203831

Can you please provide a sample csv file that you were using for this?

Umairnaseem1 commented 3 years ago

Hello @tys203831

have you resolved this issue ? as I am facing a similar problem in my project. It would be very helpful if you can guide me thanks

ChillarAnand commented 3 years ago

Related to https://github.com/ChillarAnand/stocktrends/issues/23

Prasanna28Devadiga commented 3 years ago

Is there a way to fix this @ChillarAnand? Also it will be great if you can let us know why this issue is arising. So that we can avoid such cases for the timebeing

ChillarAnand commented 3 years ago

I am not able to reproduce the issue. @Prasanna28Devadiga

If someone can share sample data to reproduce this issue, it will be helpful.

Prasanna28Devadiga commented 3 years ago

@ChillarAnand Here is a sample data that i was facing the issue with: https://drive.google.com/file/d/1mt_T3wH2ICaqCG6UcR9kkyEWpH-hV9dB/view?usp=sharing It is the Intraday (1 minute) data for 'IDEA' stock downloaded from the yfinance api.

Prasanna28Devadiga commented 3 years ago

@ChillarAnand were you able to find out what exactly is going wrong/ ways to workaround ?

chranga commented 3 years ago

Hey guys, I think the issue is from the ATR function, called in the renko_DF function. df2.brick_size = round(ATR(DF,120)[-1],0)

The reason for the NaNs is because of the ATR for the Malaysian ticker. Try the below

print(round(ATR(ohlc,120)[-1],0))
print(ATR(ohlc,120)[-1])

Output is 0, when rounded and 0.0594166616598765

So in the renko_DF function, just change df2.brick_size = round(ATR(DF,120)[-1],0) to df2.brick_size = round(ATR(DF,120)[-1],1)

Which gives an ATR of 0.1 and the NaN doesnt appear anymore.

Alternately, just assign a df2.brick_size = with a fixed value (to get the fixed brick size) and it will work.

Also note that you will get NaN's whenever a stock price < 1. I think by simply adding an if/else statement within the function to check if price is > 1 or < 1 and set the rounding to 0 or 1 will help resolve the issue. closes #18

ChillarAnand commented 3 years ago

@chranga Thanks for submitting detailed report.

Would you like to send pull request for the same?

chranga commented 3 years ago

@chranga Thanks for submitting detailed report.

Would you like to send pull request for the same?

Hi, a bit new to this. But I edited the above comment with closes #18 but it doesnt seem to work?