AlgoPro UniversityCourse home
Part 2 · Lesson

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.

textWeak prompt — invites look-ahead, zero costs, hand-wavy data
write a python backtest for a moving average crossover strategy
textStrong prompt — pins down data, costs, and the look-ahead trap
Write 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:

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.