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

Pandas datafeed and simple test strat return Order Canceled/Margin/Rejected

  • hi,

    I receive this log resopnse:

    2019-06-25, Close, 5821658823529.41
    2019-06-25, Close, 5827325581395.35
    2019-06-25, Order Canceled/Margin/Rejected
    2019-06-25, Close, 5831271259418.73
    2019-06-25, Order Canceled/Margin/Rejected

    class CustomDataLoader(btfeeds.PandasData):
        lines = ('g_p',)
        params = (
            #('open', 'Open'),
            #('high', 'High'),
            #('low', 'Low'),
            #('close', 'Close'),
            #('volume', None),
            #('openinterest', None),
            ('g_prediction', -1),
    class TestStrategy(bt.Strategy):
        def __init__(self):
            self.close = self.datas[0].close
   = self.datas[0].g_p
            self.order = None
            self.buyprice = None
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0]
            print('%s, %s' % (dt.isoformat(), txt))
        def next(self):
            self.log('Close, %.2f' % self.close[0])
            if self.order:
            if == 0:
                if self.position:
                    self.order = self.sell()
            if == 1:
       = 1, exectype=Order.Market)
                #print('%03d %f' % (len(self),[0],))
                self.order =
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
            # 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():
                    self.log('BUY EXECUTED, Price: %.2f'   %(order.executed.price))
                    self.buyprice = order.executed.price
                    #self.buycomm = order.executed.comm
                else:  # Sell
                    self.log('SELL EXECUTED, Price: %.2f'   %(order.executed.price))
                self.bar_executed = len(self)
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
            self.order = None
        def notify_trade(self, trade):
            if not trade.isclosed:
            self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                     (trade.pnl, trade.pnlcomm))
    cerebro = bt.Cerebro()
    data = CustomDataLoader(dataname= df5, plot=True)
    cerebro.addsizer(bt.sizers.FixedSize, stake=10)

    running on jupyter.

    I assume I am missing out on setting something but I have used the generic strat in the quickstart guide as an example

  • @azuric said in Pandas datafeed and simple test strat return Order Canceled/Margin/Rejected:


    Your strategy is showing quite a large stock price. The order is rejected because there isn't $58 trillion in the account. Check your data columns going in. Try inserting this right after next...

    def next(self):
        self.log("Date {} Open {:.2f} High {:.2f} Low {:.2f} Close {:.2f}".format(

    See if the values match your input data.

  • Making my account extremely large did solve the issue.

    Alternatively I can divide close by 10**10


Log in to reply