Article-24340-ONNX-Models-P.../Files/make_model.py
526 lines
22 KiB
Python
# Copyright 2026, Allan Munene Mutiiria.
# https://t.me/Forex_Algo_Trader
import os
import math
import warnings
from datetime import datetime, timezone
import numpy as np
import torch
import torch.nn as nn
import MetaTrader5 as mt5
# What the terminal is asked for
SYMBOL = "XAUUSDm"
TIMEFRAME = mt5.TIMEFRAME_M15
MAX_BARS = 400000
# How far ahead a label looks, and how far price must travel to earn one
HORIZON = 33
BARRIER = 2.5
# The shape of the network, which is also the shape the viewer draws
INPUTS = 16
HIDDEN_A = 24
HIDDEN_B = 12
OUTPUTS = 3
# How many recent bars are written out for the MQL5 side to check itself against
PROOF_BARS = 300
# How the training runs
EPOCHS = 400
BATCH = 256
LEARN = 0.004
TEST_SHARE = 0.20
CHECK_SHARE = 0.15
PATIENCE = 40
torch.manual_seed(7)
np.random.seed(7)
def pull_bars():
"""Read the symbol's history straight out of the running terminal."""
if not mt5.initialize():
raise SystemExit(f"cannot reach the terminal: {mt5.last_error()}")
info = mt5.symbol_info(SYMBOL)
if info is None:
mt5.shutdown()
raise SystemExit(f"{SYMBOL} is not in the Market Watch")
if not info.visible:
mt5.symbol_select(SYMBOL, True)
# A symbol just made visible holds nothing yet, so a small request wakes
# the terminal up before the real one is made
mt5.copy_rates_from_pos(SYMBOL, TIMEFRAME, 0, 10)
# How many bars one request may return is a terminal setting rather than a
# fixed number, so the ask is halved until the terminal answers
rates = None
want = MAX_BARS
while want >= 10000:
rates = mt5.copy_rates_from_pos(SYMBOL, TIMEFRAME, 0, want)
if rates is not None and len(rates) > 0:
print(f" asked for {want:,} bars, received {len(rates):,}")
break
want //= 2
point = info.point
fallback = info.spread * point
mt5.shutdown()
if rates is None or len(rates) < 5000:
got = 0 if rates is None else len(rates)
raise SystemExit(
f"only {got:,} bars came back for {SYMBOL} at this timeframe.\n"
" Open that chart, press Home to load the history, and raise\n"
" Tools, Options, Charts, Max bars in chart if it is set low")
# Every bar carries the spread it saw, so the middle of those is what a
# trade normally pays. Reading it once would catch whatever minute the
# script happened to run in, and at rollover that is several times wider
if "spread" in rates.dtype.names:
seen = np.sort(rates["spread"].astype(np.float64)) * point
spread = float(seen[len(seen) // 2])
print(f" spread across the sample: typical {spread:.3f}, "
f"quietest {seen[len(seen) // 20]:.3f}, worst {seen[-len(seen) // 20]:.3f}")
else:
spread = fallback
return rates, spread
def true_range(high, low, close):
"""The range a bar covered, counting any gap from the bar before it."""
prev = np.roll(close, 1)
prev[0] = close[0]
return np.maximum(high - low, np.maximum(abs(high - prev), abs(low - prev)))
def moving_average(x, span):
"""A smoothed average that weights recent bars more heavily."""
out = np.empty_like(x)
out[0] = x[0]
k = 2.0 / (span + 1.0)
for i in range(1, len(x)):
out[i] = x[i] * k + out[i - 1] * (1.0 - k)
return out
def rolling_mean(x, n):
"""The plain average of the last n values, ending at each position."""
c = np.cumsum(np.insert(x, 0, 0.0))
out = np.empty_like(x)
out[:n] = x[:n].mean() if n > 1 else x[:n]
out[n:] = (c[n + 1:] - c[1:-n]) / n
return out
def rolling_max(x, n):
"""The highest value over the last n bars, ending at each position."""
out = np.copy(x)
for i in range(1, len(x)):
out[i] = x[max(0, i - n + 1):i + 1].max()
return out
def rolling_min(x, n):
"""The lowest value over the last n bars, ending at each position."""
out = np.copy(x)
for i in range(1, len(x)):
out[i] = x[max(0, i - n + 1):i + 1].min()
return out
def build_features(rates):
"""Turn raw bars into sixteen numbers that carry no price level."""
o = rates["open"].astype(np.float64)
h = rates["high"].astype(np.float64)
l = rates["low"].astype(np.float64)
c = rates["close"].astype(np.float64)
t = rates["time"].astype(np.int64)
tr = true_range(h, l, c)
atr14 = rolling_mean(tr, 14)
atr50 = rolling_mean(tr, 50)
# A floor keeps a flat stretch from dividing by nothing
safe = np.maximum(atr14, 1e-9)
hi20, lo20 = rolling_max(h, 20), rolling_min(l, 20)
hi50, lo50 = rolling_max(h, 50), rolling_min(l, 50)
span20 = np.maximum(hi20 - lo20, 1e-9)
span50 = np.maximum(hi50 - lo50, 1e-9)
ema12 = moving_average(c, 12)
ema50 = moving_average(c, 50)
rng = np.maximum(h - l, 1e-9)
hour = (t // 3600) % 24
def back(x, n):
"""The same series shifted n bars into the past."""
s = np.roll(x, n)
s[:n] = x[0]
return s
f = np.column_stack([
(c - back(c, 1)) / safe, # momentum over one bar
(c - back(c, 3)) / safe, # over three
(c - back(c, 6)) / safe, # over six
(c - back(c, 12)) / safe, # over the label horizon
(c - back(c, 24)) / safe, # over two hours
(c - lo20) / span20, # place inside the recent range
(c - lo50) / span50, # place inside the longer range
atr14 / np.maximum(atr50, 1e-9), # is the market faster than usual
(c - o) / rng, # who won this bar
(h - np.maximum(o, c)) / rng, # rejection from above
(np.minimum(o, c) - l) / rng, # rejection from below
(c - ema12) / safe, # stretch against the short mean
(c - ema50) / safe, # stretch against the long mean
(ema12 - ema50) / safe, # trend strength
np.sin(2.0 * math.pi * hour / 24), # session, as an angle
np.cos(2.0 * math.pi * hour / 24),
])
return f, h, l, c, atr14
def build_labels(h, l, c, atr):
"""Walk each bar forward until price touches a barrier above or below."""
n = len(c)
y = np.zeros(n, dtype=np.int64)
for i in range(n - HORIZON - 1):
reach = atr[i] * BARRIER
if reach <= 0.0:
continue
up, dn = c[i] + reach, c[i] - reach
y[i] = 1
# The first barrier touched decides the label, so the walk stops there
for k in range(i + 1, i + 1 + HORIZON):
if h[k] >= up:
y[i] = 2
break
if l[k] <= dn:
y[i] = 0
break
return y
class Net(nn.Module):
"""Three dense layers, which is the shape the neuron view can draw."""
def __init__(self):
super().__init__()
self.l1 = nn.Linear(INPUTS, HIDDEN_A)
self.a1 = nn.ReLU()
self.l2 = nn.Linear(HIDDEN_A, HIDDEN_B)
self.a2 = nn.ReLU()
self.l3 = nn.Linear(HIDDEN_B, OUTPUTS)
self.sm = nn.Softmax(dim=1)
def forward(self, x):
return self.sm(self.l3(self.a2(self.l2(self.a1(self.l1(x))))))
def report(name, pred, truth):
"""Say how often the model was right, and whether that beats guessing."""
acc = float((pred == truth).mean())
names = ["down", "flat", "up"]
print(f"\n {name}: {acc * 100:.2f}% of all {len(truth):,} bars called correctly")
print(" predicted")
print(" " + "".join(f"{n:>8s}" for n in names) + " found")
hits = []
for a in range(3):
row = [int(((truth == a) & (pred == b)).sum()) for b in range(3)]
total = max(sum(row), 1)
hits.append(row[a] / total)
print(f" actual {names[a]:>4s}" + "".join(f"{v:8d}" for v in row) +
f"{hits[a] * 100:9.1f}%")
balanced = sum(hits) / 3.0
print(f" averaged over the three classes: {balanced * 100:.2f}% "
f"against {100 / 3:.2f}% for a coin")
# What a trader needs is not how often a class was found but whether a
# call can be trusted, so each column is scored against how often that
# class turns up at all
print("\n when it made a call")
edge = []
for b in range(3):
said = int((pred == b).sum())
if said == 0:
print(f" it never said {names[b]}")
continue
right = int(((pred == b) & (truth == b)).sum()) / said
base = float((truth == b).mean())
edge.append(right - base)
print(f" said {names[b]:4s} {said:6,} times, right {right * 100:5.1f}%, "
f"that class occurs {base * 100:5.1f}%, edge {(right - base) * 100:+5.1f} points")
return acc, balanced, (max(edge) if edge else 0.0)
def expectancy(pred, truth, reach, cost):
"""Turn the calls into money, and check they beat trading without a model."""
names = ["down", "flat", "up"]
print(f"\n taking every signal, winning or losing {reach:.2f} and paying {cost:.2f}")
best = -1e9
for b in (0, 2):
took = int((pred == b).sum())
if took == 0:
continue
# A call is right when price reached its barrier, wrong when it reached the other
win = int(((pred == b) & (truth == b)).sum())
lose = int(((pred == b) & (truth == (2 - b))).sum())
went = int(((pred == b) & (truth == 1)).sum())
net = (win - lose) * reach - took * cost
per = net / took
best = max(best, per)
# A rate under half loses at any spread, which separates a costs
# problem from a model that simply calls the wrong way
rate = win / max(win + lose, 1)
print(f" {names[b]:4s} {took:6,} taken right {win:5,} wrong {lose:5,} "
f"nowhere {went:5,} won {rate * 100:.1f}% per trade {per:+.3f}")
if rate < 0.5:
print(f" under half, so this call loses even with no spread at all")
# A market that drifted one way pays anyone who leaned that way, model or
# not, so the signals are measured against simply taking every bar
print("\n against trading every bar without a model")
blind = -1e9
for b in (0, 2):
win = int((truth == b).sum())
lose = int((truth == (2 - b)).sum())
took = len(truth)
per = ((win - lose) * reach - took * cost) / took
blind = max(blind, per)
print(f" always {names[b]:4s} {took:6,} trades won "
f"{win / max(win + lose, 1) * 100:.1f}% per trade {per:+.3f}")
# Every signal is held for the whole horizon, so most of them overlap one
# another. An account holds one position and waits, so the signals are
# walked in order and any that arrives while a trade is open is passed over
print("\n taking one position at a time, as an account really would")
real, proven = -1e9, False
for b in (0, 2):
held, taken, win, lose = 0, 0, 0, 0
for i in range(len(pred)):
if held > 0:
held -= 1
continue
if pred[i] != b:
continue
taken += 1
held = HORIZON
if truth[i] == b:
win += 1
elif truth[i] == (2 - b):
lose += 1
if taken == 0:
continue
net = (win - lose) * reach - taken * cost
if net / taken > real:
real = net / taken
# A handful of trades cannot tell an edge from a coin, so the spread of
# a coin over the same count is stated beside the result
res = max(win + lose, 1)
rate = win / res
swing = 1.96 * math.sqrt(0.25 / res)
sure = abs(rate - 0.5) > swing
print(f" {names[b]:4s} {taken:6,} trades right {win:5,} wrong {lose:5,} "
f"won {rate * 100:.1f}% per trade {net / taken:+.3f} total {net:+,.0f}")
if sure and net > 0.0:
proven = True
print(f" a coin over {res:,} trades lands between "
f"{(0.5 - swing) * 100:.1f}% and {(0.5 + swing) * 100:.1f}%, so this is "
f"{'outside that' if sure else 'inside that and cannot be told from luck'}")
return real, blind, proven
def save_onnx(model, folder):
"""Write the trained network where the terminal and the viewer can read it."""
path = os.path.join(folder, "model.onnx")
model.eval()
# The legacy exporter emits a plain Gemm chain, which the viewer draws in full,
# so its deprecation notice is expected rather than a problem to fix
warnings.filterwarnings("ignore", category=DeprecationWarning)
torch.onnx.export(
model, torch.zeros(1, INPUTS, dtype=torch.float32), path,
input_names=["input"], output_names=["output"],
opset_version=13, dynamo=False,
)
return path
def main():
print("Reading history from the terminal")
rates, spread = pull_bars()
print(f" {len(rates):,} bars of {SYMBOL}")
feats, h, l, c, atr = build_features(rates)
labels = build_labels(h, l, c, atr)
# The first fifty bars have no history behind them, the last few no future
keep = slice(50, len(c) - HORIZON - 1)
X = feats[keep].astype(np.float32)
y = labels[keep]
when = rates["time"][keep].astype(np.int64)
# A row with no number in it would poison the training
good = np.isfinite(X).all(axis=1)
X, y, when = X[good], y[good], when[good]
counts = np.bincount(y, minlength=3)
share = counts / counts.sum() * 100
print(f"\n labels: down {share[0]:.1f}%, flat {share[1]:.1f}%, up {share[2]:.1f}%")
print(f" a barrier of {BARRIER} ATR costs {spread * 2 / (atr.mean() * BARRIER) * 100:.0f}% of a win")
if share[1] > 45.0:
print(f" flat covers {share[1]:.0f}% of the bars, which crowds out the two")
print(" that matter, so lower BARRIER or lengthen HORIZON")
elif share[1] < 5.0:
print(f" flat covers only {share[1]:.0f}% of the bars, so raise BARRIER")
# The split is by time, because a shuffled one lets the model see its own future
n = len(X)
cutA = int(n * (1.0 - TEST_SHARE - CHECK_SHARE))
cutB = int(n * (1.0 - TEST_SHARE))
Xtr, ytr = X[:cutA], y[:cutA]
Xck, yck = X[cutA:cutB], y[cutA:cutB]
Xte, yte = X[cutB:], y[cutB:]
# Naming the dates makes it obvious that nothing from the test months
# was ever seen during training
def span(a, b):
first = datetime.fromtimestamp(int(when[a]), timezone.utc)
last = datetime.fromtimestamp(int(when[b - 1]), timezone.utc)
return f"{first:%d %b %Y} to {last:%d %b %Y}"
# A model can only learn what the sample contains, so the span is stated
# plainly rather than assumed to be enough
years = (int(when[n - 1]) - int(when[0])) / (365.25 * 24 * 3600)
print(f"\n the sample covers {years:.1f} years")
if years < 2.0:
print(" that is likely one or two market moods, so a pattern found here")
print(" may not survive the next one")
print(f" training on {len(Xtr):,} bars {span(0, cutA)}")
print(f" checking on {len(Xck):,} bars {span(cutA, cutB)}")
print(f" testing on {len(Xte):,} bars {span(cutB, n)}")
# The scale is taken from the training bars only, then applied to all three
mean = Xtr.mean(axis=0)
std = np.maximum(Xtr.std(axis=0), 1e-6)
Xtr = (Xtr - mean) / std
Xck = (Xck - mean) / std
Xte = (Xte - mean) / std
net = Net()
# Rarer classes are weighted up so the model cannot just answer with the common one
weight = torch.tensor(len(ytr) / (3.0 * np.maximum(np.bincount(ytr, minlength=3), 1)),
dtype=torch.float32)
loss_fn = nn.CrossEntropyLoss(weight=weight)
opt = torch.optim.Adam(net.parameters(), lr=LEARN)
# The rate eases off when the check score stops improving
plan = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, mode="max", factor=0.5, patience=12)
tX, ty = torch.tensor(Xtr), torch.tensor(ytr)
cX, cy = torch.tensor(Xck), torch.tensor(yck)
best, bestAt, bestState = 0.0, 15, None
print("\n training")
for epoch in range(EPOCHS):
# A fresh order each pass stops the model learning the sequence itself
net.train()
order = torch.randperm(len(tX))
total = 0.0
for start in range(0, len(tX), BATCH):
idx = order[start:start + BATCH]
opt.zero_grad()
# The loss reads the scores before Softmax, so it is taken one layer in
out = net.l3(net.a2(net.l2(net.a1(net.l1(tX[idx])))))
loss = loss_fn(out, ty[idx])
loss.backward()
opt.step()
total += loss.item() * len(idx)
# The check bars decide when to stop, so the test bars stay untouched
net.eval()
with torch.no_grad():
pck = net(cX).argmax(dim=1)
hits = [float(((cy == a) & (pck == a)).sum()) / max(float((cy == a).sum()), 1.0)
for a in range(3)]
score = sum(hits) / 3.0
plan.step(score)
if score > best and epoch >= 15:
best, bestAt = score, epoch
bestState = {k: v.clone() for k, v in net.state_dict().items()}
if (epoch + 1) % 25 == 0:
print(f" epoch {epoch + 1:4d} loss {total / len(tX):.4f} "
f"check {score * 100:.2f}% best {best * 100:.2f}%")
# Nothing is gained by running on once the check score has stalled
if epoch - bestAt >= PATIENCE:
print(f" stopped at epoch {epoch + 1}, no gain for {PATIENCE} rounds")
break
# The weights kept are the ones that scored best on bars held back from training
if bestState is not None:
net.load_state_dict(bestState)
print(f" kept the weights from epoch {bestAt + 1}")
net.eval()
with torch.no_grad():
ptr = net(torch.tensor(Xtr)).argmax(dim=1).numpy()
pte = net(torch.tensor(Xte)).argmax(dim=1).numpy()
print("")
report("on the bars it trained on", ptr, ytr)
acc, balanced, edge = report("on the bars it never saw", pte, yte)
# The model is trained with class weights, so it is judged on the balanced
# score rather than on how often it agrees with the most common answer
base = float(np.bincount(yte, minlength=3).max()) / len(yte)
# Statistics that cannot pay the spread are not an edge, so the calls are priced
real, blind, proven = expectancy(pte, yte, float(atr.mean()) * BARRIER, spread * 2.0)
print("")
print(f" a coin scores 33.33% balanced, this model scores {balanced * 100:.2f}%")
print(f" always answering the most common class scores {base * 100:.2f}% of all bars")
if balanced > 0.36:
print(f" the calls carry an edge of up to {edge * 100:.1f} points over the base rate")
else:
print(" it does not separate the classes at all")
# Only the one position at a time figure describes an account, so the
# verdict follows that rather than the signal count
if real <= 0.0:
print(f" traded one position at a time the best side loses {real:.3f} a trade,")
print(" so these weights carry no edge worth acting on")
elif not proven:
print(f" the best side makes {real:+.3f} a trade, but over so few trades a coin")
print(" would do the same often enough that this is not yet an edge")
elif real <= blind:
print(f" traded properly the best side makes {real:+.3f}, but trading every bar")
print(f" one way makes {blind:+.3f}, so the gain is drift rather than the model")
else:
print(f" traded one position at a time the best side makes {real:+.3f} against")
print(f" {blind:+.3f} for trading blind, and it clears the range a coin would")
print(" reach, so the model is adding something of its own")
# The shared folder is the one the terminal and every tester agent can both
# read, so everything the programs need is written there and nowhere else
folder = os.path.dirname(os.path.abspath(__file__))
while os.path.basename(folder) != "MQL5" and os.path.dirname(folder) != folder:
folder = os.path.dirname(folder)
root = os.path.dirname(os.path.dirname(folder))
target = os.path.join(root, "Common", "Files", "ONNX Model Viewer Part 4")
os.makedirs(target, exist_ok=True)
path = save_onnx(net, target)
with open(os.path.join(target, "model_scale.txt"), "w") as f:
f.write(" ".join(f"{v:.10g}" for v in mean) + "\n")
f.write(" ".join(f"{v:.10g}" for v in std) + "\n")
# The MQL5 side has to build these same sixteen numbers from live bars.
# Writing the last few hundred out lets it prove that it does, because a
# feature that is close but not equal produces a confident wrong answer
proof = os.path.join(target, "model_proof.csv")
raw = feats[keep][good]
with open(proof, "w") as f:
f.write("time," + ",".join(f"f{i}" for i in range(INPUTS)) + "\n")
for i in range(max(0, len(raw) - PROOF_BARS), len(raw)):
f.write(str(int(when[i])) + "," +
",".join(f"{v:.10g}" for v in raw[i]) + "\n")
print(f"\n wrote {path}")
print(f" wrote {os.path.join(target, 'model_scale.txt')}")
print(f" wrote {proof}, holding {min(PROOF_BARS, len(raw)):,} bars to check against")
if __name__ == "__main__":
main()