Hi,
Do you have on your mind to add any machine learning library in backtrader or any ml sample?
Rgds,
Jj
Hi,
Do you have on your mind to add any machine learning library in backtrader or any ml sample?
Rgds,
Jj
ok, thanks, i will wait up Standard Deviation. Congrats on the community!!!!
This code could be adapted to use two timeseries backtrader:
https://github.com/vikasrtr/pyLinearRegression/blob/master/models/LinearRegression.py
https://github.com/vikasrtr/pyLinearRegression/blob/master/models/LinearRegressionGradientDescent.py
def linreg(X, Y):
"""
Linear regression y = ax + b
"""
if len(X) != len(Y): raise ValueError, 'unequal length'
N = len(X)
Sx = Sy = Sxx = Syy = Sxy = 0.0
for x, y in map(None, X, Y):
Sx = Sx + x
Sy = Sy + y
Sxx = Sxx + x*x
Syy = Syy + y*y
Sxy = Sxy + x*y
det = Sxx * N - Sx * Sx
a, b = (Sxy * N - Sy * Sx)/det, (Sxx * Sy - Sx * Sxy)/det
meanerror = residual = 0.0
for x, y in map(None, X, Y):
meanerror = meanerror + (y - Sy/N)**2
residual = residual + (y - a * x - b)**2
RR = 1 - residual/meanerror
ss = residual / (N-2)
Var_a, Var_b = ss * N / det, ss * Sxx / det
return a, b, RR
if X and Y are cointegrated:
calculate Beta between X and Y
calculate spread as X - Beta * Y
calculate z-score of spread
# entering trade (spread is away from mean by two sigmas):
if z-score > 2:
sell spread (sell 1000 of X, buy 1000 * Beta of Y)
if z-score < -2:
buy spread (buy 1000 of X, sell 1000 * Beta of Y)
# exiting trade (spread converged close to mean):
if we're short spread and z-score < 1:
close the trades
if we're long spread and z-score > -1:
close the trades
loop: repeat above on each new bar, recalculating rolling Beta and spread etc.
Thanks, i was using the above code, i have changed and now it is working well.
self.ols=OLS_Transformation(self.data0,self.data1)
self.spread_mean = bt.indicators.MovingAverageSimple(self.ols.spread, period=self.p.period)
self.spread_std = bt.indicators.StandardDeviation(self.ols.spread, period=self.p.period)
I think a new feature is mandatory, check cointegration between both pairs.
import pandas as pd
import backtrader as bt
class is Cointegrated(bt.indicators.PeriodN):
_mindatas = 2 # ensure at least 2 data feeds are passed
lines = (('cointegration'),)
def next(self):
y, x = (d for d in (self.data0, self.data1))
results = coint(x,y)
self.lines.cointegration[0] = results[1]
# you should define a p-value limit and compare with the line value before operating
I have tested and it is working well. could you add in the next release? Also, if you consider important, half-life quantopian post help people to calibrate the period in the pair trading strategy.
https://www.quantopian.com/posts/pair-trade-with-cointegration-and-mean-reversion-tests