Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Machine learning + backtrader

    General Discussion
    8
    27
    19115
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      junajo10 last edited by

      Hi,

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

      Rgds,

      Jj

      1 Reply Last reply Reply Quote 2
      • B
        backtrader administrators last edited by

        Not at the moment.

        1 Reply Last reply Reply Quote 0
        • J
          junajo10 last edited by

          Ok, thanks. In the future if you consider it please let me know. I would like to suggest some ideas

          1 Reply Last reply Reply Quote 0
          • RandyT
            RandyT last edited by

            Please suggest some ideas. This is an area I plan to go in near future after getting my head fully around current backtrader capability.

            1 Reply Last reply Reply Quote 0
            • R
              revelator101 last edited by

              What's stopping you from doing any machine learning? Why cant you just import the libraries, run the models & execute based on the parameters?

              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

              1 Reply Last reply Reply Quote 1
              • RandyT
                RandyT last edited by

                Great thread. Thanks for sharing.

                1 Reply Last reply Reply Quote 0
                • J
                  junajo10 last edited by

                  Hi,

                  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

                  1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader last edited by

                    At this point machine learning opportunities are endless from both ML feature building and from ML algo selecting points of view. imho implementing and further support of special ML functions/procedures into bt will be just wasting of time.

                    • If my answer helped, hit reputation up arrow at lower right corner of the post.
                    • Python Debugging With Pdb
                    • New to python and bt - check this out
                    1 Reply Last reply Reply Quote 2
                    • J
                      junajo10 last edited by

                      hi,

                      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 .

                      R 1 Reply Last reply Reply Quote 0
                      • R
                        remroc @junajo10 last edited by

                        @junajo10 I maybe we could give it a try if you have an article with an example...

                        1 Reply Last reply Reply Quote 0
                        • J
                          junajo10 last edited by

                          Here, i have found a case that could be a good starting point.

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

                          Kalman filter : Pairs trading

                          R 1 Reply Last reply Reply Quote 0
                          • R
                            remroc @junajo10 last edited by

                            @junajo10 Thanks Junajo10 ! I need some documentation to understand this filter and it is related to ML ?

                            Do you have any tips ?

                            Thanks,

                            Remroc

                            1 Reply Last reply Reply Quote 0
                            • J
                              junajo10 last edited by

                              https://www.quantopian.com/posts/quantopian-lecture-series-kalman-filters

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

                              http://numericalmethod.com/papers/course1/lecture5.pdf

                              1 Reply Last reply Reply Quote 1
                              • Maxim Korobov
                                Maxim Korobov last edited by Maxim Korobov

                                From 101 (csv, pandas) to some ML usage and only for trading + code, not just ML + code or trading theory.
                                https://www.udacity.com/course/machine-learning-for-trading--ud501

                                1 Reply Last reply Reply Quote 1
                                • R
                                  remroc last edited by

                                  thanks guys. really good stuff :thumbsup:

                                  1 Reply Last reply Reply Quote 0
                                  • R
                                    remroc last edited by

                                    Hi guys,

                                    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

                                    1 Reply Last reply Reply Quote 0
                                    • J
                                      junajo10 last edited by

                                      https://github.com/quantopian/research_public/blob/master/lectures/kalman_filter/kalman_filter_pair_trade.py

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

                                      1 Reply Last reply Reply Quote 0
                                      • B
                                        backtrader administrators last edited by backtrader

                                        @junajo10 said in Machine learning + backtrader:

                                        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.

                                        Maxim Korobov 1 Reply Last reply Reply Quote 2
                                        • J
                                          junajo10 last edited by

                                          Thanks @backtrader .

                                          @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

                                          RandyT 1 Reply Last reply Reply Quote 0
                                          • Maxim Korobov
                                            Maxim Korobov @backtrader last edited by

                                            @backtrader, add this filter to main package.

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors