Guides

Patterns, clocks and signals

Besides the body that runs once per bar, a script can react to market events, define named patterns that emit signals, and build its own clocks.

Event handlers#

HandlerRuns forDetails
on trade(t)every tradeMarket data
on order(o)every order-by-order add, cancel, modify, fillOrder flow
on match(m)every aggressive executionOrder flow
on book(b)every book snapshot (250 ms)Order flow
on session(s)each session boundaryMarket data
on signal(s) / on signal(s from "Name") / on signal(s from alias)each signal of a patternbelow
on clock nameeach tick of a custom clockbelow
on fill(f), on exit(x)fills and closed trades of a strategyStrategies

Patterns#

A pattern is a named condition evaluated on a stream of events. Each time the condition becomes true it emits a signal: a mark on the chart (a diamond, green for "buy", red for "sell", violet without a side) that other code can listen to.

Text
pattern "Name" {
  on: trade                  // the event stream: trade, level, match, order, book or bar
  side: "buy"                // optional
  price: expression          // optional
  cooldown: 30s              // optional
  confirm: 2s                // optional
  show: [value, value]       // optional
  statements…                // computed at each event
  when condition             // exactly one
}
SettingValueError
on: (required)trade, level, match, order, book or barE934 if missing or unknown
side:"buy", "sell" (also "long", "short"), a signed number, or naE934
price:The signal's price. By default, the event's price (the bar's close for on: bar).—
cooldown:Minimum delay between two signals at the same price and side. A duration; a plain number is read as seconds, na as 0.E934 otherwise
confirm:The condition must stay true for at least this long before the signal is emitted. A duration; a plain number is read as seconds, na as 0.E934 otherwise
show:Values added to the signal's explanation.—

The explanation. Each signal carries a text that lists each top-level and term of when with its values, then the show: values, for example lv.replenishments = 1 ≥ minRefills = 1.

StrateScript
indicator "Big bars" { overlay: true }
minRank = input.float("Min volume rank", 95)
pattern "Big bar" {
  on: bar, cooldown: 3m
  side: close > open ? "buy" : "sell"
  show: [volume]
  rk = stats.rank(volume, 100)
  when rk >= minRank
   and close != open
}
on signal(s from "Big bar") { label.new(s.time, s.price, s.side) }
StrateScript
indicator "Defended ask" { overlay: true, level_history: 2m }
minRefills = input.int("Min refills", 1, min: 1)
pattern "Defended ask" {
  on: order
  side: "sell"
  cooldown: 30s
  lv = order.is_add and order.side == "ask" ? mbo.level(order.price, window: 60s) : na
  when not na(lv)
   and lv.replenishments >= minRefills
}
on signal(s from "Defended ask") { label.new(s.time, s.price, s.explain) }
plot(close, title: "Close")

Listening to signals: on signal#

Text
on signal(s) { … }                    // every pattern of the script
on signal(s from "Name") { … }        // one pattern
on signal(s from alias) { … }         // every pattern of an imported script
FieldValue
s.namePattern name.
s.timeTime of the signal.
s.priceIts price (na if none).
s.side"buy", "sell" or na.
s.startWhen the condition became true (before confirm:).
s.explainThe explanation text.

from "Name" with no such pattern gives E934, with the list of patterns. In a research script, the signal is what you pass to study(); in a strategy, you can act on it with enter().

signal(...), the function, is something else: it only draws a small mark at the top of the chart, whatever its first argument. It draws at most one mark per call place and per bar (a later call at the same place on the same bar replaces it). Put it inside an if.

Custom clocks#

A clock fires at moments you define, independently of the chart's bars.

Text
clock name = clock.time(1s)
on clock name { … }

Clocks are evaluated after each market event (trade, order-by-order event, book snapshot):

ClockFires
clock.time(d)On the first event at or after each multiple of the duration d (aligned on the clock: 1m fires at each new minute).
clock.volume(n)Each time n contracts have traded since it last fired.
clock.trades(n)Each time n trades have occurred since it last fired.
clock.events(n)Every n market events of any kind.
clock.range(d)Each time the traded high and low since it last fired are d apart (a price distance, e.g. clock.range(4 ticks)).
when(condition)Each time the condition becomes true (it must turn false before firing again).

The thresholds (d, n) are evaluated once, when the script starts; they must be positive (E935 otherwise).

StrateScript
indicator "Imbalance clock" { overlay: true }
threshold = input.int("Imbalance", 200, min: 1)
state theta = 0
on trade(t) { theta += t.delta }
clock vib = when(abs(theta) >= threshold)
on clock vib {
  label.new(event.time, close, str(theta), color: theta > 0 ? #26a69a : #ef5350)
  theta = 0
}
plot(close, title: "Close")
StrateScript
indicator "One-second sampler" { overlay: false }
clock sec = clock.time(1s)
state spreadSum = 0
state n = 0
on clock sec {
  if book.available {
    spreadSum += book.spread / instrument.tick
    n += 1
  }
}
plot(n > 0 ? spreadSum / n : na, #2962ff, title: "Mean spread (ticks)")
spreadSum = 0
n = 0

The shipped pattern library#

Strate ships definitions written in StrateScript, imported from strate/patterns/<file>. They are competing definitions: none is a default, and each exposes its thresholds as settings you can change or override in the import line.

FileDefinition
absorption_held_volumeHigh aggressor volume at a price that stayed at the best bid or ask for a share of the window, and is still displayed.
absorption_replenishmentHigh aggressor volume at a price that was emptied and refilled many times.
absorption_exhaustionHigh aggressor volume at a price while that side's aggressor flow is dying out.
absorption_hiddenAggressors traded far more than the displayed orders filled at that price (volume never shown).
refill_same_orderA level emptied and refilled many times by an order already seen at that price.
refill_new_ordersA level emptied and refilled many times by new orders.
sweepOne aggressive order that traded through several prices, for a large total size; marked once per order.
stop_runA trade beyond the recent high or low, inside an unusual burst of same-side trades.
spoofing_pulled_before_touchA large order placed away from the best price, resting a while, never filled, canceled in full as price came closer.
liquidity_pullLarge size canceled at or next to the best price, mostly without being traded against.

Most of them need order-by-order data. Each file starts with a comment that states its definition precisely.

The script menu of the StrateScript panel lists them as Definition: entries. Choosing one adds its code to the chart as a new script: it is ordinary StrateScript, so you can read it, run it, or copy it as the starting point of your own definition.

StrateScript
indicator "Two absorption definitions" { overlay: true }
import "strate/patterns/absorption_held_volume" as strict { minVol: 99 }
import "strate/patterns/absorption_held_volume" as loose { minVol: 80 }
on signal(s from strict) { label.new(s.time, s.price, "strict") }
on signal(s from loose) { label.new(s.time, s.price, "loose") }

The keys you can override are the names of the imported script's top-level declarations. For absorption_held_volume they are window, held, normWindow, volUnit and minVol.

STRATE / PRODUCT SCREENSHOT

Actual platform screenshot.