Skip to content

Commit

Permalink
refresh correlation.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
enarjord committed Jun 4, 2022
1 parent 7416d26 commit 0a56dac
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions correlation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"from time import time, sleep\n",
"import os\n",
"import pandas as pd\n",
"import asyncio\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
Expand Down Expand Up @@ -64,35 +65,19 @@
"outputs": [],
"source": [
"info = await bot.public_get(bot.endpoints[\"exchange_info\"])\n",
"symbols = [e[\"symbol\"] for e in info[\"symbols\"] if e[\"symbol\"].endswith(\"USDT\")]\n",
"\n",
"interval = \"5m\"\n",
"\n",
"fpath = f\"logs/binance/ohlcvs_{interval}/\"\n",
"if os.path.exists(fpath):\n",
" print(\"loading cache\")\n",
" csvs = {}\n",
" for f in [x for x in os.listdir(fpath) if x.endswith(\".csv\")]:\n",
" csvs[f[:-4]] = pd.read_csv(fpath + f)\n",
"else:\n",
" fpath = make_get_filepath(f\"logs/binance/ohlcvs_{interval}/\")\n",
"\n",
" data = {}\n",
" for s in sorted(symbols):\n",
" print(s, end=\" \")\n",
" ohlcvs = await bot.fetch_ohlcvs(symbol=s, interval=interval)\n",
" data[s] = ohlcvs\n",
" sleep(0.5)\n",
" # cache as csv for later use\n",
" csvs = {}\n",
" fpath = make_get_filepath(f\"logs/binance/ohlcvs_{interval}/\")\n",
" for s in data:\n",
" csvs[s] = pd.DataFrame(data[s])\n",
" csvs[s].to_csv(f\"{fpath}{s}.csv\")\n",
"n_days = (\n",
" (csvs[symbols[0]].timestamp.iloc[-1] - csvs[symbols[0]].timestamp.iloc[0]) / 1000 / 60 / 60 / 24\n",
")\n",
"print(\"n days\", n_days)"
"csvs = {}\n",
"syms = [x['symbol'] for x in info['symbols'] if x['symbol'].endswith('USDT')]\n",
"n = 7\n",
"interval = '5m'\n",
"while True:\n",
" if not syms:\n",
" break\n",
" subset = syms[:n]\n",
" syms = syms[n:]\n",
" res = await asyncio.gather(*[bot.fetch_ohlcvs(symbol=s, interval=interval) for s in subset])\n",
" for s, r in zip(subset, res):\n",
" csvs[s] = pd.DataFrame(r)\n",
" print(subset, len(syms))"
]
},
{
Expand Down Expand Up @@ -163,17 +148,8 @@
"outputs": [],
"source": [
"# sum correlation of each symbol to every other symbol\n",
"sorted([(sum(corrs_dict[s].values()), s) for s in corrs_dict])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# sum abs correlation of each symbol to every other symbol\n",
"sorted([(sum(corrs_dict_abs[s].values()), s) for s in corrs_dict])"
"corr_sorted = sorted([(sum(corrs_dict[s].values()), s) for s in corrs_dict])\n",
"corr_sorted"
]
},
{
Expand All @@ -186,7 +162,8 @@
"mean_prices = pd.concat([e.close / e.close.iloc[0] for e in csvs.values()], axis=1).mean(axis=1)\n",
"\n",
"# compare individual symbol to mean\n",
"symbol = \"SOLUSDT\"\n",
"symbol = \"SXPUSDT\"\n",
"# symbol = corr_sorted[0][1] # least correlated symbol\n",
"mean_prices.rename(\"mean_prices\").plot()\n",
"(csvs[symbol].close / csvs[symbol].close.iloc[0]).rename(symbol).plot()\n",
"plt.legend()"
Expand Down Expand Up @@ -230,7 +207,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 0a56dac

Please sign in to comment.