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

Close Order doesn't close whole size and sell order gets rejected



  • Hi There,

    i am trying to work on a momentum strategy.

    Thanks to Backtrader, Teddy Koker and Analysing Alpha!

    There are two issue i try to understand:

    1. Why is the microsoft position not totally closed on the 2020-04-02 ? but 3 of 36 shares?
    2. Why is the order rejected? I have Cheat on Close activated and the order is lower than the closing price (MSFT on the 2020-04-02):
      b845eb1e-ebe0-4d73-a477-334b885729d0-grafik.png
    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import pandas as pd
    import numpy as np
    import backtrader as bt
    from scipy.stats import linregress
    import datetime as dt
    from pandas.testing import assert_frame_equal # prevents an error from popping up by using pandas_datareader
    import pandas_datareader as web
    import backtrader.plot
    
    globalparams = dict(start=dt.datetime(2019, 1, 1),
                        end=dt.datetime(2020, 10, 31),
                        symbols=["NVDA","SPY", "GOOG", "AMZN", "MSFT"],           #, "GOOG", "MSFT", "AMZN", "SNY", "VZ", "IBM", "HPQ", "QCOM", "NVDA"
                        cash=1000000,
                        commission=0.02,
                        coc='True',
     )
    
    def momentum_func(self, price_array):
        r = np.log(price_array)
        slope, _, rvalue, _, _ = linregress(np.arange(len(r)), r)
        annualized = (1 + slope) ** 252
        return (annualized * (rvalue ** 2))
    
    
    class Momentum(bt.ind.OperationN):
        lines = ('trend',)
        params = dict(period=90)
        func = momentum_func
    
    class Strategy(bt.Strategy):
        params = dict(
            momentum=Momentum,
            momentum_period=180,
            num_positions=2,
            when=bt.timer.SESSION_START,
            timer=True,
            weekdays=[],
            weekcarry=False,
            monthdays=[1],
            monthcarry=True,
            printlog=True
        )
    
        def log(self, txt, dt=None, doprint=False):
            ''' Logging function fot this strategy'''
            if self.params.printlog or doprint:
                dt = dt or self.datas[0].datetime.date(0)
                print('%s, %s' % (dt.isoformat(), txt))
    
        def __init__(self):
            self.i = 0
            self.o = dict()  # orders per data (main, stop, limit, manual-close)
            self.securities = self.datas[1:]
            self.inds = {}
    
            self.add_timer(
                when=self.p.when,
                monthdays=self.p.monthdays,
                monthcarry=self.p.monthcarry
            )
    
            for security in self.securities:
                self.inds[security] = self.p.momentum(security,
                                                      period=self.p.momentum_period)
    
        def notify_order(self, order):
            if order.status in [order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                return
            if order.status in [order.Submitted]:
                if order.isbuy():
                
                    dt, dn = self.datetime.date(), order.data._name
                    print('Buy {} {} {} Price {} Value {} Size {} Cash {}'.format(
                            order.getstatusname(), dt, dn, order.created.price, order.created.size * order.created.price , order.created.size, self.broker.getcash()))
                if order.issell():
                    dt, dn = self.datetime.date(), order.data._name
                    print('Sell {} {} {} Price {} Value {} Size {}'.format(
                            order.getstatusname(), dt, dn, order.created.price, order.created.size * order.created.price, order.created.size))
    
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                return
    
            # Check if an order has been completed
            # Attention: broker could reject order if not enough cash
            if order.status in [order.Completed]:
                if order.isbuy():
                    dt, dn = self.datetime.date(), order.data._name
                    print('Buy {} {} Price {} Value {} Size {}'.format(
                        dt, dn, order.executed.price, order.executed.value, order.executed.size))
    
                if order.issell():# Sell
                    dt, dn = self.datetime.date(), order.data._name
                    print('Sell {} {} Price {} Value {} Size {}'.format(
                        dt, dn, order.executed.price, order.executed.value, order.executed.size))
    
    
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
        
             
        def notify_timer(self, timer, when, *args, **kwargs):
            if self._getminperstatus() < 0:
                self.rebalance()
    
        def rebalance(self):
            rankings = list(self.securities)
            rankings.sort(key=lambda s: self.inds[s][0], reverse=True)
            pos_size = 1 / self.p.num_positions
    
            # Sell stocks no longer meeting ranking filter.
            for i, d in enumerate(rankings):
                if self.getposition(d).size:
                    if i > self.p.num_positions:
                        self.close(d)
                        
            # Buy and rebalance stocks with remaining cash
            for i, d in enumerate(rankings[:self.p.num_positions]):
                self.order_target_percent(d, target=pos_size)
    
        def next(self):
            x = "Y" #if it is empty the code doesn't work o.O
    
        def stop(self):
            self.log('| %2d | %2d |  %.2f |' %
                     (self.p.momentum_period,
                      self.p.num_positions,
                      self.broker.getvalue()),
                     doprint=True)
    
    if __name__ == '__main__':
        cerebro = bt.Cerebro(maxcpus=1)
        
        datafeeds = {s: web.DataReader(s, "yahoo", globalparams['start'], globalparams['end']) for s in globalparams['symbols']}
    
        for df in datafeeds.values():
            df["OpenInterest"] = 0  # PandasData reader expects an OpenInterest column;
    
        for ticker, data in datafeeds.items():
            if ticker == "SPY":
                benchdata = bt.feeds.PandasData(dataname=data, name='SPY', plot=True)
                cerebro.adddata(benchdata)
        datafeeds.pop('SPY')
        
            # Add securities as datas1:
        for ticker, data in datafeeds.items():
            print(f"Adding ticker: {ticker}")
            data = bt.feeds.PandasData(dataname=data, 
                                              name=ticker,
                                              plot=False)
            data.plotinfo.plotmaster = benchdata
            cerebro.adddata(data)
    
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Add Strategy
        stop = len(datafeeds) + 1
        #cerebro.optstrategy(Strategy,
        #                    momentum_period=range(50, 300),
        #                    num_positions=range(1, len(datafeeds) + 1))
        
        cerebro.addstrategy(Strategy)
        
        cerebro.broker.set_coc(True)
        
        # Run the strategy. Results will be output from stop.
        cerebro.run(stdstats=True, tradehistory=True)
        cerebro.plot(style = 'candle')
    

    I am quite a newbie as the used code I piece together proves...
    Thank you in advance!



  • Would be helpful if you could provide the log of your run and the relevant part of the data feed.

    As a note: where do you set the starting cash amount ? ( it is 10000 by default - is it enough for you ?)



  • Hi vladisld,

    thank you for your reply. I used the standard amount.

    The log is:

    Adding ticker: NVDA
    Adding ticker: GOOG
    Adding ticker: AMZN
    Adding ticker: MSFT
    Starting Portfolio Value: 10000.00
    Buy Submitted 2019-10-02 MSFT Price 137.07000732421875 Value 4934.520263671875 Size 36 Cash 1594.1796875
    Buy Submitted 2019-10-02 AMZN Price 1735.6500244140625 Value 3471.300048828125 Size 2 Cash 1594.1796875
    Buy 2019-10-02 MSFT Price 137.07000732421875 Value 4934.520263671875 Size 36
    Buy 2019-10-02 AMZN Price 1735.6500244140625 Value 3471.300048828125 Size 2
    Sell Submitted 2019-11-04 AMZN Price 1791.43994140625 Value -3582.8798828125 Size -2
    Buy Submitted 2019-11-04 GOOG Price 1273.739990234375 Value 5094.9599609375 Size 4 Cash 82.099609375
    Sell 2019-11-04 AMZN Price 1791.43994140625 Value 3471.300048828125 Size -2
    Buy 2019-11-04 GOOG Price 1273.739990234375 Value 5094.9599609375 Size 4
    Buy Submitted 2020-01-03 NVDA Price 239.91000366210938 Value 5517.930084228516 Size 23 Cash 82.099609375
    2020-01-03, Order Canceled/Margin/Rejected
    Buy Submitted 2020-02-04 NVDA Price 240.3300018310547 Value 6008.250045776367 Size 25 Cash 82.099609375
    2020-02-04, Order Canceled/Margin/Rejected
    Buy Submitted 2020-03-03 NVDA Price 276.42999267578125 Value 5805.029846191406 Size 21 Cash 82.099609375
    2020-03-03, Order Canceled/Margin/Rejected
    Buy Submitted 2020-04-02 NVDA Price 243.07000732421875 Value 4861.400146484375 Size 20 Cash 82.099609375
    Sell Submitted 2020-04-02 MSFT Price 152.11000061035156 Value -456.3300018310547 Size -3
    2020-04-02, Order Canceled/Margin/Rejected
    2020-04-02, Order Canceled/Margin/Rejected
    Sell Submitted 2020-05-04 GOOG Price 1320.6099853515625 Value -5282.43994140625 Size -4
    Buy Submitted 2020-05-04 NVDA Price 282.7799987792969 Value 5655.5999755859375 Size 20 Cash 5713.6795654296875
    Sell Submitted 2020-05-04 MSFT Price 174.57000732421875 Value -349.1400146484375 Size -2
    2020-05-04, Order Canceled/Margin/Rejected
    Sell 2020-05-04 GOOG Price 1320.6099853515625 Value 5094.9599609375 Size -4
    Sell 2020-05-04 MSFT Price 174.57000732421875 Value 274.1400146484375 Size -2
    Buy Submitted 2020-06-02 NVDA Price 352.25 Value 5636.0 Size 16 Cash 77.6795654296875
    Buy Submitted 2020-06-02 AMZN Price 2471.0400390625 Value 4942.080078125 Size 2 Cash 77.6795654296875
    2020-06-02, Order Canceled/Margin/Rejected
    Buy 2020-06-02 NVDA Price 352.25 Value 5636.0 Size 16
    Buy Submitted 2020-07-02 NVDA Price 381.20001220703125 Value 381.20001220703125 Size 1 Cash 77.6795654296875
    Buy Submitted 2020-07-02 AMZN Price 2878.699951171875 Value 5757.39990234375 Size 2 Cash 77.6795654296875
    2020-07-02, Order Canceled/Margin/Rejected
    2020-07-02, Order Canceled/Margin/Rejected
    Buy Submitted 2020-08-04 AMZN Price 3111.889892578125 Value 6223.77978515625 Size 2 Cash 77.6795654296875
    2020-08-04, Order Canceled/Margin/Rejected
    Buy Submitted 2020-09-02 AMZN Price 3499.1201171875 Value 6998.240234375 Size 2 Cash 77.6795654296875
    2020-09-02, Order Canceled/Margin/Rejected
    Sell Submitted 2020-10-02 NVDA Price 544.5800170898438 Value -544.5800170898438 Size -1
    Buy Submitted 2020-10-02 AMZN Price 3221.260009765625 Value 6442.52001953125 Size 2 Cash 622.2595825195312
    2020-10-02, Order Canceled/Margin/Rejected
    Sell 2020-10-02 NVDA Price 544.5800170898438 Value 352.25 Size -1
    2020-10-30, | 180 |  2 |  15026.64 |
    

    The MSFT Data feed is:
    71d4bf75-7c2e-4f23-9c2d-a07e701b56d6-grafik.png

    Best,
    Jonny8



  • Could you print the order reference number order.ref every time the order info is printed ?



  • @vladisld said in Close Order doesn't close whole size and sell order gets rejected:

    order.ref

    Like this?

    Adding ticker: NVDA
    Adding ticker: GOOG
    Adding ticker: AMZN
    Adding ticker: MSFT
    Starting Portfolio Value: 10000.00
    Buy Submitted 2019-10-02 MSFT Ref 41 Price 137.07000732421875 Value 4934.520263671875 Size 36 Cash 1594.1796875
    Buy Submitted 2019-10-02 AMZN Ref 42 Price 1735.6500244140625 Value 3471.300048828125 Size 2 Cash 1594.1796875
    Buy 2019-10-02 MSFT Price 137.07000732421875 Value 4934.520263671875 Size 36
    Buy 2019-10-02 AMZN Price 1735.6500244140625 Value 3471.300048828125 Size 2
    Sell Submitted 2019-11-04 AMZN Ref 43 Price 1791.43994140625 Value -3582.8798828125 Size -2
    Buy Submitted 2019-11-04 GOOG Ref 44 Price 1273.739990234375 Value 5094.9599609375 Size 4 Cash 82.099609375
    Sell 2019-11-04 AMZN Price 1791.43994140625 Value 3471.300048828125 Size -2
    Buy 2019-11-04 GOOG Price 1273.739990234375 Value 5094.9599609375 Size 4
    Buy Submitted 2020-01-03 NVDA Ref 45 Price 239.91000366210938 Value 5517.930084228516 Size 23 Cash 82.099609375
    2020-01-03, Order Canceled/Margin/Rejected
    Buy Submitted 2020-02-04 NVDA Ref 46 Price 240.3300018310547 Value 6008.250045776367 Size 25 Cash 82.099609375
    2020-02-04, Order Canceled/Margin/Rejected
    Buy Submitted 2020-03-03 NVDA Ref 47 Price 276.42999267578125 Value 5805.029846191406 Size 21 Cash 82.099609375
    2020-03-03, Order Canceled/Margin/Rejected
    Buy Submitted 2020-04-02 NVDA Ref 48 Price 243.07000732421875 Value 4861.400146484375 Size 20 Cash 82.099609375
    Sell Submitted 2020-04-02 MSFT Ref 49 Price 152.11000061035156 Value -456.3300018310547 Size -3
    2020-04-02, Order Canceled/Margin/Rejected
    2020-04-02, Order Canceled/Margin/Rejected
    Sell Submitted 2020-05-04 GOOG Ref 50 Price 1320.6099853515625 Value -5282.43994140625 Size -4
    Buy Submitted 2020-05-04 NVDA Ref 51 Price 282.7799987792969 Value 5655.5999755859375 Size 20 Cash 5713.6795654296875
    Sell Submitted 2020-05-04 MSFT Ref 52 Price 174.57000732421875 Value -349.1400146484375 Size -2
    2020-05-04, Order Canceled/Margin/Rejected
    Sell 2020-05-04 GOOG Price 1320.6099853515625 Value 5094.9599609375 Size -4
    Sell 2020-05-04 MSFT Price 174.57000732421875 Value 274.1400146484375 Size -2
    Buy Submitted 2020-06-02 NVDA Ref 53 Price 352.25 Value 5636.0 Size 16 Cash 77.6795654296875
    Buy Submitted 2020-06-02 AMZN Ref 54 Price 2471.0400390625 Value 4942.080078125 Size 2 Cash 77.6795654296875
    2020-06-02, Order Canceled/Margin/Rejected
    Buy 2020-06-02 NVDA Price 352.25 Value 5636.0 Size 16
    Buy Submitted 2020-07-02 NVDA Ref 55 Price 381.20001220703125 Value 381.20001220703125 Size 1 Cash 77.6795654296875
    Buy Submitted 2020-07-02 AMZN Ref 56 Price 2878.699951171875 Value 5757.39990234375 Size 2 Cash 77.6795654296875
    2020-07-02, Order Canceled/Margin/Rejected
    2020-07-02, Order Canceled/Margin/Rejected
    Buy Submitted 2020-08-04 AMZN Ref 57 Price 3111.889892578125 Value 6223.77978515625 Size 2 Cash 77.6795654296875
    2020-08-04, Order Canceled/Margin/Rejected
    Buy Submitted 2020-09-02 AMZN Ref 58 Price 3499.1201171875 Value 6998.240234375 Size 2 Cash 77.6795654296875
    2020-09-02, Order Canceled/Margin/Rejected
    Sell Submitted 2020-10-02 NVDA Ref 59 Price 544.5800170898438 Value -544.5800170898438 Size -1
    Buy Submitted 2020-10-02 AMZN Ref 60 Price 3221.260009765625 Value 6442.52001953125 Size 2 Cash 622.2595825195312
    2020-10-02, Order Canceled/Margin/Rejected
    Sell 2020-10-02 NVDA Price 544.5800170898438 Value 352.25 Size -1
    2020-10-30, | 180 |  2 |  15026.64 |
    


  • Somehow i can't find how to edit my post:

    Adding ticker: NVDA
    Adding ticker: GOOG
    Adding ticker: AMZN
    Adding ticker: MSFT
    Starting Portfolio Value: 10000.00
    Buy Submitted 2019-10-02 MSFT Ref 61 Price 137.07000732421875 Value 4934.520263671875 Size 36 Cash 1594.1796875
    Buy Submitted 2019-10-02 AMZN Ref 62 Price 1735.6500244140625 Value 3471.300048828125 Size 2 Cash 1594.1796875
    Buy 2019-10-02 MSFT Ref 61 Price 137.07000732421875 Value 4934.520263671875 Size 36
    Buy 2019-10-02 AMZN Ref 62 Price 1735.6500244140625 Value 3471.300048828125 Size 2
    Sell Submitted 2019-11-04 AMZN Ref 63 Price 1791.43994140625 Value -3582.8798828125 Size -2
    Buy Submitted 2019-11-04 GOOG Ref 64 Price 1273.739990234375 Value 5094.9599609375 Size 4 Cash 82.099609375
    Sell 2019-11-04 AMZN Ref 63 Price 1791.43994140625 Value 3471.300048828125 Size -2
    Buy 2019-11-04 GOOG Ref 64 Price 1273.739990234375 Value 5094.9599609375 Size 4
    Buy Submitted 2020-01-03 NVDA Ref 65 Price 239.91000366210938 Value 5517.930084228516 Size 23 Cash 82.099609375
    2020-01-03, Order Canceled/Margin/Rejected
    Buy Submitted 2020-02-04 NVDA Ref 66 Price 240.3300018310547 Value 6008.250045776367 Size 25 Cash 82.099609375
    2020-02-04, Order Canceled/Margin/Rejected
    Buy Submitted 2020-03-03 NVDA Ref 67 Price 276.42999267578125 Value 5805.029846191406 Size 21 Cash 82.099609375
    2020-03-03, Order Canceled/Margin/Rejected
    Buy Submitted 2020-04-02 NVDA Ref 68 Price 243.07000732421875 Value 4861.400146484375 Size 20 Cash 82.099609375
    Sell Submitted 2020-04-02 MSFT Ref 69 Price 152.11000061035156 Value -456.3300018310547 Size -3
    2020-04-02, Order Canceled/Margin/Rejected
    2020-04-02, Order Canceled/Margin/Rejected
    Sell Submitted 2020-05-04 GOOG Ref 70 Price 1320.6099853515625 Value -5282.43994140625 Size -4
    Buy Submitted 2020-05-04 NVDA Ref 71 Price 282.7799987792969 Value 5655.5999755859375 Size 20 Cash 5713.6795654296875
    Sell Submitted 2020-05-04 MSFT Ref 72 Price 174.57000732421875 Value -349.1400146484375 Size -2
    2020-05-04, Order Canceled/Margin/Rejected
    Sell 2020-05-04 GOOG Ref 70 Price 1320.6099853515625 Value 5094.9599609375 Size -4
    Sell 2020-05-04 MSFT Ref 72 Price 174.57000732421875 Value 274.1400146484375 Size -2
    Buy Submitted 2020-06-02 NVDA Ref 73 Price 352.25 Value 5636.0 Size 16 Cash 77.6795654296875
    Buy Submitted 2020-06-02 AMZN Ref 74 Price 2471.0400390625 Value 4942.080078125 Size 2 Cash 77.6795654296875
    2020-06-02, Order Canceled/Margin/Rejected
    Buy 2020-06-02 NVDA Ref 73 Price 352.25 Value 5636.0 Size 16
    Buy Submitted 2020-07-02 NVDA Ref 75 Price 381.20001220703125 Value 381.20001220703125 Size 1 Cash 77.6795654296875
    Buy Submitted 2020-07-02 AMZN Ref 76 Price 2878.699951171875 Value 5757.39990234375 Size 2 Cash 77.6795654296875
    2020-07-02, Order Canceled/Margin/Rejected
    2020-07-02, Order Canceled/Margin/Rejected
    Buy Submitted 2020-08-04 AMZN Ref 77 Price 3111.889892578125 Value 6223.77978515625 Size 2 Cash 77.6795654296875
    2020-08-04, Order Canceled/Margin/Rejected
    Buy Submitted 2020-09-02 AMZN Ref 78 Price 3499.1201171875 Value 6998.240234375 Size 2 Cash 77.6795654296875
    2020-09-02, Order Canceled/Margin/Rejected
    Sell Submitted 2020-10-02 NVDA Ref 79 Price 544.5800170898438 Value -544.5800170898438 Size -1
    Buy Submitted 2020-10-02 AMZN Ref 80 Price 3221.260009765625 Value 6442.52001953125 Size 2 Cash 622.2595825195312
    2020-10-02, Order Canceled/Margin/Rejected
    Sell 2020-10-02 NVDA Ref 79 Price 544.5800170898438 Value 352.25 Size -1
    2020-10-30, | 180 |  2 |  15026.64 |
    

    now with all references (creation and execution of orders)



  • Adding ticker: NVDA
    Adding ticker: GOOG
    Adding ticker: AMZN
    Adding ticker: MSFT
    Starting Portfolio Value: 10000.00
    Buy Submitted 2019-10-02 MSFT Ref 81 Price 137.07000732421875 Value 4934.520263671875 Size 36 Cash 1594.1796875
    Buy Submitted 2019-10-02 AMZN Ref 82 Price 1735.6500244140625 Value 3471.300048828125 Size 2 Cash 1594.1796875
    Buy 2019-10-02 MSFT Ref 81 Price 137.07000732421875 Value 4934.520263671875 Size 36
    Buy 2019-10-02 AMZN Ref 82 Price 1735.6500244140625 Value 3471.300048828125 Size 2
    Sell Submitted 2019-11-04 AMZN Ref 83 Price 1791.43994140625 Value -3582.8798828125 Size -2
    Buy Submitted 2019-11-04 GOOG Ref 84 Price 1273.739990234375 Value 5094.9599609375 Size 4 Cash 82.099609375
    Sell 2019-11-04 AMZN Ref 83 Price 1791.43994140625 Value 3471.300048828125 Size -2
    Buy 2019-11-04 GOOG Ref 84 Price 1273.739990234375 Value 5094.9599609375 Size 4
    Buy Submitted 2020-01-03 NVDA Ref 85 Price 239.91000366210938 Value 5517.930084228516 Size 23 Cash 82.099609375
    Order Canceled/Margin/Rejected Ref 85
    Buy Submitted 2020-02-04 NVDA Ref 86 Price 240.3300018310547 Value 6008.250045776367 Size 25 Cash 82.099609375
    Order Canceled/Margin/Rejected Ref 86
    Buy Submitted 2020-03-03 NVDA Ref 87 Price 276.42999267578125 Value 5805.029846191406 Size 21 Cash 82.099609375
    Order Canceled/Margin/Rejected Ref 87
    Buy Submitted 2020-04-02 NVDA Ref 88 Price 243.07000732421875 Value 4861.400146484375 Size 20 Cash 82.099609375
    Sell Submitted 2020-04-02 MSFT Ref 89 Price 152.11000061035156 Value -456.3300018310547 Size -3
    Order Canceled/Margin/Rejected Ref 88
    Order Canceled/Margin/Rejected Ref 89
    Sell Submitted 2020-05-04 GOOG Ref 90 Price 1320.6099853515625 Value -5282.43994140625 Size -4
    Buy Submitted 2020-05-04 NVDA Ref 91 Price 282.7799987792969 Value 5655.5999755859375 Size 20 Cash 5713.6795654296875
    Sell Submitted 2020-05-04 MSFT Ref 92 Price 174.57000732421875 Value -349.1400146484375 Size -2
    Order Canceled/Margin/Rejected Ref 91
    Sell 2020-05-04 GOOG Ref 90 Price 1320.6099853515625 Value 5094.9599609375 Size -4
    Sell 2020-05-04 MSFT Ref 92 Price 174.57000732421875 Value 274.1400146484375 Size -2
    Buy Submitted 2020-06-02 NVDA Ref 93 Price 352.25 Value 5636.0 Size 16 Cash 77.6795654296875
    Buy Submitted 2020-06-02 AMZN Ref 94 Price 2471.0400390625 Value 4942.080078125 Size 2 Cash 77.6795654296875
    Order Canceled/Margin/Rejected Ref 94
    Buy 2020-06-02 NVDA Ref 93 Price 352.25 Value 5636.0 Size 16
    Buy Submitted 2020-07-02 NVDA Ref 95 Price 381.20001220703125 Value 381.20001220703125 Size 1 Cash 77.6795654296875
    Buy Submitted 2020-07-02 AMZN Ref 96 Price 2878.699951171875 Value 5757.39990234375 Size 2 Cash 77.6795654296875
    Order Canceled/Margin/Rejected Ref 95
    Order Canceled/Margin/Rejected Ref 96
    Buy Submitted 2020-08-04 AMZN Ref 97 Price 3111.889892578125 Value 6223.77978515625 Size 2 Cash 77.6795654296875
    Order Canceled/Margin/Rejected Ref 97
    Buy Submitted 2020-09-02 AMZN Ref 98 Price 3499.1201171875 Value 6998.240234375 Size 2 Cash 77.6795654296875
    Order Canceled/Margin/Rejected Ref 98
    Sell Submitted 2020-10-02 NVDA Ref 99 Price 544.5800170898438 Value -544.5800170898438 Size -1
    Buy Submitted 2020-10-02 AMZN Ref 100 Price 3221.260009765625 Value 6442.52001953125 Size 2 Cash 622.2595825195312
    Order Canceled/Margin/Rejected Ref 100
    Sell 2020-10-02 NVDA Ref 99 Price 544.5800170898438 Value 352.25 Size -1
    2020-10-30, | 180 |  2 |  15026.64 |
    

    incl. rejected orders @.@



  • Correct way to write this:

    def next(self):
        pass
    

    The problem in your code is here:

        # Sell stocks no longer meeting ranking filter.
        for i, d in enumerate(rankings):
            if self.getposition(d).size:
                if i > self.p.num_positions:
                    self.close(d)
    

    if i > 2, close the position, but since python uses zero-based indexing, it will only close a position if a stock is in 4th spot



  • @hghhgghdf-dfdf
    Thank you for your help!

    I see that this might be another issue.
    But does it explain why only a share of my shares get sold or the order gets rejected?

    I am trying to adapt the official canon at the moment since it most likely work most smotthly (once i manage to get it running): https://www.backtrader.com/blog/2019-07-19-rebalancing-conservative/rebalancing-conservative/



  • But does it explain why only a share of my shares get sold or the order gets rejected?

    self.order_target_percent(d, target=pos_size)
    

    This line sold those 3 shares. You're ordering for the target pct to be hit every candle so as the value of your account fluctuates, this method will add/subtract to your position to match the pct.



  • So order_target_percent just tried to bring the share of MSFT back to the 50% of the total portfolio.
    Great! Thnk you for that!

    Sell Submitted 2020-04-02 MSFT Ref 89 Price 152.11000061035156 Value -456.3300018310547 Size -3
    Order Canceled/Margin/Rejected Ref 89
    

    This is still beyond my limited understanding...



  • not sure either.
    I'd suggest cleaning up previous mistakes and walking through the code with breakpoints and more detailed logging. These kind of issues are always much easier to fix if you have personal access to the code/data.



  • @Jonny8 said in Close Order doesn't close whole size and sell order gets rejected:

    @hghhgghdf-dfdf
    Thank you for your help!
    I see that this might be another issue.

    I submit this is not another issue and @hghhgghdf-dfdf has identified your problem. If you are not selling the position you think you are selling, then you don't have the cash to make further purchases, resulting in margin rejections.

    Let's say you wish to hold 4 positions. You want to close out any positions in the rankings list past 4 positions. So using zero based number, you enumerate :

    0, 1, 2, 3, 4, 5 etc
    

    To eliminate those that are greater than four postions, you need >=. Try this:

     # Sell stocks no longer meeting ranking filter.
            for i, d in enumerate(rankings):
                if self.getposition(d).size:
                    if i >= self.p.num_positions:
                        self.close(d)
    
    


  • @run-out

    Thank you as well!
    That helped but somehow there are still rejected orders... I thought the orders issued first will be placed first:

    Adding ticker: NVDA
    Adding ticker: GOOG
    Adding ticker: AMZN
    Adding ticker: MSFT
    Starting Portfolio Value: 10000.00
    Buy Submitted 2019-10-02 MSFT Ref 61 Price 137.07000732421875 Value 4934.520263671875 Size 36 Cash 1594.1796875 Broker value 9868.039428710938
    Buy Submitted 2019-10-02 AMZN Ref 62 Price 1735.6500244140625 Value 3471.300048828125 Size 2 Cash 1594.1796875 Broker value 9868.039428710938
    Buy 2019-10-02 MSFT Ref 61 Price 137.07000732421875 Value 4934.520263671875 Size 36
    Buy 2019-10-02 AMZN Ref 62 Price 1735.6500244140625 Value 3471.300048828125 Size 2
    Sell Submitted 2019-11-04 AMZN Ref 63 Price 1791.43994140625 Value -3582.8798828125 Size -2 Broker value 10451.379699707031
    Buy Submitted 2019-11-04 GOOG Ref 64 Price 1273.739990234375 Value 5094.9599609375 Size 4 Cash 82.099609375 Broker value 10451.379699707031
    Sell 2019-11-04 AMZN Ref 63 Price 1791.43994140625 Value 3471.300048828125 Size -2
    Buy 2019-11-04 GOOG Ref 64 Price 1273.739990234375 Value 5094.9599609375 Size 4
    Sell Submitted 2020-01-03 GOOG Ref 65 Price 1367.3699951171875 Value -5469.47998046875 Size -4 Broker value 11173.579498291016
    Buy Submitted 2020-01-03 NVDA Ref 66 Price 239.91000366210938 Value 5517.930084228516 Size 23 Cash 33.649505615234375 Broker value 11173.579498291016
    Sell 2020-01-03 GOOG Ref 65 Price 1367.3699951171875 Value 5094.9599609375 Size -4
    Buy 2020-01-03 NVDA Ref 66 Price 239.91000366210938 Value 5517.930084228516 Size 23
    Sell Submitted 2020-02-04 MSFT Ref 67 Price 174.3800048828125 Value -6277.68017578125 Size -36 Broker value 11885.509811401367
    Buy Submitted 2020-02-04 NVDA Ref 68 Price 240.3300018310547 Value 240.3300018310547 Size 1 Cash 1613.1798553466797 Broker value 11885.509811401367
    Buy Submitted 2020-02-04 GOOG Ref 69 Price 1485.93994140625 Value 4457.81982421875 Size 3 Cash 1613.1798553466797 Broker value 11885.509811401367
    Sell 2020-02-04 MSFT Ref 67 Price 174.3800048828125 Value 4934.520263671875 Size -36
    Buy 2020-02-04 NVDA Ref 68 Price 240.3300018310547 Value 240.3300018310547 Size 1
    Buy 2020-02-04 GOOG Ref 69 Price 1485.93994140625 Value 4457.81982421875 Size 3
    Sell Submitted 2020-03-03 NVDA Ref 70 Price 276.42999267578125 Value -276.42999267578125 Size -1 Broker value 11981.530258178711
    Buy Submitted 2020-03-03 GOOG Ref 71 Price 1389.1099853515625 Value 1389.1099853515625 Size 1 Cash 500.49986267089844 Broker value 11981.530258178711
    Sell 2020-03-03 NVDA Ref 70 Price 276.42999267578125 Value 239.92750358581543 Size -1
    Buy 2020-03-03 GOOG Ref 71 Price 1389.1099853515625 Value 1389.1099853515625 Size 1
    Sell Submitted 2020-04-02 GOOG Ref 72 Price 1105.6199951171875 Value -4422.47998046875 Size -4 Broker value 10786.389877319336
    Sell Submitted 2020-04-02 NVDA Ref 73 Price 243.07000732421875 Value -243.07000732421875 Size -1 Broker value 10786.389877319336
    Buy Submitted 2020-04-02 MSFT Ref 74 Price 152.11000061035156 Value 5171.740020751953 Size 34 Cash 5166.049850463867 Broker value 10786.389877319336
    Order Canceled/Margin/Rejected Ref 74
    Sell 2020-04-02 GOOG Ref 72 Price 1105.6199951171875 Value 5846.9298095703125 Size -4
    Sell 2020-04-02 NVDA Ref 73 Price 243.07000732421875 Value 239.92750358581543 Size -1
    Sell Submitted 2020-05-04 NVDA Ref 75 Price 282.7799987792969 Value -282.7799987792969 Size -1 Broker value 11565.920028686523
    Buy Submitted 2020-05-04 MSFT Ref 76 Price 174.57000732421875 Value 5586.240234375 Size 32 Cash 5448.829849243164 Broker value 11565.920028686523
    Order Canceled/Margin/Rejected Ref 76
    Sell 2020-05-04 NVDA Ref 75 Price 282.7799987792969 Value 239.92750358581543 Size -1
    Sell Submitted 2020-06-02 NVDA Ref 77 Price 352.25 Value -704.5 Size -2 Broker value 12863.259780883789
    Buy Submitted 2020-06-02 AMZN Ref 78 Price 2471.0400390625 Value 4942.080078125 Size 2 Cash 1211.249771118164 Broker value 12863.259780883789
    Sell 2020-06-02 NVDA Ref 77 Price 352.25 Value 479.85500717163086 Size -2
    Buy 2020-06-02 AMZN Ref 78 Price 2471.0400390625 Value 4942.080078125 Size 2
    Sell Submitted 2020-08-04 NVDA Ref 79 Price 440.4100036621094 Value -440.4100036621094 Size -1 Broker value 16013.299667358398
    Sell 2020-08-04 NVDA Ref 79 Price 440.4100036621094 Value 239.92750358581543 Size -1
    Sell Submitted 2020-09-02 NVDA Ref 80 Price 552.8400268554688 Value -552.8400268554688 Size -1 Broker value 19023.019454956055
    Sell 2020-09-02 NVDA Ref 80 Price 552.8400268554688 Value 239.92750358581543 Size -1
    2020-10-30, | 180 |  2 |  16799.92 |
    

    Like 75 before 76?



  • The second issue is that you are trying to invest 100% of the portfolio. This is a bit challenging as stock prices move after you calculate the nuber of shares to buy, which means you might not have enough money to purchase the shares you intended.

    In your data, your broker margin rejects are of this nature.

    Buy Submitted 2020-04-02 MSFT Ref 74 Price 152.11000061035156 Value 5171.740020751953 Size 34 Cash 5166.049850463867 Broker value 10786.389877319336
    Order Canceled/Margin/Rejected Ref 74
    

    Note the value you are trying to purchase and the cash available. This is why the trade is rejected, not enough funds.

    One remedy is to use a buffer factor to reduce the overall account balance that you calculate share sizes from Typically depending on volatility a 2% buffer is enough to solve this problem.

    account market value = broker value * (1 - buffer)
    

    You would use the account market value to calculate shares to buy. This should leave enough cash to cover fluctuations.

    Also you could consider using Cheat On Open, which is designed for this problem.

    https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open/



  • @run-out

    Thanks again for the long reply!
    I just tried cheat on open, but the result is the same.
    I thought about including a buy margin and it seems this is the way to go... for that example...
    Hence, i assume the orders are not done by theirs reference numbers otherwise order 75 would not be canceled?

    Best



});