From 0af334bdf943cfe62718a0db666c9572fccbb692 Mon Sep 17 00:00:00 2001 From: Oleg Sheynin Date: Wed, 30 Jul 2025 23:23:22 +0000 Subject: [PATCH] initial cleaning after refactoring --- .../notebooks/single_pair_test.ipynb | 0 lib/utils/db_inspector.py | 169 ------------------ ...test_NEW.ipynb => pair_trading_test.ipynb} | 134 ++------------ 3 files changed, 17 insertions(+), 286 deletions(-) rename {research => __DELETE__/research}/notebooks/single_pair_test.ipynb (100%) delete mode 100644 lib/utils/db_inspector.py rename research/notebooks/{pair_trading_test_NEW.ipynb => pair_trading_test.ipynb} (99%) diff --git a/research/notebooks/single_pair_test.ipynb b/__DELETE__/research/notebooks/single_pair_test.ipynb similarity index 100% rename from research/notebooks/single_pair_test.ipynb rename to __DELETE__/research/notebooks/single_pair_test.ipynb diff --git a/lib/utils/db_inspector.py b/lib/utils/db_inspector.py deleted file mode 100644 index 99da030..0000000 --- a/lib/utils/db_inspector.py +++ /dev/null @@ -1,169 +0,0 @@ -#!/usr/bin/env python3 -""" -Database inspector utility for pairs trading results database. -Provides functionality to view all tables and their contents. -""" - -import sqlite3 -import sys -import json -import os -from typing import List, Dict, Any - -def list_tables(db_path: str) -> List[str]: - """List all tables in the database.""" - conn = sqlite3.connect(db_path) - cursor = conn.cursor() - - cursor.execute(""" - SELECT name FROM sqlite_master - WHERE type='table' - ORDER BY name - """) - - tables = [row[0] for row in cursor.fetchall()] - conn.close() - return tables - -def view_table_schema(db_path: str, table_name: str) -> None: - """View the schema of a specific table.""" - conn = sqlite3.connect(db_path) - cursor = conn.cursor() - - cursor.execute(f"PRAGMA table_info({table_name})") - columns = cursor.fetchall() - - print(f"\nTable: {table_name}") - print("-" * 50) - print("Column Name".ljust(20) + "Type".ljust(15) + "Not Null".ljust(10) + "Default") - print("-" * 50) - - for col in columns: - cid, name, type_, not_null, default_value, pk = col - print(f"{name}".ljust(20) + f"{type_}".ljust(15) + f"{bool(not_null)}".ljust(10) + f"{default_value or ''}") - - conn.close() - -def view_config_table(db_path: str, limit: int = 10) -> None: - """View entries from the config table.""" - conn = sqlite3.connect(db_path) - cursor = conn.cursor() - - cursor.execute(f""" - SELECT id, run_timestamp, config_file_path, fit_method_class, - datafiles, instruments, config_json - FROM config - ORDER BY run_timestamp DESC - LIMIT {limit} - """) - - rows = cursor.fetchall() - - if not rows: - print("No configuration entries found.") - return - - print(f"\nMost recent {len(rows)} configuration entries:") - print("=" * 80) - - for row in rows: - id, run_timestamp, config_file_path, fit_method_class, datafiles, instruments, config_json = row - - print(f"ID: {id} | {run_timestamp}") - print(f"Config: {config_file_path} | Strategy: {fit_method_class}") - print(f"Files: {datafiles}") - print(f"Instruments: {instruments}") - print("-" * 80) - - conn.close() - -def view_results_summary(db_path: str) -> None: - """View summary of trading results.""" - conn = sqlite3.connect(db_path) - cursor = conn.cursor() - - # Get results summary - cursor.execute(""" - SELECT date, COUNT(*) as trade_count, - ROUND(SUM(symbol_return), 2) as total_return - FROM pt_bt_results - GROUP BY date - ORDER BY date DESC - """) - - results = cursor.fetchall() - - if not results: - print("No trading results found.") - return - - print(f"\nTrading Results Summary:") - print("-" * 50) - print("Date".ljust(15) + "Trades".ljust(10) + "Total Return %") - print("-" * 50) - - for date, trade_count, total_return in results: - print(f"{date}".ljust(15) + f"{trade_count}".ljust(10) + f"{total_return}") - - # Get outstanding positions summary - cursor.execute(""" - SELECT COUNT(*) as position_count, - ROUND(SUM(unrealized_return), 2) as total_unrealized - FROM outstanding_positions - """) - - outstanding = cursor.fetchone() - if outstanding and outstanding[0] > 0: - print(f"\nOutstanding Positions: {outstanding[0]} positions") - print(f"Total Unrealized Return: {outstanding[1]}%") - - conn.close() - -def main() -> None: - if len(sys.argv) < 2: - print("Usage: python db_inspector.py [command]") - print("Commands:") - print(" tables - List all tables") - print(" schema - Show schema for all tables") - print(" config - View configuration entries") - print(" results - View trading results summary") - print(" all - Show everything (default)") - print("\nExample: python db_inspector.py results/equity.db config") - sys.exit(1) - - db_path = sys.argv[1] - command = sys.argv[2] if len(sys.argv) > 2 else "all" - - if not os.path.exists(db_path): - print(f"Database file not found: {db_path}") - sys.exit(1) - - try: - if command in ["tables", "all"]: - tables = list_tables(db_path) - print(f"Tables in database: {', '.join(tables)}") - - if command in ["schema", "all"]: - tables = list_tables(db_path) - for table in tables: - view_table_schema(db_path, table) - - if command in ["config", "all"]: - if "config" in list_tables(db_path): - view_config_table(db_path) - else: - print("Config table not found.") - - if command in ["results", "all"]: - if "pt_bt_results" in list_tables(db_path): - view_results_summary(db_path) - else: - print("Results table not found.") - - except Exception as e: - print(f"Error inspecting database: {str(e)}") - import traceback - traceback.print_exc() - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/research/notebooks/pair_trading_test_NEW.ipynb b/research/notebooks/pair_trading_test.ipynb similarity index 99% rename from research/notebooks/pair_trading_test_NEW.ipynb rename to research/notebooks/pair_trading_test.ipynb index eb54281..04e81c1 100644 --- a/research/notebooks/pair_trading_test_NEW.ipynb +++ b/research/notebooks/pair_trading_test.ipynb @@ -14,7 +14,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -40,7 +40,7 @@ "ROOT_DIR = \"/home/oleg/develop/pairs_trading\"\n", "os.chdir(ROOT_DIR)\n", "\n", - "CONFIG_FILE = f\"{ROOT_DIR}/configuration/new_zscore.cfg\"\n", + "CONFIG_FILE = f\"{ROOT_DIR}/configuration/ols.cfg\"\n", "\n", "# Date for data file selection (format: YYYYMMDD)\n", "TRADING_DATE = \"20250605\" # Change this to your desired date\n", @@ -117,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -160,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -213,7 +213,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -288,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -301,12 +301,9 @@ " global PT_RESULTS\n", "\n", " \n", - " import pandas as pd\n", - " from tools.data_loader import load_market_data\n", " from pt_strategy.trading_pair import TradingPair\n", " from pt_strategy.trading_strategy import PtResearchStrategy\n", " from pt_strategy.results import PairResearchResult\n", - " from research.research_tools import create_pairs\n", "\n", " # Create trading pair\n", " PT_RESULTS = PairResearchResult(config=PT_BT_CONFIG)\n", @@ -335,10 +332,7 @@ " display(pair.market_data_.head())\n", "\n", " display(pair.market_data_.tail())\n", - "\n", - "# setup()\n", - "# prepare_config()\n", - "# run_strategy()" + "\n" ] }, { @@ -350,7 +344,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -374,7 +368,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -383,100 +377,6 @@ " PT_RESULTS.analyze_pair_performance()" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Performance" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "def performance_results() -> None:\n", - " global pair_trades\n", - " global bt_result\n", - " global SYMBOL_A\n", - " global SYMBOL_B\n", - " global FIT_METHOD_TYPE\n", - " global PT_BT_CONFIG\n", - "\n", - " from pt_trading.results import BacktestResult\n", - "\n", - " if pair_trades is not None and len(pair_trades) > 0:\n", - " # Print detailed results using BacktestResult methods\n", - " # bt_result.print_single_day_results()\n", - " \n", - " # Print trading signal details\n", - " print(f\"\\nDetailed Trading Signals:\")\n", - " print(f\"{'Time':<20} {'Action':<15} {'Symbol':<10} {'Price':<12} {'Scaled Dis-eq':<15} {'Status':<10}\")\n", - " print(\"-\" * 90)\n", - " \n", - " for _, trade in pair_trades.head(10).iterrows(): # Show first 10 trades\n", - " time_str = str(trade['time'])[:19] \n", - " action_str = str(trade['action'])[:14]\n", - " symbol_str = str(trade['symbol'])[:9]\n", - " price_str = f\"${trade['price']:.2f}\"\n", - " diseq_str = f\"{trade.get('scaled_disequilibrium', 'N/A'):.3f}\" if 'scaled_disequilibrium' in trade else 'N/A'\n", - " status = trade.get('status', 'N/A')\n", - " \n", - " print(f\"{time_str:<20} {action_str:<15} {symbol_str:<10} {price_str:<12} {diseq_str:<15} {status:<10}\")\n", - " \n", - " if len(pair_trades) > 10:\n", - " print(f\"... and {len(pair_trades)-10} more trading signals\")\n", - " \n", - " bt_result.collect_single_day_results([pair_trades])\n", - "\n", - " # bt_result.print_grand_totals()\n", - " # bt_result.print_outstanding_positions() \n", - " else:\n", - " print(f\"\\nNo trading signals generated\")\n", - " print(f\"Backtest completed with no trades\")\n", - " \n", - " # Still print any outstanding information\n", - " print(f\"\\nConfiguration Summary:\")\n", - " print(f\" Pair: {SYMBOL_A} & {SYMBOL_B}\")\n", - " print(f\" Strategy: {FIT_METHOD_TYPE}\")\n", - " print(f\" Open threshold: {PT_BT_CONFIG['dis-equilibrium_open_trshld']}\")\n", - " print(f\" Close threshold: {PT_BT_CONFIG['dis-equilibrium_close_trshld']}\")\n", - " print(f\" Training window: {PT_BT_CONFIG['training_minutes']} minutes\")\n", - " \n", - " print(\"\\n\" + \"=\"*80)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "def print_summary():\n", - " global pair_trades\n", - "\n", - " from pt_trading.results import BacktestResult\n", - "\n", - " if pair_trades is not None and len(pair_trades) > 0:\n", - " all_results: Dict[str, Dict[str, Any]] = {}\n", - " all_results[f\"{TRADING_DATE}-{pair.name()}\"] = {\n", - " \"trades\": bt_result.trades.copy(), \n", - " \"outstanding_positions\": bt_result.outstanding_positions.copy()\n", - " }\n", - "\n", - " if all_results:\n", - " aggregate_bt_results = BacktestResult(config=PT_BT_CONFIG)\n", - " aggregate_bt_results.calculate_returns(all_results)\n", - " aggregate_bt_results.print_grand_totals()\n", - " aggregate_bt_results.print_outstanding_positions()\n", - "\n", - "\n", - " \n", - "# performance_results()" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -486,7 +386,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -496,12 +396,12 @@ "Setup complete!\n", "Current working directory: /home/oleg\n", "Trading Parameters:\n", - " Configuration: /home/oleg/develop/pairs_trading/configuration/new_zscore.cfg\n", + " Configuration: /home/oleg/develop/pairs_trading/configuration/ols.cfg\n", " Symbol A: ADA-USDT\n", " Symbol B: SOL-USDT\n", " Trading Date: 2025-06-05\n", "\n", - "Loading /home/oleg/develop/pairs_trading/configuration/new_zscore.cfg configuration using HJSON...\n", + "Loading /home/oleg/develop/pairs_trading/configuration/ols.cfg configuration using HJSON...\n", "✓ Successfully loaded configuration\n", " Training window: 120 minutes\n", " Open threshold: 2\n", @@ -605,13 +505,13 @@ " time action symbol side price disequilibrium scaled_disequilibrium signed_scaled_disequilibrium status\n", "0 2025-06-05 22:13:00 OPEN ADA-USDT BUY 0.622229 -2.338327 2.338327 -2.338327 OPEN\n", "1 2025-06-05 22:13:00 OPEN SOL-USDT SELL 143.277122 -2.338327 2.338327 -2.338327 OPEN\n", - ": *** Position is NOT CLOSED. ***\n", + ": *** Position is NOT CLOSED. ***\n", "CLOSE_POSITION TRADES:\n", " time action symbol side price disequilibrium scaled_disequilibrium signed_scaled_disequilibrium status\n", "0 2025-06-05 22:27:00 CLOSE ADA-USDT SELL 0.623272 0.0 0.0 0.0 CLOSE_POSITION\n", "1 2025-06-05 22:27:00 CLOSE SOL-USDT BUY 143.996271 0.0 0.0 0.0 CLOSE_POSITION\n", "\n", - "Created trading pair: \n", + "Created trading pair: \n", "Market data shape: (120, 7)\n", "Column names: ['close_ADA-USDT', 'close_SOL-USDT']\n", "\n", @@ -843,7 +743,7 @@ " Price Ratio: Mean=0.00, Std=0.00\n", " Correlation: 0.9902\n", "\n", - "Created trading pair: \n", + "Created trading pair: \n", "Market data shape: (120, 7)\n", "Column names: ['close_ADA-USDT', 'close_SOL-USDT']\n" ] @@ -6339,9 +6239,9 @@ }, "text/html": [ "
\n", - "