Technology Aug 23, 2026 · 5 min read

How I built an FVG trading bot for OKX and made 99% of its signals useless on purpose

How I built an FVG trading bot for OKX and made 99% of its signals useless on purpose If you trade crypto futures, you know the drill. You're staring at the chart at 3am because you're scared to blink and miss "the perfect entry". Or worse, you get in emotionally, chase a pump, and hand b...

DE
DEV Community
by Xbs950812

How I built an FVG trading bot for OKX and made 99% of its signals useless on purpose

If you trade crypto futures, you know the drill. You're staring at the chart at 3am because you're scared to blink and miss "the perfect entry". Or worse, you get in emotionally, chase a pump, and hand back all your profit in one bad night.

I got liquidated once because my stop was at -5% and the liquidation price was at -2%. Price gapped straight through my stop. That's how this project started.

I built FVG Killer, a bot that trades one setup only: the ICT Fair Value Gap, on OKX perpetuals. The repo is open-source: https://github.com/Xbs950812/okx_fvg_agent

1. What it trades

FVG stands for Fair Value Gap, from the ICT (Inner Circle Trader) framework.

The idea is simple: one violent candle moves price fast and leaves a "vacuum" where almost nobody got filled. The theory says market makers rebalance and price tends to come back and fill at least half of that vacuum. So the bot waits for price to retrace into the gap, enters, takes profit at the 50% level (consequent encroachment in ICT-speak), and stops out outside the gap.

Detection pipeline:

  • Pre-filter: at least a 3-sigma move and 5x volume expansion

  • Three-candle gap detection, scanning 1H and 4H

  • It tracks the top 100 contracts around the clock, even when it holds nothing

2. The part nobody tells you: saying no

Textbooks show you three candles and call it a day. Reality: a naive detector spits out dozens of signals a day and 99% of them are garbage. I built five gates to reject them. Each has a real log line from production:

  • Freshness: gap older than ~100 candles? Drop it. [Freshness] SNXX 1H FVG 186 candles old > 24, drop

  • ATR grade: gap width less than 0.5x ATR is a weak setup. [ATRGrade] width 0.16/ATR 0.37 = 0.43 < 0.5, weak C-grade

  • Direction: don't long a coin that just pumped 14%, don't short one that dumped. [MoverDir] ETHFI 4H long rejected: +14.4% in 24h

  • Depth: if the resting order is 6% off price, you're catching a falling knife. [DepthGate] 6.06% deviation > 5.0%, fall back

  • Liquidity: notional vs top-10 book depth over 5%? Refuse the trade.

A trading system's real product isn't the entry logic. It's the rejection logic. That sentence took me months to actually believe.

3. Position sizing, and why I don't trust you to believe me

Signals don't matter if one drawdown wipes you out. So I use rolling fractional Kelly over the last 100 realized trades, EWMA-smoothed (lambda 0.97). Under 50 samples it runs 1/4 Kelly, over 50 it moves to 1/2.

And instead of asking anyone to trust the formula, I shipped the validation. The Monte Carlo is 300 paired paths x 1,000 trades, so both sizing rules see identical trade sequences:

  • Edge decays (0.5 -> 0.4 win rate): rolling Kelly median drawdown 83.1%, fixed 30% sizing wipes out 100% of accounts

  • Edge disappears (0.5 -> 0.25): rolling Kelly survives 27 doublings, fixed sizing goes to 2^-21

  • Edge stable: rolling Kelly grows about 20% slower. That's the price of not dying.

I know "100% wipeout" sounds dramatic, but that's literally what the numbers say when the edge decays.

4. Six analyst agents argue before every trade

Every signal goes through a debate first, inspired by the TradingAgents repo (86k stars). Six analyst agents do an independent read, then cross-examine each other, then reach a consensus. Each analyst has a reputation score that goes up and down. They've talked me out of trades that would have been disasters.

On top of that: regime detection with a state machine, a 461-factor alpha library (Alpha101, GTJA191, Qlib158, academic factors), and online learning for signal quality prediction.

5. The engineering stuff

In trading, engineering is money:

  • 197 unit tests, all passing

  • Paper/dry-run mode that literally cannot place a real order

  • Global API rate limiter (10 QPS) so the exchange doesn't throttle us

  • Three circuit breakers: daily loss limit, daily trade cap, drawdown breaker

  • Startup 3-way reconciliation: exchange positions vs local state vs protective orders

  • Atomic state writes, so a power cut can't corrupt the state file

6. The licensing, said plainly

This is source-available (PolyForm Shield + author terms), not traditional open source. No subscription, no course, no license fee. Paper mode and backtests are free forever. On live trades I take 10% of profit only when the trade is green, auto-withdrawn on-chain once it hits 20 USDT. If you lose money, I make nothing. Removing the royalty module needs a commercial license.

The logic is simple: I only earn if you earn first. And if that's not for you, the README documents the detection, gates, Kelly and Monte Carlo so thoroughly that you can take the ideas for free.

7. Quick start

git clone https://github.com/Xbs950812/okx_fvg_agent.git
cd okx_fvg_agent
pip install -r requirements.txt
cp config.example.json config.json  # paste your OKX API keys
python agent.py  # ALWAYS start in paper mode first!

One more thing

Perpetual futures can destroy your whole account.

DE
Source

This article was originally published by DEV Community and written by Xbs950812.

Read original article on DEV Community
Back to Discover

Reading List