Indicators

Indicators are the building blocks and key entities in the library. It is straightforward to use them, such as in this snippet

import btalib
import pandas as pd

df = pd.read_csv('2006-day-001.txt', parse_dates=True, index_col='Date')
sma = btalib.sma(df)

And that is all what is needed to calculate a Simple Moving Average on the closing price of the input.

There is of course some machinery and boilerplate in the background, because Indicators are objects and many indicators are defined by re-using other indicators and applying indicators on indicators.

In order to make things as easy, straightforward and declarative as possible, indicators do define some concepts, which are explained below with the stochastic indicator as example. Some of the concepts have the same meaning but different implementation details, when comparing what a class and the instance of class offer.

The class is accessed as: btalib.stochastic and the instance with the variable stoc after executing:

stoc = btalib.stochastic(some_inputs)

Inputs

Defined as an iterable of names, which will generally be used to match the actual data in the input. See the section Indicator Input for a detailed explanation.

  • class attribute

    In this case the inputs as in btalib.stochastic.inputs is simply an iterable which contains the name of the inputs expected by the indicator

    The actual inputs for the stochastic are: 'high', 'low', 'close'

  • instance attribute

    In this case the inputs are reachable again as stochastic.inputs (and the shorthand stochastic.i, but the attribute contains and object which works offers dot-notation, and iterable and mapping interfaces.

    Input close can be accessed in the instance as follows (self is used, because inputs are usually only accessed when working inside the indicator, change self to stoc for accessing the inputs over the indicator instance)

    • self.inputs.close
    • self.i.close

    • self.inputs[2] (0 for high, 1 for low and 2 for close)

    • self.i[2]

    • self.input2

    • self.i2

    • self.inputs['close']

    • self.i['close']

    • self.input_close

    • self.i_close

    The choice of notations should be enough to satisfy each and every type of user.

Hint

For users who have worked with backtrader, the library defines additional aliases in the form of self.datas[i], self.d[i] self.datai, self.di, where i in each of the notation examples is the index corresponding to the input definition. For ´closefor example, one would haveself.datas[i]`

self.data is an alias to self.data0 is also defined

Note

In most cases only users developing indicators will ever have to directly deal with "Inputs"

Outputs

Defined as an iterable of names, which will be used to access the actual calculations of the indicator. See the section Indicator Output for a detailed explanation.

  • class attribute

    In this case the inputs as in btalib.stochastic.outputs is simply an iterable which contains the name of the outputs to be generated by the indicator

    The actual outputs of the stochastic are: 'k', 'd'

  • instance attribute

    In this case the outputs are reachable again as stochastic.outputs (and the shorthand stochastic.o, but the attribute contains and object which works offers dot-notation, and iterable and mapping interfaces.

    Input k can be accessed in the instance as:

    • stoc.k (yes ... directly in the instance if wished)
    • stoc.outputs.k
    • stoc.o.k

    • stoc.outputs[0] (for output d the index would be 1)

    • stoc.o[0]

    • stoc.output0

    • stoc.o0

    • stock.outputs['k']

    • stock.o['k']

    The choice of notations should be enough to satisfy each type of user.

Hint

For users who have worked with backrader the library aliases self.outputs to self.lines and self.o to self.l. The outputs are named "lines" in backtrader

Outputs - DataFrame

With an indicator instance in the hand, one can access all outputs in DataFrame format with the df attribute.

impot btalib
import pandas as pd

df = pd.read_csv('2006-day-001.txt', parse_dates=True, index_col='Date')
sma = btalib.sma(df, period=15)
print(sma.df)

Et voilá!

Outputs - Series

There is a direct series access attribute in the instance of the indicators with stoc.series. It offers direct access to the underlying panda.Series structures for each declared output

As in the case of the inputs and outputs it has dot-notation, and iterable and mapping interfaces.

The outputs can be retrieved by simply unpacking the iterable

k_series, d_series = stoc.series

And also by using **kwargs notation

series_dict = dict(**stoc.series)  # or dict(stoc.series)

See the section Indicator Output for a more detailed explanation.

Outputs - Iterable/Mapping

The indicator itself offers also iterable and mapping interfaces to retrieve the outputs. Not directly of the series as shown above, but the internal objects representing the outputs, which can be fed to other indictator.

Retrieving the outputs unpacking the iterable

k_out, d_out = stoc

Retrieving the outputs unpacking over the mapping interface

output_dict = dict(**stoc)  # or dict(stoc)

Params

Indicators define params so that parametrization via **kwargs during instantiation is easy.

The class attribute holds the default defined values as a dict

impot btalib

print(btalib.sma.params)
{`period': 30}

When instantiating the parameters can be customized and later fetched over the params (or the shorthad p) instance (which is no longer a dict but tries as possible to be, whilst offering dot-notation

impot btalib
import pandas as pd

df = pd.read_csv('2006-day-001.txt', parse_dates=True, index_col='Date')
sma = btalib.sma(df, period=15)
print(sma.params.period)
print(sma.p.period)
print(sma.params['period'])
print(list(sma.params))
print(dict(sma.params))

with the expected output showing up

15
15
15
['period']
{'period': 15}