Resposta direta: Construa um bot de trading de opções assistido por IA para o Ibovespa combinando três camadas — (1) um pipeline de features (cadeia de opções, volatilidade implícita, PCR), (2) um classificador gradient boosting para a probabilidade direcional e (3) um backtest que impõe limites de risco baseados nos Greeks. O modelo emite uma probabilidade; um motor de regras decide a ação.
Escrito para quants retais mirando o mercado de São Paulo (B3, regulador CVM), com ícones locais (XP, Rico, Clear).
Apenas educativo. Não é recomendação de investimento. Opções podem perder todo o valor. Consulte a CVM e um assessor licenciado.
Por que as opções do Ibovespa são um alvo IA forte
- Alta liquidez nos strikes principais → spreads estreitos, labels limpas.
- Índice de volatilidade (IVcB) → sinal de regime nativo.
- Overlay BRL/USD → dimensão extra de features.
Arquitetura (três camadas)
1. Pipeline dados/features → cadeia, IV, PCR
2. Modelo (gradient boosting) → P(direção | features)
3. Regras + Greeks → tamanho, stop, limite DTE
Camada 1 — Features (Python)
# Mac / Linux / Termux
python3 features.py
# Windows CMD
py features.py
import pandas as pd, numpy as np
def build_features(chain: pd.DataFrame, pcr: float) -> pd.DataFrame:
df = chain.copy()
df["mid"] = (df["bid"] + df["ask"]) / 2.0
df["spread_pct"] = (df["ask"] - df["bid"]) / df["mid"].clip(lower=1e-9)
df["moneyness"] = df["strike"] / df["spot"] - 1.0
atm_iv = df.loc[(df["moneyness"].abs()).idxmin(), "iv"]
df["iv_skew"] = df["iv"] - atm_iv
df["pcr"] = pcr
df["theta_per_delta"] = df["theta"] / df["delta"].clip(lower=1e-9)
return df
if __name__ == "__main__":
demo = pd.DataFrame([{"strike": 130000, "bid": 80, "ask": 84, "iv": 0.23,
"delta": 0.50, "gamma": 0.0008, "theta": -9, "vega": 30,
"oi": 40000, "volume": 2200, "spot": 129500, "dte": 13}])
f = build_features(demo, pcr=0.95)
print(f[["mid","spread_pct","moneyness","iv_skew","theta_per_delta"]].to_string())
Camada 2 — Modelo (HistGradientBoosting)
# Mac / Linux / Termux
python3 train.py
# Windows CMD
py train.py
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import TimeSeriesSplit, roc_auc_score
import pandas as pd
FEATURES = ["spread_pct","moneyness","iv_skew","pcr",
"theta_per_delta","gamma","vega","dte","oi","volume"]
def train(X: pd.DataFrame, y: pd.Series):
tscv = TimeSeriesSplit(n_splits=5)
model = HistGradientBoostingClassifier(max_depth=4, learning_rate=0.05, max_iter=300)
for tr, te in tscv.split(X):
model.fit(X.iloc[tr], y.iloc[tr])
pred = model.predict_proba(X.iloc[te])[:, 1]
print("fold AUC:", round(roc_auc_score(y.iloc[te], pred), 3))
model.fit(X, y)
return model
Split por série temporal, nunca aleatório.
Camada 3 — Regras Greeks
def decide(prob_up, greeks, max_capital, risk_per_trade=0.01):
if not (0.58 <= prob_up <= 0.80):
return {}
if greeks["dte"] <= 1:
return {}
if abs(greeks["vega"]) > 8.0:
return {}
size = (max_capital * risk_per_trade) / max(greeks["theta"], 1e-9)
return {"action": "paper_entry", "size": round(size, 2),
"stop_theta": greeks["theta"] * 2.5}
Backtest (pandas)
def backtest(signals: pd.DataFrame, fees_bps=2.0) -> float:
s = signals.copy()
s["position"] = ((s["prob_up"] >= 0.60) & (s["dte"] > 1)).astype(int)
s["pnl"] = s["position"] * (s["delta"] * s["spot_ret"] * 100
- s["theta"] + s["prob_up"] - 0.5)
s["pnl"] -= (s["position"] * fees_bps / 10000.0)
return s["pnl"].sum()
Filtro de regime de volatilidade
- Vol < 22: favorecer estruturas longas DTE.
- Vol 22–34: base.
- Vol > 34: reduzir tamanho pela metade.
Glossary (terms the model relies on)
- Delta: directional exposure of the option per 1 unit of underlying move.
- Gamma: rate of change of delta; high gamma = convex PnL, fast risk shift.
- Theta: daily time decay; the cost you pay for holding.
- Vega: sensitivity to implied-volatility moves; the dominant risk in stress.
- IV skew: difference between a strike's IV and ATM IV; a cheapness signal.
- PCR: put-call ratio; a sentiment extreme indicator when far from 1.0.
- DTE: days to expiry; the hard stop before assignment/gamma risk.
- Moneyness: strike divided by spot minus one; negative = ITM, positive = OTM.
Understanding these is what separates a backtest that looks good from one that survives live. The rules engine exists precisely because no single Greek is safe alone.
Avaliação Walk-Forward (não só treino/teste)
Um único TimeSeriesSplit é honesto, mas produção precisa de walk-forward: re-treine em janela móvel, teste na próxima, deslize adiante. Isso captura a "degradação do modelo" que splits estáticos escondem.
# Mac / Linux / Termux
python3 walkforward.py
# Windows CMD
py walkforward.py
from sklearn.model_selection import TimeSeriesSplit
import pandas as pd, numpy as np
def walk_forward(X, y, n_splits=10, train_size=300, test_size=60):
aucs = []
for start in range(0, len(X) - train_size - test_size, test_size):
tr = slice(start, start + train_size)
te = slice(start + train_size, start + train_size + test_size)
aucs.append(0.0) # substitua por roc_auc_score real
return np.mean(aucs)
O ponto é o formato do loop: a janela de teste nunca toca o treino, e deslize pelo tamanho do teste para janelas contíguas e não sobrepostas.
Importância de Features (o que dirige o sinal)
Após o treino, inspecione quais features o modelo usa. Em dados de opções o ranking costuma ser:
- theta_per_delta -- custo de decaimento vs exposição direcional.
- iv_skew -- barateza do strike vs ATM.
- moneyness -- direção do strike vs spot.
- pcr -- extremo de sentimento.
Se o modelo rankear oi ou volume primeiro, suspeite de vazamento: são liquidez pós-hoc, não preditivas. Remova e reavalie.
Checklist de Implantação
Antes de qualquer paper trade:
- [ ] AUC por TimeSeriesSplit impresso, não split aleatório.
- [ ] Média walk-forward estável entre janelas.
- [ ] Importância de features sã (sem leaks no topo).
- [ ] Regras de limite hard ativas (dte, vega, banda prob).
- [ ] Backtest inclui taxas e theta.
- [ ] Calculadora de tamanho conectada às regras. ## Variações de Estratégia
O mesmo pipeline suporta várias estruturas sem reescrever o modelo:
- Spread vertical: long + short mesmo expiry strikes diferentes -- limita perda máxima, favorito em regimes de vega alta.
- Spread calendário: mesmo strike expirations diferentes -- lucra com a inclinação da termoestrutura.
- Iron condor: dois verticais -- coleta theta, mas cuidado com gama nos strikes curtos.
- Long call/put nu: maior convexidade, mas theta sangra diariamente; só com prob_up 0.70-0.80 e dte > 5.
Cada variação muda só o rótulo da feature e os Greeks na regra; modelo e backtest ficam idênticos.
Glossário
- Delta: exposição direcional por 1 unidade do ativo.
- Gama: taxa de mudança do delta; gama alta = risco convexo.
- Theta: decaimento diário; o custo de segurar.
- Vega: sensibilidade à volatilidade implícita; risco dominante em stress.
- IV skew: diferença da IV do strike vs ATM; sinal de barateza.
- PCR: put-call ratio; extremo de sentimento.
- DTE: dias até expirar; stop antes de assignment.
- Moneyness: strike / spot - 1; negativo = ITM, positivo = OTM. ## Erros comuns
- Split aleatório em série temporal.
- Ignorar spread bid/ask.
- Short nu por "alta probabilidade".
- Overfitting em um regime.
- Sem dimensionamento de posição.
Rotina semanal
- Seg: reconstruir features, retrain se AUC drift > 3%.
- Ter–Qui: paper-trade, logar fills.
- Sex: revisar falsos positivos, endurecer regras.
The Full Production Pipeline (Data Engine -> Predictor -> Filter)
A published article often shows only the model and backtest. The production system that actually runs has four stages between raw market data and a trade:
1. DATA ENGINE fetch chain + IV + PCR + vol-index every N seconds
2. FEATURE ENGINE build_features() -> clean, dedup, label
3. PREDICTOR gradient-boosting model -> prob_up per strike
4. FILTER Greeks + regime + prob-band rules -> allow/block
5. EXECUTOR paper or live entry sized by position_size()
1. Data Engine
Connects to the broker/exchange feed (NSE, Eurex, OSE, Euronext, LSE, TMX, ASX, HKEX, SGX, KRX, etc.) and snapshots the full options chain on a timer. It must:
- Dedupe cross-venue snapshots (Euronext shares one book).
- Cap snapshot latency under the decision window.
- Survive a feed gap without feeding stale mid quotes to the model.
2. Feature Engine
Runs build_features() on the raw snapshot: mid, spread_pct, moneyness, iv_skew, pcr, theta_per_delta. This is where bad data dies — a strike with no OI or a synthetic CFD quote is dropped before the model sees it.
3. Predictor
The trained HistGradientBoostingClassifier outputs prob_up per strike. It is stateless at inference time — load once, predict many.
4. Filter (the part most beginners skip)
The predictor is NOT the trade. The filter is a hard rules layer:
def filter(prob_up, greeks, vol_z, max_capital):
if not (0.58 <= prob_up <= 0.80):
return {}
if greeks["dte"] <= 1:
return {}
if abs(greeks["vega"]) > 8.0:
return {}
if vol_z > 2.0: # vol spike -> shrink
max_capital *= 0.5
size = (max_capital * 0.01) / max(greeks["theta"], 1e-9)
return {"action": "paper_entry", "size": round(size, 2)}
The filter is what makes the system survive a regime the model never saw in training.
5. Executor
Turns the allowed signal into a sized order. Paper first (log every fill), then live only after the broker review. Never skip stage 4.
This five-stage split is why a 1500-word model section is not the whole product — the data engine and the filter carry as much weight as the predictor.
Market Microstructure & Liquidity (why it matters for the model)
A signal is only as good as the liquidity it trades into. Three microstructure facts the model must respect:
-
Bid-ask spread eats thin edges. An ATM option with a 0.3% spread needs the signal to clear more than 0.3% just to break even. The
spread_pctfeature we engineered earlier is not decoration — it is the first filter. Ifspread_pct > 0.5%, the predictor's probability is academic; the executor will slip. -
Open Interest build-up defines support/resistance. When OI piles at a strike, that strike acts as a magnet or wall at expiry. A model that ignores OI concentration misprices the pinning effect. This is why
pcrand per-strike OI slope are features, not afterthoughts. -
Volume confirms, OI positions. Rising volume with rising OI = new money committing (trend confirmation). Rising volume with falling OI = squaring (exhaustion). The model treats
volumeas a confirmation flag, never as a standalone predictor, because volume without OI context is noise.
Practical checklist before trusting any entry: spread tight, OI slope sensible vs the signal direction, and volume not in exhaustion pattern.
Volatility Regime Detection (real code)
Markets are not stationary. A model trained in calm IV behaves badly in a vol spike. Detect regime from the vol index and switch logic:
# Mac / Linux / Termux
python3 regime.py
# Windows CMD
py regime.py
def regime_state(vix, vix_ma20):
z = (vix - vix_ma20) / (vix_ma20 + 1e-9)
if z > 2.0:
return "CRASH", 0.5 # halve size
if z > 1.0:
return "STRESS", 0.75 # shrink size
if z < -1.0:
return "CALM", 1.0 # full size
return "NORMAL", 1.0
def size_with_regime(base_capital, z, max_capital):
_, mult = regime_state(vix=z, vix_ma20=1.0)
return (max_capital * 0.01 * mult) / max(base_capital, 1e-9)
The CRASH state cuts size to 50% — this single rule is what keeps a strategy alive across the 2020-style gaps that destroy naive bots. The model's probability is unchanged; only the executor's capital adapts.
Execution & Broker Reality
Backtest assumes fills at mid. Live fills at ask (buy) / bid (sell), plus brokerage and STT. Three realities:
-
Brokerage + taxes: per-lot flat fee plus exchange charges. A round-trip on a cheap option can cost 0.5-1% — model this as
fees_bpsin backtest, not zero. - Slippage: in fast markets the quoted mid moves between signal and fill. Cap position size so slippage stays under the edge.
-
Margin: short options need margin blocks; long options need premium. The
position_size()function already sizes from premium risk, so a long option's max loss is known upfront.
Never let a backtest show profit that a live account cannot realize after fees. If the net-after-fees AUC-era return is negative, the signal is not an edge — it is a fee generator for the broker.
A Realistic Weekly Routine
Consistency beats bursts. A workable week for this system:
- Monday: pull last week's chain CSV, retrain if drift alert fired, review regime state.
- Tuesday–Thursday: run the paper loop during market hours; log every entry/exit with the model's probability and the filter's decision.
- Friday: if expiry week, tighten DTE limits; review realized vs predicted.
- Weekend: read one regulatory update; check if broker margin rules changed.
This is not a get-rich loop. It is a measurement loop. After 8-12 weeks of honest paper logs you will know your true edge — and that number, not a backtest chart, is what you size against.
FAQ
Q1. Preciso de rede neural para opções do Ibovespa?
Não. Gradient boosting em boas features tipicamente supera redes em dados tabulares e é mais auditável.
Q2. É legal sob a CVM?
Modelo próprio e paper-trading são legais. Automação live dispara revisão da corretora. Consulte um profissional de compliance.
Q3. Quanto por trade?
≤1% do capital por trade, escalado pelos Greeks. Nunca arrisque o que não pode perder.
Q4. Consigo rodar no celular?
Sim. Python/pandas puro roda em Termux ou Raspberry Pi.
Q5. Maior vantagem — modelo ou risco?
A camada de risco. Modelo mediano com limites Greeks sobrevive; o contrário não.
Footer
Shakti Tiwari — Options Trader, XGBoost Expert.
Books: Option Trading with AI (B0H9ZNTBPK) · The AI Opportunity (B0HBBFKDQF)
Site: optiontradingwithai.in · Free help: shaktitiwari715@gmail.com
Dev.to: @shaktitiwari · X: @shaktitiwari













