File size: 1,723 Bytes
6aecc4e
 
 
 
bc4bba1
6aecc4e
f7ecac2
bc4bba1
6f41d8a
6aecc4e
6f41d8a
6aecc4e
 
 
bc4bba1
6aecc4e
 
 
bc4bba1
 
6f41d8a
6aecc4e
bc4bba1
 
 
 
 
 
 
6f41d8a
bc4bba1
 
6aecc4e
6f41d8a
bc4bba1
6f41d8a
 
 
bc4bba1
6aecc4e
 
 
 
 
bc4bba1
 
 
 
6f41d8a
 
bc4bba1
6f41d8a
 
6aecc4e
bc4bba1
6aecc4e
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
import requests
import pandas as pd
from datetime import datetime, timedelta

API_KEY = "UUrRnlZXDllgVVn9bekrSTiBYRUqNFwp"  # πŸ” Replace this with your actual key

def fetch_ohlcv(symbol="AAPL"):
    start_date = (datetime.utcnow() - timedelta(days=5)).strftime('%Y-%m-%d')
    end_date = (datetime.utcnow() - timedelta(days=1)).strftime('%Y-%m-%d')

    url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/minute/{start_date}/{end_date}"
    params = {
        "adjusted": "true",
        "sort": "asc",
        "limit": 5000,
        "apiKey": API_KEY,
    }

    print("🟑 URL:", url)
    print("🟑 Params:", params)

    try:
        response = requests.get(url, params=params)
        print("πŸ”΅ Status Code:", response.status_code)

        # Show full response if something goes wrong
        if response.status_code != 200:
            print("❌ Full response:", response.text)
            return None

        data = response.json()
        print("πŸ” Response JSON:", data)

        if "results" not in data:
            print("❌ 'results' key not found in response.")
            return None

        if not data["results"]:
            print("❌ Empty 'results' list.")
            return None

        df = pd.DataFrame(data["results"])
        df["t"] = pd.to_datetime(df["t"], unit="ms")
        df.set_index("t", inplace=True)
        df.rename(columns={
            "o": "Open", "h": "High", "l": "Low", "c": "Close", "v": "Volume"
        }, inplace=True)

        df = df[["Open", "High", "Low", "Close", "Volume"]]

        print(f"βœ… Successfully loaded {len(df)} rows.")
        return df

    except Exception as e:
        print("❌ Exception:", str(e))
        return None