https://github.com/yllvar/mean-reverse-bitget
Production standard Mean Reversion Trading on Bitget Written in Ocaml
https://github.com/yllvar/mean-reverse-bitget
Last synced: 27 days ago
JSON representation
Production standard Mean Reversion Trading on Bitget Written in Ocaml
- Host: GitHub
- URL: https://github.com/yllvar/mean-reverse-bitget
- Owner: yllvar
- Created: 2026-05-13T08:34:00.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2026-06-09T05:15:27.000Z (about 2 months ago)
- Last Synced: 2026-06-09T07:17:38.774Z (about 2 months ago)
- Language: OCaml
- Size: 51.8 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Mean Reverse Bitget
An OCaml mean reversion trading bot for the Bitget exchange (SOL/USDT spot). Built with Dune, Lwt, Cohttp, and Digestif.
## Architecture
```
lib/
├── auth.ml HMAC-SHA256 signing + header construction
├── client.ml Lwt-based HTTP GET/POST (cohttp-lwt-unix)
├── types.ml JSON deserialization (yojson) for all API responses
├── bitget.ml API surface: balance, ticker, orderbook, candles, place_order
├── strategy.ml Mean reversion signal: z-score ±1.5σ on rolling price window
└── trader.ml Continuous event loop with graceful shutdown (SIGINT)
bin/
└── main.ml Entry point: loads .env → initialises price window → runs loop
```
## Setup
Requires OCaml ≥ 4.14 with opam and dune.
```bash
git clone git@github.com:yllvar/mean-reverse-bitget.git
cd mean-reverse-bitget
cp .env.example .env # fill in your Bitget API credentials
dune exec bin/main.exe
```
## Configuration
Create a `.env` file (gitignored, never commit it):
```
BITGET_API_KEY=bg_your_key
BITGET_SECRET_KEY=your_secret
BITGET_PASSPHRASE=your_passphrase
```
## How It Works
1. Fetches 20 one-minute candles to build an initial price window
2. Polls the SOL/USDT ticker every 10 seconds
3. Maintains a rolling window of the last 20 close prices
4. Computes z-score: `(current_price − mean) / stddev`
5. **BUY** when z < −1.5 (price abnormally low)
6. **SELL** when z > +1.5 (price abnormally high)
7. **HOLD** otherwise
## Dependencies
| Library | Purpose |
|---|---|
| `cohttp-lwt-unix` | HTTP client |
| `yojson` | JSON parsing |
| `digestif` | HMAC-SHA256 |
| `base64` | Base64 encoding |
| `lwt` | Async concurrency |
| `tls-lwt` | TLS/HTTPS support |
## Development
```bash
dune build # compile
dune exec bin/main.exe # run
```