Created
February 4, 2022 12:59
-
-
Save habakan/943ec48cf4e6b27043e5f4c4562a7e2e to your computer and use it in GitHub Desktop.
Partial Staking.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Partial Staking.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyOIyNuuX0jMl5JlQOSgvV0N", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/habakan/943ec48cf4e6b27043e5f4c4562a7e2e/partial-staking.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Partial Staking Script" | |
], | |
"metadata": { | |
"id": "ARmskyF2yqDy" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "llN3c8HbrCTg" | |
}, | |
"outputs": [], | |
"source": [ | |
"!pip install numerapi" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import numerapi\n", | |
"from tqdm.auto import tqdm\n", | |
"\n", | |
"\n", | |
"public_id = \"NUMERAI_PUBLIC_ID\"\n", | |
"secret_key = \"NUMERAI_SECRET_KEY\"\n", | |
"napi = numerapi.NumerAPI(public_id=public_id, secret_key=secret_key)\n", | |
"signals_napi = numerapi.SignalsAPI(public_id=public_id, secret_key=secret_key)\n", | |
"stake_weight = 0.5" | |
], | |
"metadata": { | |
"id": "xGDhuVsDrLKy" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"class Perfetcher:\n", | |
" def __init__(self, napi, signals=False):\n", | |
" self.napi = napi\n", | |
" self.signals = signals\n", | |
" self.round_resolved_name = 'roundResolved'\n", | |
" if signals:\n", | |
" self.fetch = self.fetch_signals_round_performances\n", | |
" self.payout_name = 'payout'\n", | |
" else:\n", | |
" self.fetch = self.fetch_round_performances\n", | |
" self.payout_name = 'payoutSettled'\n", | |
"\n", | |
" def fetch_models(self):\n", | |
" self.models = self.napi.get_models()\n", | |
"\n", | |
" def fetch_round_performances(self, username):\n", | |
" query = \"\"\"\n", | |
" query($username: String!) {\n", | |
" v2UserProfile(username: $username) {\n", | |
" latestRoundPerformances {\n", | |
" changeRequestActualAmount\n", | |
" changeRequestAmount\n", | |
" changeRequestType\n", | |
" correlation\n", | |
" correlationWithMetamodel\n", | |
" date\n", | |
" leaderboardBonus\n", | |
" mmc\n", | |
" payoutPending\n", | |
" payoutPendingDelta\n", | |
" payoutSettled\n", | |
" roundNumber\n", | |
" roundOpenTime\n", | |
" roundResolveTime\n", | |
" roundResolved\n", | |
" selectedStakeValue\n", | |
" tournamentName\n", | |
" weekPayoutSelection\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
" \"\"\"\n", | |
" arguments = {'username': username}\n", | |
" data = self.napi.raw_query(query, arguments)['data']['v2UserProfile']['latestRoundPerformances']\n", | |
" return data\n", | |
"\n", | |
" def fetch_signals_round_performances(self, username):\n", | |
" query = \"\"\"\n", | |
" query($modelName: String!) {\n", | |
" v2SignalsProfile(modelName: $modelName) {\n", | |
" roundModelPerformances {\n", | |
" payout\n", | |
" roundNumber\n", | |
" roundOpenTime\n", | |
" roundResolveTime\n", | |
" roundResolved\n", | |
" selectedStakeValue\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
" \"\"\"\n", | |
" arguments = {'modelName': username}\n", | |
" data = self.napi.raw_query(query, arguments)['data']['v2SignalsProfile']['roundModelPerformances']\n", | |
" return data\n", | |
"\n", | |
" def get_latest_resolved_round(self, pfs):\n", | |
" if not self.signals:\n", | |
" pfs = pfs[::-1]\n", | |
" for round_pf in pfs:\n", | |
" if round_pf[self.round_resolved_name]:\n", | |
" return round_pf\n", | |
"\n", | |
" def get_payout(self, round_pf):\n", | |
" return float(round_pf[self.payout_name]) if round_pf[self.payout_name] else 0\n", | |
" \n", | |
" def get_latest_payout(self, pfs):\n", | |
" return self.get_payout(self.get_latest_resolved_round(pfs))\n", | |
"\n", | |
" def fetch_all_performances(self):\n", | |
" self.fetch_models()\n", | |
" pfs = {}\n", | |
" for model_name in tqdm(self.models.keys()):\n", | |
" try:\n", | |
" pfs[model_name] = self.fetch(model_name)\n", | |
" except Exception as e:\n", | |
" print(f'error models is {model_name}')\n", | |
" print(e)\n", | |
" return pfs\n", | |
"\n", | |
" def fetch_latest_payouts(self):\n", | |
" self.fetch_models()\n", | |
" payouts = {}\n", | |
" for model_name in tqdm(self.models.keys()):\n", | |
" try:\n", | |
" payouts[model_name] = self.get_latest_payout(self.fetch(model_name))\n", | |
" except Exception as e:\n", | |
" print(f'error models is {model_name}')\n", | |
" print(e)\n", | |
" return payouts" | |
], | |
"metadata": { | |
"id": "USdfnMj-ruin" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def calc_weighted_stake(payouts, stake_weight):\n", | |
" return {key: value * stake_weight for (key, value) in payouts.items()}" | |
], | |
"metadata": { | |
"id": "agry7GJHWj7X" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Classic Staking" | |
], | |
"metadata": { | |
"id": "aDycHTCxyl-a" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"classic_fetcher = Perfetcher(napi)\n", | |
"classic_latest_payouts = classic_fetcher.fetch_latest_payouts()\n", | |
"print(classic_latest_payouts)" | |
], | |
"metadata": { | |
"id": "gRnAwa4rvKaC" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"classic_stake = calc_weighted_stake(classic_latest_payouts, stake_weight)\n", | |
"print(classic_stake)" | |
], | |
"metadata": { | |
"id": "i-YNkhicyX7B" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Caution: This staking script can not interrupt.\n", | |
"\n", | |
"for model_name, stake in tqdm(classic_stake.items()):\n", | |
" if stake <= 0:\n", | |
" continue\n", | |
" try:\n", | |
" napi.stake_increase(stake, classic_fetcher.models[model_name])\n", | |
" except Exception as e:\n", | |
" print(f'error models is {model_name}')\n", | |
" print(e)" | |
], | |
"metadata": { | |
"id": "2sMq5kNvC0Yt" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Signals Staking" | |
], | |
"metadata": { | |
"id": "ApzJ2zTJyj0U" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"signals_fetcher = Perfetcher(signals_napi, True)\n", | |
"signals_latest_payouts = signals_fetcher.fetch_latest_payouts()\n", | |
"print(signals_latest_payouts)" | |
], | |
"metadata": { | |
"id": "5a34MNxhuaEg" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"signals_stake = calc_weighted_stake(signals_latest_payouts, stake_weight)\n", | |
"print(signals_stake)" | |
], | |
"metadata": { | |
"id": "TZokqBAXCbxH" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def signals_stake_increase(nmr, model_id, signals_napi):\n", | |
" query = '''\n", | |
" mutation($value: String!\n", | |
" $type: String!\n", | |
" $tournamentNumber: Int!\n", | |
" $modelId: String) {\n", | |
" v2ChangeStake(value: $value\n", | |
" type: $type\n", | |
" modelId: $modelId\n", | |
" tournamentNumber: $tournamentNumber) {\n", | |
" dueDate\n", | |
" requestedAmount\n", | |
" status\n", | |
" type\n", | |
" }\n", | |
" }\n", | |
" '''\n", | |
"\n", | |
" arguments = {\n", | |
" 'value': str(nmr),\n", | |
" 'type': 'increase',\n", | |
" 'modelId': model_id,\n", | |
" 'tournamentNumber': 11,\n", | |
" }\n", | |
" result = signals_napi.raw_query(query, arguments, authorization=True)" | |
], | |
"metadata": { | |
"id": "h0VAtNO77rdf" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Caution: This staking script can not interrupt.\n", | |
"\n", | |
"for model_name, stake in tqdm(signals_stake.items()):\n", | |
" if stake <= 0:\n", | |
" continue\n", | |
" try:\n", | |
" signals_stake_increase(stake, signals_fetcher.models[model_name], signals_napi)\n", | |
" except Exception as e:\n", | |
" print(f'error models is {model_name}')\n", | |
" print(e)" | |
], | |
"metadata": { | |
"id": "Rdj0pv42Fh-B" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "VYUywSle7GrM" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment