Do you have on your mind to add any machine learning library in backtrader or any ml sample?

Rgds,

Jj

]]>Initialize counter in the `__init()__`

, add 1 to the counter every `next()`

call. If it equals to 30 call your function and zero counter. You may want to call your function also at the very beginning of the cycle in the `prenext()`

call.

Here is what I have with the alternate calculation commented out.

```
lag = series.shift(1)
lag.ix[0] = lag.ix[1]
s_ret = series - lag
s_ret.ix[0] = s_ret.ix[1]
lag2 = sm.add_constant(lag)
model = sm.OLS(s_ret, lag2)
res = model.fit()
# halflife = round(-np.log(2) / res.params[1], 0)
halflife = np.log(0.5) / np.log(np.abs(res.params[1]))
```

]]>The `KalmanMovingAverage`

should be something like this. The actual calculation can yield values already with the 2nd data tick, but to make it similar to other moving averages and avoid the actual very steep ramp-up phase (seen in the Quantopian chart), the `period`

parameter is used to decide when to start delivering values, skipping the initial warm-up.

```
import pykalman
class KMA(bt.indicators.MovingAverageBase):
alias = ('KalmanMovingAverage',)
lines = ('kma',)
params = (
('initial_state_covariance', 1.0),
('observation_covariance', 1.0),
('transition_covariance', 0.05),
)
def __init__(self):
self.addminperiod(self.p.period) # when to deliver values
self._dlast = self.data(-1) # get previous day value
def nextstart(self):
self._k1 = self._dlast[0]
self._c1 = self.p.initial_state_covariance
self._kf = pykalman.KalmanFilter(
transition_matrices=[1],
observation_matrices=[1],
observation_covariance=self.p.observation_covariance,
transition_covariance=self.p.transition_covariance,
initial_state_mean=self._k1,
initial_state_covariance=self._c1,
)
self.next()
def next(self):
k1, self._c1 = self._kf.filter_update(self._k1, self._c1, self.data[0])
self.lines.kma[0] = self._k1 = k1
```

An example getting it compared to the `SimpleMovingAverage`

and `ExponentialMovingAverage`

.

@backtrader said "The difficult thing here is to select assets for which it makes sense to apply the filter".

For solving thsi problem use these tecniques:

Augmented Dickey Fuller

Hurst exponent

Half Life

https://www.quantstart.com/articles/kalman-filter-based-pairs-trading-strategy-in-qstrader

Here is a version of the `KalmanFilter`

described in that article (seems 1-to-1 identical to the one that implements Ernie Chan's strategy)

```
import numpy as np
import pandas as pd
class KalmanFilter(bt.Indicator):
_mindatas = 2 # needs at least 2 data feeds
lines = ('et', 'sqrt_qt',)
params = dict(
delta=1e-4,
vt=1e-3,
)
def __init__(self):
self.wt = self.p.delta / (1 - self.p.delta) * np.eye(2)
self.theta = np.zeros(2)
self.P = np.zeros((2, 2))
self.R = None
self.d1_prev = self.data1(-1) # data1 yesterday's price
def next(self):
F = np.asarray([self.data0[0], 1.0]).reshape((1, 2))
y = self.d1_prev[0]
if self.R is not None: # self.R starts as None, self.C set below
self.R = self.C + self.wt
else:
self.R = np.zeros((2, 2))
yhat = F.dot(self.theta)
et = y - yhat
# Q_t is the variance of the prediction of observations and hence
# \sqrt{Q_t} is the standard deviation of the predictions
Qt = F.dot(self.R).dot(F.T) + self.p.vt
sqrt_Qt = np.sqrt(Qt)
# The posterior value of the states \theta_t is distributed as a
# multivariate Gaussian with mean m_t and variance-covariance C_t
At = self.R.dot(F.T) / Qt
self.theta = self.theta + At.flatten() * et
self.C = self.R - At * F.dot(self.R)
# Fill the lines
self.lines.et[0] = et
self.l.sqrt_qt[0] = sqrt_Qt
```

The filter per-se is not directly used in the decision making process. Knowing (from the article) which relations `et`

and `sqrt_qt`

need to have to trigger signals, the behavior can be packed in another indicator

```
class KalmanSignals(bt.Indicator):
_mindatas = 2 # needs at least 2 data feeds
lines = ('long', 'short',)
def __init__(self):
kf = KalmanFilter(self.data0, self.data1)
et, sqrt_qt = kf.lines.et, kf.lines.sqrt_qt
self.lines.long = et < -1.0 * sqrt_qt
# longexit is et > -1.0 * sqrt_qt ... the opposite of long
self.lines.short = et > sqrt_qt
# shortexit is et < sqrt_qt ... the opposite of short
```

And the signals can be easily applied in the strategy `next`

method for an easy to follow logic

```
class St(bt.Strategy):
params = (
)
def __init__(self):
self.ksig = KalmanSignals(self.data0, self.data1)
def next(self):
size = self.position.size
if not size:
if self.ksig.long:
self.buy()
elif self.ksig.short:
self.sell()
elif size > 0:
if not self.ksig.long:
self.close()
elif not self.ksig.short: # implicit size < 0
self.close()
```

The difficult thing here is to select assets for which it makes sense to apply the filter.

]]>For me the problem is to migrate this code to backtrader .

]]>Here is an implementation of Kalman filter in a quantopian algo :

https://www.quantopian.com/posts/ernie-chans-ewa-slash-ewc-pair-trade-with-kalman-filter

Let's tackle this...

Remroc

]]>https://www.udacity.com/course/machine-learning-for-trading--ud501 ]]>

https://www.quantstart.com/articles/kalman-filter-based-pairs-trading-strategy-in-qstrader

]]>Do you have any tips ?

Thanks,

Remroc

]]>https://www.quantopian.com/posts/582de686358fe9440b0007f2#587e9dae9ea10879ef9ecb16

Kalman filter : Pairs trading

]]>I was thinking more in defining a style guide that how to combine bt with the best ML libraries. For example in the regression linear example, the use is not direct .

]]>In the first phase the kind of things that i want to learn is the indicators that i should use depend on the asset class

https://www.linkedin.com/pulse/selecting-your-indicators-machine-learning-tad-slaff

Finally q-learning Could be the target

]]>There's plenty of examples and ideas on Quantopian in python:

https://www.quantopian.com/posts/machine-learning-on-quantopian-part-3-building-an-algorithm

]]>