Skip to content
Wildflower
Join free
Automated trading and AI

Free Trading School · Level 7 · Course 7.11

Turning rules into exact logic

Lesson 2 of 19 in Automated trading and AIModule 1: What automation is

Advanced · Automated trading and AI

Turning rules into exact logic

Wildflower Trading School · Lesson 7.11.1.2

Wildflower

A computer can only follow rules that are exact. Words like strong, near or big mean nothing to it. By the end of this lesson, you'll know how to find the vague words in your rules, and turn each one into a number. You'll also know the questions every rule needs answered before it can run on its own.

1 of 8

What you'll learn

  • Give every vague word a number. A computer can't measure words like strong or near.
  • Close every gap. Leave one, and the coder picks an answer the program then trades.
  • Write an else for every if. Cover open trades, unfilled orders and the end of day.
  • Write the rules as plain steps. This pseudocode lets a person check the logic first.
  • Run the rules once. Exact rules give the same entry, stop and target.

Exercise

  1. Take your own written rules.
  2. Underline every word a computer couldn't measure, and rewrite each one with a number.
  3. Then answer the four “else” questions: already in a trade, entry not filled, stop and target in one candle, end of day.

Next: Alerts and webhooks.

Quick check

  1. 1. A rule says “buy near support”. What happens when it's coded?

  2. 2. Which of these is exact enough for a computer?

  3. 3. One candle touches both the stop and the target. Why do the rules need to cover this?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Alerts and webhooks” is waiting next time.

Read the full notes

To automate a set of rules, every vague word needs a number a computer can measure, and every “if” needs an answer for the cases people usually handle without thinking, such as an open trade, an unfilled order or the end of the day.

  • A computer does exactly what it's told and never fills a gap with judgement. A vague rule doesn't stop the code from running: the coder picks an answer, and the program trades it. See The two-person test.
  • Find the vague words and give each a number, for example: “strong trend” → close above the 20-candle ; “big candle” → range at least 2× the last 20 candles' average; “near” → within 4 ; “early” → times plus a time zone. Examples of the kind of answer, not settings to use.
  • Every “if” needs an “else”: a new signal while in a trade; an entry that doesn't fill; and both inside one ; the end of the day and .
  • Write the exact rules as so a person can check the logic before any real code is written. Code for them is taught in Coding strategies in Pine Script and Writing entry and exit rules in Python.
  • Made-up: entry 5,018.25, stop 1 tick below a 5,010.50 low = 5,010.25. Risk 8 points = 32 ticks = $40 a contract on the ; target 2× risk = 5,034.25.
  • Answer to the drill: stop 5,015.50; 6 points = 24 ticks = $30.

Worked example

Made-up numbers

Running exact rules once

Made-up numbers · Micro S&P 500 · 1 tick = 0.25 points = $1.25

  1. Entry: 5,018.25.
  2. Lowest low of the last 5 candles 5,010.50, so the stop is 5,010.25.
  3. Risk: 8 points = 32 ticks = $40 a contract.
  4. Target: 5,018.25 + 2 × 8 = 5,034.25.

Exact rules give everyone the same entry, stop and target.

Your turn

Drill

Made-up numbers · Micro S&P 500 · 1 tick = 0.25 points = $1.25 · entry 5,021.50 · lowest low of last 5 candles 5,015.75 · stop 1 tick below it

Trading is risky. You can lose money, and automated trading can lose like any other trading. This lesson explains how things work. It isn’t advice.