How do you turn an idea like “buy when it breaks the high” into a condition a machine can mark true or false?
This post writes a specification with the trigger, the moment the signal is known, the entry price, SL and TP, costs and the cases to skip. Then it tests two ideas, a 20-candle breakout and an EMA cross, on small inputs. The easy mistake: words like “strong”, “near” or “holds” with no measurement. A machine can’t check them; turn them into numbers or label them as your own judgment.
Terms used below:
- Specification (spec): a written description of the inputs, rules and outputs, precise enough that two people implementing it get the same result.
- Trigger: the condition that fires a signal.
- Expiry: how long a signal or pending order stays valid.
- Warm-up: the candles an indicator needs before its value means anything.
- Event: one logged signal, with an id, a time and the rule version.
Vague sentences and specs a machine can check
| Vague | Measurable |
|---|---|
| “Liquidity got taken, nice entry” | Symbol and feed, timeframe, closed candles only |
| “Strong”, “near”, “holds”, with no measure | Close > the highest High of the previous 20 candles |
| “Long when it breaks the high” | Entry at the next candle’s open; SL, expiry and costs written separately |
| Anything subjective is labelled as subjective |
A machine can only check true/false conditions on data that exists at that moment.
Write down the symbol or feed, the timeframe, closed candles only, how pivots or zones are measured, the trigger, the entry and the expiry. Define “strong”, “near” and “holds” with a measurement, or record them as a subjective decision. Choose the SL, exit, size and costs up front. Ask the AI for a table of passing and failing examples so you can check each condition, before anyone writes a bot.
Pin down “breaks the high”
The idea: long when price breaks the 20-candle high. One precise version:
| When | What | Note |
|---|---|---|
| Before the current candle | Take the highest High of the previous 20 candles | The current candle isn’t included |
| At the close | The current candle closes | Close > that highest High gives a signal |
| Next candle’s open | Assumed entry | The earliest price after the signal; a stop entry inside the candle is a different variant |
A case to check by hand: the previous 20 candles top out at 104; the current candle trades to 105 but closes at 103. That’s not a close breakout, because the close of 103 didn’t clear 104. If it had closed at 104.5, the signal fires at the close and the assumed entry is the next candle’s open. Stop, expiry and costs still have to be written separately. If you’d rather enter with a stop order inside the candle, write that down as its own variant.
The same timing applies to pivots: a pivot marked 3 candles back is only known once those 3 candles have closed, so the machine can act on it at the earliest right after that confirmation.
Worked example: “buy the EMA cross” made precise
The long condition: on two consecutive closed H1 candles, EMA8 on the previous candle ≤ EMA21 on the previous candle, and EMA8 on the current candle > EMA21 on the current candle. Source Close, offset 0, the same data source for both, enough warm-up. The signal only exists once the current candle has closed; a cross that appears and vanishes inside the candle doesn’t count. Assumed entry, SL, TP, expiry, costs and a cooldown are separate lines; none of them follows from the cross sentence.
Give the AI three small inputs: no cross yet; a cross that has just completed;
the same candle sent again. The outputs must be: no signal; one event; no
duplicate event. Put rule_version on both the event and the report, so a
later change to the condition never mixes with data from the old version.
For EURUSD or gold, add lot rules, the contract specification and BID/ASK. Don’t just swap the symbol BTC for XAUUSD and keep BTC’s quantity. If the contract sizing isn’t known, have the program refuse the incomplete config rather than guess.
| Gate | Question | If not |
|---|---|---|
| 1 | Has the candle closed? | Don’t compute; ignore a cross that only appears inside the candle |
| 2 | Is there enough warm-up for the EMAs? | No signal yet |
| 3 | EMA8 previous ≤ EMA21 previous, and EMA8 current > EMA21 current? | No signal |
| 4 | No event logged for this candle yet? | Don’t add a duplicate |
| Passed | Log one event with rule_version |
Both ideas as code, src/rules.py:
# src/rules.py: two ideas written as true/false conditions on CLOSED candles. Reads lists only; sends no orders.
RULE_VERSION = "ema-cross-h1-v1"
def breakout_20(highs, closes, i, n=20):
"""Close of closed candle i above the highest High of the n candles BEFORE it (candle i not included).
Returns None when there aren't n earlier candles: no guess."""
if i < n:
return None
return closes[i] > max(highs[i - n:i])
def ema(values, n):
"""EMA seeded with the simple mean of the first n values; None during warm-up."""
out, k = [None] * len(values), 2 / (n + 1)
if len(values) >= n:
out[n - 1] = sum(values[:n]) / n
for i in range(n, len(values)):
out[i] = values[i] * k + out[i - 1] * (1 - k)
return out
def ema_cross_events(candles, seen=None):
"""Long event when EMA8 <= EMA21 on the previous closed H1 candle and EMA8 > EMA21 on this one.
candles: closed H1 candles [{'time_utc', 'close'}]. seen: event ids already logged, so a resent candle adds nothing."""
seen = set() if seen is None else seen
closes = [c["close"] for c in candles]
fast, slow = ema(closes, 8), ema(closes, 21)
events = []
for i in range(1, len(candles)):
if None in (fast[i - 1], slow[i - 1], fast[i], slow[i]):
continue # not enough warm-up yet
if fast[i - 1] <= slow[i - 1] and fast[i] > slow[i]:
eid = f"{RULE_VERSION}-{candles[i]['time_utc']}"
if eid not in seen:
seen.add(eid)
events.append({"id": eid, "time_utc": candles[i]["time_utc"], "side": "long",
"rule_version": RULE_VERSION})
return events
def contract_spec(cfg):
"""Refuse a config that doesn't say how the contract is sized, instead of guessing (e.g. BTC quantity on XAUUSD)."""
missing = [k for k in ("symbol", "contract_size", "volume_step", "price_side") if k not in cfg]
if missing:
raise SystemExit(f"Config is missing {missing}: refusing to guess the contract.")
return cfgThe four checks from this post as tests, tests/test_rules.py:
import pytest
from src.rules import breakout_20, contract_spec, ema_cross_events
def test_high_above_but_close_below_the_line_is_not_a_breakout():
highs = [100.0] * 19 + [104.0] + [105.0] # the 20 earlier candles top out at 104
closes = [99.0] * 20 + [103.0] # candle 20: High 105, Close 103
assert breakout_20(highs, closes, 20) is False
closes[20] = 104.5
assert breakout_20(highs, closes, 20) is True
assert breakout_20(highs, closes, 19) is None # only 19 earlier candles: no guess
def candles(closes):
return [{"time_utc": f"2026-11-02T{h:02d}:00:00Z" if h < 24 else f"2026-11-03T{h - 24:02d}:00:00Z", "close": c}
for h, c in enumerate(closes)]
FALLING = [110 - 0.5 * i for i in range(30)] # EMA8 under EMA21
def test_no_cross_no_event():
assert ema_cross_events(candles(FALLING)) == []
def test_a_cross_gives_one_event_and_a_resent_candle_adds_none():
c = candles(FALLING + [104, 106, 108, 110])
seen = set()
first = ema_cross_events(c, seen)
assert len(first) == 1 and first[0]["rule_version"] == "ema-cross-h1-v1"
assert ema_cross_events(c, seen) == [] # the same candles sent again: no duplicate
def test_missing_contract_details_are_refused():
with pytest.raises(SystemExit):
contract_spec({"symbol": "XAUUSD", "volume_step": 0.01})Run with .venv\Scripts\python -m pytest -q tests; you should see
4 passed. The cross test prints one event like
ema-cross-h1-v1-2026-11-03T08:00:00Z; sending the same candles again adds
nothing.
Try it: turn a discretionary method into measurable rules
Act as a specification coach; don't write code yet. Help me turn the method [METHOD NAME] into a table with: observable conditions, the data needed, when each condition is confirmed, entry and exit conditions, the invalidation point, costs, the cases to skip, and what can't be automated yet. Keep the original terms separate from the conventions I choose. Ask at most 5 important questions about what's missing. Don't fill in parameters yourself. If a condition is still subjective, suggest a chart-marking exercise instead of pretending it can be backtested.Fill the [brackets] with your own details. Never paste passwords, API keys or tokens into a prompt, and check whatever the AI returns on the demo account before relying on it.
The order to work in:
- Write each condition as true/false on the data available at that moment.
- Write down the exit, the invalidation, costs, sessions and the no-trade cases.
- Mark what is subjective; don’t pretend an AI can backtest reading by eye.
Key takeaways
- A 20-candle breakout by close: the old high is 104, a candle with High 105 but Close 103 doesn’t count; when it does, entry is the next candle’s open.
- A pivot marked 3 candles back is only known after those 3 candles; the machine can enter at the earliest after that confirmation.
- Each spec tests one variant; parts that need a human eye, like counting Elliott waves, are practised by marking charts, not passed off as a backtest.
Comments