CoolFace
Apppublic

Sumitx369/Paper_Trading_Backtesting

sourceHugging Faceupdated 4mo agoView on Hugging Face
2likes
App README

๐Ÿ“ˆ Paper Trading Backtester (Indian stocks)

A paper-trading / strategy-backtesting platform for Indian equities (NSE/BSE). Users define a trading strategy two ways โ€” by editing a small code template or with a no-code condition builder โ€” and a fast C++ engine backtests it against historical data pulled from yfinance.

Architecture

Browser (Plotly UI)
        โ”‚  JWT
        โ–ผ
FastAPI  โ”€โ”€โ–บ  yfinance + cache (parquet + in-memory TTL)
   โ”‚
   โ”‚  buy/sell expressions  โ”€โ–บ  DSL compiler  โ”€โ–บ  bytecode + indicator specs
   โ–ผ
C++ engine (pybind11)  โ”€โ–บ  indicators + expression VM + portfolio sim
   โ”‚
   โ–ผ
metrics (return, Sharpe, max-DD, win-rate, vs Buy&Hold) + equity/trades
  • โ€”API: FastAPI (JWT auth, SQLite via SQLAlchemy).
  • โ€”Strategy input compiles to a single JSON expression spec โ†’ bytecode. No user code is ever executed โ€” code mode is parsed with Python's ast over a strict whitelist (c.rsi(14), c.close, c.crossover(...), โ€ฆ).
  • โ€”Engine: C++ (built with pybind11, no CMake). Computes SMA/EMA/RSI/MACD/Bollinger/ATR, evaluates buy/sell conditions per bar, simulates a long-only portfolio with fees+slippage.
  • โ€”Market: Indian stocks. NSE tickers RELIANCE.NS, BSE โ€ฆโ€‹.BO; values in โ‚น. Lookback capped at 1 year; intraday intervals are validated against yfinance limits.

Run locally (conda)

bash
# one-time: build the C++ engine + install deps into the env
conda run -n trading_testing pip install -r requirements.txt
conda run -n trading_testing pip install .            # builds `trading_engine`

# run the API + UI
conda run -n trading_testing uvicorn app.main:app --reload --port 8000
# open http://localhost:8000

Tests:

bash
conda run -n trading_testing pytest -q

Run with Docker

bash
docker build -t trading-backtester .
docker run -p 7860:7860 -e JWT_SECRET=$(openssl rand -hex 32) trading-backtester
# open http://localhost:7860

Deploy (Hugging Face Spaces)

Create a Docker Space, push this repo, and set a JWT_SECRET secret. The YAML header at the top of this README configures the Space (port 7860). Note: free-tier storage is ephemeral โ€” the SQLite DB resets on rebuild. Swap DATABASE_URL to a free Postgres (e.g. Neon) for durable accounts.

API (overview)

MethodPathPurpose
POST/auth/register, /auth/loginget a JWT
GET/market/symbolspopular NSE tickers
GET/market/intervalsvalid interval ร— lookback combos
GET/market/previewOHLCV preview (cached)
GET/dsl/catalog, /dsl/templateindicators + code template
POST/backtest/runrun a backtest (code or no-code spec)
GET/POST/strategiessave / list strategies (auth)