Machine learning + backtrader
-
From the Quantopian Lecture on Kalman.
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), theperiod
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
andExponentialMovingAverage
. -
@junajo10 Do you have any suggestions for the Half Life algorithm? I have run across a couple of them which give vastly different results.
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]))
-
Some additional reference would be needed.
-
I've found this link which offers other links to relevant info and the resulting two algorithms I have attempted. Values of the two formulas are very different with one of the formulas returning negative values. As I understand Half-Life of a time series, it indicates some time value for complete cycle of the reversion to mean process. However, given the results, I'm not quite grasping the measurement value and how it applies to the concept of "time".
-
@backtrader @administrators How to update my ML model(example decision tree)after 30 next (bar, cycle)? Pls write example code? :)
-
Initialize counter in the
__init()__
, add 1 to the counter everynext()
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 theprenext()
call. -
ok, many thanks @ab_trader