Created
May 12, 2022 20:51
-
-
Save KenoLeon/e0d2d57723ee172e188f362b4cee0573 to your computer and use it in GitHub Desktop.
Parse and filter API response example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| # Filter for days where the price was between 36000 and 37000 | |
| for day in historical_prices['prices']: | |
| if 36000 < day[1] < 37000: | |
| print(unix_to_date(day[0]), day[1]) | |
| # >> | |
| # 2022-05-05 36612.229548803036 | |
| # 2022-05-06 36116.394294982965 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment