-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
166 lines (142 loc) · 5.26 KB
/
Copy pathrun.py
File metadata and controls
166 lines (142 loc) · 5.26 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
stocker - Unified CLI and Web App Runner
Usage:
python run.py # Launch web app dashboard & open in browser
python run.py --mode ui # Launch web app dashboard explicitly
python run.py --mode live --ticker INFY.NS # Run CLI live prediction for a stock
python run.py --mode fast --ticker RELIANCE.NS # Run fast backtest
python run.py --mode full --ticker TCS.NS # Run full 5-fold backtest
"""
import argparse
import sys
import os
import time
import webbrowser
import threading
from pathlib import Path
# Ensure root directory is on Python path
ROOT = Path(__file__).resolve().parent
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
def open_browser_delayed(url: str, delay_seconds: float = 1.2):
def _open():
time.sleep(delay_seconds)
try:
webbrowser.open(url)
except Exception:
pass
t = threading.Thread(target=_open, daemon=True)
t.start()
def start_ui(host: str = "127.0.0.1", port: int = 8000, reload: bool = True, open_browser: bool = True):
import uvicorn
url = f"http://{host}:{port}/app"
print("\n" + "=" * 60)
print(" 🚀 Stocker - Regime-Aware Stock Prediction System")
print("=" * 60)
print(f" • Web UI Dashboard: {url}")
print(f" • Landing Page: http://{host}:{port}/")
print(f" • API Docs (Swagger): http://{host}:{port}/docs")
print(f" • Server running on: http://{host}:{port}")
print("=" * 60 + "\n")
if open_browser:
open_browser_delayed(url, delay_seconds=1.2)
uvicorn.run("ui.app:app", host=host, port=port, reload=reload)
def run_live_cli(ticker: str, force_refresh: bool = False):
from backtest.predictor import StockerPredictor
from utils.config import DATA_SOURCE, UPSTOX_ACCESS_TOKEN
print(f"\n[stocker] Running live prediction for {ticker}...")
predictor = StockerPredictor(
ticker,
data_source=DATA_SOURCE,
access_token=UPSTOX_ACCESS_TOKEN,
)
predictor.fit(force_refresh=force_refresh)
result = predictor.predict()
print("\n" + "=" * 55)
print(f" PREDICTION REPORT: {ticker}")
print("=" * 55)
print(f" Current Live Price: INR {result.get('current_price', 0):,.2f} ({result.get('current_price_source', 'N/A')})")
print(f" Detected Regime: {result.get('regime', 'N/A')}")
print(f" Strategy Paradigm: {result.get('paradigm', 'N/A')}")
print(f" ML Specialist: {result.get('ml_model', 'N/A')}")
print(f" Signal: {result.get('signal', 'N/A').upper()}")
if result.get("paradigm") == "regression":
ret = result.get("predicted_return_pct", 0)
target = result.get("predicted_price", 0)
print(f" Expected Return: {'+' if ret > 0 else ''}{ret:.3f}%")
print(f" Target Price: INR {target:,.2f}")
else:
print(f" Direction: {result.get('prediction', 'N/A')}")
print(f" Model Confidence: {result.get('confidence', 'N/A')}%")
print("=" * 55 + "\n")
def run_backtest_cli(ticker: str, mode: str = "fast", verbose: bool = True):
from backtest.engine import run_backtest
from utils.config import DATA_SOURCE, UPSTOX_ACCESS_TOKEN
print(f"\n[stocker] Running {mode} backtest for {ticker}...")
run_backtest(
ticker=ticker,
verbose=verbose,
profile=mode,
data_source=DATA_SOURCE,
access_token=UPSTOX_ACCESS_TOKEN,
)
def main():
parser = argparse.ArgumentParser(
description="stocker — Regime-Aware Adaptive Stock Prediction",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--mode",
choices=["ui", "live", "fast", "full"],
default="ui",
help="Operating mode:\n"
" ui - Launch Web UI Dashboard (default)\n"
" live - Run live prediction in terminal\n"
" fast - Run fast single-fold backtest\n"
" full - Run full 5-fold backtest",
)
parser.add_argument(
"--ticker",
default="RELIANCE.NS",
help="NSE ticker symbol (e.g. RELIANCE.NS, TCS.NS, INFY.NS)",
)
parser.add_argument(
"--host",
default="127.0.0.1",
help="Host to bind UI server to (default: 127.0.0.1)",
)
parser.add_argument(
"--port",
type=int,
default=8000,
help="Port to bind UI server to (default: 8000)",
)
parser.add_argument(
"--no-browser",
action="store_true",
help="Do not automatically open browser on startup",
)
parser.add_argument(
"--no-reload",
action="store_true",
help="Disable uvicorn auto-reload",
)
parser.add_argument(
"--refresh",
action="store_true",
help="Force download fresh market data from online sources",
)
args = parser.parse_args()
if args.mode == "ui":
start_ui(
host=args.host,
port=args.port,
reload=not args.no_reload,
open_browser=not args.no_browser,
)
elif args.mode == "live":
run_live_cli(ticker=args.ticker, force_refresh=args.refresh)
elif args.mode in ("fast", "full"):
run_backtest_cli(ticker=args.ticker, mode=args.mode)
if __name__ == "__main__":
main()