backtrader.signal module

Signal Module - Signal indicator for trading strategies.

This module provides the Signal indicator which wraps a data line to provide trading signal values for strategy execution.

Constants:

SIGNAL_NONE: No signal. SIGNAL_LONGSHORT: Both long and short signals. SIGNAL_LONG: Long entry signal. SIGNAL_LONG_INV: Inverted long signal. SIGNAL_LONG_ANY: Any long signal variant. SIGNAL_SHORT: Short entry signal. SIGNAL_SHORT_INV: Inverted short signal. SIGNAL_SHORT_ANY: Any short signal variant. SIGNAL_LONGEXIT: Long exit signal. SIGNAL_LONGEXIT_INV: Inverted long exit signal. SIGNAL_LONGEXIT_ANY: Any long exit variant. SIGNAL_SHORTEXIT: Short exit signal. SIGNAL_SHORTEXIT_INV: Inverted short exit signal. SIGNAL_SHORTEXIT_ANY: Any short exit variant.

Classes:

Signal: Indicator that wraps a data line as a trading signal.

Example

>>> class MyStrategy(bt.Strategy):
...     def __init__(self):
...         # Create a signal indicator from a data line
...         self.signal = bt.Signal(self.data.close)
...         self.sma = bt.indicators.SMA(self.data.close, period=20)
...
...     def next(self):
...         if self.signal[0] > 0:
...             self.buy()
...         elif self.signal[0] < 0:
...             self.sell()
class backtrader.signal.Signal[source]

Bases: Indicator

Signal indicator for trading strategies.

The Signal class wraps a data line to provide trading signal values for strategy execution. It inherits from Indicator and exposes a single signal line that can be used to generate long/short signals.

SignalTypes

List of available signal type constants.

lines

Tuple containing the signal line name.

Example

>>> class MyStrategy(bt.Strategy):
...     def __init__(self):
...         # Create a signal indicator from a data line
...         self.signal = bt.Signal(self.data.close)
...         self.sma = bt.indicators.SMA(self.data.close, period=20)
...
...     def next(self):
...         if self.signal[0] > 0:
...             self.buy()
...         elif self.signal[0] < 0:
...             self.sell()
SignalTypes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
__init__(*args, **kwargs)
frompackages = ()
packages = ()