Backtrader Community

    • 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/

    Is backtrader event driven or vectorized?

    General Discussion
    4
    6
    3316
    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.
    • T
      Taewoo Kim last edited by

      Sorry for the complete noob question. I am new to backtrader, but it looks promising

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

        backtrader takes a dual approach to the problem. This is controlled with the runonce (boolean) parameter to either a instantiation of Cerebro or to cerebro.run like in

        The default is runonce=True

        cerebro = Cerebro(runonce=True)  # or False
        

        or

        cerebro = Cerebro()
        ...
        ...
        cerebro.run(runonce=True)  # or False
        

        1. runonce=True

        This could be called a pseudo-vectorized of half-vectorized approach. Built-in operations feature a once method which calculate things in batch mode in a tight inner loop.

        • Data feeds are fully pre-loaded

        • Indicators (and sub-indicators thereof) are pre-calculated in batch-mode

        • Then, the Strategy instance(s) are run step-by-step

        Being the goal to offer an increase in speed, but still allow for fine grained logic in the next method of the strategy

        Rough calculations indicate that it is somehow between 20-30% than runonce=False

        Drawback: Because indicators are precalculated (and therefore the buffers are preallocated), the data synchronization mechanism cannot pause the actual movement of a data feed when synchronizing the timestamps for the strategy, keeping the buffers to the final same length. This has no actual impact for backtesting but because matplotlib expects all things to have the same x length for plotting, it may not be possible to create a plot of the backtesting.

        Drawback 2: The implementation of this mode prevented that some indicators can be fully defined in recursive terms with a single formula. A choice had to be made between having this or having the recursive formulas.

        Nice Thing: If a user implements a custom Indicator and only provides a next method (intented for step-by-step, see below), the code automatically detects it and will still pre-calculate the indicator using the next method instead of the missing once method. The calculation loop will not be so tight as it could be, but users don't have to worry about implementing once

        2. runonce=False

        This is a 100% step-by-step mode. Also named next because only the next method of the different indicators, strategies et al., play a role.

        Everything is calculated one step at a time. The reason being the addition of data feeds which would be providing the data points one step at a time (not necessarily live feeds, it could have been reading out of a socket from a database connection).

        If cerebro is run with preload=False (disable the preloading of data feeds) it will switch to this mode.

        S 1 Reply Last reply Reply Quote 3
        • S
          shaodaodao @backtrader last edited by

          @backtrader
          when I turn on the runonce=False,(nothing else changed) the final value is greatly reduced...
          from 16700 to 2700...
          Really confused why. Could you please give some explains? Many thanks!

          vladisld 1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld @shaodaodao last edited by

            @shaodaodao Changing the value of runonce parameter should have no influence on the back testing / optimizations results - it's purely a performance optimization ( at least in theory )

            There is a small chance that there is bug of cause - but I suspect there is something in your code that may interfere with the way the framework is supposed to work - however without seeing the full code - it is hard to tell.

            If you could provide your code ( or just a sample of it that demonstrates the issue) + sample data - we'll be able to tell for sure.

            S 1 Reply Last reply Reply Quote 1
            • S
              shaodaodao @vladisld last edited by

              @vladisld
              I found an interesting issue.
              Because I used bt.talib.BBands and customized some parameters, then after cerebro run the final value is huge different for runonce=true or false.
              But when I switched to Backtrader's own BBands and set the same parameters, this bug doesn't exist.

              So I guess the connection with ta-lib has some issues for runonce selection.

              vladisld 1 Reply Last reply Reply Quote 0
              • vladisld
                vladisld @shaodaodao last edited by

                @shaodaodao said in Is backtrader event driven or vectorized?:

                Because I used bt.talib.BBands and customized some parameters, then after cerebro run the final value is huge different for runonce=true or false

                That's interesting. As I've said above, it would be great if you could share some code sample + data (simplified as you wish) - so we could play with it and see what causes this discrepancy.

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