mango-explorer/Pandas.ipynb

187 lines
6.1 KiB
Plaintext
Raw Normal View History

2021-04-14 08:51:39 -07:00
{
"cells": [
{
"cell_type": "markdown",
"id": "rapid-robin",
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"
]
},
{
"cell_type": "markdown",
"id": "contrary-residence",
2021-04-15 02:59:04 -07:00
"metadata": {},
"source": [
"# 🥭 Mango + Pandas 🐼🐼 [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gl/OpinionatedGeek%2Fmango-explorer/HEAD?filepath=Pandas.ipynb)\n",
2021-04-15 02:59:04 -07:00
"\n",
"_🏃 To run this notebook press the ⏩ icon in the toolbar above._\n",
"\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": "medium-jackson",
2021-04-14 08:51:39 -07:00
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import time\n",
"\n",
"from Context import default_context\n",
"from Classes import Group, MarginAccount, OpenOrders\n",
"\n",
"start_time = time.time()\n",
"\n",
"print(\"Loading group...\")\n",
"group = Group.load(default_context)\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading prices...\")\n",
"prices = group.get_prices()\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading margin accounts...\")\n",
"margin_accounts = MarginAccount.load_all_for_group(default_context, default_context.program_id, group)\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading open orders accounts...\")\n",
"open_orders = OpenOrders.load_raw_open_orders_accounts(default_context, group)\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Installing open orders accounts...\")\n",
"open_orders_by_address = {key: value for key, value in [(str(address), open_orders_account) for address, open_orders_account in open_orders]}\n",
"for margin_account in margin_accounts:\n",
" margin_account.install_open_orders_accounts(group, open_orders_by_address)\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"print(\"Loading pandas dataframe...\")\n",
"data = []\n",
"df_index = []\n",
"for index, margin_account in enumerate(margin_accounts):\n",
" balance_sheet = margin_account.get_balance_sheet(group, prices)\n",
" df_index += [str(margin_account.address)]\n",
" data += [{\"Owner\": margin_account.owner, \"Liabilities\": balance_sheet.liabilities, \"Assets\": balance_sheet.assets, \"Settled Assets\": balance_sheet.settled_assets, \"Unsettled Assets\": balance_sheet.unsettled_assets, \"Collateral Ratio\": balance_sheet.collateral_ratio}]\n",
"df = pd.DataFrame(data, index=df_index)\n",
"\n",
"print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
"def render_styled(df: pd.DataFrame):\n",
" return df.style.format({\n",
" \"Liabilities\": \"${:,.2f}\",\n",
" \"Assets\": \"${:,.2f}\",\n",
" \"Settled Assets\": \"${:,.2f}\",\n",
" \"Unsettled Assets\": \"${:,.2f}\",\n",
" \"Collateral Ratio\": \"{:,.2%}\"\n",
"}).hide_index()\n"
2021-04-14 08:51:39 -07:00
]
},
{
"cell_type": "markdown",
"id": "changed-parker",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# Total assets and liabilities"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aging-termination",
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",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"id": "cosmetic-express",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# Top 10 margin accounts with most assets"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "joined-sponsorship",
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": "formal-upset",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# Top 10 margin accounts with most liabilities"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bridal-valuation",
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": "headed-orange",
2021-04-14 08:51:39 -07:00
"metadata": {},
"source": [
"# Top 10 least collateralised margin accounts\n",
"\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": "expensive-wilderness",
2021-04-14 08:51:39 -07:00
"metadata": {},
"outputs": [],
"source": [
"render_styled(df[df[\"Collateral Ratio\"] != 0].sort_values(\"Collateral Ratio\", ascending=True).head(10))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}