Created
December 4, 2020 21:45
-
-
Save adamwhitakerwilson/d80783b954e60686d1a0238ef92c9d31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"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.7.4-final" | |
}, | |
"orig_nbformat": 2, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"from collections import defaultdict, deque\n", | |
"import matplotlib.pyplot as plt\n", | |
"import pandas as pd\n", | |
"import json\n", | |
"import numpy as np\n", | |
"\n", | |
"wallet_ens = \"demo.eth\"\n", | |
"token_address = \"0x0D8775F648430679A709E98d2b0Cb6250d2887EF\" #BAT\n", | |
"token_address_2 = \"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599\" #wbtc\n", | |
"API_KEY = \"ckey_d65eb47a46724d75ab16743f3bd\"\n", | |
"QUOTE_CURRENCY = \"USD\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Transfer():\n", | |
" contract_ticker_symbol = None\n", | |
" contract_decimals = None\n", | |
" logo_url = None\n", | |
" transfer_type = None\n", | |
" block_signed_at = None\n", | |
" quote_rate = None\n", | |
" delta = None\n", | |
" delta_quote = None\n", | |
" exchange_rate = None" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_transfers_by_addresses(wallet_address, token_address):\n", | |
"\n", | |
" transfer_list = []\n", | |
"\n", | |
" covalent_transfers_v2 = 'https://api.covalenthq.com/v1/1/address/'+wallet_address+'/transfers_v2/'\n", | |
" \n", | |
" payload = {\n", | |
" \"key\": API_KEY,\n", | |
" \"page-size\":9999, \n", | |
" \"contract-address\":token_address\n", | |
" }\n", | |
" \n", | |
" transfers = requests.get(url=covalent_transfers_v2, params=payload)\n", | |
" transfer_data = transfers.json()\n", | |
" successful_transfers = []\n", | |
" gas_spent = 0\n", | |
" for item in transfer_data[\"data\"][\"items\"]:\n", | |
" if item[\"successful\"]:\n", | |
" gas_spent = item[\"gas_spent\"]\n", | |
" for t in item[\"transfers\"]:\n", | |
" successful_transfers.append(t)\n", | |
"\n", | |
" for transfer in successful_transfers:\n", | |
" t = Transfer()\n", | |
" t.contract_ticker_symbol= transfer[\"contract_ticker_symbol\"]\n", | |
" t.contract_decimals = int(transfer[\"contract_decimals\"])\n", | |
" t.logo_url = transfer[\"logo_url\"]\n", | |
" t.transfer_type = transfer[\"transfer_type\"]\n", | |
" t.block_signed_at = transfer[\"block_signed_at\"]\n", | |
" t.quote_rate = float(transfer[\"quote_rate\"])\n", | |
" t.delta = int(transfer[\"delta\"])\n", | |
" t.delta_quote = float(transfer[\"delta_quote\"])\n", | |
" t.gas_spent = gas_spent\n", | |
" transfer_list.append(t)\n", | |
" \n", | |
" return transfer_list" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_spot_price(quote_currency, symbol):\n", | |
" covalent_spot_price_url = \"https://api.covalenthq.com/v1/prices/\"+quote_currency+\"/\"+symbol+\"/\"\n", | |
"\n", | |
" payload = {\n", | |
" \"key\": API_KEY,\n", | |
" \"page-size\":9999, \n", | |
" }\n", | |
"\n", | |
" spot_price = requests.get(url=covalent_spot_price_url, params=payload)\n", | |
" spot_price_data = spot_price.json()\n", | |
"\n", | |
" return spot_price_data[\"data\"][\"prices\"][0][1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_exchange_rates(symbol, quote_currency, to_, from_):\n", | |
" covalent_historical_prices_url = \"https://api.covalenthq.com/v1/pricing/historical/\"+quote_currency+\"/\"+symbol+\"/?to=\"+to_+\"&from=\"+from_+\"&key=\"+API_KEY\n", | |
"\n", | |
" historical_prices = requests.get(url=covalent_historical_prices_url)\n", | |
" historical_pricing_data = historical_prices.json()\n", | |
" return historical_pricing_data[\"data\"][\"prices\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def calculate_roi(wallet_address, token_address):\n", | |
"\n", | |
" transfers = get_transfers_by_addresses(wallet_address, token_address)\n", | |
" transfers.sort(key=lambda x: x.block_signed_at, reverse=False)\n", | |
"\n", | |
" exchange_rates = get_exchange_rates(transfers[0].contract_ticker_symbol, \"USD\" ,transfers[0::][0].block_signed_at[:10], transfers[::-1][0].block_signed_at[:10])\n", | |
" \n", | |
" divisor = int(float(\"1e\"+str(transfers[0].contract_decimals)))\n", | |
" ONE_D = int(float(\"1e18\"))\n", | |
"\n", | |
" exchange_rate_map = dict()\n", | |
"\n", | |
" for e in exchange_rates:{\n", | |
" exchange_rate_map.update({e[\"date\"]:e[\"price\"]})\n", | |
" }\n", | |
" \n", | |
" for t in transfers:\n", | |
" t.exchange_rate = exchange_rate_map[t.block_signed_at[:10]]\n", | |
" t.delta /= divisor\n", | |
"\n", | |
" token_delta = 0\n", | |
" fiat_delta_quote = 0\n", | |
" t = []\n", | |
" s = []\n", | |
" count = 0\n", | |
" inventory = deque()\n", | |
" closed = []\n", | |
"\n", | |
" running_profit_total = 0 \n", | |
"\n", | |
" for transfer in transfers:\n", | |
" print((transfer.transfer_type, transfer.delta_quote))\n", | |
" if len(inventory) == 0:\n", | |
" inventory.append(transfer)\n", | |
" continue\n", | |
"\n", | |
" if transfer.transfer_type == inventory[0].transfer_type:\n", | |
" inventory.append(transfer)\n", | |
" continue\n", | |
" \n", | |
" while len(inventory) > 0 and transfer.delta > 0:\n", | |
" quantity_transfered = min(transfer.delta, inventory[0].delta) \n", | |
"\n", | |
" pnl = quantity_transfered * (transfer.exchange_rate - inventory[0].exchange_rate)\n", | |
" if transfer.transfer_type == \"IN\":\n", | |
" pnl *= -1\n", | |
" \n", | |
" running_profit_total += pnl\n", | |
" \n", | |
" closed.append((transfer.block_signed_at[:10],transfer.transfer_type, transfer.exchange_rate,quantity_transfered, pnl ))\n", | |
"\n", | |
"\n", | |
" transfer.delta -= quantity_transfered\n", | |
" inventory[0].delta -= quantity_transfered\n", | |
"\n", | |
" if inventory[0].delta == 0:\n", | |
" inventory.popleft()\n", | |
" if transfer.delta > 0:\n", | |
" inventory.append(transfer)\n", | |
"\n", | |
" purchasing_cost = 0\n", | |
" price_today = get_spot_price(\"USD\", transfers[0].contract_ticker_symbol)\n", | |
" today = 0\n", | |
"\n", | |
" for i in inventory:\n", | |
" purchasing_cost += ((i.delta) * i.exchange_rate)\n", | |
" today += (i.delta) * float(price_today)\n", | |
"\n", | |
" print(\"--------------------------------\")\n", | |
" print(\"--------------------------------\")\n", | |
" print(\"PC: \"+str(purchasing_cost))\n", | |
" print(\"Today: \"+str(today))\n", | |
" print(\"P/L: \"+str(today - purchasing_cost))\n", | |
" print(\"ROI: \"+str((today - purchasing_cost)/purchasing_cost * 100))\n", | |
" print(\"--------------------------------\")\n", | |
" print(\"--------------------------------\")\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"('IN', 9.471422)\n", | |
"('IN', 1.1251721)\n", | |
"('IN', 19.49626)\n", | |
"--------------------------------\n", | |
"--------------------------------\n", | |
"PC: 30.09285309596737\n", | |
"Today: 33.02701390536193\n", | |
"P/L: 2.9341608093945624\n", | |
"ROI: 9.750357668106114\n", | |
"--------------------------------\n", | |
"--------------------------------\n" | |
] | |
} | |
], | |
"source": [ | |
"calculate_roi(wallet_address, token_address)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"('IN', 99.40732)\n", | |
"('OUT', 88.49539)\n", | |
"('IN', 99.58289)\n", | |
"('IN', 9.020481)\n", | |
"('IN', 49.687737)\n", | |
"('OUT', 28.461788)\n", | |
"--------------------------------\n", | |
"--------------------------------\n", | |
"PC: 142.65441436419002\n", | |
"Today: 296.802515099625\n", | |
"P/L: 154.14810073543498\n", | |
"ROI: 108.057014164246\n", | |
"--------------------------------\n", | |
"--------------------------------\n" | |
] | |
} | |
], | |
"source": [ | |
"calculate_roi(wallet_address, token_address_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment