yougandar commited on
Commit
bc4bba1
Β·
verified Β·
1 Parent(s): 6f41d8a

Update polygon_loader.py

Browse files
Files changed (1) hide show
  1. polygon_loader.py +22 -17
polygon_loader.py CHANGED
@@ -2,50 +2,55 @@ import requests
2
  import pandas as pd
3
  from datetime import datetime, timedelta
4
 
5
- API_KEY = "UUrRnlZXDllgVVn9bekrSTiBYRUqNFwp" # Replace this with your Polygon.io key
6
 
7
  def fetch_ohlcv(symbol="AAPL"):
8
- # Fixed time window: 2 days ago to 1 day ago
9
- start_date = (datetime.utcnow() - timedelta(days=2)).strftime('%Y-%m-%d')
10
  end_date = (datetime.utcnow() - timedelta(days=1)).strftime('%Y-%m-%d')
11
 
12
  url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/minute/{start_date}/{end_date}"
13
-
14
  params = {
15
  "adjusted": "true",
16
  "sort": "asc",
17
- "limit": 500,
18
  "apiKey": API_KEY,
19
  }
20
 
21
- print(f"🟑 URL: {url}")
22
- print(f"🟑 Params: {params}")
23
 
24
  try:
25
- res = requests.get(url, params=params)
26
- print(f"πŸ”΅ Status Code: {res.status_code}")
27
- print(f"πŸ”΅ Raw Response: {res.text[:300]}...") # Limit preview
 
 
 
 
28
 
29
- res.raise_for_status()
30
- data = res.json()
31
 
32
  if "results" not in data:
33
- print("❌ 'results' key missing in response.")
34
  return None
35
 
36
  if not data["results"]:
37
- print("❌ 'results' is empty.")
38
  return None
39
 
40
  df = pd.DataFrame(data["results"])
41
  df["t"] = pd.to_datetime(df["t"], unit="ms")
42
  df.set_index("t", inplace=True)
43
- df.rename(columns={"o": "Open", "h": "High", "l": "Low", "c": "Close", "v": "Volume"}, inplace=True)
 
 
 
44
  df = df[["Open", "High", "Low", "Close", "Volume"]]
45
 
46
- print(f"βœ… Loaded {len(df)} rows")
47
  return df
48
 
49
  except Exception as e:
50
- print(f"❌ Exception: {e}")
51
  return None
 
2
  import pandas as pd
3
  from datetime import datetime, timedelta
4
 
5
+ API_KEY = "UUrRnlZXDllgVVn9bekrSTiBYRUqNFwp" # πŸ” Replace this with your actual key
6
 
7
  def fetch_ohlcv(symbol="AAPL"):
8
+ start_date = (datetime.utcnow() - timedelta(days=5)).strftime('%Y-%m-%d')
 
9
  end_date = (datetime.utcnow() - timedelta(days=1)).strftime('%Y-%m-%d')
10
 
11
  url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/minute/{start_date}/{end_date}"
 
12
  params = {
13
  "adjusted": "true",
14
  "sort": "asc",
15
+ "limit": 5000,
16
  "apiKey": API_KEY,
17
  }
18
 
19
+ print("🟑 URL:", url)
20
+ print("🟑 Params:", params)
21
 
22
  try:
23
+ response = requests.get(url, params=params)
24
+ print("πŸ”΅ Status Code:", response.status_code)
25
+
26
+ # Show full response if something goes wrong
27
+ if response.status_code != 200:
28
+ print("❌ Full response:", response.text)
29
+ return None
30
 
31
+ data = response.json()
32
+ print("πŸ” Response JSON:", data)
33
 
34
  if "results" not in data:
35
+ print("❌ 'results' key not found in response.")
36
  return None
37
 
38
  if not data["results"]:
39
+ print("❌ Empty 'results' list.")
40
  return None
41
 
42
  df = pd.DataFrame(data["results"])
43
  df["t"] = pd.to_datetime(df["t"], unit="ms")
44
  df.set_index("t", inplace=True)
45
+ df.rename(columns={
46
+ "o": "Open", "h": "High", "l": "Low", "c": "Close", "v": "Volume"
47
+ }, inplace=True)
48
+
49
  df = df[["Open", "High", "Low", "Close", "Volume"]]
50
 
51
+ print(f"βœ… Successfully loaded {len(df)} rows.")
52
  return df
53
 
54
  except Exception as e:
55
+ print("❌ Exception:", str(e))
56
  return None