File size: 14,377 Bytes
ed581c9 e863dee ed581c9 e863dee ed581c9 e863dee ed581c9 7818025 e863dee 7818025 e863dee ed581c9 e863dee ed581c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
import numerapi
from numerapi import utils
from project_tools import project_config, project_utils
from typing import List, Dict
import pandas as pd
import numpy as np
napi = numerapi.NumerAPI()
# def get_round
# depreciated
# def get_model_history(model):
# res = napi.daily_user_performances(model)
# res = pd.DataFrame.from_dict(res)
# res['payoutPending'] = res['payoutPending'].astype(np.float64)
# res['payoutSettled'] = res['payoutSettled'].astype(np.float64)
# res['stakeValue'] = res['stakeValue'].astype(np.float64)
# res['deltaRatio'] = res['payoutPending'] / res['stakeValue']
# res['realised_pl'] = project_utils.series_reverse_cumsum(res['payoutSettled'])
# res['floating_pl'] = project_utils.series_reverse_cumsum(res['payoutPending']) - res['realised_pl']
# res['current_stake'] = res['stakeValue'] - res['floating_pl']
# rename_dict = {'stakeValue':'floating_stake'}
# res = res.rename(columns=rename_dict)
# # res['equity'] = res['stakeValue'] + res['floating_pl']
# # cols = res.columns.tolist()
# # res = res[['model'] + cols]
#
# res['model'] = model
# cols = ['model', 'date', 'current_stake', 'floating_stake', 'payoutPending', 'floating_pl', 'realised_pl']
# res = res[cols]
# return res
def get_portfolio_overview(models, onlylatest=True):
res_df = []
for m in models:
# try:
print(f'extracting information for model {m}')
if onlylatest:
mdf = get_model_history_v3(m).loc[0:0]
else:
mdf = get_model_history_v3(m)
res_df.append(mdf)
# except:
# print(f'no information for model {m} is available')
if len(res_df)>0:
res_df = pd.concat(res_df, axis=0)
# res_df['date'] = res_df['date'].dt.date
if onlylatest:
return res_df.sort_values(by='floating_pl', ascending=False).reset_index(drop=True)
else:
return res_df.reset_index(drop=True)
else:
return None
def get_competitions(tournament=8):
"""Retrieves information about all competitions
Args:
tournament (int, optional): ID of the tournament, defaults to 8
-- DEPRECATED there is only one tournament nowadays
Returns:
list of dicts: list of rounds
Each round's dict contains the following items:
* datasetId (`str`)
* number (`int`)
* openTime (`datetime`)
* resolveTime (`datetime`)
* participants (`int`): number of participants
* prizePoolNmr (`decimal.Decimal`)
* prizePoolUsd (`decimal.Decimal`)
* resolvedGeneral (`bool`)
* resolvedStaking (`bool`)
* ruleset (`string`)
Example:
>>> NumerAPI().get_competitions()
[
{'datasetId': '59a70840ca11173c8b2906ac',
'number': 71,
'openTime': datetime.datetime(2017, 8, 31, 0, 0),
'resolveTime': datetime.datetime(2017, 9, 27, 21, 0),
'participants': 1287,
'prizePoolNmr': Decimal('0.00'),
'prizePoolUsd': Decimal('6000.00'),
'resolvedGeneral': True,
'resolvedStaking': True,
'ruleset': 'p_auction'
},
..
]
"""
# self.logger.info("getting rounds...")
query = '''
query($tournament: Int!) {
rounds(tournament: $tournament) {
number
resolveTime
openTime
resolvedGeneral
resolvedStaking
}
}
'''
arguments = {'tournament': tournament}
result = napi.raw_query(query, arguments)
rounds = result['data']['rounds']
# convert datetime strings to datetime.datetime objects
for r in rounds:
utils.replace(r, "openTime", utils.parse_datetime_string)
utils.replace(r, "resolveTime", utils.parse_datetime_string)
utils.replace(r, "prizePoolNmr", utils.parse_float_string)
utils.replace(r, "prizePoolUsd", utils.parse_float_string)
return rounds
def daily_submissions_performances(username: str) -> List[Dict]:
"""Fetch daily performance of a user's submissions.
Args:
username (str)
Returns:
list of dicts: list of daily submission performance entries
For each entry in the list, there is a dict with the following
content:
* date (`datetime`)
* correlation (`float`)
* roundNumber (`int`)
* mmc (`float`): metamodel contribution
* fnc (`float`): feature neutral correlation
* correlationWithMetamodel (`float`)
Example:
>>> api = NumerAPI()
>>> api.daily_user_performances("uuazed")
[{'roundNumber': 181,
'correlation': -0.011765912,
'date': datetime.datetime(2019, 10, 16, 0, 0),
'mmc': 0.3,
'fnc': 0.1,
'correlationWithMetamodel': 0.87},
...
]
"""
query = """
query($username: String!) {
v2UserProfile(username: $username) {
dailySubmissionPerformances {
date
correlation
corrPercentile
roundNumber
mmc
mmcPercentile
fnc
fncPercentile
correlationWithMetamodel
}
}
}
"""
arguments = {'username': username}
data = napi.raw_query(query, arguments)['data']['v2UserProfile']
performances = data['dailySubmissionPerformances']
# convert strings to python objects
for perf in performances:
utils.replace(perf, "date", utils.parse_datetime_string)
# remove useless items
performances = [p for p in performances
if any([p['correlation'], p['fnc'], p['mmc']])]
return performances
def daily_submissions_performances_V3(modelname: str) -> List[Dict]:
query = """
query($modelName: String!) {
v3UserProfile(modelName: $modelName) {
roundModelPerformances{
roundNumber
roundResolveTime
corr
corrPercentile
mmc
mmcMultiplier
mmcPercentile
tc
tcPercentile
tcMultiplier
fncV3
fncV3Percentile
corrWMetamodel
payout
roundResolved
roundResolveTime
corrMultiplier
mmcMultiplier
selectedStakeValue
}
stakeValue
nmrStaked
}
}
"""
arguments = {'modelName': modelname}
data = napi.raw_query(query, arguments)['data']['v3UserProfile']
performances = data['roundModelPerformances']
# convert strings to python objects
for perf in performances:
utils.replace(perf, "date", utils.parse_datetime_string)
# remove useless items
performances = [p for p in performances
if any([p['corr'], p['tc'], p['mmc']])]
return performances
def get_lb_models(limit=20000, offset=0):
query = """
query($limit: Int, $offset: Int){
v2Leaderboard(limit:$limit, offset:$offset){
username
}
}
"""
arguments = {'limit':limit, 'offset':offset}
data = napi.raw_query(query, arguments)['data']['v2Leaderboard']
model_list = [i['username'] for i in data]
return model_list
def get_round_model_performance(roundNumber: int, model: str):
query = """
query($roundNumber: Int!, $username: String!) {
roundSubmissionPerformance(roundNumber: $roundNumber, username: $username) {
corrMultiplier
mmcMultiplier
roundDailyPerformances{
correlation
mmc
corrPercentile
mmcPercentile
payoutPending
}
selectedStakeValue
}
}
"""
arguments = {'roundNumber': roundNumber,'username': model}
data = napi.raw_query(query, arguments)['data']['roundSubmissionPerformance']
latest_performance = data['roundDailyPerformances'][-1] #[-1] ### issue with order
res = {}
res['model'] = model
res['roundNumber'] = roundNumber
res['corrMultiplier'] = data['corrMultiplier']
res['mmcMultiplier'] = data['mmcMultiplier']
res['selectedStakeValue'] = data['selectedStakeValue']
for key in latest_performance.keys():
res[key] = latest_performance[key]
return res
def get_user_profile(username: str) -> List[Dict]:
"""Fetch daily performance of a user's submissions.
Args:
username (str)
Returns:
list of dicts: list of daily submission performance entries
For each entry in the list, there is a dict with the following
content:
* date (`datetime`)
* correlation (`float`)
* roundNumber (`int`)
* mmc (`float`): metamodel contribution
* fnc (`float`): feature neutral correlation
* correlationWithMetamodel (`float`)
Example:
>>> api = NumerAPI()
>>> api.daily_user_performances("uuazed")
[{'roundNumber': 181,
'correlation': -0.011765912,
'date': datetime.datetime(2019, 10, 16, 0, 0),
'mmc': 0.3,
'fnc': 0.1,
'correlationWithMetamodel': 0.87},
...
]
"""
query = """
query($username: String!) {
v2UserProfile(username: $username) {
dailySubmissionPerformances {
date
correlation
corrPercentile
roundNumber
mmc
mmcPercentile
fnc
fncPercentile
correlationWithMetamodel
}
}
}
"""
arguments = {'username': username}
data = napi.raw_query(query, arguments)['data']#['v2UserProfile']
# performances = data['dailySubmissionPerformances']
# # convert strings to python objects
# for perf in performances:
# utils.replace(perf, "date", utils.parse_datetime_string)
# # remove useless items
# performances = [p for p in performances
# if any([p['correlation'], p['fnc'], p['mmc']])]
return data
def download_dataset(filename: str, dest_path: str = None,
round_num: int = None) -> None:
""" Download specified file for the current active round.
Args:
filename (str): file to be downloaded
dest_path (str, optional): complate path where the file should be
stored, defaults to the same name as the source file
round_num (int, optional): tournament round you are interested in.
defaults to the current round
tournament (int, optional): ID of the tournament, defaults to 8
Example:
>>> filenames = NumerAPI().list_datasets()
>>> NumerAPI().download_dataset(filenames[0]}")
"""
if dest_path is None:
dest_path = filename
query = """
query ($filename: String!
$round: Int) {
dataset(filename: $filename
round: $round)
}
"""
args = {'filename': filename, "round": round_num}
dataset_url = napi.raw_query(query, args)['data']['dataset']
utils.download_file(dataset_url, dest_path, show_progress_bars=True)
# function using V3UserProfile
def model_payout_history(model):
napi = numerapi.NumerAPI()
query = """
query($model: String!) {
v3UserProfile(modelName: $model) {
roundModelPerformances{
payout
roundNumber
roundResolved
roundResolveTime
corrMultiplier
mmcMultiplier
selectedStakeValue
}
stakeValue
nmrStaked
}
}
"""
arguments = {'model': model}
payout_info = napi.raw_query(query, arguments)['data']['v3UserProfile']['roundModelPerformances']
payout_info = pd.DataFrame.from_dict(payout_info)
payout_info = payout_info[~pd.isnull(payout_info['payout'])].reset_index(drop=True)
return payout_info
def get_model_history_v3(model):
res = model_payout_history(model)
res = pd.DataFrame.from_dict(res)
res['payout'] = res['payout'].astype(np.float64)
res['current_stake'] = res['selectedStakeValue'].astype(np.float64)
res['payout_cumsum'] = project_utils.series_reverse_cumsum(res['payout'])
res['date'] = pd.to_datetime(res['roundResolveTime']).dt.date
res['realised_pl'] = res['payout_cumsum']
latest_realised_pl = res[res['roundResolved'] == True]['payout_cumsum'].values[0]
res.loc[res['roundResolved'] == False, 'realised_pl'] = latest_realised_pl
res['floating_pl'] = 0
payoutPending_values = res[res['roundResolved'] == False]['payout'].values
payoutPending_cumsum = payoutPending_values[::-1].cumsum()[::-1]
res.loc[res['roundResolved'] == False, 'floating_pl'] = payoutPending_cumsum
res['model'] = model
# res['floating_pl'] = res['current_stake'] + res['payoutPending']
res['floating_stake'] = res['current_stake'] + res['floating_pl']
cols = ['model', 'date', 'current_stake', 'floating_stake', 'payout', 'floating_pl', 'realised_pl', 'roundResolved',
'roundNumber']
res = res[cols]
return res
|