All Posts

Improving Random Python Internet Study Notes

  • Apr 22, 2018

Every now and then, samples with backtrader code pop up in the Internet. There are several in what it looks to me to be Chinese. The latest one is here:

Read more ...


A Dynamic Indicator

  • Feb 06, 2018

Indicators are difficult beasts. Not because they are difficult to code in general, but mostly because the name is misleading and people have different expectations as to what an indicator is.

Read more ...


Stop Trading

  • Feb 01, 2018

Trading can be dangerous and trading using stop orders can help into either avoiding big losses or securing profits. backtrader provides you with several mechanisms to implement Stop - based strategies

Read more ...


Developing a Recursive Indicator (with a seed)

  • Jan 27, 2018

One of the initial goals of backtrader was:

Read more ...


The DJI 10 day streak

  • Aug 08, 2017

It has hit the news. The DJI is hitting all time highs with already 10 consecutive updays and 9 all time highs. See for example:

Read more ...


Evaluating external historical performance

  • Jul 05, 2017

With release 1.9.55.122, backtrader can now be used to evaluate the performance of an external set of orders. This can be used for example:

Read more ...


Renko Bricks

  • Jun 26, 2017

The Renko Bricks is a different way to present the evolution of prices in which price plays a more important role than time. This has been introduced as a filter in release 1.9.54.122 of backtrader

Read more ...


Backtrader: the fund

  • Jun 19, 2017

Already for a while, backtrader has been in use, so to say, professionally, besides the the known usage of backtrader some banks and trading houses, for the Backtrader Fund.

Read more ...


Release 1.9.51.121

  • Jun 12, 2017

Even being a minor release, there are some interesting things which probably grant having a dedicated blog post for them.

Read more ...


Strategy Selection Revisited

  • May 16, 2017

The original Strategy Selection approach used two strategies, which were manually registered and a simple [0, 1] list to decide which would be the target of the strategy.

Read more ...


Timers

  • May 03, 2017

Release 1.9.44.116 added timers to the arsenal of tools available in backtrader. This functionality allows to get a call back to the notify_timer (available in Cerebro and Strategy) at given points in time, with a fine grained end-user control.

Read more ...


Cheat On Open

  • May 01, 2017

Release 1.9.44.116 adds support for Cheat-On-Open. This seems to be a demanded feature for people who go all-in, having made a calculation after the close of a bar, but expecting to be matched against the open price.

Read more ...


Trading Calendar

  • Apr 16, 2017

Release 1.9.42.116 adds support for Trading Calendars. This is useful when resampling in for example the following scenarios:

Read more ...


Multi Example

  • Apr 09, 2017

A couple of topics in the Community seem to be oriented as to how to keep track of orders, especially when several data feeds are in play and also including when multiple orders are working together like in the case of bracket orders

Read more ...


Bracket Orders

  • Apr 01, 2017

Release 1.9.37.116 adds bracket orders giving a very broad spectrum of orders which are supported by the backtesting broker (Market, Limit, Close, Stop, StopLimit, StopTrail, StopTrailLimit, OCO)

Read more ...


StopTrail(Limit)

  • Mar 22, 2017

Release 1.9.35.116 adds the StopTrail and StopTrailLimit order execution types to the backtesting arsenal.

Read more ...


OCO orders

  • Mar 19, 2017

Release 1.9.34.116 adds OCO (aka One Cancel Others) to the backtesting arsenal.

Read more ...


Plotting on the same axis

  • Mar 17, 2017

The previous post Futures and Spot Compensation, was plotting the original data and the slightly (randomly) modified data on the same space, but not on the same axis.

Read more ...


Futures and Spot Compensation

  • Mar 15, 2017

Release 1.9.32.116 adds support for an interesting use case presented in the Community

Read more ...


Plotting Date Ranges

  • Mar 07, 2017

The release, 1.9.31.x added the capability to make partial plots.

Read more ...


Kalman et al.

  • Feb 14, 2017

The support for the directives below starts with commit #1146c83d9f9832630e97daab3ec7359705dc2c77 in the development branch

Read more ...


PercentRank reloaded

  • Feb 05, 2017

The community user @randyt has been able to stretch backtrader to its limits. Finding some of the obscure corners, even adding pdb statements here and there, and has been the driving force behind getting a much more refined synchronization of resampled streams.

Read more ...


Crossing over numbers

  • Feb 04, 2017

An oversight has been corrected with Release 1.9.27.105 of backtrader. It was an oversight because all pieces of the puzzle were in place, but the activation was not made in all corners.

Read more ...


BTFD - Reality Bites

  • Dec 28, 2016

The previous post managed to replicate the BTFD strategy, finding out that the real gains were 16x rather than 31x.

Read more ...


BTFD (Buy The F... Dip)

  • Dec 26, 2016
  • Dec 30, 2016

Added operations log for the CloseClose approach (updated sample below too)

A post in Reddit calling for replication of a BTFD strategy turned out to be the little push needed to add yet another feature to backtrader: leverage

The links:

Read more ...


Gold vs SP500

  • Dec 13, 2016

Sometimes getting hints about where backtrader is in use helps understanding what people may be looking for and using the platform for.

The reference:

https://estrategiastrading.com/oro-bolsa-estadistica-con-python/

Read more ...


Arrows for the BuySell Observer

  • Dec 10, 2016

backtrader was conceived to try to deliver ease of use. Creation of indicators and other usual suspects should be easy.

And of course, customizing existing items should also be part of the deal.

Read more ...


Shorting the cash

  • Dec 06, 2016

From the very first moment backtrader was enabled for shorting anything, including stock-like and future-like instruments. When a short was done, the cash was decreased and the value of the shorted asset used for the total net liquidation value.

Removing from one side and adding to the other keeps things in balance.

It seems that people prefer to have cash increased, which probably lends to more spending.

Read more ...


Hidden Powers of Python (3)

  • Nov 25, 2016

Last, but not least, in this series about how the hidden powers of Python are used in backtrader is how some of the magic variables show up.

Read more ...


Hidden Powers of Python (2)

  • Nov 23, 2016

Let’s tackle a bit more how the hidden powers of Python are used in backtrader and how this is implemented to try to hit the main goal: ease of use

Read more ...


Hidden Powers of Python (1)

  • Nov 20, 2016

It’s only when meeting real users of backtrader when one can realize if the abstractions and Python powers used in the platform make sense.

Without leaving the pythonic motto aside, backtrader tries to give the users as much control as possible, whilst at the same time simplifying the usage by putting into action the hidden powers that Python offers.

The first example in this the first post of a series.

Read more ...


Strategy Selection

  • Oct 29, 2016

Houston we have a problem:

cerebro is not meant to be run several times. This is not the 1st time and rather than thinking that users are doing it wrong, it seems it is a use case.

Read more ...


Notebook - Automatic Inline Plotting

  • Sep 17, 2016

Release 1.9.1.99 adds automatic inline plotting when running inside a Jupyter Notebook.

Some of the questions around backtrader show people using the platform inside a Notebook and supporting this and making it the default behavior should make things consistent.

Read more ...


Data Synchronization Rework

  • Sep 17, 2016

In the latest release the minor number has been moved from 8 to 9 to indicate a change which may have some behavioral impact, regardless, even if compatibility has been taken into account.

With release 1.9.0.99 the entire mechanism to synchronize multiple datas using datetime has been reworked (for both next and once modes).

Read more ...


Variability Weighted Return (or VWR)

  • Sep 06, 2016

Following some hints about an improved SharpeRatio, backtrader has added this analyzer to its arsenal.

The literature is at:

https://www.crystalbull.com/sharpe-ratio-better-with-log-returns/

Read more ...


Optimization improvements

  • Sep 05, 2016

Version 1.8.12.99 of backtrader includes an improvement in how data feeds and results are managed during multiprocessing.

Read more ...


Target Orders

  • Sep 02, 2016

Until version 1.8.10.96 smart staking was possible with backtrader over the Strategy methods: buy and sell. It was all about adding a Sizer to the equation which is responsible for the size of the stake.

What a Sizer cannot do is decide if the operation has to be a buy or a sell. And that means that a new concept is needed in which a small intelligence layer is added to make such decision.

Read more ...


Rolling over Futures

  • Aug 31, 2016

Not every provider offers a continuous future for the instruments with which one can trade. Sometimes the data offered is that of the still valid expiration dates, i.e.: those still being traded

This is not so helpful when it comes to backtesting because the data is scattered over several different instruments which additionally ... overlap in time.

Read more ...


Credit Interest

  • Aug 22, 2016

In some situations, the cash amount in real brokers may be decreased because the operation on assets includes an interest rate. Examples:

Short selling of stocks

Read more ...


Dickson Moving Average

  • Aug 17, 2016

The reddit post from below names this average the Dickson Moving Average after its own author Nathan Dickson (reddit handle)

Read more ...


Stock Screening

  • Aug 15, 2016

Looking for some other things I came across a question on one of the StackOverlow family sites: Quantitative Finance aka Quant StackExchange. The question:

Read more ...


Strategy with Signals

  • Aug 01, 2016

Operating backtrader is also possible without having to write a Strategy. Although this is the preferred way, due to the object hierarchy which makes up the machinery, using Signals is also possible.

Read more ...


MACD Settings

  • Jul 30, 2016

In the Algotrading site of Reddit a thread about optimizing MACD settings was found.

Read more ...


Pinkfish Challenge

  • Jul 29, 2016

(Sample and changes added to release 1.7.1.93)

Along the way backtrader has gotten maturity, new features and of course complexity. Many of the new features have been introduced after requests, comments, questions from users. Small challenges which have proven that most of the design decisions were at least not that wrong even if some things could have been done in many other ways, sometimes probably in a better way.

Read more ...


TA-Lib

  • Jul 26, 2016

Even if backtrader offers an already high number of built-in indicators and developing an indicator is mostly a matter of defining the inputs, outputs and writing the formula in a natural manner, some people want to use TA-LIB. Some of the reasons:

Indicator X is in the library and not in backtrader (the author would gladly accept a request)

Read more ...


Sizers Smart Staking

  • Jul 23, 2016

Release 1.6.4.93 marks a major milestone in backtrader even if the change in version numbering is a minor one.

Read more ...


Benchmarking

  • Jul 22, 2016

backtrader includes 2 different types of objects which can aid with tracking:

Observers

Read more ...


Pyfolio Integration

  • Jul 17, 2016

Feb 2017

Read more ...


Volume Filling

  • Jul 14, 2016
  • Jul 15, 2016

Up until now the default volume filling strategy in backtrader has been rather simple and straightforward:

Ignore volume

Read more ...


Trading a Day in Steps

  • Jul 13, 2016

It seems that somewhere in the world there is an interest that can be summarized as follows:

Read more ...


Visual Chart Live Data/Trading

  • Jul 12, 2016

Starting with release 1.5.1.93, backtrader supports Visual Chart Live Feeds and Live Trading.

Read more ...


Ultimate Oscillator

  • Jun 22, 2016

One of the goals when the development of backtrader was kickstarted was to make it very easy (at least for the author himself) to develop new Indicators to test ideas both mathematically and visually.

Read more ...


Live Data/Live Trading

  • Jun 21, 2016

Starting with release 1.5.0, backtrader supports Live Data Feeds and Live Trading. The first integrated entity is:

Interactive Brokers

This was long sought goal since the inception of the platform as a small idea. The design ideas have proven to be flexible enough to accommodate the needed changes. All whilst keeping the same interface which means: backtest once, trade many times. The same code/api/primitives/notifications are meant for backtesting and live data feeding/trading.

Read more ...


Saving Memory

  • May 09, 2016

Release 1.3.1.92 has reworked and fully implemented the memory saving schemes that were previously in place, although not much touted and less used.

Read more ...


Mixing Timeframes in Indicators

  • May 05, 2016

Release 1.3.0.92 brings up the possibility to have data (from either data feeds and/or indicators) from different timeframes mixed.

Read more ...


Updated on Jan 13, 2017

  • Apr 28, 2016
  • Jan 13, 2017

This post is kept for historical reasons. The indicator and sample have been updated in the sources and PivotPoint can now auto-couple itself, removing boilerplate for the user code.

A new post will be written referencing this one. Meanwhile, please check the updated sample in the sources.

Read more ...


Synchronizing different markets

  • Apr 19, 2016

The more the usage the more the mix of ideas and unexpected scenarios that backtrader has to face. And with each new one, a challenge to see if the platform can live up to the expectations set forth when development started, flexibility and ease of use were the targets and Python was chosen as the cornerstone.

Read more ...


Bid-Ask Data to OHLC

  • Apr 16, 2016

Lately backtrader executed an escape-from-ohlc-land by implementing line overriding, which allows to redefine the entire hierarchy and for example have data feeds which only feature bid, ask and datetime lines.

Read more ...


Escape from OHLC Land

  • Mar 08, 2016

One of the key concepts applied during the conception and development of backtrader was flexibility. The metaprogramming and introspection capabilities of Python were (and still are) the basis to keep many things flexible whilst still being able to deliver.

Read more ...


Release 1.2.1.88

  • Mar 07, 2016

Changing the minor version number from 1 to 2 has taken sometime, but the deprecation of the old DataResampler and DataReplayer have led to it.

Read more ...


Data Filters

  • Nov 21, 2015

Some time ago Ticket #23 got me thinking about a potential improvement for the discussion which was held in the context of that ticket.

Read more ...


User Defined Commissions

  • Nov 20, 2015

The commission schemes implementation was reworked not so long ago. The most important: part of the rework involved:

Retaining the original CommissionInfo class and behavior

Read more ...


Extending Commissions

  • Nov 05, 2015

Commissions and asociated functionality were managed by a single class CommissionInfo which was mostly instantiated by calling broker.setcommission.

Read more ...


MultiTrades

  • Oct 05, 2015

One can now add a unique identifier to each trade, even if running on the same data.

Read more ...


Bar Synchronization

  • Oct 04, 2015

The lack of a standard formula in the literature and/or industry is not the problem, because the problem can actually be summarized as:

Read more ...


Tick Data and Resampling

  • Sep 25, 2015
  • Oct 06, 2015

backtrader could already do resampling up from minute data. Accepting tick data was not a problem, by simply setting the 4 usual fields (open, high, low, close) to the tick value.

But passing the tick data to be resampled produced the same data again. As or release 1.1.11.88 this is no longer so. Now

TimeFrame (backtrader.TimeFrame) has been extended to contain constants and names for “Ticks”, “MicroSeconds” and “Seconds”

Read more ...


Plotting on the same axis

  • Sep 21, 2015
  • Sep 22, 2015

Following a comment on the blog a slight addition (luckily just a few lines of code) has been made to plotting.

The abilitiy to plot any indicator on any other indicator

A potential use case:

Saving precious screen real estate by plotting some indicators together and having more room to appreciate the OHLC bars

Read more ...


Writers - Write it down

  • Sep 14, 2015

With the 1.1.7.88 release backtrader gets a new addition: writers

Read more ...


MultiData Strategy

  • Sep 03, 2015

Because nothing in the world lives in isolation it can well be that the trigger to buy an asset is actually another asset.

Using different analysis techniques a correlation may have been found between two different datas.

backtrader supports using different data sources simultaneously so it can possibly be used for the purpose in most cases.

Read more ...


Real World Usage

  • Aug 27, 2015

Finally it seems it pays having gotten down to developing backtrader.

Following what seemed like the end of the world when looking at the European markets in the last weeks, a friend asked if I could have a look at the data in our charting package to see how the falling range compared against previous similar occurrences.

Of course I could, but I said I could do more than looking into the charts, because I could quickly:

Read more ...


Data - Replay

  • Aug 25, 2015

The time is gone and testing a strategy against a fully formed and closed bar is good, but it could be better.

This is where Data Replay comes in to help. If:

The strategy operates on data with a timeframe X (example: daily)

and

Read more ...


Data - Multiple Timeframes

  • Aug 24, 2015

Sometimes investing decisions are taken using different timeframes:

Weekly to evaluate the trend

Daily to execute the entry

Or 5 minutes vs 60 minutes.

Read more ...


Data Resampling

  • Aug 23, 2015

When data is only available in a single timeframe and the analysis has to be done for a different timeframe, it’s time to do some resampling.

“Resampling” should actually be called “Upsampling” given that one goes from a source timeframe to a larger time frame (for example: days to weeks)

“Downsampling” is not yet possible.

Read more ...


Pandas DataFeed Support

  • Aug 21, 2015

Amongst some minor enhancementss and some OrderedDict tweaks for better Python 2.6 support, the latest release from backtrader adds support for analyzing data from a Pandas Dataframe or Time Series.

Read more ...


Automating backtrader BackTesting

  • Aug 16, 2015
  • Aug 22, 2015

So far all backtrader examples and working samples have started from scratch creating a main Python module which loads datas, strategies, observers and prepares cash and commission schemes.

One of the goals of algorithmic trading is the automation of trading and given that bactrader is a backtesting platform intented to check trading algorithms (hence is an algotrading platform), automating the use of backtrader was an obvious goal.

Read more ...


Observers and Statistics

  • Aug 12, 2015
  • Aug 19, 2015

Strateties running inside the backtrader do mostly deal with datas and indicators.

Datas are added to Cerebro instances and end up being part of the input of strategies (parsed and served as attributes of the instance) whereas Indicators are declared and managed by the Strategy itself.

All backtrader sample charts have so far had 3 things which seem to be taken for granted because they are not declared anywhere:

Cash and Value (what’s happening with the money in the broker)

Read more ...


Datafeed Development

  • Aug 11, 2015

Adding a new CSV based data feed is easy. The existing base class CSVDataBase provides the framework taking most of the work off the subclasses which in most cases can simply do:

Read more ...


Order Management and Execution

  • Aug 08, 2015
  • Aug 11, 2015

Backtesting, and hence backtrader, would not be complete if orders could not be simulated. To do so, the following is available in the platform.

Read more ...


Extending a data feed

  • Aug 07, 2015

Issues in GitHub are actually pushing into finishing documentation parts or helping me to understand if backtrader has the ease of use and flexibility I envisioned from the first moments and the decisions made along the way.

Read more ...


CSV Data Feed Development

  • Aug 06, 2015

backtrader already offers a Generic CSV Data feed and some specific CSV Data Feeds. Summarizing:

GenericCSVData

VisualChartCSVData

Read more ...


A rough Zipline comparison

  • Aug 05, 2015

Open Source is not a war (at least for me) and if anyone finds the API, ways of doing things, naming conventions or others better in zipline, I will not preach to move him/her away from that platform or any other.

Read more ...


A Generic CSV Data Feed

  • Aug 04, 2015

An issue has led to the implementation of GenericCSVData which can be used to parse different CSV formats.

Read more ...


Improving Commissions: Stocks vs Futures

  • Jul 31, 2015

Posting backtrader usage examples has given me an insight into things that were missing. For starters:

Read more ...


Commissions: Stocks vs Futures

  • Jul 26, 2015

backtrader has been born out of necessity. My own ... to have the feeling I control my own backtesting platform and can experiment new ideas. But in doing so and fully open sourcing it from the very beginning it was clear it has to have a way to fulfill the needs and wishes of others.

Being a traders future I could have chosen to code point based calculations and fixed price per round commissions, but it would have been a mistake.

Read more ...


Multicore Optimization

  • Jul 23, 2015
  • Jul 24, 2015

Making use of all available cores was something I had in mind for backtrader but never got done. Support of natural operations, removal of array notation, inclusion of new indicators and bla, bla, bla.

In reality I am not a great fan of optimization and consequently neither a great fan of utilizing all cores for it. A good idea, imho, is worth a million optimizations.

Read more ...


Extending an Indicator

  • Jul 21, 2015

In Object Oriented Programming, and of course in Python itself, extension of an existing class can be achieved in two ways.

Inheritance (or subclassing)

Read more ...


Developing an Indicator

  • Jul 18, 2015

After much ado in fine tuning backtrader (give it had already been running for a while) I decided to not only share it via GitHub but to also tell the world it was there and posted about its existence in “Reddit”.

Read more ...


Fork me on GitHub