def test_stockbets_ranking(self):
        update_ratings(self.game_id)
        with self.engine.connect() as conn:
            df = pd.read_sql("SELECT * FROM stockbets_rating", conn)
        pd.testing.assert_frame_equal(df, pd.DataFrame(self.RECORDS))

        # make the public rankings JSON
        serialize_and_pack_rankings()

        session_token = create_jwt("*****@*****.**", 1, "aaron", mins_per_session=1_000_000)
        res = self.requests_session.post(f"{HOST_URL}/public_leaderboard", cookies={"session_token": session_token},
                                         verify=False)
        self.assertEqual(res.json(), self.LEADERBOARD)
        res = self.requests_session.post(f"{HOST_URL}/home", cookies={"session_token": session_token}, verify=False)
        test_user_rank = float(rds.get(f"{PLAYER_RANK_PREFIX}_1"))
        test_user_return = float(rds.get(f"{THREE_MONTH_RETURN_PREFIX}_1"))
        self.assertEqual(res.json()["rating"], test_user_rank)
        self.assertEqual(test_user_rank, 1144.0)
        self.assertEqual(res.json()["three_month_return"], test_user_return)
        self.assertEqual(test_user_return, 0.0762641150000003)
Exemplo n.º 2
0
def compile_and_pack_player_leaderboard(game_id: int, start_time: float = None, end_time: float = None):
    user_ids = get_active_game_user_ids(game_id)
    usernames = get_game_users(game_id)
    user_colors = assign_colors(usernames)
    records = []
    for user_id in user_ids:
        user_info = get_user_information(user_id)  # this is where username and profile pic get added in
        cash_balance = get_current_game_cash_balance(user_id, game_id)
        balances = get_active_balances(game_id, user_id)
        stocks_held = list(balances["symbol"].unique())
        portfolio_value = get_user_portfolio_value(game_id, user_id)
        stat_info = make_stat_entry(color=user_colors[user_info["username"]],
                                    cash_balance=cash_balance,
                                    portfolio_value=portfolio_value,
                                    stocks_held=stocks_held,
                                    return_ratio=rds.get(f"{RETURN_RATIO_PREFIX}_{game_id}_{user_id}"),
                                    sharpe_ratio=rds.get(f"{SHARPE_RATIO_PREFIX}_{game_id}_{user_id}"))
        records.append({**user_info, **stat_info})

    if check_single_player_mode(game_id):
        for index in TRACKED_INDEXES:
            index_info = query_to_dict("""
                SELECT name as username, avatar AS profile_pic 
                FROM index_metadata WHERE symbol = %s""", index)[0]
            portfolio_value = get_index_portfolio_value(game_id, index, start_time, end_time)
            stat_info = make_stat_entry(color=user_colors[index_info["username"]],
                                        cash_balance=None,
                                        portfolio_value=portfolio_value,
                                        stocks_held=[],
                                        return_ratio=rds.get(f"{RETURN_RATIO_PREFIX}_{game_id}_{index}"),
                                        sharpe_ratio=rds.get(f"{SHARPE_RATIO_PREFIX}_{game_id}_{index}"))
            records.append({**index_info, **stat_info})

    benchmark = get_game_info(game_id)["benchmark"]  # get game benchmark and use it to sort leaderboard
    records = sorted(records, key=lambda x: -x[benchmark])
    output = dict(days_left=_days_left(game_id), records=records)
    s3_cache.set(f"{game_id}/{LEADERBOARD_PREFIX}", json.dumps(output))
Exemplo n.º 3
0
    def test_game_player_stats(self):
        game_id = 3
        calculate_and_pack_game_metrics(game_id)

        sharpe_ratio_3_4 = rds.get("sharpe_ratio_3_4")
        while sharpe_ratio_3_4 is None:
            sharpe_ratio_3_4 = rds.get("sharpe_ratio_3_4")
        sharpe_ratio_3_3 = rds.get("sharpe_ratio_3_3")
        sharpe_ratio_3_1 = rds.get("sharpe_ratio_3_1")
        return_ratio_3_1 = rds.get("return_ratio_3_1")
        return_ratio_3_3 = rds.get("return_ratio_3_3")
        return_ratio_3_4 = rds.get("return_ratio_3_4")
        self.assertIsNotNone(sharpe_ratio_3_3)
        self.assertIsNotNone(sharpe_ratio_3_1)
        self.assertIsNotNone(return_ratio_3_1)
        self.assertIsNotNone(return_ratio_3_3)
        self.assertIsNotNone(return_ratio_3_4)
Exemplo n.º 4
0
def get_user_information(user_id: int):
    sql = "SELECT id, name, email, profile_pic, username, created_at FROM users WHERE id = %s"
    info = query_to_dict(sql, user_id)[0]
    info["rating"] = float(rds.get(f"{PLAYER_RANK_PREFIX}_{user_id}"))
    info["three_month_return"] = float(rds.get(f"{THREE_MONTH_RETURN_PREFIX}_{user_id}"))
    return info
Exemplo n.º 5
0
def get_cache_price(symbol):
    data = rds.get(symbol)
    if data is None:
        return None, None
    return [float(x) for x in data.split("_")]