Skip to main content

10.2. Rules

A rule is one sentence of your method: when this is true, do that. The first half is the condition; the second, the action.

Conditions​

A condition compares a primitive with something:

  • with a number: gain_pct >= 50, the gain is 50% or more,
  • with another primitive: cmp < w_ema_21, the price is below the 21-week EMA,
  • or with a small calculation: gain_pct < -sl_pct, the loss is greater than your stop-loss percentage. Calculations can use the four arithmetic signs and brackets, such as total_invested / capital * 100.

The comparisons are <, <=, >, >=, == (equal to) and != (not equal to), and between, for a range.

Conditions join with AND, when all must be true, and OR, when any one will do, and can be grouped in brackets. So

(w_rsi_14 > 80 OR gain_from_last_tranche_pct >= 30) AND gain_pct > 40

reads: the weekly RSI is above 80, or the price has run 30% since the last tranche, and in either case the position is up more than 40%.

One safeguard is worth knowing. If a condition needs a figure Tradestory does not have, such as an indicator for a stock with too little price history, the rule does not fire. A rule never acts on missing data.

Actions​

The action is one of four:

  • Sell: close the position.
  • Trim: sell part of it.
  • Add: buy another tranche.
  • Hold: do nothing. A Hold rule is a way to say not yet: placed among your exit rules, it keeps the exits below it from firing, for example in the first weeks after a buy.

Each action carries a reason, in your words, which the Dashboard shows when the rule fires. A reason such as Price below the 21-week EMA tells you at a glance why a position is marked.

Writing a Rule​

Open Plan → Rules and choose + New Rule. Give the rule a name, write its condition, and under Action choose the type and write the reason.

The condition editor has three views, chosen under Edit as: Visual, Text and JSON. All three edit the same condition; switch between them as you like.

The Visual Builder​

The Visual view builds a condition one row at a time. Each row is one comparison, read left to right: something, a comparison sign, something else.

  • The left side is a field, chosen from a list grouped into position fields, thresholds and indicators. The fx button before it switches the left side to a calculation you type, such as cmp / avg_cost * 100 - 100, and back.
  • The comparison is one of >, >=, <, <=, == and !=.
  • The right side has a button of its own, which cycles through three kinds of thing to compare with. 123 is a number you type. ⇄ is another field, such as comparing cmp with w_ema_21. fx is a calculation, such as -sl_pct or avg_cost * 1.05.

+ Add condition adds another row. Between rows sits a single AND / OR switch, which joins all the rows the same way: all of them must hold, or any one will do.

When to Use Text​

The Visual view keeps to one joiner for the whole rule. Many rules need more: this or that, and in either case the other. A condition like that has a group in brackets inside it, and the Visual view cannot show it. When a condition has such a group, the Visual button is locked, with the note This rule uses nested groups — edit in TEXT or JSON.

So for a compound condition, write it in the Text view. Text reads as the conditions in this section are printed: AND, OR and NOT, brackets for grouping, and the same comparisons and calculations. As with arithmetic, AND binds more tightly than OR, so a OR b AND c means a OR (b AND c); when in doubt, add brackets. Tradestory points to the place if something cannot be read.

JSON shows the same condition in the form Tradestory stores it, for those who prefer it.

Try It​

Figure 10-1 is a working copy of the condition editor. Build a condition in the Visual view, use the toggles, add rows, or start from one of the examples above it. The last example has a group in brackets: notice that it opens in Text, and that Visual is locked. Below the editor, choose a position from our example to see whether the condition would fire for it, part by part, with the figures used. A field this example does not include shows as not available, and the part does not hold, just as a rule never fires on missing data.

Plan · Rules · try it

Condition

Edit as:
Check againstas of 16 Oct 2026

○ Does not fire for KTKBANK

  • ✗ cmp < w_ema_21 — left 329.85, right 301.54
Figure 10-1: Try it: the condition editor, checked against positions from the example as of 16 October 2026

Rules live in your library, ready to be used in strategies. Let's build some strategies.