Episode 28 · AI Trading · Building a Complete Trading System

A prompt with acceptance criteria: one small task, proven by a test

Give the AI one sizing function with units, limits, an expected test case and a refusal case, then ask it to point at the lines and the test results.

What does a prompt need so the AI does exactly one small thing and can prove it did it right?

This post writes a prompt that hands the AI a position-sizing function, with units, what’s forbidden, an expected test case and a case that must be refused. The easy mistake: accepting “the code is clean and bug-free” as a check. Ask for the file names, the tests and the results of an actual run.

Terms used below:

  • Acceptance criteria: concrete tests that tell you whether an output meets the request.
  • Step: the smallest increment a quantity can change by, e.g. 0.1.
  • Minimum quantity: the smallest size the contract allows.
  • Refusal case: an input where the right answer is “no valid size”, with a reason.

The five parts of a checkable prompt

Part Content Example in this post
1. Goal One task Write a function that computes quantity
2. Files and data What the AI reads A made-up contracts.json; the unit from a sample specification
3. Rules The written rules A clear rules.md
4. Limits What it may not do No broker, no real orders
5. Verification How you’ll know Tests for minimum and step, and a failing case; list assumptions and changed files

Ask the AI to list its assumptions, the files it changed and the test results. Then work one example by hand yourself. Keep one goal per prompt; don’t add unrelated goals.

A sizing prompt instead of an all-purpose bot

Start from a made-up contracts.json and a clear rules.md. State the exact function, its inputs and outputs, and the units. Forbid real orders for the whole exercise. Give the expected case, and require that a minimum size above the budget is refused with a reason.

Step Input or calculation Result
1 Budget 50 USD, risk 22 USD per unit Units stated in the prompt
2 50 ÷ 22 2.27
3 Round down to the 0.1 step Expected 2.2
4 Minimum quantity above the budget? Must refuse and say why

contracts.json (made-up contracts, for practice only):

{
  "DEMO-LINEAR": {"unit": "contracts", "value_per_point_per_unit": 1.0, "min_qty": 0.1, "step": 0.1, "max_qty": 100},
  "DEMO-BIGMIN": {"unit": "contracts", "value_per_point_per_unit": 1.0, "min_qty": 5, "step": 1, "max_qty": 100}
}

A version of the function, src/sizing.py:

# src/sizing.py: position size from a risk budget, for a made-up linear contract read from contracts.json.
# Pure calculation: no broker, no orders, no network.
import json
import math
from pathlib import Path

CONTRACTS = Path(__file__).resolve().parent.parent / "contracts.json"


class SizingRefused(Exception):
    """Raised with the reason when no valid size exists. Never round up to make a trade possible."""


def quantity(budget_usd, risk_usd_per_unit, step, min_qty, max_qty):
    """Largest quantity on the step grid whose risk fits the budget. Rounds DOWN; refuses below min_qty."""
    if budget_usd <= 0 or risk_usd_per_unit <= 0 or step <= 0:
        raise SizingRefused("budget, risk per unit and step must all be positive")
    raw = budget_usd / risk_usd_per_unit
    q = round(math.floor(raw / step + 1e-9) * step, 8)     # +1e-9: floating-point tolerance
    if q < min_qty:
        raise SizingRefused(f"minimum {min_qty} would risk {min_qty * risk_usd_per_unit:g} USD > budget {budget_usd:g} USD")
    return min(q, max_qty)


def quantity_for(symbol, budget_usd, sl_points):
    """Same, with the contract's own unit, step and limits read from contracts.json; unknown symbols are refused."""
    specs = json.loads(CONTRACTS.read_text(encoding="utf-8"))
    if symbol not in specs:
        raise SizingRefused(f"{symbol} isn't in contracts.json: refusing to guess its contract")
    s = specs[symbol]
    return quantity(budget_usd, sl_points * s["value_per_point_per_unit"], s["step"], s["min_qty"], s["max_qty"])

The acceptance criteria as tests, tests/test_sizing.py:

import pytest

from src.sizing import SizingRefused, quantity, quantity_for


def test_budget_50_risk_22_step_0_1_gives_2_2():
    assert quantity(50, 22, 0.1, 0.1, 100) == 2.2          # 50 / 22 = 2.27, rounded down to the 0.1 step


def test_exact_fit_isnt_rounded_away():
    assert quantity(50, 25, 0.1, 0.1, 100) == 2.0


def test_minimum_above_budget_is_refused_with_the_reason():
    with pytest.raises(SizingRefused, match="minimum 5 would risk 110 USD > budget 50 USD"):
        quantity_for("DEMO-BIGMIN", 50, 22)


def test_unknown_symbol_is_refused():
    with pytest.raises(SizingRefused, match="isn't in contracts.json"):
        quantity_for("XAUUSD", 50, 22)

Run .venv\Scripts\python -m pytest -q tests: you should see 4 passed. The refusal reads minimum 5 would risk 110 USD > budget 50 USD, which tells you exactly why there’s no trade, instead of a size quietly rounded up.

Worked example: a prompt with acceptance instead of “write the best bot”

“Write the best bot” Prompt with acceptance criteria
“Clean, bug-free code” isn’t a check A sample OHLC file with t/O/H/L/C, time zone UTC
Doesn’t say what data goes in Signal, sizing, execution and reporting kept separate
No expected result Output: code, the run command, one test, a limits report
Forbidden: no extra indicators, no real orders

Start the prompt with concrete inputs: a sample OHLC file with t/O/H/L/C and time zone UTC; a USD demo account; a linear contract; a separate specification file. Then give the locked spec and ask for signal, sizing, execution and reporting as separate parts, each with its own tests.

State the output: source code, the install and run commands in the project’s environment, one input/output test, and a report of the limits. State what’s forbidden: no added indicators, no rule changes to make the equity curve look better, no reading tokens from the chat, no real orders. These let you check what the assistant did; they don’t guarantee the code is right.

When the code arrives, ask specifically:

Gate Question If the answer is only general
1 Which line decides when the signal is known? Ask for the file path and the line
2 Which line checks the budget? Ask for the file path and the line
3 Which test proves an order can’t be sent twice, and what did it print when run? Ask for the test path and the actual run result
Passed You can check what the assistant did; that still doesn’t make the code right by itself

If you can’t check it yet, stay on sample data and don’t connect an account.

Try it: a prompt with scope, output and checks

Be a programming tutor for a beginner. First read the specification I give you and ask if the entry or exit criteria are unclear. Then only propose a plan; don't edit files yet. Limits: [PLATFORM + VERSION], you may only create or edit the files [LIST], don't connect a real account, don't send orders, don't create or read secrets. Output: (1) a plain-language explanation, (2) the missing assumptions, (3) the planned files, (4) the verification steps and expected results, (5) the risks of getting it wrong. Use the current official documentation for syntax and APIs, and give the links.

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:

  1. One small task per prompt; split the project into stages.
  2. Ask the AI to explain the difference between a simulation and an order sent to a broker.
  3. Read the diff, look at each command before it runs, and keep the change history.

Key takeaways

  • A prompt has five parts: goal, files and data, rules, limits such as no real orders, and how it’s checked; one task at a time.
  • The test case (made-up contract): budget 50, risk 22 per unit, step 0.1: 50 ÷ 22 = 2.27, rounded down to 2.2; a minimum above the budget is refused.
  • When the code arrives, make the AI point at the line that decides when the signal is known, the line that checks the budget, and the test against duplicate orders.

Comments