ProfilingAI / src /core /data_provider.py
Sandrine Guétin
Version propre de DeepVest
2106f78
raw
history blame
1.18 kB
import finnhub
import datetime
import pandas as pd
class FinnhubDataProvider:
def __init__(self):
self.client = finnhub.Client(api_key="csrq269r01qj3u0os3r0csrq269r01qj3u0os3rg")
def get_stock_data(self, symbol, start_date, end_date):
start_timestamp = int(datetime.datetime.strptime(start_date, '%Y-%m-%d').timestamp())
end_timestamp = int(datetime.datetime.strptime(end_date, '%Y-%m-%d').timestamp())
try:
data = self.client.stock_candles(symbol, 'D', start_timestamp, end_timestamp)
if data['s'] == 'ok':
df = pd.DataFrame({
'date': pd.to_datetime(data['t'], unit='s'),
'close': data['c'],
'high': data['h'],
'low': data['l'],
'open': data['o'],
'volume': data['v']
})
df.set_index('date', inplace=True)
return df
else:
print(f"Error fetching data for {symbol}")
return None
except Exception as e:
print(f"Error: {e}")
return None