Skip to content

Instantly share code, notes, and snippets.

@apalepu23
Created October 1, 2020 15:42
Show Gist options
  • Save apalepu23/f50d633a1ffeff5941e42a9cbd7c9193 to your computer and use it in GitHub Desktop.
Save apalepu23/f50d633a1ffeff5941e42a9cbd7c9193 to your computer and use it in GitHub Desktop.
Bankruptcy and Liquidation Price Calculator
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bankruptcy and liquidation price\n",
"### DerivaDEX"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%reload_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# imports"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bankruptcy price"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def compute_bankruptcy_px(collateral, position_side, position_size, avg_entry_px, mark_px):\n",
" unrealized_pnl = position_size * position_side * (mark_px - avg_entry_px)\n",
" total_account_value = collateral + unrealized_pnl\n",
" return mark_px - position_side * (total_account_value / position_size)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Alice"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"collateral = 10_000\n",
"position_side = 1\n",
"position_size = 4\n",
"avg_entry_px = 10_000\n",
"mark_px = 11_000"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7500.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_bankruptcy_px(collateral, position_side, position_size, avg_entry_px, mark_px)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bob"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"collateral = 20_000\n",
"position_side = -1\n",
"position_size = 4\n",
"avg_entry_px = 10_000\n",
"mark_px = 11_000"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15000.0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_bankruptcy_px(collateral, position_side, position_size, avg_entry_px, mark_px)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Liquidation price"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def compute_liquidation_px(collateral, position_side, position_size, avg_entry_px, mmr):\n",
" return (collateral - position_size * position_side * avg_entry_px) / (position_size * (mmr - position_side))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Alice"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"collateral = 10_000\n",
"position_side = 1\n",
"position_size = 4\n",
"avg_entry_px = 10_000\n",
"mmr = 0.03"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7731.958762886598"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_liquidation_px(collateral, position_side, position_size, avg_entry_px, mmr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bob"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"collateral = 20_000\n",
"position_side = -1\n",
"position_size = 4\n",
"avg_entry_px = 10_000\n",
"mark_px = 11_000"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14563.106796116504"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_liquidation_px(collateral, position_side, position_size, avg_entry_px, mmr)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment