Trading a Day in StepsΒΆ

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

  • Introduce an order using daily bars but using the opening price

This comes from the conversations in tickets #105 Order execution logic with current day data and #101 Dynamic stake calculation

backtrader tries to remain as realistic as possible and the following premise applies when working with daily bars:

  • When a daily bar is being evaluated, the bar is already over

It makes sense because all price (open/high/low/close) components are known. It would actually seem illogical to allow an action on the open price when the close price is already known.

The obvious approach to this is to use intraday data and enter when the opening prices is known. But it seems intraday data is not so widespread.

This is where adding a filter to a data feed can help. A filter that:

  • Converts daily data into intraday-like data

Blistering barnacles!!! The curious reader will immediately point out that upsampling for example Minutes to Days is logical and works, but that downsampling Days to Minutes cannot be done.

And it is 100% right. The filter presented below will not try that, but a much humble and simpler goal:

  • Break a daily bar in 2 parts
    1. A bar with only the opening price and no volume
    2. A 2nd bar which is a copy of the regular daily bar

This can still be held as a logical approach:

  • Upon seeing the opening price, the trader can act
  • The order is matched during the rest of the day (actually may or may not be matched depending on execution type and price constraints)

The full code is presented below. Let’s see a sample run with a well known data of 255 daily bars:

$ ./ --data ../../datas/2006-day-001.txt


Calls,Len Strat,Len Data,Datetime,Open,High,Low,Close,Volume,OpenInterest
- I could issue a buy order during the Opening
- I could issue a buy order during the Opening
- I could issue a buy order during the Opening
- I could issue a buy order during the Opening
- I could issue a buy order during the Opening
- I could issue a buy order during the Opening

The following happens:

  • next is called: 510 times which is 255 x 2

  • The len of the Strategy and that of the data reaches a total of 255, which is the expected: the data has only those many bars

  • Every time the len of the data increases, the 4 price components have the same value, namely the open price

    Here a remark is printed out to indicate that during this opening phase action could be taken, like for example buying.


  • The daily data feed is being replayed with 2 steps for each day, giving the option to act in between open and the rest of the price components

The filter will be added to the default distribution of backtrader in the next release.

The sample code including the filter.

from __future__ import (absolute_import, division, print_function,

import argparse
from datetime import datetime, time

import backtrader as bt

class DayStepsFilter(object):
    def __init__(self, data):
        self.pendingbar = None

    def __call__(self, data):
        # Make a copy of the new bar and remove it from stream
        newbar = [data.lines[i][0] for i in range(data.size())]
        data.backwards()  # remove the copied bar from stream

        openbar = newbar[:]  # Make an open only bar
        o = newbar[data.Open]
        for field_idx in [data.High, data.Low, data.Close]:
            openbar[field_idx] = o

        # Nullify Volume/OpenInteres at the open
        openbar[data.Volume] = 0.0
        openbar[data.OpenInterest] = 0.0

        # Overwrite the new data bar with our pending data - except start point
        if self.pendingbar is not None:

        self.pendingbar = newbar  # update the pending bar to the new bar
        data._add2stack(openbar)  # Add the openbar to the stack for processing

        return False  # the length of the stream was not changed

    def last(self, data):
        '''Called when the data is no longer producing bars
        Can be called multiple times. It has the chance to (for example)
        produce extra bars'''
        if self.pendingbar is not None:
            data.backwards()  # remove delivered open bar
            data._add2stack(self.pendingbar)  # add remaining
            self.pendingbar = None  # No further action
            return True  # something delivered

        return False  # nothing delivered here

class St(bt.Strategy):
    params = ()

    def __init__(self):

    def start(self):
        self.callcounter = 0
        txtfields = list()
        txtfields.append('Len Strat')
        txtfields.append('Len Data')

        self.lcontrol = 0

    def next(self):
        self.callcounter += 1

        txtfields = list()
        txtfields.append('%04d' % self.callcounter)
        txtfields.append('%04d' % len(self))
        txtfields.append('%04d' % len(self.data0))
        txtfields.append('%.2f' %[0])
        txtfields.append('%.2f' % self.data0.high[0])
        txtfields.append('%.2f' % self.data0.low[0])
        txtfields.append('%.2f' % self.data0.close[0])
        txtfields.append('%.2f' % self.data0.volume[0])
        txtfields.append('%.2f' % self.data0.openinterest[0])

        if len( > self.lcontrol:
            print('- I could issue a buy order during the Opening')

        self.lcontrol = len(

def runstrat():
    args = parse_args()

    cerebro = bt.Cerebro()
    data = bt.feeds.BacktraderCSVData(


    cerebro.addstrategy(St), runonce=False, preload=False)
    if args.plot:

def parse_args():
    parser = argparse.ArgumentParser(
        description='Sample for pivot point and cross plotting')

    parser.add_argument('--data', required=False,
                        help='Data to be read in')

    parser.add_argument('--plot', required=False, action='store_true',
                        help=('Plot the result'))

    return parser.parse_args()

if __name__ == '__main__':


Fork me on GitHub