For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:


  • Hi guys, it's been a couple days for me to start backtrader, before then I used zipline from Quantopian or bt (not backtrader, but the other one)..

    Obviously, this is one of the most comprehensive open source backtesting platform, but I don't know why I really confused with the codes.. So instead of building my sklearn indicators inside the system, I use my extended datafeed, but failed to feed it into the system......

    Can somebody help me to give example how feed the extended code and build the signals? I have read all the documentation, including extending datafeed but always failed to test it. Here's my datafeed, the idea is I'll buy when daily mean states > 0 and sell when daily mean states < 0. Thanks


  • administrators

    See for example here:

    class CustomDataLoader(btfeeds.PandasData):
        lines = ('TOTAL_SCORE','Beta',)
        params = (
            ('openinterest',None), #None= column not present
        datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])

    and here

    class PandasDataOptix(btfeeds.PandasData):
        lines = ('optix_close', 'optix_pess', 'optix_opt',)
        params = (('optix_close', -1),
                  ('optix_pess', -1),
                  ('optix_opt', -1))
        datafields = btfeeds.PandasData.datafields + (

    You have to add the name of your extra lines to the lines definition and extend the datafields definition.

    Once you have the lines available you only have to check in next:

    def next(self):
        if > 0:

    Where of course, position management may also play a role.

Log in to reply