Languages: English · 中文(繁體) · 中文 (简体) · 日本語 · 한국어 · Tiếng Việt · العربية · Français · Español
Micro Quant is less about shiny dashboards and more about a repeatable trading logic stack: it pulls OHLC data from MetaTrader 5, persists it into Postgres, and evaluates systematic decisions through layered AI-guided signals (Basic news, Tech snapshot, trade plans, and STL overlays). The UI reflects that philosophy—a unified hygiene of alignment toggles, reasoned closes, persisted prefs, and a data-rich execution pane—so the server can safely run periodic or modal trades while you watch the logs and evidence stack.
The static landing page (Quant by Lazying.art) lives under docs/ and is published through GitHub Pages (trade.lazying.art via docs/CNAME). The repo also includes guiding references for the AI Trade Plan prompts, integration notes, and docs for Micro Quant’s automated flows.
- Chain of truth: Basic news checks (text + scores) and Tech snapshots (heavy technical context + STL) feed a single AI trade plan per symbol/timeframe; both periodic auto runs and manual modals share this pipeline plus detailed reasoning logs.
- Alignment-first execution: Accept-Tech/Hold-Neutral toggles, ignore-basics switch, and partial-close wrappers ensure we only follow Tech when it matters, close opposites before new entries, and avoid unnecessary exits.
- Immutable data: Every fetch writes to Postgres with
ON CONFLICThygiene, while/api/datareads the sanitized series for the UI. Preferences (auto volumes,close_fraction, hide-tech toggles) persist via/api/preferencesso state survives refreshes. - Safety-first trading:
TRADING_ENABLEDandsafe_maxenforce manual/auto permissioning, while/api/closeand the periodic runner log closure reasons (tech neutral, misalignment, etc.) to keep records traceable.
- Fetch bars from MT5:
/api/fetch?symbol=XAUUSD&tf=H1&count=500 - Persist OHLC into Postgres (
ohlc_barstable) - Serve chart UI at
/using Chart.js (close price line)
- Ubuntu with MT5 installed via Wine (e.g.,
mt5linux.sh). Ensure the terminal is running and logged in to your demo or real account. - Python 3.10+
- PostgreSQL running locally; create
metatrader_dband userlachlan(or adjust).
# Windows (PowerShell)
# 1) Create venv with Python 3.11 (MetaTrader5 has no wheels for 3.13 yet)
py -3.11 -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install --upgrade pip
pip install -r requirements.txt
# 2) Configure env
Copy-Item .env.example .env
# Edit .env and set DATABASE_URL, MT5_PATH (e.g. C:\\Program Files\\MetaTrader 5\\terminal64.exe), and your MT5 demo creds
# Load env for this session
Get-Content .env | Where-Object { $_ -and $_ -notmatch '^#' } | ForEach-Object { $n,$v = $_ -split '=',2; [Environment]::SetEnvironmentVariable($n, $v, 'Process') }
# 3) Run app
python -m app.server
# Open http://localhost:8888
# Linux/mac (bash)
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# Alternative: local 3.11 venv (if your Conda env is 3.13)
# Requires python3.11 on your system
# sudo apt install python3.11 python3.11-venv
bash scripts/bootstrap_venv311.sh
source .venv311/bin/activate
# DB (use your own user/password as needed)
# createdb -h localhost -p 5432 -U lachlan metatrader_db
# Configure env (copy and edit)
cp .env.example .env
# then edit .env with your MT5 PATH and credentials
# export env vars into your shell
set -a; source .env; set +a
# Run app
python -m app.server
# Open http://localhost:8888Notes:
MT5_PATHshould point to yourterminal64.exeunder the Wine prefix used by the install script.- You can omit
MT5_LOGIN/MT5_PASSWORD/MT5_SERVERif your terminal is already running and logged in. If provided, the app will attemptmt5.login(). - Default DB env var is
DATABASE_URL. If not set, the app triesDATABASE_MT_URLthenDATABASE_QT_URL.
GET /api/fetch?symbol=XAUUSD&tf=H1&count=500[&mode=inc|full][&persist=1]- Fetch from MT5 and upsert to DB. If
persist=1the server also saveslast_symbol/last_tf/last_countas the new defaults; background/bulk fetches omit this to avoid overwriting your selection.
- Fetch from MT5 and upsert to DB. If
GET /api/data?symbol=XAUUSD&tf=H1&limit=500— read from DB for chartingGET /api/strategy/run?symbol=XAUUSD&tf=H1&fast=20&slow=50- Run SMA(20/50) crossover. If
TRADING_ENABLED=1the server will place a market order (very basic demo logic). The UI’s header “Auto” just schedules calls to this endpoint; actual order placement is still gated byTRADING_ENABLED.
- Run SMA(20/50) crossover. If
POST /api/trade— manual Buy/Sell from the UI (also gated byTRADING_ENABLED).POST /api/close— flatten positions (works even whenTRADING_ENABLED=0):- Current symbol: form body
symbol=...; optionalside=long|short|both(defaultboth). - All symbols:
?scope=alland optional&side=.... - Response contains
closed_countand aclosedarray with per-ticket results (retcodes).
- Current symbol: form body
GET /— full UI (candles, indicators, STL, news/AI panels, trading controls)
See sql/schema.sql. Primary key on (symbol, timeframe, ts) prevents duplicate bars; inserts use ON CONFLICT ... DO UPDATE.
- MT5 initialize failed: set
MT5_PATHto the exactterminal64.exeand ensure the terminal is running once manually. - Login failed: specify
MT5_SERVERexactly as shown in the MT5 terminal or omit and rely on the existing session. - No data for symbol: make sure the symbol exists with your broker and is visible in Market Watch; you can adjust the symbol (e.g.,
XAUUSD,XAUUSD.a,GOLDdepending on broker). - Postgres connection: ensure
DATABASE_URLis correct; test withpsql "$DATABASE_URL" -c 'select 1;'.
- Environment guard: export
TRADING_ENABLED=0(default) to disable any auto/place orders originating from/api/strategy/runand the manual Buy/Sell buttons. SetTRADING_ENABLED=1only for demo environments. - Header “Auto” button: re-runs the strategy periodically; it never bypasses
TRADING_ENABLED. - Close positions (always allowed): the UI has “Close Current” and “Close ALL” with a confirmation modal. Toggle which sides to close (Long/Short). The backend closes by ticket so it works for both netting and hedging accounts.
- STL auto-compute is now controlled per symbol×timeframe via the “Auto STL” switch in the STL panel. Default is OFF (helps avoid UI lag). When ON for a pair/TF, missing/stale STL runs auto-compute; otherwise use the Recalculate buttons.
- The state persists in the DB via
/api/preferencesusing keys likestl_auto_compute:SYMBOL:TFand also inlocalStoragefor fast startup.
- The server remembers
last_symbol/last_tf/last_countpreferences and injects them into the page. - The UI also writes
localStorage(last_symbol/last_tf)on selection so refreshes snap back immediately. ?reset=1on/ignores stored prefs for that load only.- To force a specific default at startup, set
PIN_DEFAULTS_TO_XAU_H1=1(optional; off by default).
- When you request an AI trade plan, the server ensures the latest Basic Health and Tech Snapshot runs exist for the current symbol/TF (creating them if missing) and then builds the prompt with: Basic + Tech + live Technical Snapshot.
- The UI supports both Line and Candlestick (via
chartjs-chart-financial). - For scheduled fetching, add a periodic callback in Tornado that calls
/api/fetchlogic.- Or set env on server:
AUTO_FETCH=1 AUTO_FETCH_SYMBOL=XAUUSD AUTO_FETCH_TF=H1 AUTO_FETCH_SEC=60 - For demo trading, export
TRADING_ENABLED=1 TRADING_VOLUME=0.1(use demo accounts only).
- Or set env on server:

