{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import gc\n", "\n", "sns.set_style(\"darkgrid\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "open_markets = pd.read_parquet(\"../data/markets_live_data.parquet\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 947 entries, 0 to 946\n", "Data columns (total 23 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 creationTimestamp 947 non-null object \n", " 1 id 947 non-null object \n", " 2 liquidityMeasure 947 non-null int64 \n", " 3 liquidityParameter 947 non-null object \n", " 4 openingTimestamp 947 non-null object \n", " 5 outcomeTokenAmounts 947 non-null object \n", " 6 title 947 non-null object \n", " 7 sample_timestamp 947 non-null int64 \n", " 8 open 947 non-null bool \n", " 9 total_trades 947 non-null int64 \n", " 10 dist_gap_perc 947 non-null float64 \n", " 11 first_outcome 947 non-null object \n", " 12 second_outcome 947 non-null object \n", " 13 sample_datetime 947 non-null datetime64[ns]\n", " 14 first_token_perc 947 non-null float64 \n", " 15 second_token_perc 947 non-null float64 \n", " 16 mean_trade_size 947 non-null float64 \n", " 17 total_bet_amount 947 non-null float64 \n", " 18 price_weighted_first_outcome_perc 947 non-null float64 \n", " 19 price_weighted_second_outcome_perc 947 non-null float64 \n", " 20 bought_tokens_first_perc 947 non-null float64 \n", " 21 bought_tokens_second_perc 947 non-null float64 \n", " 22 resolutionTimestamp 0 non-null object \n", "dtypes: bool(1), datetime64[ns](1), float64(9), int64(3), object(9)\n", "memory usage: 163.8+ KB\n" ] } ], "source": [ "open_markets.info()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "open\n", "False 834\n", "True 113\n", "Name: count, dtype: int64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open_markets.open.value_counts()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "all_trades = pd.read_parquet('../data/all_trades_profitability.parquet')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 88355 entries, 0 to 88354\n", "Data columns (total 21 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 trader_address 88355 non-null object \n", " 1 market_creator 88355 non-null object \n", " 2 trade_id 88355 non-null object \n", " 3 creation_timestamp 88355 non-null datetime64[ns, UTC]\n", " 4 title 88355 non-null object \n", " 5 market_status 88355 non-null object \n", " 6 collateral_amount 88355 non-null float64 \n", " 7 outcome_index 88355 non-null object \n", " 8 trade_fee_amount 88355 non-null float64 \n", " 9 outcomes_tokens_traded 88355 non-null float64 \n", " 10 current_answer 88355 non-null int64 \n", " 11 is_invalid 88355 non-null bool \n", " 12 winning_trade 88355 non-null bool \n", " 13 earnings 88355 non-null float64 \n", " 14 redeemed 88355 non-null bool \n", " 15 redeemed_amount 88355 non-null float64 \n", " 16 num_mech_calls 88355 non-null int64 \n", " 17 mech_fee_amount 88355 non-null float64 \n", " 18 net_earnings 88355 non-null float64 \n", " 19 roi 88355 non-null float64 \n", " 20 staking 88355 non-null object \n", "dtypes: bool(3), datetime64[ns, UTC](1), float64(8), int64(2), object(7)\n", "memory usage: 12.4+ MB\n" ] } ], "source": [ "all_trades.info()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "market_status\n", "CLOSED 8210\n", "Name: count, dtype: int64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_trades.market_status.value_counts()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "all_trades[\"creation_date\"] = all_trades[\"creation_timestamp\"].dt.date" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Metrics we can compute at the trader agent level (for closed markets)\n", "\n", "\n", "* ROI per market and per day (sorted by creation date)\n", "* number of trades per market and per day\n", "* net earnings\n", "* earnings\n", "* bet amount\n", "* nr mech calls\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
trader_addresstitlenr_trades_per_market
00x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Apple implement significant changes in th...3
10x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Apple launch the iPhone 16, Watch, and Ai...2
20x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Bayer Leverkusen retain the Bundesliga ti...1
30x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Chick-fil-A successfully launch a streami...2
40x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Donald Trump's new cryptocurrency platfor...1
\n", "
" ], "text/plain": [ " trader_address \\\n", "0 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "1 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "2 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "3 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "4 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "\n", " title nr_trades_per_market \n", "0 Will Apple implement significant changes in th... 3 \n", "1 Will Apple launch the iPhone 16, Watch, and Ai... 2 \n", "2 Will Bayer Leverkusen retain the Bundesliga ti... 1 \n", "3 Will Chick-fil-A successfully launch a streami... 2 \n", "4 Will Donald Trump's new cryptocurrency platfor... 1 " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume_trades_per_trader_and_market = all_trades.groupby([\"trader_address\", \"title\"])[\"roi\"].count().reset_index()\n", "volume_trades_per_trader_and_market.rename(columns={\"roi\":\"nr_trades_per_market\"}, inplace=True)\n", "volume_trades_per_trader_and_market.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Adding multibet category" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "volume_trades_per_trader_and_market[\"multibet\"] = volume_trades_per_trader_and_market.apply(lambda x: True if x.nr_trades_per_market > 1 else False, axis=1)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
trader_addresstitlenr_trades_per_marketmultibet
00x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Apple implement significant changes in th...3True
10x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Apple launch the iPhone 16, Watch, and Ai...2True
20x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Bayer Leverkusen retain the Bundesliga ti...1False
30x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Chick-fil-A successfully launch a streami...2True
40x022b36c50b85b8ae7addfb8a35d76c59d5814834Will Donald Trump's new cryptocurrency platfor...1False
\n", "
" ], "text/plain": [ " trader_address \\\n", "0 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "1 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "2 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "3 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "4 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 \n", "\n", " title nr_trades_per_market \\\n", "0 Will Apple implement significant changes in th... 3 \n", "1 Will Apple launch the iPhone 16, Watch, and Ai... 2 \n", "2 Will Bayer Leverkusen retain the Bundesliga ti... 1 \n", "3 Will Chick-fil-A successfully launch a streami... 2 \n", "4 Will Donald Trump's new cryptocurrency platfor... 1 \n", "\n", " multibet \n", "0 True \n", "1 True \n", "2 False \n", "3 True \n", "4 False " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume_trades_per_trader_and_market.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Global dataset" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "trader_agent_metrics = pd.merge(all_trades, volume_trades_per_trader_and_market, on=['trader_address', 'title'])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
trader_addressmarket_creatortrade_idcreation_timestamptitlemarket_statuscollateral_amountoutcome_indextrade_fee_amountoutcomes_tokens_traded...earningsredeemedredeemed_amountnum_mech_callsmech_fee_amountnet_earningsroicreation_datenr_trades_per_marketmultibet
00x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x0017cd58d6a7ee1451388c7d5b1051b4c0a041f50x02...2024-08-25 02:37:35+00:00Will the first floating offshore wind research...CLOSED0.45042610.0090090.729589...0.729589False0.020.020.2501540.5217692024-08-251False
10x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x012bf74d4c7060799f590c7c08accc0e9938e6c40x02...2024-08-30 03:24:45+00:00Will SpaceX's Polaris Dawn mission launch on 3...CLOSED0.41966200.0083930.610289...0.610289False0.010.010.1722340.3931792024-08-301False
20x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x02e93f85cbc48b380c725d58e85e083c112bd0180x02...2024-08-26 02:48:20+00:00Will Apple implement significant changes in th...CLOSED0.64173210.0128351.572272...0.000000False0.040.04-0.694567-1.0000002024-08-263True
30x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x02e93f85cbc48b380c725d58e85e083c112bd0180x02...2024-08-27 00:23:25+00:00Will Apple implement significant changes in th...CLOSED0.48250610.0096501.013458...0.000000False0.040.04-0.532156-1.0000002024-08-273True
40x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x02e93f85cbc48b380c725d58e85e083c112bd0180x02...2024-08-28 01:41:30+00:00Will Apple implement significant changes in th...CLOSED0.56793010.0113591.285445...0.000000False0.040.04-0.619289-1.0000002024-08-283True
\n", "

5 rows × 23 columns

\n", "
" ], "text/plain": [ " trader_address market_creator \\\n", "0 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "1 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "2 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "3 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "4 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "\n", " trade_id \\\n", "0 0x0017cd58d6a7ee1451388c7d5b1051b4c0a041f50x02... \n", "1 0x012bf74d4c7060799f590c7c08accc0e9938e6c40x02... \n", "2 0x02e93f85cbc48b380c725d58e85e083c112bd0180x02... \n", "3 0x02e93f85cbc48b380c725d58e85e083c112bd0180x02... \n", "4 0x02e93f85cbc48b380c725d58e85e083c112bd0180x02... \n", "\n", " creation_timestamp \\\n", "0 2024-08-25 02:37:35+00:00 \n", "1 2024-08-30 03:24:45+00:00 \n", "2 2024-08-26 02:48:20+00:00 \n", "3 2024-08-27 00:23:25+00:00 \n", "4 2024-08-28 01:41:30+00:00 \n", "\n", " title market_status \\\n", "0 Will the first floating offshore wind research... CLOSED \n", "1 Will SpaceX's Polaris Dawn mission launch on 3... CLOSED \n", "2 Will Apple implement significant changes in th... CLOSED \n", "3 Will Apple implement significant changes in th... CLOSED \n", "4 Will Apple implement significant changes in th... CLOSED \n", "\n", " collateral_amount outcome_index trade_fee_amount outcomes_tokens_traded \\\n", "0 0.450426 1 0.009009 0.729589 \n", "1 0.419662 0 0.008393 0.610289 \n", "2 0.641732 1 0.012835 1.572272 \n", "3 0.482506 1 0.009650 1.013458 \n", "4 0.567930 1 0.011359 1.285445 \n", "\n", " ... earnings redeemed redeemed_amount num_mech_calls mech_fee_amount \\\n", "0 ... 0.729589 False 0.0 2 0.02 \n", "1 ... 0.610289 False 0.0 1 0.01 \n", "2 ... 0.000000 False 0.0 4 0.04 \n", "3 ... 0.000000 False 0.0 4 0.04 \n", "4 ... 0.000000 False 0.0 4 0.04 \n", "\n", " net_earnings roi creation_date nr_trades_per_market multibet \n", "0 0.250154 0.521769 2024-08-25 1 False \n", "1 0.172234 0.393179 2024-08-30 1 False \n", "2 -0.694567 -1.000000 2024-08-26 3 True \n", "3 -0.532156 -1.000000 2024-08-27 3 True \n", "4 -0.619289 -1.000000 2024-08-28 3 True \n", "\n", "[5 rows x 23 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trader_agent_metrics.head()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "trader_agent_metrics.sort_values(by=[\"trader_address\", \"title\", \"creation_timestamp\"],inplace=True)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
trader_addressmarket_creatortrade_idcreation_timestamptitlemarket_statuscollateral_amountoutcome_indextrade_fee_amountoutcomes_tokens_traded...earningsredeemedredeemed_amountnum_mech_callsmech_fee_amountnet_earningsroicreation_datenr_trades_per_marketmultibet
20x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x02e93f85cbc48b380c725d58e85e083c112bd0180x02...2024-08-26 02:48:20+00:00Will Apple implement significant changes in th...CLOSED0.64173210.0128351.572272...0.000000False0.040.04-0.694567-1.0000002024-08-263True
30x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x02e93f85cbc48b380c725d58e85e083c112bd0180x02...2024-08-27 00:23:25+00:00Will Apple implement significant changes in th...CLOSED0.48250610.0096501.013458...0.000000False0.040.04-0.532156-1.0000002024-08-273True
40x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x02e93f85cbc48b380c725d58e85e083c112bd0180x02...2024-08-28 01:41:30+00:00Will Apple implement significant changes in th...CLOSED0.56793010.0113591.285445...0.000000False0.040.04-0.619289-1.0000002024-08-283True
410x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x493b27d17cd2672631b30f32115f52eb2ec101850x02...2024-08-29 00:40:05+00:00Will Apple launch the iPhone 16, Watch, and Ai...CLOSED1.79872110.0359742.906159...2.906159False0.030.031.0414630.5585162024-08-292True
420x022b36c50b85b8ae7addfb8a35d76c59d5814834quickstart0x493b27d17cd2672631b30f32115f52eb2ec101850x02...2024-08-30 03:04:05+00:00Will Apple launch the iPhone 16, Watch, and Ai...CLOSED1.29438210.0258881.952878...1.952878False0.030.030.6026080.4462872024-08-302True
\n", "

5 rows × 23 columns

\n", "
" ], "text/plain": [ " trader_address market_creator \\\n", "2 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "3 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "4 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "41 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "42 0x022b36c50b85b8ae7addfb8a35d76c59d5814834 quickstart \n", "\n", " trade_id \\\n", "2 0x02e93f85cbc48b380c725d58e85e083c112bd0180x02... \n", "3 0x02e93f85cbc48b380c725d58e85e083c112bd0180x02... \n", "4 0x02e93f85cbc48b380c725d58e85e083c112bd0180x02... \n", "41 0x493b27d17cd2672631b30f32115f52eb2ec101850x02... \n", "42 0x493b27d17cd2672631b30f32115f52eb2ec101850x02... \n", "\n", " creation_timestamp \\\n", "2 2024-08-26 02:48:20+00:00 \n", "3 2024-08-27 00:23:25+00:00 \n", "4 2024-08-28 01:41:30+00:00 \n", "41 2024-08-29 00:40:05+00:00 \n", "42 2024-08-30 03:04:05+00:00 \n", "\n", " title market_status \\\n", "2 Will Apple implement significant changes in th... CLOSED \n", "3 Will Apple implement significant changes in th... CLOSED \n", "4 Will Apple implement significant changes in th... CLOSED \n", "41 Will Apple launch the iPhone 16, Watch, and Ai... CLOSED \n", "42 Will Apple launch the iPhone 16, Watch, and Ai... CLOSED \n", "\n", " collateral_amount outcome_index trade_fee_amount outcomes_tokens_traded \\\n", "2 0.641732 1 0.012835 1.572272 \n", "3 0.482506 1 0.009650 1.013458 \n", "4 0.567930 1 0.011359 1.285445 \n", "41 1.798721 1 0.035974 2.906159 \n", "42 1.294382 1 0.025888 1.952878 \n", "\n", " ... earnings redeemed redeemed_amount num_mech_calls mech_fee_amount \\\n", "2 ... 0.000000 False 0.0 4 0.04 \n", "3 ... 0.000000 False 0.0 4 0.04 \n", "4 ... 0.000000 False 0.0 4 0.04 \n", "41 ... 2.906159 False 0.0 3 0.03 \n", "42 ... 1.952878 False 0.0 3 0.03 \n", "\n", " net_earnings roi creation_date nr_trades_per_market multibet \n", "2 -0.694567 -1.000000 2024-08-26 3 True \n", "3 -0.532156 -1.000000 2024-08-27 3 True \n", "4 -0.619289 -1.000000 2024-08-28 3 True \n", "41 1.041463 0.558516 2024-08-29 2 True \n", "42 0.602608 0.446287 2024-08-30 2 True \n", "\n", "[5 rows x 23 columns]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trader_agent_metrics.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding weekly time window\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/gp/02mb1d514ng739czlxw1lhh00000gn/T/ipykernel_79146/2162443128.py:4: UserWarning: Converting to PeriodArray/Index representation will drop timezone information.\n", " trader_agent_metrics[\"creation_timestamp\"].dt.to_period(\"W\").dt.strftime(\"%b-%d\")\n" ] } ], "source": [ "trader_agent_metrics = trader_agent_metrics.sort_values(by=\"creation_timestamp\", ascending=True)\n", "\n", "trader_agent_metrics[\"month_year_week\"] = (\n", " trader_agent_metrics[\"creation_timestamp\"].dt.to_period(\"W\").dt.strftime(\"%b-%d\")\n", ")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
trader_addressmarket_creatortrade_idcreation_timestamptitlemarket_statuscollateral_amountoutcome_indextrade_fee_amountoutcomes_tokens_traded...redeemedredeemed_amountnum_mech_callsmech_fee_amountnet_earningsroicreation_datenr_trades_per_marketmultibetmonth_year_week
22090x95ecc70d9f4feb162ed9f41c4432d990c36c8f57quickstart0x33ec6978224941c04b51704cbc55943898c13a1b0x95...2024-07-20 02:10:10+00:00Will iOS 18's significant upgrade for the iPho...CLOSED0.16000010.0032000.310165...False0.00000030.03-0.193200-1.0000002024-07-202TrueJul-21
65880xf089874165be0377680683fd5187a058dea82683quickstart0x5658c1834d053e92143956069926ccef1cb3c92b0xf0...2024-07-20 05:18:00+00:00Will Argentina win the Copa America 2024?CLOSED1.00000000.0200001.839649...True1.83964920.020.7996490.7688932024-07-202TrueJul-21
13160x49f4e3d8edc85efda9b0a36d96e406a59b13fcc2quickstart0x33ec6978224941c04b51704cbc55943898c13a1b0x49...2024-07-20 05:28:50+00:00Will iOS 18's significant upgrade for the iPho...CLOSED1.07085510.0214171.925006...False0.00000030.03-1.122272-1.0000002024-07-202TrueJul-21
77760xe283e408c6017447da9fe092d52c386753699680pearl0xb13be57227a3e806b36c72ab1cd5792df2e5e8070xe2...2024-07-20 05:51:20+00:00Will a scientific study be published on 23 Jul...CLOSED1.00000010.0200001.839649...False0.00000020.02-1.040000-1.0000002024-07-201FalseJul-21
63440x480e5b5abd27cd754745871116e79caf90468dd4quickstart0x5658c1834d053e92143956069926ccef1cb3c92b0x48...2024-07-20 06:32:05+00:00Will Argentina win the Copa America 2024?CLOSED1.20000000.0240001.964669...True1.96466920.020.7206690.5793162024-07-201FalseJul-21
\n", "

5 rows × 24 columns

\n", "
" ], "text/plain": [ " trader_address market_creator \\\n", "2209 0x95ecc70d9f4feb162ed9f41c4432d990c36c8f57 quickstart \n", "6588 0xf089874165be0377680683fd5187a058dea82683 quickstart \n", "1316 0x49f4e3d8edc85efda9b0a36d96e406a59b13fcc2 quickstart \n", "7776 0xe283e408c6017447da9fe092d52c386753699680 pearl \n", "6344 0x480e5b5abd27cd754745871116e79caf90468dd4 quickstart \n", "\n", " trade_id \\\n", "2209 0x33ec6978224941c04b51704cbc55943898c13a1b0x95... \n", "6588 0x5658c1834d053e92143956069926ccef1cb3c92b0xf0... \n", "1316 0x33ec6978224941c04b51704cbc55943898c13a1b0x49... \n", "7776 0xb13be57227a3e806b36c72ab1cd5792df2e5e8070xe2... \n", "6344 0x5658c1834d053e92143956069926ccef1cb3c92b0x48... \n", "\n", " creation_timestamp \\\n", "2209 2024-07-20 02:10:10+00:00 \n", "6588 2024-07-20 05:18:00+00:00 \n", "1316 2024-07-20 05:28:50+00:00 \n", "7776 2024-07-20 05:51:20+00:00 \n", "6344 2024-07-20 06:32:05+00:00 \n", "\n", " title market_status \\\n", "2209 Will iOS 18's significant upgrade for the iPho... CLOSED \n", "6588 Will Argentina win the Copa America 2024? CLOSED \n", "1316 Will iOS 18's significant upgrade for the iPho... CLOSED \n", "7776 Will a scientific study be published on 23 Jul... CLOSED \n", "6344 Will Argentina win the Copa America 2024? CLOSED \n", "\n", " collateral_amount outcome_index trade_fee_amount \\\n", "2209 0.160000 1 0.003200 \n", "6588 1.000000 0 0.020000 \n", "1316 1.070855 1 0.021417 \n", "7776 1.000000 1 0.020000 \n", "6344 1.200000 0 0.024000 \n", "\n", " outcomes_tokens_traded ... redeemed redeemed_amount num_mech_calls \\\n", "2209 0.310165 ... False 0.000000 3 \n", "6588 1.839649 ... True 1.839649 2 \n", "1316 1.925006 ... False 0.000000 3 \n", "7776 1.839649 ... False 0.000000 2 \n", "6344 1.964669 ... True 1.964669 2 \n", "\n", " mech_fee_amount net_earnings roi creation_date \\\n", "2209 0.03 -0.193200 -1.000000 2024-07-20 \n", "6588 0.02 0.799649 0.768893 2024-07-20 \n", "1316 0.03 -1.122272 -1.000000 2024-07-20 \n", "7776 0.02 -1.040000 -1.000000 2024-07-20 \n", "6344 0.02 0.720669 0.579316 2024-07-20 \n", "\n", " nr_trades_per_market multibet month_year_week \n", "2209 2 True Jul-21 \n", "6588 2 True Jul-21 \n", "1316 2 True Jul-21 \n", "7776 1 False Jul-21 \n", "6344 1 False Jul-21 \n", "\n", "[5 rows x 24 columns]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trader_agent_metrics.head()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "market_creator\n", "quickstart 6896\n", "pearl 1314\n", "Name: count, dtype: int64" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trader_agent_metrics.market_creator.value_counts()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "DEFAULT_MECH_FEE = 0.01 # xDAI" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "def compute_metrics(trader_address: str, trader_data: pd.DataFrame) -> dict:\n", "\n", " if len(trader_data) == 0:\n", " print(\"No data to compute metrics\")\n", " return {}\n", "\n", " weekly_metrics = {}\n", " weekly_metrics[\"trader_address\"] = trader_address\n", " total_net_earnings = trader_data.net_earnings.sum()\n", " total_bet_amounts = trader_data.collateral_amount.sum()\n", " total_num_mech_calls = trader_data.num_mech_calls.sum()\n", " weekly_metrics[\"net_earnings\"] = total_net_earnings\n", " weekly_metrics[\"earnings\"] = trader_data.earnings.sum()\n", " weekly_metrics[\"bet_amount\"] = total_bet_amounts\n", " weekly_metrics[\"nr_mech_calls\"] = total_num_mech_calls\n", " total_fee_amounts = trader_data.mech_fee_amount.sum()\n", " total_costs = (\n", " total_bet_amounts\n", " + total_fee_amounts\n", " + (total_num_mech_calls * DEFAULT_MECH_FEE)\n", " )\n", " weekly_metrics[\"roi\"] = total_net_earnings / total_costs\n", " print(weekly_metrics)\n", " return weekly_metrics\n", "\n", "\n", "def compute_trader_metrics_by_trader_type(\n", " trader_address: str, week_traders_data: pd.DataFrame, trader_type: str = \"all\"\n", ") -> pd.DataFrame:\n", " \"\"\"This function computes for a specific week the different metrics: roi, net_earnings, earnings, bet_amount, nr_mech_calls.\n", " The global roi of the trader agent by computing the individual net profit and the indivicual costs values\n", " achieved per market and dividing both.\n", " It is possible to filter by trader type: multibet, singlebet, all\"\"\"\n", " assert \"trader_type\" in week_traders_data.columns\n", " filtered_traders_data = week_traders_data.loc[\n", " week_traders_data[\"trader_address\"] == trader_address\n", " ]\n", "\n", " if trader_type != \"all\": # compute only for the specific type\n", " filtered_traders_data = filtered_traders_data.loc[\n", " filtered_traders_data[\"trader_type\"] == trader_type\n", " ]\n", " if len(filtered_traders_data) == 0:\n", " return pd.DataFrame() # No Data\n", "\n", " return compute_metrics(trader_address, filtered_traders_data)\n", "\n", "\n", "def compute_trader_metrics_by_market_creator(\n", " trader_address: str, week_traders_data: pd.DataFrame, market_creator: str = \"all\"\n", ") -> dict:\n", " \"\"\"This function computes for a specific week the different metrics: roi, net_earnings, earnings, bet_amount, nr_mech_calls.\n", " The global roi of the trader agent by computing the individual net profit and the indivicual costs values\n", " achieved per market and dividing both.\n", " It is possible to filter by market creator: quickstart, pearl, all\"\"\"\n", " assert \"market_creator\" in week_traders_data.columns\n", " filtered_traders_data = week_traders_data.loc[\n", " week_traders_data[\"trader_address\"] == trader_address\n", " ]\n", " if market_creator != \"all\": # compute only for the specific market creator\n", " print(f\"Filtering only specific market creators = {market_creator}\")\n", " filtered_traders_data = filtered_traders_data.loc[\n", " filtered_traders_data[\"market_creator\"] == market_creator\n", " ]\n", " if len(filtered_traders_data) == 0:\n", " print(f\"No data. Skipping market creator {market_creator}\")\n", " return {} # No Data\n", " print(\n", " f\"Volume of data for trader {trader_address} and market creator {market_creator} = {len(filtered_traders_data)}\"\n", " )\n", " metrics = compute_metrics(trader_address, filtered_traders_data)\n", " return metrics\n", "\n", "\n", "def merge_trader_metrics(\n", " trader: str, weekly_data: pd.DataFrame, week: str\n", ") -> pd.DataFrame:\n", " trader_metrics = []\n", " # computation as specification 1 for all types of markets\n", " weekly_metrics_all = compute_trader_metrics_by_market_creator(\n", " trader, weekly_data, market_creator=\"all\"\n", " )\n", " weekly_metrics_all[\"month_year_week\"] = week\n", " weekly_metrics_all[\"market_creator\"] = \"all\"\n", " trader_metrics.append(weekly_metrics_all)\n", "\n", " # computation as specification 1 for quickstart markets\n", " weekly_metrics_qs = compute_trader_metrics_by_market_creator(\n", " trader, weekly_data, market_creator=\"quickstart\"\n", " )\n", " if len(weekly_metrics_qs) > 0:\n", " weekly_metrics_qs[\"month_year_week\"] = week\n", " weekly_metrics_qs[\"market_creator\"] = \"quickstart\"\n", " trader_metrics.append(weekly_metrics_qs)\n", " # computation as specification 1 for pearl markets\n", " weekly_metrics_pearl = compute_trader_metrics_by_market_creator(\n", " trader, weekly_data, market_creator=\"pearl\"\n", " )\n", " if len(weekly_metrics_pearl) > 0:\n", " weekly_metrics_pearl[\"month_year_week\"] = week\n", " weekly_metrics_pearl[\"market_creator\"] = \"pearl\"\n", " trader_metrics.append(weekly_metrics_pearl)\n", " result = pd.DataFrame.from_dict(trader_metrics, orient=\"columns\")\n", " print(f\"Total length of all trader metrics for this week = {len(result)}\")\n", " print(result.head())\n", " return result\n", "\n", "\n", "def compute_weekly_metrics_by_market_creator(\n", " trader_agents_data: pd.DataFrame,\n", ") -> pd.DataFrame:\n", " \"\"\"Function to compute the metrics at the trader level per week and with different categories by market creator\"\"\"\n", " contents = []\n", " all_weeks = list(trader_agents_data.month_year_week.unique())\n", " for week in all_weeks:\n", " weekly_data = trader_agents_data.loc[\n", " trader_agents_data[\"month_year_week\"] == week\n", " ]\n", " print(f\"Computing weekly metrics for week ={week} by market creator\")\n", " # traverse each trader agent\n", " traders = list(weekly_data.trader_address.unique())\n", " for trader in tqdm(traders, desc=f\"Trader' metrics\", unit=\"metrics\"):\n", " #for trader in traders:\n", " contents.append(merge_trader_metrics(trader, weekly_data, week))\n", " break\n", " print(\"End computing all weekly metrics by market creator\")\n", " return pd.concat(contents, ignore_index=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "compute_weekly_metrics_by_market_creator(trader_agent_metrics)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "tools_df = pd.read_parquet(\"../tmp/tools.parquet\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "error\n", "0 780323\n", "1 214538\n", "Name: count, dtype: int64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tools_df.error.value_counts()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "994861" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(tools_df)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21.564620585187278" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(214538/994861)*100" ] } ], "metadata": { "kernelspec": { "display_name": "hf_dashboards", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }