Skip to content

Commit

Permalink
added skeleton for price history and join method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Brent committed Sep 25, 2024
1 parent 341184e commit ffaef3b
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pandas as pd
import yfinance as yf
from src.utils import Utils

Expand All @@ -7,14 +8,42 @@ class Data:
def __init__(self):
self.utils = Utils()

def get_news(self, ticker: str) -> list:
def get_news(self, ticker: str) -> pd.DataFrame:
"""
:param ticker:
:return:
"""

ticker = yf.Ticker(ticker)
return [(x['title'],
self.utils.convert_datetime(x['providerPublishTime']))
for x in ticker.news]
return pd.DataFrame([(x['title'],
*self.utils.convert_datetime(x['providerPublishTime']))
for x in ticker.news],
columns=['news', 'original_date', 'effective_date'])

def get_price_history(self, ticker: str, period: str = '1mo') -> pd.DataFrame:
ticker = yf.Ticker(ticker)
historical_price: pd.DataFrame = ticker.history(period=period).reset_index()

# remove timestamp
historical_price['Date'] = (
historical_price['Date'].astype(str).apply(lambda x: x.split(' ')[0]))

return historical_price

def join_price_and_news(self, price: pd.DataFrame, news: pd.DataFrame):
# TODO: handle multiple peices of news in one day
df = price.merge(news, left_on='Date', right_on='effective_date')
return df

def main(self, ticker: str):
news_data: pd.DataFrame = self.get_news(ticker=ticker)
price_data: pd.DataFrame = self.get_price_history(ticker=ticker)

data = self.join_price_and_news(price=price_data, news=news_data)
self.feature_extraction(data=data)

def feature_extraction(self, data):
# TODO: extract features for model
pass

0 comments on commit ffaef3b

Please sign in to comment.