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

Buy/Sell Markers

  • I noticed that the when using multiple data feeds, the buy and sell markers can appear on different dates even though notify_order() reports them both being filled on the same date.

    Not sure if this is a misunderstanding on my part or not....

    Anyway, I created an example script to reproduce the phenomenon. The script will attempt to use order_target_percent() at fixed dates. As such, I would expect them to be filled on the same date (which they appear to be!)

    import backtrader as bt
    from datetime import datetime
    class TestStrategy(bt.Strategy):
        def __init__(self):
        def next(self):
            for i, d in enumerate(self.datas):
                dt = d.datetime.datetime().date()
                dn = d._name
                pos = self.getposition(d).size
                if dt == datetime(2017,6,1).date():
                    print('{} Sending Order: {} | Current Pos: {}'.format(dt,dn,pos))
                    if dn == 'AAPL':
                        self.order_target_percent(d, target=0.3)
                        self.order_target_percent(d, target=0.7)
                if dt == datetime(2017,9,1).date():
                    print('{} Sending Order: {} | Current Pos: {}'.format(dt,dn,pos))
                    if dn == 'AAPL':
                        self.order_target_percent(d, target=0.1)
                        self.order_target_percent(d, target=0.1)
        def notify_order(self, order):
            date =
            if order.status == order.Completed:
                print('{} >> Order Completed >> Stock: {},  Ref: {}, Size: {}, Price: {}'.format(
                                                            'NA' if not order.price else round(order.price,5)
        def notify_trade(self, trade):
            date =
            if trade.isclosed:
                print('{} >> Notify Trade >> Stock: {}, Close Price: {}, Profit, Gross {}, Net {}'.format(
    startcash = 10000
    #Create an instance of cerebro
    cerebro = bt.Cerebro()
    # data lists
    data = bt.feeds.Quandl(
        fromdate = datetime(2017,1,1),
        todate = datetime(2018,1,1),
        buffered= True
    data2 = bt.feeds.Quandl(
        fromdate = datetime(2017,1,1),
        todate = datetime(2018,1,1),
        buffered= True
    data_list = [(data, 'AAPL'), (data2, 'F')]
    #Add our strategy
    # Create a Data Feed
    for asset in data_list:
        # Add the data
    # Set our desired cash start
    # Run over everything

    This will then result in an output and chart that looks like this:

    alt text

    Am I being a dumbo and missing something obvious?

  • administrators

    I don't think so. The data feeds don't probably fully align, but there is only a master temporal axis which is tied to the strategy which is the one used by the observers. The wrongly displaced marker cannot have happened where displayed, because the price from the order execution is not reached (it is reached in the previous bar, which matches the log)

Log in to reply