Guides

Research

A research script answers the question "what happens after this signal?". You define signals, call study() on each one, and the Report tab (panel mode Parameters) measures the outcomes: price change after fixed horizons, and optionally the outcome of a target and a stop.

A first study#

StrateScript
research "Up-bar follow-through" { overlay: true }
minDelta = input.int("Min delta", 20)

pattern "Up bar" {
  on: bar
  side: "buy"
  when close > open
   and delta >= minDelta
}

pattern "Down bar" {
  on: bar
  side: "sell"
  when close < open
   and delta <= -minDelta
}

on signal(s) {
  study(s, horizons: [1m, 5m, 15m], target: 8 ticks, stop: 4 ticks, cost: 1 tick)
}

// the study is measured on trades: this handler makes sure they are loaded on the chart
on trade(t) { }

report(by: ["name", "side", "rth"], ci: 1000)

study()#

Text
study(s, horizons: [1m, 5m, 15m], target:, stop:, cost: 0)

Only in a research script (E938 elsewhere, including in a strategy).

**The signal s** is the signal received by on signal(s …), **or any object of your own type with the fields time, side, name and price** (see Signals without patterns). Anything else gives E938.

Signal fieldUse
side"buy" or "sell". A signal without a side is counted but left out of the outcomes (without_side).
nameThe group in the report (default "signal").
timeThe instant of the signal (default: the current event).
priceThe entry used if no trade has been seen yet.
ParameterValueDefaultError
horizonsA list of durations > 0, or one duration[1m, 5m, 15m]In a list, items that are not durations > 0 are dropped without a message; E938 if none is left, or if a single value is not a duration > 0.
target, stopDistances > 0 (units: a plain number is points)none≤ 0 gives E938.
costDistance ≥ 0, subtracted from each outcome0Negative gives E938.

How outcomes are measured#

report() and compare()#

Text
report(by: ["name", "side"], ci: 1000)
compare(by: ["side"], ci: 1000)

Only in a research script (E938 elsewhere).

The research report#

At the top: the number of signals, signals studied, signals without a side, incomplete studies, days, the DEV/VAL split day, and the number of settings the script declares.

Then one block per group (by name, side, rth…):

DEV / VAL. On a chart, the days that have signals are split in two, with the cut at day index ⌊n/2⌋ (this differs from strategies, which split the sessions of the loaded bars). With fewer than 2 days there is no split. In a long backtest, the VAL start is declared before running (see Strategies).

Keep one geometry per group. The target, stop and cost shown in a group's block are those of the group's first study, and the break-even win rate is computed from them. If you study the same group with different targets or stops, give each geometry its own name.

Use the same horizons in the whole script. The report's horizon columns are those of the script's first study, for every group. The outcomes of the other studies are listed by position: the second horizon of a study is shown under the second horizon of the first study. A study with horizons: [30s, 2m] next to one with [1m, 5m, 15m] is therefore shown under the wrong horizons.

Signals without patterns#

Any object of your own type with time, side, name and price fields can be studied. This lets you study raw events directly, without writing a pattern:

StrateScript
research "Large prints" { overlay: true }
type Event { time: time, side: string, name: string, price: float }
minSize = input.int("Min size", 5, min: 1)

on trade(t) {
  if t.size >= minSize {
    study(Event { time: t.time, side: t.delta > 0 ? "buy" : "sell", name: "Large print", price: t.price },
          horizons: [30s, 2m], target: 6 ticks, stop: 6 ticks, cost: 1 tick)
  }
}

report(by: ["name", "side"])

Two definitions compared side by side, each with its own name:

StrateScript
research "Sweep follow-through" { overlay: true }
type Event { time: time, side: string, name: string, price: float }

on trade(t) {
  if not na(t.levels) and t.sweep_end {
    nm = t.levels >= 3 ? "Sweep 3+ levels" : "Sweep 1-2 levels"
    study(Event { time: t.time, side: t.is_buy ? "buy" : "sell", name: nm, price: t.price }, horizons: [30s, 2m, 5m])
  }
}

compare(by: ["side"])

Trades must be loaded#

Outcomes are measured on trades. Without trades, every study is incomplete and the report shows n = 0. In a long backtest trades are always there. On a chart, trades are loaded only when the script's text uses them (see Market data): on trade, a pattern on: trade or on: level, a clock, or an of.flow-type function. A research script whose signals come only from bars should therefore include an empty handler:

Text
on trade(t) { }
STRATE / PRODUCT SCREENSHOT

Actual platform screenshot.