mango-explorer/Pandas.ipynb

328 lines
11 KiB
Plaintext
Raw Normal View History

2021-04-14 08:51:39 -07:00
{
"cells": [
{
"cell_type": "markdown",
"id": "nutritional-xerox",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
2021-04-15 02:59:04 -07:00
"# ⚠ Warning\n",
"\n",
"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.\n",
"\n",
2021-04-23 01:28:09 -07:00
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gl/OpinionatedGeek%2Fmango-explorer/HEAD?filepath=Pandas.ipynb) _🏃 To run this notebook press the ⏩ icon in the toolbar above._\n",
"\n",
"[🥭 Mango Markets](https://mango.markets/) support is available at: [Docs](https://docs.mango.markets/) | [Discord](https://discord.gg/67jySBhxrg) | [Twitter](https://twitter.com/mangomarkets) | [Github](https://github.com/blockworks-foundation) | [Email](mailto:hello@blockworks.foundation)"
2021-04-15 02:59:04 -07:00
]
},
{
"cell_type": "markdown",
"id": "persistent-thriller",
2021-04-15 02:59:04 -07:00
"metadata": {},
"source": [
"# 🥭 Mango + Pandas 🐼🐼\n",
2021-04-15 02:59:04 -07:00
"\n",
"This notebook loads margin account data into a Pandas `DataFrame`.\n",
"\n",
"The `DataFrame` is then queried for the total assets and liabilities, the Top 10 margin accounts with the most assets and the most liabilities, and then the Top 10 margin accounts closest to liquidation.\n",
"\n",
"The data remains in the `DataFrame` called `df` so you can easily add your own queries and analyses."
2021-04-14 08:51:39 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "willing-invention",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
2021-04-14 08:51:39 -07:00
"outputs": [],
"source": [
"import logging\n",
"import mango\n",
2021-04-14 08:51:39 -07:00
"import pandas as pd\n",
"import time\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "weird-healthcare",
"metadata": {},
"outputs": [],
"source": [
2021-04-14 08:51:39 -07:00
"start_time = time.time()\n",
"context = mango.default_context\n",
2021-04-14 08:51:39 -07:00
"\n",
"print(\"Loading group...\")\n",
"group = mango.Group.load(context)\n",
2021-04-14 08:51:39 -07:00
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading prices...\")\n",
"prices = group.fetch_token_prices(context)\n",
2021-04-14 08:51:39 -07:00
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading margin accounts...\")\n",
"margin_accounts = mango.MarginAccount.load_all_for_group_with_open_orders(context, context.program_id, group)\n",
2021-04-14 08:51:39 -07:00
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading pandas dataframe...\")\n",
"data = []\n",
"df_index = []\n",
"sheet_formats = {}\n",
2021-04-14 08:51:39 -07:00
"for index, margin_account in enumerate(margin_accounts):\n",
" balance_sheet = margin_account.get_balance_sheet_totals(group, prices)\n",
2021-04-14 08:51:39 -07:00
" df_index += [str(margin_account.address)]\n",
" row = {\n",
" \"Collateral Ratio\": balance_sheet.collateral_ratio,\n",
" \"Available Collateral\": balance_sheet.assets - balance_sheet.liabilities,\n",
" \"Liabilities\": balance_sheet.liabilities,\n",
" \"Assets\": balance_sheet.assets,\n",
" \"Settled Assets\": balance_sheet.settled_assets,\n",
" \"Unsettled Assets\": balance_sheet.unsettled_assets,\n",
" \"Owner\": margin_account.owner\n",
" }\n",
" intrinsic_balance_sheets = margin_account.get_intrinsic_balance_sheets(group)\n",
" priced_balance_sheets = margin_account.get_priced_balance_sheets(group, prices)\n",
" for index, sheet in enumerate(intrinsic_balance_sheets):\n",
" if sheet is None:\n",
" continue\n",
" row[f\"{sheet.token.name} Liabilities (Intrinsic)\"] = sheet.liabilities\n",
" row[f\"{sheet.token.name} Assets (Intrinsic)\"] = sheet.assets\n",
" sheet_formats[f\"{sheet.token.name} Liabilities (Intrinsic)\"] = \"{:,.8f}\"\n",
" sheet_formats[f\"{sheet.token.name} Assets (Intrinsic)\"] = \"{:,.8f}\"\n",
" priced_sheet = priced_balance_sheets[index]\n",
" row[f\"{priced_sheet.token.name} Liabilities (Priced)\"] = priced_sheet.liabilities\n",
" row[f\"{priced_sheet.token.name} Assets (Priced)\"] = priced_sheet.assets\n",
" sheet_formats[f\"{sheet.token.name} Liabilities (Priced)\"] = \"${:,.2f}\"\n",
" sheet_formats[f\"{sheet.token.name} Assets (Priced)\"] = \"${:,.2f}\"\n",
" data += [row]\n",
"\n",
"\n",
2021-04-14 08:51:39 -07:00
"df = pd.DataFrame(data, index=df_index)\n",
"\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"\n",
2021-04-14 08:51:39 -07:00
"def render_styled(df: pd.DataFrame):\n",
" all_formats = {\n",
" \"Collateral Ratio\": \"{:,.2%}\",\n",
" \"Available Collateral\": \"${:,.2f}\",\n",
" \"Liabilities\": \"${:,.2f}\",\n",
" \"Assets\": \"${:,.2f}\",\n",
" \"Settled Assets\": \"${:,.2f}\",\n",
" \"Unsettled Assets\": \"${:,.2f}\"\n",
" }\n",
" all_formats.update(sheet_formats)\n",
" return df.style.format(all_formats)\n"
2021-04-14 08:51:39 -07:00
]
},
{
"cell_type": "markdown",
"id": "interracial-interpretation",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# 🔥 Total Assets + Liabilities"
2021-04-14 08:51:39 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "filled-south",
2021-04-14 08:51:39 -07:00
"metadata": {},
"outputs": [],
"source": [
"print(f\"\"\"\n",
"Total Assets: ${df['Assets'].sum():>15,.2f}\n",
"Total Liabilities: ${df['Liabilities'].sum():>15,.2f}\n",
"Empty Accounts: {len(df[df[\"Collateral Ratio\"] == 0]):>15,}\n",
"Liquidatable: {len(df[(df[\"Collateral Ratio\"] != 0) & (df[\"Collateral Ratio\"] <= 1.1)]):>15,}\n",
"🥭 Ripe Mangoes: {len(df[(df[\"Collateral Ratio\"] > 1.1) & (df[\"Collateral Ratio\"] < 1.2)]):>15,}\n",
2021-04-14 08:51:39 -07:00
"\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "rapid-consumption",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# 🔝 Top 10 Greatest Assets\n",
"\n",
"The top 10 margin accounts with most assets"
2021-04-14 08:51:39 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "talented-bargain",
2021-04-14 08:51:39 -07:00
"metadata": {},
"outputs": [],
"source": [
"render_styled(df.sort_values(\"Assets\", ascending=False).head(10))"
]
},
{
"cell_type": "markdown",
"id": "willing-shoot",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# 🔝 Top 10 Greatest Liabilities\n",
"\n",
"The top 10 margin accounts with most liabilities"
2021-04-14 08:51:39 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "naked-mailing",
2021-04-14 08:51:39 -07:00
"metadata": {},
"outputs": [],
"source": [
"render_styled(df.sort_values(\"Liabilities\", ascending=False).head(10))"
]
},
{
"cell_type": "markdown",
"id": "greater-exception",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# 🔝 Top 10 Least Collateralised\n",
"\n",
"The top 10 least collateralised margin accounts\n",
2021-04-14 08:51:39 -07:00
"\n",
"Collect all margin accounts that have a non-zero Collateral Ratio (so have some liabilities). Then sort them from least-collateralised to most-collateralised."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fatal-shareware",
"metadata": {},
"outputs": [],
"source": [
"nonzero = df[df[\"Collateral Ratio\"] != 0]\n",
"render_styled(nonzero.sort_values(\"Collateral Ratio\", ascending=True).head(10))"
]
},
{
"cell_type": "markdown",
"id": "strategic-blade",
"metadata": {},
"source": [
"# 💧 Liquidatable\n",
"\n",
"An account is 'liquidatable' when its available collateral falls below the group's maintenance collataeral threshold.\n",
"\n",
"This code shows all liquidatable margin accounts, sorted by the available collateral (_not_ the collateral ratio)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "permanent-disease",
2021-04-14 08:51:39 -07:00
"metadata": {},
"outputs": [],
"source": [
"simplified = nonzero.drop([\"Settled Assets\", \"Unsettled Assets\"], axis=1)\n",
"liquidatable = simplified[simplified[\"Collateral Ratio\"] < group.maint_coll_ratio].copy()\n",
"\n",
"print(f\"There are {len(liquidatable)} liquidatable accounts.\")\n",
"render_styled(liquidatable.sort_values(\"Available Collateral\", ascending=False).head(len(liquidatable)))"
]
},
{
"cell_type": "markdown",
"id": "korean-directive",
"metadata": {},
"source": [
"# 🥭 Ripe Mangoes\n",
"\n",
"'Ripe mangoes' are margin accounts that are below the group's initial margin requirements but have not yet fallen below the liquidation threshold.\n",
"\n",
"This code shows all ripe mangoes, sorted by the available collateral (_not_ the collateral ratio)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "handy-waste",
"metadata": {},
"outputs": [],
"source": [
"ripe = simplified[simplified[\"Collateral Ratio\"] < group.init_coll_ratio]\n",
"only_ripe = ripe[ripe[\"Collateral Ratio\"] >= group.maint_coll_ratio].copy()\n",
"\n",
"print(f\"There are {len(only_ripe)} 🥭 ripe mangoes.\")\n",
"render_styled(only_ripe.sort_values(\"Available Collateral\", ascending=False).head(len(only_ripe)))"
2021-04-14 08:51:39 -07:00
]
}
],
"metadata": {
"kernelspec": {
"name": "python39464bit39b046140da4445497cec6b3acc85e88",
"display_name": "Python 3.9.4 64-bit"
2021-04-14 08:51:39 -07:00
},
"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.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
},
"metadata": {
"interpreter": {
"hash": "ac2eaa0ea0ebeafcc7822e65e46aa9d4f966f30b695406963e145ea4a91cd4fc"
}
2021-04-14 08:51:39 -07:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}