Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Created May 12, 2022 20:41
Show Gist options
  • Select an option

  • Save KenoLeon/3db05ff4e23e4e131f250b2dd88beef9 to your computer and use it in GitHub Desktop.

Select an option

Save KenoLeon/3db05ff4e23e4e131f250b2dd88beef9 to your computer and use it in GitHub Desktop.
historical last 100 days
from pycoingecko import CoinGeckoAPI
import datetime as dt
# today in Timestamp
today = dt.datetime.today()
today_unix = today.timestamp()
# today minus 100 days in Timestamp
today_minus_100 = today - dt.timedelta(days=100)
today_minus_100_unix = today_minus_100.timestamp()
def unix_to_date(unix_timestamp):
return dt.datetime.fromtimestamp(unix_timestamp/1000).strftime('%Y-%m-%d')
cg = CoinGeckoAPI()
# get historical prices for bitcoin from today minus 100 days to today
historical_prices = cg.get_coin_market_chart_range_by_id (id='bitcoin',
vs_currency='usd',
from_timestamp=today_minus_100_unix,
to_timestamp=today_unix)
# print each day's price
for day in historical_prices['prices']:
print(unix_to_date(day[0]), day[1])
# >>
# 2022-02-01 38835.69494322237
# 2022-02-02 37000.98249864199
# 2022-02-03 37101.351593780935
# 2022-02-04 41673.8395543094
# 2022-02-05 41493.690050910525
# 2022-02-06 42475.543220951215
# 2022-02-07 43910.929986443094
# 2022-02-08 44184.447511676175
# 2022-02-09 44383.88805541707
# 2022-02-10 43628.13953235228... etc,etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment