Skip to content

Threshold-crossing events (ta.xsignals)

View Markdown

Convert input signal threshold crossings into trend, entry, and exit events.

Indicator catalog · How to use indicators

Run this example

This complete script uses bundled data. It observes the selected output without placing trades.

import numpy as np
from openbacktesting import Backtest, Strategy
from openbacktesting.test import GOOG

data = GOOG.copy()
data["signal"] = data.Close.pct_change() * 100

class Example(Strategy):
    def init(self):
        self.value = self.ta.xsignals(inputs={'signal': 'signal'})
        self.latest_value = None

    def next(self):
        channel = self.value.trend
        if channel.valid[-1]:
            self.latest_value = float(channel[-1])

result = Backtest(data, Example).run()
print(result["_strategy"].latest_value)

Example inputs: signal is the percentage change in close.

Call and parameters

self.ta.xsignals(
    source=None,
    *,
    xa=0,
    xb=0,
    above=True,
    long=True,
    asbool=False,
    trade_offset=0,
    inputs=None,
    backend=None,
    required_outputs=None,
)

Register in init(). Read the returned channel in next() after checking valid[-1].

Parameter Meaning Type Default Allowed values
xa Entry threshold in the input signal units. int 0 No additional constraints
xb Exit threshold in the input signal units. int 0 No additional constraints
above Use upward entry-threshold crossings; False uses downward crossings. bool True No additional constraints
long Track long instead of short signal state. bool True No additional constraints
asbool Return boolean signal channels instead of numeric flags. bool False No additional constraints
trade_offset Nonnegative bar shift of entry/exit signal series. int 0 Minimum: 0

source/named source arguments accept a column name or same-run single-channel handle. Alternatively, inputs maps each input below to a column or handle. backend selects a supported implementation; required_outputs selects channels for readiness diagnostics. See shared arguments and handles.

Inputs and outputs

Required inputs: signal.

Channel Meaning
trend Trend output
trades Signed change in trend state
entries Entry signal
exits Exit signal

Reading values safely

Insufficient history or missing inputs can make a channel invalid. Check that channel's valid mask; ready is diagnostic and never suppresses next(). Multi-output results may have different valid channels on the same bar.

Indicator catalog · Custom calculations