File size: 1,181 Bytes
2106f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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