- strategy_service.py combines opportunity + portfolio signals into unified buy/sell/hold recommendations - backtest_service.py runs walk-forward backtests on historical datasets with virtual cash and positions - CLI adds `strategy` and `backtest` commands with `--decision-interval` and other tuning parameters - Add tests for both services and CLI dispatch - Update CLAUDE.md with new architecture docs - Optimize model weights via opportunity optimizer Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
4.8 KiB
4.8 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Development commands
- Install (dev):
pip install -e ".[dev]"orconda env create -f environment.yml && conda activate coinhunter - Run CLI locally:
python -m coinhunter --help - Run tests:
pytestorpython -m pytest tests/ - Run single test file:
pytest tests/test_cli.py -v - Lint:
ruff check src tests - Format:
ruff format src tests - Type-check:
mypy src
Architecture
CoinHunter V2 is a Binance-first crypto trading CLI with a flat, direct architecture:
src/coinhunter/cli.py— Single entrypoint (main()). Usesargparseto parse commands and directly dispatches to service functions. There is no separatecommands/adapter layer. Top-level commands includeinit,config,account,market,buy,sell,portfolio,opportunity,strategy,backtest,catlog,upgrade, andcompletion.src/coinhunter/services/— Contains all domain logic:account_service.py— balances, positions, overviewmarket_service.py— tickers, klines, scan universe, symbol normalizationsignal_service.py— shared market signal scoring used by scan and portfolio analysisportfolio_service.py— held-position review and add/hold/trim/exit recommendationstrade_service.py— spot order execution onlyopportunity_service.py— market scanning and entry/watch/skip recommendationsopportunity_dataset_service.py— historical kline dataset collection for backtestingopportunity_evaluation_service.py— walk-forward evaluation and model-weight optimizationresearch_service.py— external research signal providers for opportunity scoringstrategy_service.py— combines opportunity scanning and portfolio analysis into unified buy/sell/hold trade signalsbacktest_service.py— walk-forward backtest engine using historical kline datasets with virtual cash and positions
src/coinhunter/binance/spot_client.py— Thin wrapper aroundbinance.spot.Spot. Normalizes request errors intoRuntimeErrorand handles single/multi-symbol ticker responses.src/coinhunter/config.py—load_config(),get_binance_credentials(),ensure_init_files().src/coinhunter/runtime.py—RuntimePaths,get_runtime_paths(),print_json().src/coinhunter/audit.py— Writes JSONL audit events to dated files.
Runtime and environment
User data lives in ~/.coinhunter/ by default (override with COINHUNTER_HOME):
config.toml— runtime, binance, trading, signal, opportunity, and portfolio settings.env—BINANCE_API_KEYandBINANCE_API_SECRETlogs/audit_YYYYMMDD.jsonl— structured audit loglogs/dry-run/audit_YYYYMMDD.jsonl— dry-run audit log
Run coinhunter init to generate the config and env templates.
Key conventions
- Symbol normalization:
market_service.normalize_symbol()strips/,-,_, and uppercases the symbol. CLI inputs likeETH/USDT,eth-usdt, andETHUSDTare all normalized toETHUSDT. - Dry-run behavior: Trade commands support
--dry-run. If omitted, the default falls back totrading.dry_run_defaultinconfig.toml. - Client injection: Service functions accept
spot_clientas a keyword argument. This enables easy unit testing with mocks. - Error handling:
spot_client.pycatchesrequests.exceptions.SSLErrorandRequestExceptionand re-raises as human-readableRuntimeError. The CLI catches all exceptions inmain()and printserror: {message}to stderr with exit code 1. - Ticker API fallback:
spot_client.ticker_stats()usesrolling_window_tickerfor symbol-specific queries andticker_24hrfor full-market scans (no symbols). - Output modes: All commands support
--agentfor JSON output and--docto print the command's output schema.
Testing
Tests live in tests/ and use unittest.TestCase with unittest.mock.patch. The test suite covers:
test_cli.py— parser smoke tests and dispatch behaviortest_config_runtime.py— config loading, env parsing, path resolutiontest_account_market_services.py— balance/position/ticker/klines logic with mocked clientstest_trade_service.py— spot trade execution pathstest_opportunity_service.py— portfolio and scan scoring logictest_opportunity_dataset_service.py— dataset collection and walk-forward evaluationtest_opportunity_evaluation_service.py— model weight optimizationtest_strategy_service.py— combined signal generation logictest_backtest_service.py— historical backtest engine
Notes
AGENTS.mdin this repo is stale and describes a prior V1 architecture (commands/, smart executor, precheck, review engine). Do not rely on it.