forked from garagesteve1155/PowerTrader_AI
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpt_trainer.py
More file actions
125 lines (102 loc) · 4.07 KB
/
Copy pathpt_trainer.py
File metadata and controls
125 lines (102 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""PowerTrader Trainer — backward-compatible entry point.
This thin wrapper delegates to the new modular ``powertrader`` package.
It preserves the original CLI interface so existing Hub configurations
and coin-subfolder copies continue to work identically.
The original monolithic script is archived in ``legacy/pt_trainer.py``.
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
def _find_project_root() -> Path | None:
"""Walk upward from this file to find the project root (contains src/powertrader/)."""
d = Path(__file__).resolve().parent
for _ in range(5):
if (d / "src" / "powertrader").is_dir():
return d
d = d.parent
return None
def _ensure_importable() -> None:
"""Add src/ to sys.path if powertrader is not installed as a package."""
try:
import powertrader # noqa: F401
return
except ImportError:
pass
root = _find_project_root()
if root is not None:
src = str(root / "src")
if src not in sys.path:
sys.path.insert(0, src)
def _parse_args() -> argparse.Namespace:
"""Parse CLI arguments with backward-compatible positional support."""
parser = argparse.ArgumentParser(
description="PowerTrader Trainer — train prediction models per coin.",
epilog="Examples:\n"
" python pt_trainer.py # Train BTC (default)\n"
" python pt_trainer.py BTC # Train a specific coin\n"
" python pt_trainer.py ETH reprocess_yes # Retrain with full reprocessing\n"
" python pt_trainer.py --coin ETH --reprocess",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"coin_positional", nargs="?", default=None,
help="Coin to train (positional, e.g. BTC). Default: BTC",
)
parser.add_argument(
"reprocess_positional", nargs="?", default=None,
help="Legacy flag: reprocess_yes or reprocess_no",
)
parser.add_argument(
"--coin", default=None,
help="Coin to train (named arg). Overrides positional.",
)
parser.add_argument(
"--reprocess", action="store_true", default=False,
help="Full reprocessing of historical data.",
)
args = parser.parse_args()
# Resolve coin: --coin flag takes priority, then positional, then default
if args.coin:
coin = args.coin.strip().upper()
elif args.coin_positional:
coin = args.coin_positional.strip().upper()
else:
coin = "BTC"
# Resolve reprocess: --reprocess flag OR legacy positional
reprocess = args.reprocess
if args.reprocess_positional:
if args.reprocess_positional.lower() in ("reprocess_yes", "reprocess"):
reprocess = True
args.resolved_coin = coin
args.resolved_reprocess = reprocess
return args
def main() -> None:
_ensure_importable()
from powertrader.core.config import TradingConfig
from powertrader.core.constants import SETTINGS_FILENAME
from powertrader.core.logging_setup import setup_logger
from powertrader.core.market_client import KuCoinMarketClient
from powertrader.core.storage import FileStore
from powertrader.trainer.runner import TrainerRunner
args = _parse_args()
project_root = _find_project_root() or Path.cwd()
setup_logger("trainer", project_root / "logs")
setup_logger("powertrader", project_root / "logs")
settings_path = project_root / SETTINGS_FILENAME
if settings_path.is_file():
config = TradingConfig.from_file(settings_path)
else:
config = TradingConfig()
market = KuCoinMarketClient()
store = FileStore()
runner = TrainerRunner(
market=market,
config=config,
store=store,
base_dir=project_root,
)
runner.run(coins=[args.resolved_coin], reprocess=args.resolved_reprocess)
if __name__ == "__main__":
main()