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:
- 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.
- Check the setup. Conditions that must all be true, written as comparisons.
- Check the trigger. The exact event that opens the trade.
- Size and send the order. Direction, stop, target and position size from the risk rule.
- 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
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
- Leaving vague words in. "Near support" must become a number.
- Forgetting open positions. Decide what a new signal does when a trade is already running.
- Ignoring time. Weekends, session ends and news times need explicit rules.
- Acting on unfinished candles. A signal mid-candle may vanish at the close.
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.
-
1Tap the step that turns a signal into a concrete order.
Tap the chart.
Only here are direction, size, stop and target fixed and the order sent.
-
2Why is "buy when the trend is strong" not usable in pseudocode?
Every condition must be a comparison of numbers, such as close > EMA20.
-
3Which edge case should pseudocode handle?
Open positions, missing data, weekends and news times all need an explicit answer.
-
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.