Back to Trading Styles & Strategy Development

Trading Styles & Strategy Development · Pro · 4 min

Translating rules into pseudocode

You can translate a written rule set into step-by-step pseudocode with explicit conditions, order details and edge cases, ready to be coded.

Words to exact steps

"Buy the pullback in an uptrend, stop below the low, risk 1%." A human trader understands this immediately. A computer understands none of it.

Before rules become code, they go through an intermediate step: pseudocode, plain-language instructions written as precisely as a program. It is the most useful step in automation, even if you never write a line of real code, because it exposes every vague word and every situation the rules never considered.

Candle, setup, trigger, order

Most rule sets fit the same skeleton:

Steps The structure of a pseudocode rule set: on each new candle, check the setup, check the trigger, size and send the order, and manage the open trade. 1 New candle 2 Setup? 3 Trigger? 4 Size + order 5 Manage trade
  1. On each new candle. Rules based on closing prices should run once a candle has closed, as they were backtested. In MQL5, code runs on each new tick, so EAs usually check whether a new bar has begun.
  2. Check the setup. Conditions that must all be true, written as comparisons.
  3. Check the trigger. The exact event that opens the trade.
  4. Size and send the order. Direction, stop, target and position size from the risk rule.
  5. Manage the open trade. Moving the stop, partial exits, closing before news.

Every condition must be a comparison a computer can check: a number greater or smaller than another, a time inside or outside a window. Words like "strong", "clear" or "near" must become numbers.

Vague words become numbers

Comparison A rule in words says buy the pullback, stop below the low, risk 1%. In pseudocode it becomes: if the close is above the 20-period EMA and the low touched it, set the stop 0.2 below the swing low and calculate the lot size from 1% risk. Rule in words Buy the pullback Stop below the low Risk 1% Pseudocode IF close > EMA20 AND low <= EMA20 stop = low - 0.2 lots from 1% risk

The pullback rule from the building-blocks lesson, as pseudocode:

ON each new closed candle:
  IF a position is open: manage it, then STOP
  IF time is within 30 min of high-impact news: STOP
  setup   = EMA50 rising AND low <= EMA20
  trigger = close > EMA20
  IF setup AND trigger:
    stop   = most recent swing low - 0.2
    risk   = 1% of account balance
    lots   = risk / (entry - stop) per lot
    target = entry + 2 * (entry - stop)
    SEND buy order with stop and target

Writing it this way raises questions the words skipped. Which swing low, over how many candles? What if the stop distance is tiny? What happens on Friday evening? Each answer goes into the pseudocode before any code is written.

Common mistakes

The takeaway

Pseudocode turns trading rules into exact steps a program can follow. Writing it exposes every vague word and every case the rules forgot.

Check yourself

Quick quiz

4 questions. Instant feedback, nothing is saved.

  1. 1Tap the step that turns a signal into a concrete order.

    Tap the chart.

    Steps Chart for the question: Tap the step that turns a signal into a concrete order. 1 New candle 2 Setup? 3 Trigger? 4 Size + order 5 Manage trade

    Only here are direction, size, stop and target fixed and the order sent.

  2. 2Why is "buy when the trend is strong" not usable in pseudocode?

    Every condition must be a comparison of numbers, such as close > EMA20.

  3. 3Which edge case should pseudocode handle?

    Open positions, missing data, weekends and news times all need an explicit answer.

  4. 4Why check the rules on each new candle rather than on every tick?

    A candle that looks like a signal mid-way may close differently. Acting on closed candles avoids that.

of 4 correct.

Sources

Back to Trading Styles & Strategy Development