Skip to content

Instantly share code, notes, and snippets.

@ChadThackray
Created June 22, 2024 16:08
Show Gist options
  • Save ChadThackray/3552dd7de3bd0e33edd43de182010f89 to your computer and use it in GitHub Desktop.
Save ChadThackray/3552dd7de3bd0e33edd43de182010f89 to your computer and use it in GitHub Desktop.
Free Crypto Price Data from Polygon.io in Python
# Licensed under the MIT License. See comment below for full licence information.
import requests
import pandas as pd
from datetime import date, datetime, timedelta
import time
def pull_1m_data(symbol, date):
polygon_api_key = "rACH0leobCBj1JkpYoEZjveYtXFhxyJj"
polygon_rest_baseurl = "https://api.polygon.io"
symbol = "X:" + symbol
# we want 1 min candles
multiplier = 1
timespan = "minute"
# just some big number < 50000, which is polygons limit
limit = 40000
# newest data at the top
sort = "desc"
start_time = datetime.combine(date, datetime.min.time())
end_time = start_time + timedelta(days = 1)
start_time = int(start_time.timestamp() * 1000)
# take one off so we don't include start of next day
end_time = int(end_time.timestamp() * 1000) - 1
request_url = f"{polygon_rest_baseurl}/v2/aggs/ticker/{symbol}"\
+f"/range/{multiplier}/{timespan}/{start_time}/{end_time}"\
+f"?sort={sort}&limit={limit}&apiKey={polygon_api_key}"
data = requests.get(request_url).json()
if "results" in data:
return data["results"]
df = pd.DataFrame(data["results"])
df["date"] = pd.to_datetime(df["t"], unit="ms")
df = df[["date","o","h","l","c","v"]]
df.columns = ["time","open","high","low","close","volume"]
return df
else:
raise Exception("Did not receieve appropriate data")
day = date(year = 2021, month = 1, day = 1)
days_data = 2
bars = []
for i in range(days_data):
print(day)
bars += pull_1m_data("UNIUSD",day)
day -= timedelta(days = 1)
#time.sleep(15)
df = pd.DataFrame(bars)
df["date"] = pd.to_datetime(df["t"], unit="ms")
df = df[["date","o","h","l","c","v"]]
df.columns = ["time","open","high","low","close","volume"]
df = df.sort_values("time")
# don't seem to need this
#df = df.set_index('time').asfreq('1Min').ffill().reset_index()
df.to_csv("data.csv", index = False)
print(df)
@ChadThackray
Copy link
Author

Copyright 2022 Chad Thackray

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment