Stockbroker Assistant
An explainable quant assistant that forecasts prices, learns buy/sell signals, optimizes a portfolio, simulates trading strategies and stress-tests risk, then explains every recommendation in plain English. Built in Python with a Gradio interface.
- Python
- Pandas
- NumPy
- scikit-learn
- PyTorch
- LightGBM
- SciPy
- Plotly
- Gradio
- Hugging Face
Stockbroker Assistant takes a list of tickers and runs them through a full quantitative pipeline: it pulls market data, engineers features, forecasts prices, learns directional signals, builds a portfolio, simulates trading, stress-tests the result, and turns it all into BUY / HOLD / SELL advice with the reasoning attached.
Most "AI trading" demos are UI-first and opaque. This one is math-first: every block is a small, separately testable component, and every recommendation can be traced back to the numbers behind it (the forecasts, feature importances, allocation rationale and scenario impacts).
For education only. It isn't investment advice.
The pipeline
- Data. Pulls daily prices and volume with
yfinance, aligns dates across tickers, fills small gaps, and computes daily returns. Downloads are cached for the session. - Features. RSI, MACD, moving averages (5/20/50), Bollinger Bands, rate of change, short- and long-term volatility, volume ratio, correlation with the market, and market-regime flags, all aligned so nothing leaks from the future.
- Forecasts (next 5 days). Five methods: a trend model whose slope is weighted by how well it fits, mean reversion, double exponential smoothing, moving-average momentum, and ARIMA. They're blended into one ensemble path with a volatility-scaled confidence band.
- ML signals. Random Forest models predict next-day price and direction, alongside a neural network (LSTM with attention) and LightGBM. Validation uses time-ordered splits, and the output includes accuracy, error, feature importances and class probabilities that set how confident each signal is.
- Portfolio optimization. Mean-variance optimization with weight limits and diversification constraints, falling back to Sharpe-ratio maximization and then risk parity. It returns target weights, share counts, expected return, volatility, Sharpe ratio and concentration, plus a plain-English reason for each position.
- Trading simulation. A reinforcement-learning-style sandbox runs conservative, moderate and aggressive strategies with take-profits, stop-losses and holding periods, and reports trades, win rate, returns and a confidence timeline.
- Stress tests. A 20% crash, a 10% correction, a 3× volatility spike, a 15% rally and stagflation. Each scenario recomputes return, risk, Sharpe and value-at-risk, and is rated for severity.
- Advice. Combines the forecasts, ML probabilities, simulated trades and stress results into per-ticker recommendations with confidence and rationale, plus a portfolio summary covering expected return, risk, diversification and resilience.
The interface
A six-tab Gradio app: candlestick charts with technical indicators, the forecast with its confidence band, the portfolio breakdown with a risk-return chart, the trading simulation with buy/sell markers and running profit, stress-test charts, and the final advice. Charts are built with Plotly.
Built to degrade gracefully
Several heavy dependencies are optional. If ARIMA, the convex optimizer or LightGBM isn't available, each step falls back to a simpler method instead of failing, so the app always produces a full report.