A production-grade, TypeScript-based automated grid trading bot for Gate.io Spot markets. It deploys a grid of limit buy and sell orders within a defined price range, automatically rebalances as orders fill, and uses an AI signal engine to optimize grid bounds, strategy selection, and market timing.
- AI-Powered Optimization — Multi-indicator ensemble (RSI, MACD, EMA, Bollinger Bands, SMA) for range and strategy recommendations
- Dual Grid Strategies — Arithmetic (equal price steps) and Geometric (equal percentage steps), plus
automode where AI picks the best fit - AI Auto-Range — Optional dynamic grid bounds based on live volatility and trend analysis
- Paper Trading Mode — Full simulation engine for risk-free testing
- Live Trading — Direct integration with Gate.io Spot API v4 (HMAC-SHA512 auth)
- State Persistence — Automatic save/resume across restarts
- Graceful Shutdown — Cancels open orders and persists state on SIGINT/SIGTERM
- Retry Logic — Exponential backoff for API resilience
- Structured Logging — Pino-based logging with configurable levels
- Type-Safe Config — Zod schema validation for all environment variables
Grid trading places buy orders below the current price and sell orders above it. When a buy fills, a sell is placed one grid level higher. When that sell fills, a new buy is placed one level lower. Each completed buy→sell cycle captures the spread between grid levels as profit.
Price
▲
│ ── SELL ── SELL ── SELL ── SELL ← Upper bound
│ │ │ │
│ ── BUY ── BUY ── BUY ── BUY ← Lower bound
└──────────────────────────────────▶ Time
Arithmetic Grid — Fixed price steps. Best for stable, range-bound assets.
Geometric Grid — Fixed percentage steps. Best for volatile assets.
AI Auto — Analyzes volatility and trend to recommend arithmetic vs geometric and optional price bounds.
gateio-ai-grid-trading-bot/
├── src/
│ ├── ai/ # Signal engine, indicators, grid advisor
│ ├── cli/ # CLI entry (start, preview, analyze, validate)
│ ├── config/ # Zod schema and env loader
│ ├── core/ # Grid engine — order lifecycle
│ ├── exchange/ # Gate.io API client + paper simulator
│ ├── services/ # Bot orchestration and state persistence
│ ├── strategies/ # Arithmetic and geometric grid strategies
│ ├── types/ # Shared TypeScript interfaces
│ └── utils/ # Logger, retry, formatting helpers
├── tests/ # Unit tests (Vitest)
├── .env.example # Environment variable template
├── package.json
└── tsconfig.json
- Node.js 18 or higher
- npm 9 or higher
- Gate.io account with API key (for live trading only)
cd gateio-ai-grid-trading-bot
npm install
cp .env.example .envEdit .env with your configuration:
GATE_API_KEY=your_api_key_here
GATE_API_SECRET=your_api_secret_here
SYMBOL=BTC_USDT
GRID_STRATEGY=auto
UPPER_PRICE=70000
LOWER_PRICE=60000
GRID_COUNT=20
INVESTMENT=1000
PAPER_TRADING=true
AI_ENABLED=truenpm run dev -- validatenpm run dev -- previewnpm run dev -- analyzenpm run dev -- startSet PAPER_TRADING=false in .env, then:
npm run build
npm start -- start| Variable | Description | Default |
|---|---|---|
GATE_API_KEY |
Gate.io API key | — |
GATE_API_SECRET |
Gate.io API secret | — |
SYMBOL |
Trading pair (BTC_USDT or BTCUSDT) |
BTC_USDT |
GRID_STRATEGY |
arithmetic, geometric, or auto |
geometric |
UPPER_PRICE |
Top of grid price range | — |
LOWER_PRICE |
Bottom of grid price range | — |
GRID_COUNT |
Number of grid levels (min 2) | — |
INVESTMENT |
Total quote asset to deploy (USDT) | — |
PAPER_TRADING |
true for simulation, false for live |
true |
POLL_INTERVAL_MS |
Order status check interval (ms) | 5000 |
AI_ENABLED |
Enable AI signal engine | true |
AI_AUTO_RANGE |
AI sets grid bounds on startup | false |
MIN_AI_CONFIDENCE |
Minimum confidence (0–1) for AI actions | 0.35 |
CANDLE_INTERVAL |
Candle interval for AI (1h, etc.) |
1h |
AI_REBALANCE_INTERVAL_MS |
AI re-evaluation interval (0 = startup only) | 300000 |
LOG_LEVEL |
trace / debug / info / warn / error | info |
| Command | Description |
|---|---|
start |
Start the grid trading bot |
preview |
Preview grid levels (with AI hints if enabled) |
analyze |
Run AI market analysis without trading |
validate |
Validate .env configuration |
npm test
npm run typecheck
npm run lint
npm run dev -- start| Market Condition | Recommended | Grid Count | Range |
|---|---|---|---|
| Sideways / ranging | auto or geometric |
15–30 | ±5–10% from price |
| Low volatility | arithmetic | 10–20 | Tight range |
| High volatility | geometric | 20–50 | Wider range |
Enable AI_AUTO_RANGE=true to let the bot tune bounds from live Gate.io candle data.
Cryptocurrency trading involves substantial risk of loss. This bot is provided as-is with no guarantees of profitability. Always test thoroughly in paper trading mode before deploying real funds. Never invest more than you can afford to lose. The authors are not responsible for any financial losses incurred through use of this software.
MIT
👉 Telegram: @tradingtermin
Need help with setup, configuration, or troubleshooting?
Contact @tradingtermin on Telegram for:
| 🔧 | Installation and configuration help |
| 📊 | Strategy tuning and AI grid optimization |
| 🐛 | Bug reports and feature requests |
| 🚀 | Live trading setup on Gate.io |