Prompting for trading code
The difference between a prompt that produces a subtly-broken backtest and one that produces a trustworthy one is the context you give. Here is the recipe.
A vague prompt gets a generic, assumption-laden answer. A good trading prompt hands the model the exact context that stops it making the dangerous default choices. Compare these two.
write a python backtest for a moving average crossover strategyWrite a VECTORISED backtest in Python (pandas) for an SMA crossover.
Data: a DataFrame `df` with a DateTimeIndex and a 'close' column of
daily bars — assume it already exists, don't fetch anything.
Rules:
- Signal: long when SMA(fast) crosses above SMA(slow), flat otherwise.
- CRITICAL: no look-ahead. The position for day t must be decided from
information available at the close of day t-1 (use .shift(1)).
- Apply costs: 5 bps per position change (round-trip friction).
Return: the strategy's daily returns and its equity curve. Add a one-line
comment on each step. Then tell me where look-ahead could still sneak in.The context checklist for any trading prompt
Before you send a prompt that produces code you might trade, make sure it specifies:
- The data shape — what the input looks like (a
closeSeries? an OHLCV DataFrame? a DateTimeIndex?). Ambiguity here is where alignment bugs breed. - No look-ahead — say it explicitly. State that decisions use only past-and-present data. Ask it to point out residual look-ahead.
- Costs & fills — demand realistic commission/spread/slippage, or say "costless for now, I'll add costs" so the choice is *yours*, not a silent default.
- The library & version — "using ccxt", "pandas 2.x", "MQL5 for MT5" — to reduce hallucinated APIs.
- Scope — one function, not "a trading bot". Small is verifiable.
Iterate, do not accept
The first answer is a draft. Push back: "you used the current bar — fix the look-ahead", "add a transaction cost of 2 bps and show the equity curve before and after", "this assumes the column is called Close, mine is close". Each correction both improves the code and teaches the model your actual situation. Three sharp iterations beat one perfect prompt you will never write.