-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_data_fetcher.py
More file actions
291 lines (236 loc) · 9.8 KB
/
live_data_fetcher.py
File metadata and controls
291 lines (236 loc) · 9.8 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
Live Data Fetcher for TSLA Trading Bot
Fetches real-time 5-minute OHLCV data from Polygon API
"""
import os
import time
import requests
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import pytz
from typing import Optional, Dict, List
import logging
from dataclasses import dataclass
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
@dataclass
class BarData:
"""Single bar of OHLCV data"""
timestamp: datetime
open: float
high: float
low: float
close: float
volume: int
class PolygonDataFetcher:
"""Fetches live and historical data from Polygon API"""
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://api.polygon.io"
self.session = requests.Session()
self.session.headers.update({
'Authorization': f'Bearer {api_key}'
})
def get_historical_bars(self, symbol: str, timespan: str = "minute",
multiplier: int = 5, limit: int = 50000,
from_date: Optional[str] = None) -> List[BarData]:
"""
Fetch historical bars for initial data loading
Args:
symbol: Stock symbol (e.g., 'TSLA')
timespan: 'minute', 'hour', 'day'
multiplier: Number of timespans (e.g., 5 for 5-minute bars)
limit: Maximum number of bars to fetch
from_date: Start date in YYYY-MM-DD format
"""
# Use US/Eastern timezone to align with market hours
now = datetime.now(pytz.timezone('US/Eastern'))
if not from_date:
# Default to 30 days ago from current time
from_date = (now - timedelta(days=30)).strftime('%Y-%m-%d')
to_date = now.strftime('%Y-%m-%d')
logger.debug(
f"Requesting historical bars for {symbol} from {from_date} to {to_date}"
)
url = (
f"{self.base_url}/v2/aggs/ticker/{symbol}/range/"
f"{multiplier}/{timespan}/{from_date}/{to_date}"
)
params = {
'adjusted': 'true',
'sort': 'asc',
'limit': 50000
}
try:
response = self.session.get(url, params=params)
response.raise_for_status()
data = response.json()
if data.get('status') != 'OK':
logger.error(f"Polygon API error: {data}")
return []
bars = []
results = data.get('results', [])
if not results:
logger.warning(
f"No historical bars returned for {symbol} between {from_date} and {to_date}"
)
return []
for result in results:
# Convert timestamp from milliseconds to datetime
timestamp = datetime.fromtimestamp(result['t'] / 1000, tz=pytz.timezone('US/Eastern'))
bar = BarData(
timestamp=timestamp,
open=result['o'],
high=result['h'],
low=result['l'],
close=result['c'],
volume=result['v']
)
bars.append(bar)
logger.info(f"Fetched {len(bars)} historical bars for {symbol}")
return bars
except Exception as e:
logger.error(f"Error fetching historical data: {e}")
return []
def get_latest_bar(self, symbol: str) -> Optional[BarData]:
"""
Fetch the most recent completed bar
"""
# Get the last 2 bars to ensure we have the most recent completed one
bars = self.get_historical_bars(symbol, limit=2)
if not bars:
return None
# Return the most recent bar
return bars[-1]
def get_current_price(self, symbol: str) -> Optional[float]:
"""
Get current real-time price
"""
url = f"{self.base_url}/v2/last/trade/{symbol}"
try:
response = self.session.get(url)
response.raise_for_status()
data = response.json()
if data.get('status') != 'OK':
logger.error(f"Error getting current price: {data}")
return None
return data['results']['p'] # price
except Exception as e:
logger.error(f"Error fetching current price: {e}")
return None
class DataManager:
"""Manages historical and live data for the trading bot"""
def __init__(self, polygon_api_key: str, symbol: str = "TSLA"):
self.symbol = symbol
self.fetcher = PolygonDataFetcher(polygon_api_key)
self.historical_data = pd.DataFrame()
self.current_price = None
self.last_update = None
def initialize_historical_data(self, days_back: int = 30) -> bool:
"""
Load initial historical data for indicator calculations
"""
logger.info(f"Initializing historical data for {self.symbol}...")
from_date = (datetime.now() - timedelta(days=days_back)).strftime('%Y-%m-%d')
bars = self.fetcher.get_historical_bars(self.symbol, from_date=from_date)
if not bars:
logger.error("Failed to fetch historical data")
return False
# Convert to DataFrame
data = []
for bar in bars:
data.append({
'Datetime': bar.timestamp,
'Open': bar.open,
'High': bar.high,
'Low': bar.low,
'Close': bar.close,
'Volume': bar.volume
})
self.historical_data = pd.DataFrame(data)
self.historical_data.set_index('Datetime', inplace=True)
self.historical_data.sort_index(inplace=True)
logger.info(f"Loaded {len(self.historical_data)} historical bars")
logger.info(f"Data range: {self.historical_data.index[0]} to {self.historical_data.index[-1]}")
return True
def update_current_price(self) -> bool:
"""
Update current real-time price
"""
price = self.fetcher.get_current_price(self.symbol)
if price:
self.current_price = price
self.last_update = datetime.now()
return True
return False
def get_latest_complete_data(self) -> pd.DataFrame:
"""
Get the most recent complete dataset for analysis
"""
# Check if we need to fetch a new bar
latest_bar = self.fetcher.get_latest_bar(self.symbol)
if latest_bar and not self.historical_data.empty:
latest_timestamp = latest_bar.timestamp
# Check if this is a new bar we haven't seen
if latest_timestamp not in self.historical_data.index:
# Add new bar to historical data
new_row = pd.DataFrame({
'Open': [latest_bar.open],
'High': [latest_bar.high],
'Low': [latest_bar.low],
'Close': [latest_bar.close],
'Volume': [latest_bar.volume]
}, index=[latest_timestamp])
self.historical_data = pd.concat([self.historical_data, new_row])
self.historical_data.sort_index(inplace=True)
# Keep only last 1000 bars to manage memory
if len(self.historical_data) > 1000:
self.historical_data = self.historical_data.tail(1000)
logger.info(f"Added new bar: {latest_timestamp} - Close: ${latest_bar.close:.2f}")
return self.historical_data.copy()
def get_market_status(self) -> Dict[str, any]:
"""
Get current market status and data freshness
"""
now = datetime.now(pytz.timezone('US/Eastern'))
# Market hours: 9:30 AM - 4:00 PM ET
market_open = now.replace(hour=9, minute=30, second=0, microsecond=0)
market_close = now.replace(hour=16, minute=0, second=0, microsecond=0)
is_market_hours = market_open <= now <= market_close and now.weekday() < 5
return {
'current_time': now,
'is_market_hours': is_market_hours,
'current_price': self.current_price,
'last_price_update': self.last_update,
'data_bars_count': len(self.historical_data),
'latest_bar_time': self.historical_data.index[-1] if not self.historical_data.empty else None
}
def test_data_fetcher():
"""Test the data fetcher functionality"""
# Use configured Polygon API key
api_key = os.getenv('POLYGON_API_KEY', 'JlAQap9qJ8F8VrfChiPmYpticVo6SMPO')
# Initialize data manager
data_manager = DataManager(api_key, "TSLA")
# Test historical data loading
if data_manager.initialize_historical_data(days_back=7):
print("✓ Historical data loaded successfully")
print(f"Data shape: {data_manager.historical_data.shape}")
print(f"Latest bar: {data_manager.historical_data.tail(1)}")
else:
print("✗ Failed to load historical data")
return
# Test current price fetching
if data_manager.update_current_price():
print(f"✓ Current TSLA price: ${data_manager.current_price:.2f}")
else:
print("✗ Failed to fetch current price")
# Test market status
status = data_manager.get_market_status()
print(f"✓ Market status: {status}")
if __name__ == "__main__":
test_data_fetcher()