DEV Community

shakti tiwari
shakti tiwari

Posted on

How to Build Your Own Trading AI (Free, Local, Phone-Run) — Complete 2026 Guide

Build trading AI

By Shakti Tiwari — AI practitioner, markets analyst

Build guide. Not financial advice. Free help at optiontradingwithai.in.

Research note: stack verified via public tooling (Termux, Python, XGBoost, DuckDB, Hermes), 25 Jul 2026.

1. You Don't Need a Hedge Fund

A full trading AI runs on a phone (Termux + Python). Cost: $0. This guide covers the real engineering: gradient boosting, a SQL feature store, persistent memory, multi-agent orchestration, and — critically — pipeline leakage (the #1 reason backtests lie).

2. Architecture Overview

  • Data layer: public bars → DuckDB (SQL feature store)
  • Feature layer: SQL + Python transforms
  • Model layer: XGBoost (gradient boosting)
  • Memory layer: persistent JSON/DB (state across runs)
  • Agent layer: Hermes multi-agent (research + code + review)
  • Serving: cron + Telegram

3. Step 1 — Termux + Python

pkg update && pkg install python
pip install pandas numpy xgboost optuna duckdb sqlalchemy
Enter fullscreen mode Exit fullscreen mode

This is your server. No cloud bill.

4. Step 2 — DuckDB SQL Feature Store

DuckDB is an in-process SQL engine. Store bars once, query features with SQL:

import duckdb
con = duckdb.connect('market.duckdb')
con.execute("""
CREATE TABLE IF NOT EXISTS bars
(symbol TEXT, ts TIMESTAMP, open REAL, high REAL, low REAL, close REAL, vol REAL)
""")
# insert via COPY or executemany
# feature query
df = con.execute("""
SELECT symbol, ts, close,
  (close - LAG(close,20) OVER w) / LAG(close,20) OVER w AS ret_20,
  vol / AVG(vol) OVER (PARTITION BY symbol ORDER BY ts ROWS 19 PRECEDING) AS vol_ratio
FROM bars
WINDOW w AS (PARTITION BY symbol ORDER BY ts)
""").df()
Enter fullscreen mode Exit fullscreen mode

SQL keeps features reproducible and fast (columnar, no CSV mess).

5. Step 3 — Gradient Boosting (XGBoost)

Gradient boosting builds trees sequentially; each tree corrects the last. XGBoost adds regularization.

import xgboost as xgb
dtrain = xgb.DMatrix(df.drop(columns=['target']), label=df['target'])
params = {'objective':'reg:squarederror','max_depth':5,'eta':0.05,'subsample':0.8}
m = xgb.train(params, dtrain, num_boost_round=300)
Enter fullscreen mode Exit fullscreen mode

Why XGBoost over deep nets here: tabular, small data, interpretable, fast on phone.

6. Step 4 — Optuna Hyperparameter Tuning

import optuna
def obj(trial):
    p={'max_depth':trial.suggest_int('d',3,9),
       'eta':trial.suggest_loguniform('e',0.01,0.3),
       'subsample':trial.suggest_uniform('s',0.6,1.0)}
    # walk-forward inside
    return cv_score
study=optuna.create_study(direction='maximize')
study.optimize(obj, n_trials=120)
Enter fullscreen mode Exit fullscreen mode

Optuna finds the ridge between under/overfit.

7. Step 5 — Persistent Memory (State Across Runs)

Models decay. Keep state:

import json
mem = json.load(open('memory.json')) if os.path.exists('memory.json') else {}
mem['last_retrain'] = '2026-07-25'
mem['best_params'] = study.best_params
mem['equity_curve'] = [...]  # append live pnl
json.dump(mem, open('memory.json','w'))
Enter fullscreen mode Exit fullscreen mode

Persistent memory = the system remembers, not restart-blind.

8. Step 6 — Hermes Multi-Agent Orchestration

Hermes runs parallel agents:

  • Researcher agent: fetches news, summarizes via local LLM
  • Coder agent: writes feature code, tests
  • Reviewer agent: checks leakage, stats
  • Trader agent: combines signals, sizes

They work in isolation, report to a coordinator. This is how I ship 100+ projects — humans + agents beat solo.

# pseudo: Hermes spawns subagents
# each returns verified output, coordinator merges
Enter fullscreen mode Exit fullscreen mode

Advantage: reviewer catches what coder missed (e.g. leakage).

9. Step 7 — Walk-Forward (Anti-Leakage Core)

Bad: train 2020-23, test 2024 → lookahead leak if features use future.
Good: roll 3y train → 1m test, purge 5 days either side.

def walk_forward(df, train=36, test=1):
    scores=[]
    for end in range(train, len(df)-test):
        tr=df[end-train:end]; te=df[end:end+test]
        m=train_model(tr); scores.append(eval(m,te))
    return mean(scores)
Enter fullscreen mode Exit fullscreen mode

10. Pipeline Leakage — The Silent Killer

Leakage = model sees info it shouldn't. Types:

10.1 Lookahead Leak

Feature uses close[t+1]. Fix: shift targets, never future rows.

10.2 Train/Test Contamination

StandardScaler fit on full data → test stats leak. Fix: fit on train only, transform test.

10.3 Purge Gap

Near boundary, train sees test window. Fix: embargo 5-10 days.

10.4 Label Leak

Target derived from same bar you predict. Fix: predict t+1, label = next return.

10.5 Cross-Sectional Leak

Index mean includes the row. Fix: use OVER (PARTITION BY symbol) not global.

Hermes reviewer agent flags these automatically.

11. Step 8 — Inference + Telegram

feat = compute_today(con)  # SQL query
score = model.predict(feat)
if score > 0.5:
    send_telegram(f"LONG {score:.2f}")
Enter fullscreen mode Exit fullscreen mode

Cron runs 6pm daily. No human loop.

12. Free Help From Us

  • optiontradingwithai.in — free guides
  • Community Q&A
  • Open code patterns (this article)

No paywall. AI for everyone.

13. Full Pipeline Sketch

public bars
   ↓ (Python fetch)
DuckDB (SQL store)
   ↓ (SQL features)
XGBoost (Optuna-tuned)
   ↓ (walk-forward, purged)
Persistent memory (JSON)
   ↓ (Hermes agents review)
Telegram signal
Enter fullscreen mode Exit fullscreen mode

14. Risks

  • Overfit (always)
  • Regime change
  • Latency
  • Decay → weekly retrain

15. Start Plan

Wk1: Termux + DuckDB
Wk2: SQL features
Wk3: XGBoost + walk-forward
Wk4: Hermes agents
Mth2: live tiny

FAQ

Q: Coding needed?
A: Basic Python. Copy-paste works.

Q: Leakage scary?
A: Yes — section 10 is mandatory reading.

Q: Really free?
A: $0 stack.

Q: Advice?
A: Education only.

About

Shakti Tiwari, AI & markets analyst. Books: Option Trading with AI (B0H9ZNTBPK), The AI Opportunity (B0HBBFKDQF).

🌐 optiontradingwithai.in — Free help
📧 shaktitiwari715@gmail.com

🐦 X | ▶️ YouTube | 💼 LinkedIn | 💻 GitHub | 📝 Dev.to

Disclaimer: Not financial advice. Free help at optiontradingwithai.in.

Top comments (0)