Release v1.0.2
This commit is contained in:
+93
-96
@@ -30,6 +30,7 @@
|
||||
"from IPython.display import display\n",
|
||||
"import ipywidgets as widgets\n",
|
||||
"import pandas as pd\n",
|
||||
"import panel as pn\n",
|
||||
"\n",
|
||||
"START_DIR = Path.cwd().resolve()\n",
|
||||
"for candidate in (START_DIR, *START_DIR.parents):\n",
|
||||
@@ -41,21 +42,27 @@
|
||||
"import scripts.spbt_day as spbt_day\n",
|
||||
"\n",
|
||||
"spbt_day = importlib.reload(spbt_day)\n",
|
||||
"pn.extension(\"tabulator\", \"plotly\")\n",
|
||||
"\n",
|
||||
"ANALYZE_BUTTON_COLUMN = spbt_day.ANALYZE_BUTTON_COLUMN\n",
|
||||
"SELECTED_PAIR_EXECUTION_DISPLAY_COLUMNS = spbt_day.SELECTED_PAIR_EXECUTION_DISPLAY_COLUMNS\n",
|
||||
"add_total_pnl = spbt_day.add_total_pnl\n",
|
||||
"calculate_pair_theo_executions = spbt_day.calculate_pair_theo_executions\n",
|
||||
"calculate_ranked_pairs_theo_ret = spbt_day.calculate_ranked_pairs_theo_ret\n",
|
||||
"create_database_file_selector = spbt_day.create_database_file_selector\n",
|
||||
"create_pair_name_dropdown = spbt_day.create_pair_name_dropdown\n",
|
||||
"create_pair_theo_ret_analyze_grid = spbt_day.create_pair_theo_ret_analyze_grid\n",
|
||||
"create_pair_trades_market_plot = spbt_day.create_pair_trades_market_plot\n",
|
||||
"create_selected_pair_executions_grid = spbt_day.create_selected_pair_executions_grid\n",
|
||||
"create_total_pnl_histogram = spbt_day.create_total_pnl_histogram\n",
|
||||
"find_repo_root = spbt_day.find_repo_root\n",
|
||||
"format_pair_name_for_display = spbt_day.format_pair_name_for_display\n",
|
||||
"format_pair_names_for_display = spbt_day.format_pair_names_for_display\n",
|
||||
"format_pair_theo_ret_for_analyze_grid = spbt_day.format_pair_theo_ret_for_analyze_grid\n",
|
||||
"infer_trading_day_start_ns = spbt_day.infer_trading_day_start_ns\n",
|
||||
"load_selector_pair_rankings = spbt_day.load_selector_pair_rankings\n",
|
||||
"load_pair_market_data = spbt_day.load_pair_market_data\n",
|
||||
"load_trading_instructions = spbt_day.load_trading_instructions\n",
|
||||
"pair_name_from_analyze_event = spbt_day.pair_name_from_analyze_event\n",
|
||||
"show_interactive_dataframe = spbt_day.show_interactive_dataframe\n",
|
||||
"\n",
|
||||
"REPO_ROOT = find_repo_root()\n",
|
||||
@@ -190,12 +197,12 @@
|
||||
" kind=\"mergesort\",\n",
|
||||
").drop(columns=\"total_pnl\").reset_index(drop=True)\n",
|
||||
"\n",
|
||||
"pair_theo_ret_display = format_pair_names_for_display(pair_theo_ret)\n",
|
||||
"\n",
|
||||
"show_interactive_dataframe(\n",
|
||||
"pair_theo_ret_display = format_pair_theo_ret_for_analyze_grid(pair_theo_ret)\n",
|
||||
"pair_theo_ret_grid = create_pair_theo_ret_analyze_grid(\n",
|
||||
" pair_theo_ret_display,\n",
|
||||
" table_id=\"pair-theo-ret-grid\",\n",
|
||||
")"
|
||||
" height=520,\n",
|
||||
")\n",
|
||||
"display(pair_theo_ret_grid)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -216,8 +223,13 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"total_pnl_histogram = create_total_pnl_histogram(pair_theo_ret)\n",
|
||||
"total_pnl_histogram_pane = pn.pane.Plotly(\n",
|
||||
" total_pnl_histogram,\n",
|
||||
" height=360,\n",
|
||||
" sizing_mode=\"stretch_width\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"total_pnl_histogram"
|
||||
"display(total_pnl_histogram_pane)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -227,109 +239,94 @@
|
||||
"source": [
|
||||
"## Individual Pair Analysis\n",
|
||||
"\n",
|
||||
"Choose one pair for detailed follow-up analysis. Pair names are sorted alphabetically."
|
||||
"Click the Analyze button in the Pair TheoRet grid to load detailed follow-up analysis for that row. The selected-pair execution table and market/trade chart are not calculated until an Analyze button is clicked."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "choose-individual-pair",
|
||||
"id": "individual-pair-analysis",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pair_name_dropdown = create_pair_name_dropdown(selector_pair_rankings)\n",
|
||||
"display(pair_name_dropdown)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "selected-individual-pair",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"selected_pair_name = pair_name_dropdown.value\n",
|
||||
"format_pair_name_for_display(selected_pair_name)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "selected-pair-theo-executions-context",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Selected Pair Theoretical Executions\n",
|
||||
"\n",
|
||||
"Create the theoretical asset-level executions used by the PnL calculation for the selected pair. `TARGET` rows trade the position difference from the current theoretical position to the new target position, where target size is `10000 * strength / reference_price`; `CLOSE` rows flatten the current theoretical position. Positive size is `BUY`; negative size is `SELL`; USD value is signed as the opposite cash movement."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "selected-pair-theo-executions",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"selected_pair_theo_executions = calculate_pair_theo_executions(\n",
|
||||
" selected_pair_name,\n",
|
||||
" trading_instructions,\n",
|
||||
" min_pctg_change=MIN_TARGET_STRENGTH_CHANGE_PCTG,\n",
|
||||
"selected_pair_name = None\n",
|
||||
"selected_pair_theo_executions = pd.DataFrame()\n",
|
||||
"selected_pair_theo_executions_display = pd.DataFrame(\n",
|
||||
" columns=SELECTED_PAIR_EXECUTION_DISPLAY_COLUMNS\n",
|
||||
")\n",
|
||||
"selected_pair_market_data = pd.DataFrame()\n",
|
||||
"selected_pair_market_trades_plot = None\n",
|
||||
"\n",
|
||||
"selected_pair_theo_execution_columns = [\n",
|
||||
" \"time\",\n",
|
||||
" \"asset\",\n",
|
||||
" \"action\",\n",
|
||||
" \"side\",\n",
|
||||
" \"strength\",\n",
|
||||
" \"size\",\n",
|
||||
" \"price\",\n",
|
||||
" \"usd_value\",\n",
|
||||
"]\n",
|
||||
"selected_pair_theo_executions_display = selected_pair_theo_executions.reindex(\n",
|
||||
" columns=selected_pair_theo_execution_columns\n",
|
||||
"selected_pair_message = pn.pane.Markdown(\n",
|
||||
" \"Click Analyze in the Pair TheoRet grid to load individual-pair details.\"\n",
|
||||
")\n",
|
||||
"show_interactive_dataframe(\n",
|
||||
"selected_pair_theo_executions_grid = create_selected_pair_executions_grid(\n",
|
||||
" selected_pair_theo_executions_display,\n",
|
||||
" table_id=\"selected-pair-theo-executions-grid\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "selected-pair-market-trades-context",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Selected Pair Trades on Market Data\n",
|
||||
"\n",
|
||||
"Load full available 1-minute market data for the selected pair's instruments from `ohlcv_1min`, starting at midnight UTC of the trading day inferred from `trading_instructions`. Close prices are shown as relative prices from each instrument's close at that midnight. Theoretical executions are overlaid at their execution `reference_price`, normalized by the same midnight close. Execution markers use execution timestamps directly and do not require a matching OHLCV row."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "selected-pair-market-trades-plot",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"trading_day_start_ns = infer_trading_day_start_ns(trading_instructions)\n",
|
||||
"\n",
|
||||
"conn = connect_selected_database()\n",
|
||||
"try:\n",
|
||||
" selected_pair_market_data = load_pair_market_data(\n",
|
||||
" conn,\n",
|
||||
" selected_pair_name,\n",
|
||||
" trading_day_start_ns=trading_day_start_ns,\n",
|
||||
" )\n",
|
||||
"finally:\n",
|
||||
" conn.close()\n",
|
||||
"\n",
|
||||
"selected_pair_market_trades_plot = create_pair_trades_market_plot(\n",
|
||||
" selected_pair_name,\n",
|
||||
" selected_pair_market_data,\n",
|
||||
" selected_pair_theo_executions,\n",
|
||||
" height=360,\n",
|
||||
")\n",
|
||||
"selected_pair_market_trades_plot_pane = pn.pane.Plotly(\n",
|
||||
" None,\n",
|
||||
" height=520,\n",
|
||||
" sizing_mode=\"stretch_width\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"selected_pair_market_trades_plot"
|
||||
"\n",
|
||||
"def analyze_pair_click(event):\n",
|
||||
" global selected_pair_name\n",
|
||||
" global selected_pair_theo_executions\n",
|
||||
" global selected_pair_theo_executions_display\n",
|
||||
" global selected_pair_market_data\n",
|
||||
" global selected_pair_market_trades_plot\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" selected_pair_name = pair_name_from_analyze_event(pair_theo_ret_grid, event)\n",
|
||||
" selected_pair_message.object = (\n",
|
||||
" f\"Selected pair: **{format_pair_name_for_display(selected_pair_name)}**\"\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" selected_pair_theo_executions = calculate_pair_theo_executions(\n",
|
||||
" selected_pair_name,\n",
|
||||
" trading_instructions,\n",
|
||||
" min_pctg_change=MIN_TARGET_STRENGTH_CHANGE_PCTG,\n",
|
||||
" )\n",
|
||||
" selected_pair_theo_executions_display = selected_pair_theo_executions.reindex(\n",
|
||||
" columns=SELECTED_PAIR_EXECUTION_DISPLAY_COLUMNS\n",
|
||||
" )\n",
|
||||
" selected_pair_theo_executions_grid.value = selected_pair_theo_executions_display\n",
|
||||
"\n",
|
||||
" trading_day_start_ns = infer_trading_day_start_ns(trading_instructions)\n",
|
||||
" conn = connect_selected_database()\n",
|
||||
" try:\n",
|
||||
" selected_pair_market_data = load_pair_market_data(\n",
|
||||
" conn,\n",
|
||||
" selected_pair_name,\n",
|
||||
" trading_day_start_ns=trading_day_start_ns,\n",
|
||||
" )\n",
|
||||
" finally:\n",
|
||||
" conn.close()\n",
|
||||
"\n",
|
||||
" selected_pair_market_trades_plot = create_pair_trades_market_plot(\n",
|
||||
" selected_pair_name,\n",
|
||||
" selected_pair_market_data,\n",
|
||||
" selected_pair_theo_executions,\n",
|
||||
" )\n",
|
||||
" selected_pair_market_trades_plot_pane.object = selected_pair_market_trades_plot\n",
|
||||
" except Exception as exc:\n",
|
||||
" selected_pair_message.object = f\"**Error:** {exc}\"\n",
|
||||
" selected_pair_market_trades_plot_pane.object = None\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pair_theo_ret_grid.on_click(analyze_pair_click, column=ANALYZE_BUTTON_COLUMN)\n",
|
||||
"\n",
|
||||
"display(\n",
|
||||
" pn.Column(\n",
|
||||
" selected_pair_message,\n",
|
||||
" \"### Theoretical Executions\",\n",
|
||||
" selected_pair_theo_executions_grid,\n",
|
||||
" \"### Trades on Market Data\",\n",
|
||||
" selected_pair_market_trades_plot_pane,\n",
|
||||
" )\n",
|
||||
")"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user