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


  • administrators

    Change the license to something else, I don't like GPL, it's too restrictive

    The sources contain over 32k lines of python, plus 12k lines in samples, plus 25k lines in reStructuredText documentation. For sure there are comments, license boilerplate and empty lines. All can be freely accessed and further shared for the total cost of None. It seems to be really restrictive

    It is slow, speed it up

    This is a python based platform. Python has many advantages but raw speed is for sure not one of them.

    Use numpy, pandas or something else like numba, nuitka or the latest super-mega-pythonizer-plus to speed it up

    This project has the clear goal of being Pure-Python only and that goal is not going to be removed from the 1.x versions. To aid those wanting to integrate code with needs like for example numpy, the functionality packages and frompackages was done.

    The platform is slow, heavy, misses so many features and the API and usage patterns are awful

    Yes, backtrader could have a formula an solution for each and every case in the world but it doesn't (at least yet) and even a different API, but it doesn't. It supports gazillion things, but this may not be enough.

    The front page of the repository ( has a listing with several other open source Python platforms you may choose from if they better suit your needs. Namely:

    If after seeing the docs and some samples (see the blog also) you feel this is not your cup of tea, you can always have a look at similar Python platforms:

    There is no specific endorsement of any of them. Pick your own.

    My simple algorithm does nothing and here is a log of the datetime stamps


    You are feeding the platform with the data in reverse order from the future to the past. Usual cases:

    • You are using the YahooFinanceData feed (online download from Yahoo) and for whatever reason you have set this: reverse=False (the default is True)

    • You are using the YahooFinanceDataCSV feed for pre-downloaded data from Yahoo which was not reversed. Use reverse=True

    • In other cases, check your data source (probably a file which is in reverse order)

    Session Start/End Times and TimeFrame/Compression

    Mostly informational originally, those parameters to data feeds have gained ground to help when aligning data feeds. Specifying them will solve many problems, as in:

    data = bt.feeds.MyChosenFeed(
        timeframe=bt.TimeFrame.Minutes, compression=5,
        sessionstart=datetime.time(9, 0), sessionend=datetime.time(16, 0)

    But my file only contains 5-Minutes bars, the platform should recognize it

    This is no artificial intelligence. And if it were, the entire file should be scanned to recognize that (and discard missing timestamps) and to learn what the session start and end times are.

    I want to trade using bid-ask prices

    Not a goal of this platform. You may still feed the prices to your own defined lines in the data feed and use them. See the documentation for extending data feeds.

    I want to trade zillion tickers with a live broker

    backtesting with any number of symbols is not an issue because in that case the data sources tend to be frozen and deliver data each and every time.

    Doing that with for example Interactive Brokers is going to generate pacing violations due to the restrictions imposed on data download.

    Doing that with other feeds beyond a few symbols will have the effect of no longer being able to synchronize all symbols.

    This platform grew up from backtesting to live and the basic synchronization mechanisms chosen will not scale to zillion symbols with live ticking.

    With multiple data feeds some times the timestamp and prices of a data bar are repeated

    No. Those multiple data feeds have different trading calendars. This repetition is not a repetition. If some data feeds are trading on day x + 1 and the repeating data feed is not, the only data that this feed delivers to you is the one from day x which was so far the last trading day.

    How do I know if the data which is repeating didn't trade?

    Data feeds can be queried with len(data). If the length has increase, the data has delivered something. If not, it hasn't

    exactbars=1 gives me problem X, Y, Z

    Use exactbars with 0 (default) or -1, -2 which are softer settings and not so aggressive at reducing buffer lengths of all elements.

    Mixing replaying and resampling breaks things

    The many turns made in the project to synchronize data feeds have proven that some things like water and oil cannot be mixed and that not all use cases of mixing resampling/replaying can work. It cannot be done with the current architecture and the core is better left alone.

    pyfolio doesn't work

    PyFolio changed its API and the integration no longer works. The documentation and blog post reflect this fact. This may be fixed in the future. The latest version tested version was 0.5.1 (dependencies associated to that version should also match and that may prove more difficult)

    My market orders are not matched with the closing price but next day with the opening price

    Yes. The close price is that from a closed bar. The next incoming price is the open and is the one the platform can open for matching.

    Look for cheat-on-close in the broker documentation to let you be matched with the already closed closing close price. You can also have some fine grained control with coc for orders (orders with that parameter set to True will use cheat-on-close even if the functionality has not been enabled for all orders in the broker)

    I want the plaform to make calculations with the opening price of the next bar

    That's also cheating. Even if your data is preloaded the platform itself will not look into the future. You have a chance to make the calculations (or force them by specifying the price) by activating cheat-on-open in Cerebro (see the docs) which gives you a chance to act during the next_open method which happens when the next bar has already been loaded, but before the broker has acted.

    Or use timers with cheat=True and activate coo=True (coo -> cheat-on-open) in the broker for a very similar approach

    Maximum stakes - all-in

    Without entering in the philosophical debate as to why that would ever be done, see the 2 cheating entries above. Or else your orders may be rejected due to a mismatch between the calculation and the order matching prices.

    Writing my own broker integration

    This is a fine art, because each broker has different delivery/synchronization mechanisms. Use the source Luke.

    Optimization doesn't work when working with Jupyter Notebooks and Spyder

    You are probably using Windows and those two platforms initialize the Python kernel long before it reaches the __main__ part of your script. That has side-effect for multiprocessing on Windows, because Python (nothing to do with backtrader) can no longer properly initialize the multiprocessing module.

    Don't run optimization code inside those 2 platforms when running under Windows.

    Optimization is slow and consumes RAM

    Indeed. Parameter overfitting is also a great and formidable enemy of algorithmic trading.

    I want to use the GPU for optimization

    Nice idea, but the multiprocessing module in Python won't do that.

    The documentation has some errors

    Feel free to report them so they can be corrected. Errors will not simply correct themselves (as unbelievable as it might seem)


    backtrader doesn't support slicing for lines objects and this is a design decision following the [0] and [-1] indexing scheme. With regular indexable Python objects you would do things like

    myslice = self.my_sma[0:]  # slice from the beginning til the end

    But remember that with the choice for 0 ... it is actually the currently delivered value, there is nothing after it. Also:

    myslice = self.my_sma[0:-1]  # slice from the beginning til the end

    Again ... 0 is the current value and -1 is the latest (previous) delivered value. That's why a slice from 0 -> -1 makes no sense in the backtrader ecosystem.

    If slicing were ever to be supported, it would look like:

    myslice = self.my_sma[:0]  # slice from current point backwards to the beginning


    myslice = self.my_sma[-1:0]  # last value and current value


    myslice = self.my_sma[-3:-1]  # from last value backwards to the 3rd last value

    Getting a slice

    An array with the latest values can still be gotten. The syntax:

    myslice = self.my_sma.get(ago=0, size=1)  # default values shown

    That would have returned an arry with 1 value (size=1) with the current moment 0 as the staring point to look backwards.

    To get 10 values from the current point in time (i.e.: the last 10 values):

    myslice = self.my_sma.get(size=10)  # ago defaults to 0

    Of course the array has the ordering you would expect. The leftmost value is the oldest one and the rightmost value is the most current (it is a regular python array and not a lines object)

    To get the last 10 values skipping only the current point:

    myslice = self.my_sma.get(ago=-1, size=10)

Log in to reply