# MetaTrader 5 → NewsFlow Market Bridge

This is the fastest way to make **Market feed: not connected** disappear if you already have NAS100/NQ streaming in MetaTrader 5.

## What it does

`NewsFlow_MT5_Bridge.mq5` is an Expert Advisor that:

1. reads the terminal's complete tick history every second with `CopyTicksRange()`
2. keeps the tick timestamp, bid, ask, last and volume
3. uses MT5 `TICK_FLAG_BUY` / `TICK_FLAG_SELL` when the broker supplies real trade-side flags
4. otherwise uses a clearly labelled tick-rule approximation
5. POSTs batches to `api/ingest-trades.php`
6. sends a heartbeat to `api/bridge-heartbeat.php`

## Install

### 1. In MetaTrader 5

Open:

**File → Open Data Folder → MQL5 → Experts**

Copy:

`NewsFlow_MT5_Bridge.mq5`

into that folder.

Open MetaEditor and compile it.

### 2. Allow your NewsFlow URL

In MT5:

**Tools → Options → Expert Advisors**

Tick:

**Allow WebRequest for listed URL**

Add only the site origin, for example:

`https://monitor.yourdomain.co.za`

MetaTrader requires this before an EA can call your PHP API.

### 3. Add the EA to the chart

Open the NAS100 / US Tech 100 / NQ chart that already receives the prices you want.

Drag **NewsFlow_MT5_Bridge** onto that chart.

Set:

- `InpNewsFlowBaseUrl` = `https://YOUR-DOMAIN/PATH`
- `InpIngestToken` = the `ingest_token` from your NewsFlow `config.php`
- `InpOutputSymbol` = `NQ`

Enable Algo Trading.

### 4. Verify

Within 10 seconds open:

`market-setup.php`

You should see:

- Provider: MetaTrader 5
- Status: running
- Source Symbol: your broker's symbol, e.g. `NAS100.x`
- Trades Sent: increasing
- Feed Quality

Then the main dashboard should show:

`MetaTrader 5 · market feed live`

and Last Price should populate.

## Feed quality

### Best case

If your MT5 broker provides actual exchange trade prints, the MQL5 tick flags can include:

- `TICK_FLAG_BUY`
- `TICK_FLAG_SELL`

and `volume_real`.

The bridge labels this:

`exchange_or_broker_trade_flags`

### CFD/index case

Many NAS100 CFDs only provide broker quote ticks, not centralized CME trade volume. The bridge then labels the feed:

`broker_tick_estimate`

The web app still works and can plot price/delta-like absorption, but that delta is a broker-tick approximation rather than true CME contract delta.

For institutional-quality footprint/absorption, use the included Databento CME bridge instead.

## Why this route is useful

It requires no always-on Python process and no extra market-data API if you already have a live MT5 chart. The terminal itself becomes the market-data bridge.
