Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError:cannot convert float NaN to integer #18

Open
tan-yong-sheng opened this issue Jul 19, 2021 · 10 comments
Open

ValueError:cannot convert float NaN to integer #18

tan-yong-sheng opened this issue Jul 19, 2021 · 10 comments

Comments

@tan-yong-sheng
Copy link

tan-yong-sheng commented Jul 19, 2021

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
Copy link
Owner

Thanks, @tys203831

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

@Umairnaseem1
Copy link

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
Copy link
Owner

Related to #23

@Prasanna28Devadiga
Copy link

Prasanna28Devadiga commented Sep 27, 2021

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
Copy link
Owner

I am not able to reproduce the issue. @Prasanna28Devadiga

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

@Prasanna28Devadiga
Copy link

@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
Copy link

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

@chranga
Copy link

chranga commented Oct 24, 2021

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
Copy link
Owner

@chranga Thanks for submitting detailed report.

Would you like to send pull request for the same?

@chranga
Copy link

chranga commented Oct 25, 2021

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants