Skip to content

Commit

Permalink
Upgraded and compatibility added for yfinance==0.2.54
Browse files Browse the repository at this point in the history
  • Loading branch information
pranjal-joshi committed Feb 23, 2025
1 parent 2f05a50 commit 41102b1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 43 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ retrying
scipy==1.11.2
TA-Lib-Precompiled
tabulate
yfinance==0.2.32
yfinance==0.2.54
alive-progress==1.6.2
Pillow
scikit-learn==1.3.2
Expand Down
6 changes: 5 additions & 1 deletion src/classes/Changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from classes.ColorText import colorText

VERSION = "2.25"
VERSION = "2.26"

changelog = colorText.BOLD + '[ChangeLog]\n' + colorText.END + colorText.BLUE + '''
[1.00 - Beta]
Expand Down Expand Up @@ -302,4 +302,8 @@
[2.25]
1. Reduced docker image size by 50% (Special Thanks to https://github.com/smitpsanghavi)
[2.26]
1. Bugfixes - yfinance package updated to 0.2.54 to fix Yahoo Finance API issue
2. Minor Improvements to maintain backward compatibility of the yfinance df
''' + colorText.END
36 changes: 35 additions & 1 deletion src/classes/Fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,12 @@ def fetchStockData(self, stockCode, period, duration, proxyServer, screenResults
progress=False,
timeout=10,
start=self._getBacktestDate(backtest=backtestDate)[0],
end=self._getBacktestDate(backtest=backtestDate)[1]
end=self._getBacktestDate(backtest=backtestDate)[1],
auto_adjust=False
)
# For df backward compatibility towards yfinance 0.2.32
data = self.makeDataBackwardCompatible(data)
# end
if backtestDate != datetime.date.today():
dateDict = self._getDatesForBacktestReport(backtest=backtestDate)
backtestData = yf.download(
Expand Down Expand Up @@ -260,6 +264,7 @@ def fetchStockData(self, stockCode, period, duration, proxyServer, screenResults
# Get Daily Nifty 50 Index:
def fetchLatestNiftyDaily(self, proxyServer=None):
data = yf.download(
auto_adjust=False,
tickers="^NSEI",
period='5d',
interval='1d',
Expand All @@ -268,6 +273,7 @@ def fetchLatestNiftyDaily(self, proxyServer=None):
timeout=10
)
gold = yf.download(
auto_adjust=False,
tickers="GC=F",
period='5d',
interval='1d',
Expand All @@ -276,19 +282,24 @@ def fetchLatestNiftyDaily(self, proxyServer=None):
timeout=10
).add_prefix(prefix='gold_')
crude = yf.download(
auto_adjust=False,
tickers="CL=F",
period='5d',
interval='1d',
proxy=proxyServer,
progress=False,
timeout=10
).add_prefix(prefix='crude_')
data = self.makeDataBackwardCompatible(data)
gold = self.makeDataBackwardCompatible(gold, column_prefix='gold_')
crude = self.makeDataBackwardCompatible(crude, column_prefix='crude_')
data = pd.concat([data, gold, crude], axis=1)
return data

# Get Data for Five EMA strategy
def fetchFiveEmaData(self, proxyServer=None):
nifty_sell = yf.download(
auto_adjust=False,
tickers="^NSEI",
period='5d',
interval='5m',
Expand All @@ -297,6 +308,7 @@ def fetchFiveEmaData(self, proxyServer=None):
timeout=10
)
banknifty_sell = yf.download(
auto_adjust=False,
tickers="^NSEBANK",
period='5d',
interval='5m',
Expand All @@ -305,6 +317,7 @@ def fetchFiveEmaData(self, proxyServer=None):
timeout=10
)
nifty_buy = yf.download(
auto_adjust=False,
tickers="^NSEI",
period='5d',
interval='15m',
Expand All @@ -313,13 +326,18 @@ def fetchFiveEmaData(self, proxyServer=None):
timeout=10
)
banknifty_buy = yf.download(
auto_adjust=False,
tickers="^NSEBANK",
period='5d',
interval='15m',
proxy=proxyServer,
progress=False,
timeout=10
)
nifty_buy = self.makeDataBackwardCompatible(nifty_buy)
banknifty_buy = self.makeDataBackwardCompatible(banknifty_buy)
nifty_sell = self.makeDataBackwardCompatible(nifty_sell)
banknifty_sell = self.makeDataBackwardCompatible(banknifty_sell)
return nifty_buy, banknifty_buy, nifty_sell, banknifty_sell

# Load stockCodes from the watchlist.xlsx
Expand Down Expand Up @@ -352,3 +370,19 @@ def fetchWatchlist(self):
f'[+] watchlist_template.xlsx created in {os.getcwd()} as a referance template.' + colorText.END)
return None
return data

def makeDataBackwardCompatible(self, data:pd.DataFrame, column_prefix:str=None) -> pd.DataFrame:
data = data.droplevel(level=1, axis=1)
data = data.rename_axis(None, axis=1)
column_prefix = '' if column_prefix is None else column_prefix
data = data[
[
f'{column_prefix}Open',
f'{column_prefix}High',
f'{column_prefix}Low',
f'{column_prefix}Close',
f'{column_prefix}Adj Close',
f'{column_prefix}Volume'
]
]
return data
18 changes: 9 additions & 9 deletions src/classes/Screener.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ def getCandleType(self, dailyData):


# Preprocess the acquired data
def preprocessData(self, data, daysToLookback=None):
def preprocessData(self, data:pd.DataFrame, daysToLookback=None):
if daysToLookback is None:
daysToLookback = self.configManager.daysToLookback
if self.configManager.useEMA:
sma = ScreenerTA.EMA(data['Close'],timeperiod=50)
lma = ScreenerTA.EMA(data['Close'],timeperiod=200)
data.insert(6,'SMA',sma)
data.insert(7,'LMA',lma)
data.insert(len(data.columns),'SMA',sma)
data.insert(len(data.columns),'LMA',lma)
else:
sma = data.rolling(window=50).mean()
lma = data.rolling(window=200).mean()
data.insert(6,'SMA',sma['Close'])
data.insert(7,'LMA',lma['Close'])
data.insert(len(data.columns),'SMA',sma['Close'])
data.insert(len(data.columns),'LMA',lma['Close'])
vol = data.rolling(window=20).mean()
rsi = ScreenerTA.RSI(data['Close'], timeperiod=14)
data.insert(8,'VolMA',vol['Volume'])
data.insert(9,'RSI',rsi)
data.insert(len(data.columns),'VolMA',vol['Volume'])
data.insert(len(data.columns),'RSI',rsi)
data = data[::-1] # Reverse the dataframe
# data = data.fillna(0)
# data = data.replace([np.inf, -np.inf], 0)
Expand Down Expand Up @@ -411,7 +411,7 @@ def findReversalMA(self, data, screenDict, saveDict, maLength, percentage=0.015)
maRev = ScreenerTA.EMA(data['Close'],timeperiod=maLength)
else:
maRev = ScreenerTA.MA(data['Close'],timeperiod=maLength)
data.insert(10,'maRev',maRev)
data.insert(len(data.columns),'maRev',maRev)
data = data[::-1].head(3)
if data.equals(data[(data.Close >= (data.maRev - (data.maRev*percentage))) & (data.Close <= (data.maRev + (data.maRev*percentage)))]) and data.head(1)['Close'].iloc[0] >= data.head(1)['maRev'].iloc[0]:
if self.configManager.stageTwo:
Expand All @@ -426,7 +426,7 @@ def findReversalMA(self, data, screenDict, saveDict, maLength, percentage=0.015)
def findRSICrossingMA(self, data, screenDict, saveDict, maLength=9):
data = data[::-1]
maRsi = ScreenerTA.MA(data['RSI'], timeperiod=maLength)
data.insert(10,'maRsi',maRsi)
data.insert(len(data.columns),'maRsi',maRsi)
data = data[::-1].head(3)
if data['maRsi'].iloc[0] <= data['RSI'].iloc[0] and data['maRsi'].iloc[1] > data['RSI'].iloc[1]:
screenDict['MA-Signal'] = colorText.BOLD + colorText.GREEN + f'RSI-MA-Buy' + colorText.END
Expand Down
Loading

0 comments on commit 41102b1

Please sign in to comment.