# BlocConnect — the Python forward-test that matches live by construction

Write a strategy once. Run it three ways — **backtest**, **forward** (live data,
paper account), **live** — through *one* execution engine, so what you see in
the forward test is what you get in production. No backtest-vs-live divergence,
because there is only one engine.

```python
from blocconnect import Strategy, backtest, forward, sma, lowest, highest

class Donchian(Strategy):
    symbol = "BTCUSDT"; interval = "5m"; warmup = 60; risk_pct = 1.0
    def on_bar(self, bars):
        if self.position is None and bars[-1].c > highest(bars[:-1], 50):
            self.long(sl_price=lowest(bars[-20:], 20),
                      tp_rules=[{"r":1.5,"pct":50},{"r":3.0,"pct":50}],
                      be_at_r=1.0, trail_r=2.0)
        elif self.position == "long" and bars[-1].c < lowest(bars[:-1], 20):
            self.close()

backtest(Donchian, csv="btc_5m.csv").report("report.html")          # history
forward(Donchian, hook="https://mexc.blocconnect.com/hook/TOKEN")    # live -> paper
```

## Why it's different

- **One engine, three modes.** Backtest, forward and live share the same fill
  logic — risk-based sizing, taker fees charged on entry *and* exit, 2 bps
  slippage, resting stops/targets that fill *at* the trigger. Forward = live.
- **Statistical honesty.** Every run reports the **Deflated Sharpe Ratio**
  (corrects an apparently-good Sharpe for short samples, fat tails, and how many
  strategies you tried first), bootstrap confidence intervals, and a Monte-Carlo
  distribution of the drawdown you should actually expect.
- **Retirement conditions.** Declare your hypothesis up front; the engine tells
  you — with statistical backing — the moment the edge stops holding.
- **Deterministic record & replay.** Every bar a forward run sees is recorded;
  `replay()` reproduces it bit-for-bit, for audit and re-testing on real data.
- **Live HTML dashboard.** The forward run writes a self-contained report that
  refreshes itself — equity curve, R-distribution, ledger, verdict.
- **Zero dependencies.** Pure standard library. `pip install websocket-client`
  only if you want millisecond bar detection (otherwise it polls).

## Install

```bash
pip install https://app.blocconnect.com/static/sdk/blocconnect-2.0.0-py3-none-any.whl
pip install websocket-client          # optional: real-time feed
```
Or drop the `blocconnect/` folder next to your script.

## The modes

```python
from blocconnect import backtest, forward, live, replay, Hypothesis

hyp = Hypothesis(min_sharpe=1.0, max_drawdown=-0.20,
                 min_profit_factor=1.3, min_deflated_sharpe=0.95,
                 min_trades=30, trials_considered=20)

res = backtest(Donchian, csv="btc_5m.csv", trials=20, hypothesis=hyp)
res.print_summary(); res.report("backtest.html")

forward(Donchian, hook="https://mexc.blocconnect.com/hook/TOKEN",
        record="session.rec", hypothesis=hyp)        # live data, paper account

replay("session.rec", strategy_cls=Donchian).report("replay.html")

live(Donchian, hook="https://mexc.blocconnect.com/hook/LIVE-TOKEN", confirm=True)
```

Forward testing is part of **The Strategist**. Your keys and code stay on your
machine — only signals travel; sizing, stops and risk run server-side, paper or
live, identically.
