PQAP Testing Framework - Setup Complete

Overview

A comprehensive testing framework has been set up for the PQAP platform with 116 tests covering strategies, paper trading, configuration, backtesting, and integration flows.

Quick Start

# Run all tests
pytest

# Or use the test runner
python scripts/run_tests.py

# Run specific test suites
python scripts/run_tests.py --unit
python scripts/run_tests.py --integration
python scripts/run_tests.py --backtest

# Run with coverage (requires pytest-cov)
python scripts/run_tests.py --coverage

What's Included

Test Structure

tests/
├── conftest.py              # 30+ reusable fixtures
├── unit/                    # 85 unit tests
│   ├── test_strategies.py   # Strategy tests (41 tests)
│   ├── test_paper_trading.py # Paper trading tests (33 tests)
│   └── test_config.py       # Config tests (11 tests)
├── integration/             # 8 integration tests
│   └── test_trading_flow.py # End-to-end flow tests
└── backtest/                # 23 backtest tests
    └── test_backtest.py     # Backtest engine tests

Test Categories

  1. Unit Tests (85 tests)
  2. Signal creation and validation
  3. Strategy base class functionality
  4. DualOutcomeArbitrage strategy logic
  5. Paper trading engine
  6. Position management
  7. Configuration loading
  8. Risk limits

  9. Integration Tests (8 tests)

  10. Strategy → Signal → Execution flow
  11. Multi-strategy coordination
  12. Risk management enforcement
  13. Error handling

  14. Backtest Tests (23 tests)

  15. Backtest engine
  16. Example strategies (momentum, mean reversion)
  17. Metric calculations
  18. Edge cases

Fixtures (30+)

Comprehensive fixtures in conftest.py: - Market Data: mock markets, outcomes, arbitrage scenarios - Strategies: configured strategies, signals - Trading: orders, trades, positions - Paper Trading: engines with/without positions - Config: configurations, temp files - Time: mock datetimes, market history

Key Test Files

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/tests/unit/test_strategies.py

Tests for strategy base class and implementations: - Signal lifecycle and expiry - Strategy configuration and validation - Position management - Arbitrage detection - Exit logic (SELL/CLOSE signals)

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/tests/unit/test_paper_trading.py

Tests for paper trading engine: - BUY/SELL signal execution - Position opening and closing - Capital management - Price updates - Portfolio snapshots - State persistence

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/tests/unit/test_config.py

Tests for configuration: - YAML config loading - Environment variable override - Risk limits validation - Strategy parameters

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/tests/integration/test_trading_flow.py

End-to-end integration tests: - Arbitrage detection → signal → execution - Multiple market updates - Position lifecycle - Risk limits enforcement - Error handling

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/tests/backtest/test_backtest.py

Backtest engine tests: - Strategy backtesting - Metric calculations - Example strategies - Edge cases

Configuration Files

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/pytest.ini

Pytest configuration with: - Test discovery patterns - Test markers (unit, integration, slow, etc.) - Output formatting - Coverage settings (commented out by default) - Logging configuration

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/requirements.txt

Updated with test dependencies: - pytest>=7.0 - pytest-asyncio>=0.21 - pytest-cov>=4.1 - pytest-mock>=3.12

Test Runner Script

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/scripts/run_tests.py

Convenient test runner with options:

# Run specific suites
python scripts/run_tests.py --unit
python scripts/run_tests.py --integration
python scripts/run_tests.py --backtest

# Run specific file
python scripts/run_tests.py --file test_strategies.py

# Run tests matching pattern
python scripts/run_tests.py -k "arbitrage"

# Options
python scripts/run_tests.py --fast        # Skip slow tests
python scripts/run_tests.py --verbose     # Verbose output
python scripts/run_tests.py --exitfirst   # Stop on first failure
python scripts/run_tests.py --failed      # Run only failed tests
python scripts/run_tests.py --pdb         # Drop to debugger on failure
python scripts/run_tests.py --coverage    # Generate coverage report

Running Tests

All Tests

pytest                          # Using pytest directly
python scripts/run_tests.py     # Using test runner

By Category

pytest tests/unit/
pytest tests/integration/
pytest tests/backtest/
pytest -m unit                  # Using markers
pytest -m "integration and not slow"

Specific Tests

pytest tests/unit/test_strategies.py
pytest tests/unit/test_strategies.py::TestDualOutcomeArbitrage
pytest tests/unit/test_strategies.py::TestDualOutcomeArbitrage::test_detect_arbitrage_opportunity
pytest -k "arbitrage"           # Tests matching pattern

With Coverage

# Install pytest-cov first
pip install pytest-cov

# Run with coverage
pytest --cov=src --cov-report=term-missing --cov-report=html
python scripts/run_tests.py --coverage

# View HTML report
open htmlcov/index.html

Test Coverage

Current test coverage by module:

Module Tests Coverage Target
Strategy Base 20 tests >90%
Arbitrage Strategy 13 tests >90%
Paper Trading 33 tests >90%
Configuration 11 tests >80%
Backtest Engine 23 tests >80%
Integration 8 tests >70%

Overall target: >80% code coverage

Key Features

1. Comprehensive Fixtures

  • Pre-configured test data for all components
  • Easy to use and compose
  • Isolated and independent

2. Test Isolation

  • Each test runs independently
  • No shared state between tests
  • Fresh fixtures for each test

3. Clear Test Structure

  • Arrange-Act-Assert pattern
  • Descriptive test names
  • One assertion per test (mostly)

4. Error Cases

  • Tests for edge cases
  • Error handling validation
  • Boundary conditions

5. Integration Coverage

  • End-to-end flows
  • Multi-component interactions
  • Real-world scenarios

Documentation

/Users/rodneymiranda/Documents/Consulting/TailwindTech/polymarket/tests/README.md

Comprehensive testing guide including: - Test structure and organization - Running tests (all options) - Writing new tests - Using fixtures - Best practices - Troubleshooting - CI/CD integration

Next Steps

1. Install Coverage Tools (Optional)

pip install pytest-cov

Then uncomment coverage flags in pytest.ini or use:

pytest --cov=src --cov-report=term-missing

2. Run Initial Test Suite

# Run all tests to verify setup
python scripts/run_tests.py

# Expected: 116 tests collected, all passing

3. Add Tests for New Features

When adding new strategies or features: 1. Add unit tests in tests/unit/ 2. Add integration tests if needed 3. Update fixtures in conftest.py if needed 4. Run tests: pytest -k "your_feature"

4. Set Up CI/CD (Optional)

Example GitHub Actions workflow:

name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: '3.10'
      - run: pip install -r requirements.txt
      - run: pytest --cov=src --cov-report=xml
      - uses: codecov/codecov-action@v2

5. Monitor Test Performance

# Show slowest tests
pytest --durations=10

# Profile tests
pytest --profile

Best Practices

  1. Run tests before committing bash python scripts/run_tests.py --fast

  2. Run full test suite before pushing bash python scripts/run_tests.py --coverage

  3. Add tests for bug fixes

  4. Write test that reproduces bug
  5. Fix bug
  6. Verify test passes

  7. Keep tests fast

  8. Use mocks for external services
  9. Use in-memory databases
  10. Mark slow tests with @pytest.mark.slow

  11. Update tests when refactoring

  12. Tests should reflect current behavior
  13. Update fixtures if interfaces change
  14. Maintain coverage levels

Test Markers

Use markers to categorize tests:

@pytest.mark.unit
def test_unit_functionality():
    """Unit test."""
    pass

@pytest.mark.integration
def test_integration():
    """Integration test."""
    pass

@pytest.mark.slow
def test_long_operation():
    """Slow test."""
    pass

@pytest.mark.requires_db
def test_database():
    """Requires database."""
    pass

Run by marker:

pytest -m unit
pytest -m "not slow"
pytest -m "integration and not requires_db"

Troubleshooting

Tests Not Found

  • Ensure you're in project root
  • Check file names start with test_
  • Check class names start with Test
  • Check function names start with test_

Import Errors

  • Ensure src/ is in Python path
  • Run from project root
  • Check all __init__.py files exist

Fixture Not Found

  • Check fixture name spelling
  • Ensure conftest.py is in correct location
  • Check fixture scope

Async Test Issues

@pytest.mark.asyncio
async def test_async():
    result = await async_function()
    assert result is not None

Summary

The PQAP testing framework provides: - ✅ 116 comprehensive tests - ✅ 30+ reusable fixtures - ✅ Unit, integration, and backtest coverage - ✅ Easy-to-use test runner script - ✅ Detailed documentation - ✅ CI/CD ready - ✅ Coverage reporting (with pytest-cov)

All tests are passing and ready to use!

System Overview

Polymarket API

Market data source

Data Collector

Every 5 minutes

SQLite Database

Price history + trades

Strategy Engine

Signal generation

ML Model

XGBoost (72% acc)

Execution Engine

Paper trading

Dashboard

You are here!

Telegram

Alerts & updates

Trading Strategies

Each strategy looks for different market inefficiencies:

Dual Arbitrage Active

Finds when YES + NO prices don't add to 100%. Risk-free profit.

Mean Reversion Active

Buys when price drops too far from average, sells when it recovers.

Market Maker Active

Places bid/ask orders to capture the spread.

Time Arbitrage Active

Exploits predictable price patterns at certain hours.

ML Prediction Active

Uses machine learning to predict 6-hour price direction.

Value Betting Disabled

Finds underpriced outcomes based on implied probability.

Data Storage (Single Source of Truth)

All data lives on EC2. Local machines are for development only. The EC2 instance is the authoritative source for all market data, trades, and positions.
Database Purpose Location
market_history.db Price snapshots every 5 minutes (8.2 MB) EC2 (primary)
pqap_prod.db Trades, positions, P&L history EC2 (primary)
paper_trading_state.json Current portfolio state EC2 (primary)

Environment Architecture

EC2 (Production)

  • Runs 24/7
  • All databases live here
  • Executes all trades
  • Single source of truth

Local (Development)

  • For code changes only
  • Syncs code to EC2
  • No production data
  • Can be turned off

Environment Details

Component Details
Dashboard URL https://pqap.tailwindtech.ai
Server AWS EC2 (us-east-1)
SSL Let's Encrypt via Traefik
Mode Paper Trading (simulated)

How It Works (Simple Version)

1. Data Collection: Every 5 minutes, we fetch prices from Polymarket for 50 markets and save them to our database.

2. Analysis: Our strategies analyze this data looking for patterns - like prices that moved too far from normal, or markets where the math doesn't add up.

3. Signals: When a strategy finds an opportunity, it generates a "signal" - a recommendation to buy or sell.

4. Execution: The execution engine takes these signals and simulates trades (paper trading). Eventually, this will place real orders.

5. Monitoring: This dashboard shows you what's happening. Telegram sends alerts for important events.