MCPcopy Index your code
hub / github.com/kernc/backtesting.py / I

Method I

backtesting/backtesting.py:74–179  ·  view source on GitHub ↗

Declare an indicator. An indicator is just an array of values (or a tuple of such arrays in case of, e.g., MACD indicator), but one that is revealed gradually in `backtesting.backtesting.Strategy.next` much like `backtesting.backtesting.Strategy.data` is.

(self,  # noqa: E743
          func: Callable, *args,
          name=None, plot=True, overlay=None, color=None, scatter=False,
          **kwargs)

Source from the content-addressed store, hash-verified

72 return params
73
74 def I(self, # noqa: E743
75 func: Callable, *args,
76 name=None, plot=True, overlay=None, color=None, scatter=False,
77 **kwargs) -> np.ndarray:
78 """
79 Declare an indicator. An indicator is just an array of values
80 (or a tuple of such arrays in case of, e.g., MACD indicator),
81 but one that is revealed gradually in
82 `backtesting.backtesting.Strategy.next` much like
83 `backtesting.backtesting.Strategy.data` is.
84 Returns `np.ndarray` of indicator values.
85
86 `func` is a function that returns the indicator array(s) of
87 same length as `backtesting.backtesting.Strategy.data`.
88
89 In the plot legend, the indicator is labeled with
90 function name, unless `name` overrides it. If `func` returns
91 a tuple of arrays, `name` can be a sequence of strings, and
92 its size must agree with the number of arrays returned.
93
94 If `plot` is `True`, the indicator is plotted on the resulting
95 `backtesting.backtesting.Backtest.plot`.
96
97 If `overlay` is `True`, the indicator is plotted overlaying the
98 price candlestick chart (suitable e.g. for moving averages).
99 If `False`, the indicator is plotted standalone below the
100 candlestick chart. By default, a heuristic is used which decides
101 correctly most of the time.
102
103 `color` can be string hex RGB triplet or X11 color name.
104 By default, the next available color is assigned.
105
106 If `scatter` is `True`, the plotted indicator marker will be a
107 circle instead of a connected line segment (default).
108
109 Additional `*args` and `**kwargs` are passed to `func` and can
110 be used for parameters.
111
112 For example, using simple moving average function from TA-Lib:
113
114 def init():
115 self.sma = self.I(ta.SMA, self.data.Close, self.n_sma)
116
117 .. warning::
118 Rolling indicators may front-pad warm-up values with NaNs.
119 In this case, the **backtest will only begin on the first bar when
120 all declared indicators have non-NaN values** (e.g. bar 201 for a
121 strategy that uses a 200-bar MA).
122 This can affect results.
123 """
124 def _format_name(name: str) -> str:
125 return name.format(*map(_as_str, args),
126 **dict(zip(kwargs.keys(), map(_as_str, kwargs.values()))))
127
128 if name is None:
129 params = ','.join(filter(None, map(_as_str, chain(args, kwargs.values()))))
130 func_name = _as_str(func)
131 name = (f'{func_name}({params})' if params else f'{func_name}')

Callers 12

initMethod · 0.95
initMethod · 0.80
initMethod · 0.80
initMethod · 0.80
initMethod · 0.80
initMethod · 0.80
set_signalMethod · 0.80
initMethod · 0.80
initMethod · 0.80
initMethod · 0.80
initMethod · 0.80
initMethod · 0.80

Calls 4

_as_strFunction · 0.85
try_Function · 0.85
funcFunction · 0.85
_IndicatorClass · 0.85

Tested by

no test coverage detected