Sumitx369/Paper_Trading_Backtesting
2
1---2title: Paper Trading Backtester India3emoji: ๐4colorFrom: indigo5colorTo: green6sdk: docker7app_port: 78608pinned: false9---10 11# ๐ Paper Trading Backtester (Indian stocks)12 13A paper-trading / strategy-backtesting platform for **Indian equities (NSE/BSE)**.14Users define a trading strategy two ways โ by editing a small **code template** or15with a **no-code condition builder** โ and a fast **C++ engine** backtests it against16historical data pulled from `yfinance`.17 18## Architecture19 20```21Browser (Plotly UI)22 โ JWT23 โผ24FastAPI โโโบ yfinance + cache (parquet + in-memory TTL)25 โ26 โ buy/sell expressions โโบ DSL compiler โโบ bytecode + indicator specs27 โผ28C++ engine (pybind11) โโบ indicators + expression VM + portfolio sim29 โ30 โผ31metrics (return, Sharpe, max-DD, win-rate, vs Buy&Hold) + equity/trades32```33 34- **API**: FastAPI (JWT auth, SQLite via SQLAlchemy).35- **Strategy input** compiles to a single JSON expression spec โ bytecode. *No user36 code is ever executed* โ code mode is parsed with Python's `ast` over a strict37 whitelist (`c.rsi(14)`, `c.close`, `c.crossover(...)`, โฆ).38- **Engine**: C++ (built with pybind11, no CMake). Computes SMA/EMA/RSI/MACD/Bollinger/ATR,39 evaluates buy/sell conditions per bar, simulates a long-only portfolio with fees+slippage.40- **Market**: Indian stocks. NSE tickers `RELIANCE.NS`, BSE `โฆโ.BO`; values in โน.41 Lookback capped at **1 year**; intraday intervals are validated against yfinance limits.42 43## Run locally (conda)44 45```bash46# one-time: build the C++ engine + install deps into the env47conda run -n trading_testing pip install -r requirements.txt48conda run -n trading_testing pip install . # builds `trading_engine`49 50# run the API + UI51conda run -n trading_testing uvicorn app.main:app --reload --port 800052# open http://localhost:800053```54 55Tests:56 57```bash58conda run -n trading_testing pytest -q59```60 61## Run with Docker62 63```bash64docker build -t trading-backtester .65docker run -p 7860:7860 -e JWT_SECRET=$(openssl rand -hex 32) trading-backtester66# open http://localhost:786067```68 69## Deploy (Hugging Face Spaces)70 71Create a **Docker** Space, push this repo, and set a `JWT_SECRET` secret. The YAML header72at the top of this README configures the Space (port 7860). Note: free-tier storage is73ephemeral โ the SQLite DB resets on rebuild. Swap `DATABASE_URL` to a free Postgres74(e.g. Neon) for durable accounts.75 76## API (overview)77 78| Method | Path | Purpose |79|---|---|---|80| POST | `/auth/register`, `/auth/login` | get a JWT |81| GET | `/market/symbols` | popular NSE tickers |82| GET | `/market/intervals` | valid interval ร lookback combos |83| GET | `/market/preview` | OHLCV preview (cached) |84| GET | `/dsl/catalog`, `/dsl/template` | indicators + code template |85| POST | `/backtest/run` | run a backtest (code or no-code spec) |86| GET/POST | `/strategies` | save / list strategies (auth) |87 