Skip to content

Instantly share code, notes, and snippets.

@onurmatik
Last active June 13, 2022 07:12
Show Gist options
  • Save onurmatik/3f7fca885b68bb1b7bf32fc4f2e00388 to your computer and use it in GitHub Desktop.
Save onurmatik/3f7fca885b68bb1b7bf32fc4f2e00388 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "db99480b-2b2b-402a-bc27-a6d40dba63a1",
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime, timezone, timedelta\n",
"import requests\n",
"import pandas as pd\n",
"import pandas_datareader as pdr"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "27ad3310-f9ba-4d3f-83cf-1aa72efaa6b0",
"metadata": {},
"outputs": [],
"source": [
"# Get the symbols\n",
"\n",
"\n",
"LIMIT = 100 # <= 9999\n",
"\n",
"url = f\"https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=&limit={LIMIT}&sortBy=market_cap&sortType=desc&convert=USD&cryptoType=all&tagType=all&audited=false\"\n",
"\n",
"r = requests.get(url)\n",
"\n",
"data = r.json().get('data', [])\n",
"\n",
"symbols = [coin['symbol'] for coin in data['cryptoCurrencyList']]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84a90f8d-364d-42f3-9631-7067023755a4",
"metadata": {},
"outputs": [],
"source": [
"# Fetch the daily historical price data for the symbols\n",
"\n",
"\n",
"today = datetime.today()\n",
"\n",
"DATE_FIRST, DATE_LAST = today - timedelta(weeks=260), today\n",
"\n",
"dfs = []\n",
"failed = []\n",
"\n",
"for symbol in symbols:\n",
" try:\n",
" df = pdr.DataReader(\n",
" f\"{symbol}-USD\", \n",
" 'yahoo', \n",
" DATE_FIRST,\n",
" DATE_LAST\n",
" )\n",
" except KeyError:\n",
" failed.append(symbol)\n",
" continue\n",
" print(symbol, len(df))\n",
" df['Symbol'] = symbol\n",
" dfs.append(df)\n",
"\n",
"df = pd.concat(dfs)\n",
"df.reset_index(inplace=True)\n",
"df.set_index(['Symbol', 'Date'], inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9959c377-d94c-4bd2-8819-a41074946de4",
"metadata": {},
"outputs": [],
"source": [
"# Write to CSV\n",
"\n",
"\n",
"df.to_csv(f\"crypto-top{LIMIT}.csv\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "highwire-stats-venv",
"language": "python",
"name": "highwire-stats-venv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment