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
study()- How outcomes are measured
report()andcompare()- The research report
- Signals without patterns
- Trades must be loaded
A first study#
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()#
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 field | Use |
|---|---|
side | "buy" or "sell". A signal without a side is counted but left out of the outcomes (without_side). |
name | The group in the report (default "signal"). |
time | The instant of the signal (default: the current event). |
price | The entry used if no trade has been seen yet. |
| Parameter | Value | Default | Error |
|---|---|---|---|
horizons | A 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, stop | Distances > 0 (units: a plain number is points) | none | ≤ 0 gives E938. |
cost | Distance ≥ 0, subtracted from each outcome | 0 | Negative gives E938. |
How outcomes are measured#
- Entry = the last traded price at the signal's time. A study made in the body or in an
on: barpattern is dated at the bar's known close, and enters at the last trade at that time. - Horizon outcome = (last traded price at or before t + h − entry) × direction − cost, in ticks. If the data stops before t + h, the outcome is incomplete: it is never extrapolated.
- With a target and/or a stop:
- the target is reached when a trade touches it: + target − cost;
- the stop is taken at the price of the trade that crosses it: (that price − entry) × direction − cost;
- otherwise the trade exits at the longest horizon (reason
time); - MFE and MAE are measured over the longest horizon;
- results are in R (outcome ÷ stop) when there is a stop, otherwise in ticks.
report() and compare()#
report(by: ["name", "side"], ci: 1000)
compare(by: ["side"], ci: 1000)Only in a research script (E938 elsewhere).
by:a text or a list among"name","side"and"rth"(E938 otherwise)."side"is always included.compare()always puts"name"first, to set signal definitions side by side."rth"splits the outcomes intorth(9:30–16:00 New York) andeth(the rest).ci:is the number of bootstrap draws for the confidence intervals, from 0 to 20,000 (default 1,000; 0 = no interval).- No other parameter exists; unknown named arguments are ignored.
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…):
- for each horizon: number of outcomes, win rate, mean and median in ticks, 95% confidence interval (bootstrap over days), and the DEV and VAL means;
- average MFE and MAE;
- with a target or a stop: the share of exits by target, by stop and by time, win rate, the break-even win rate
100 × (stop + cost) / (target + stop), mean, interval, total, maximum drawdown, worst losing streak, DEV and VAL.
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:
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:
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:
on trade(t) { }