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

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.gp = 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].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
            
        def next(self):
            self.log('Close, %.2f' % self.close[0])
            
            if self.order:
                return
            
            if self.gp == 0:
                if self.position:
                    self.order = self.sell()
            
            if self.gp == 1:
                #self.buy(stake = 1, exectype=Order.Market)
                #print('%03d %f' % (len(self), self.data.l.close[0],))
                self.order = self.buy()
                
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                # 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():
                    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:
                return
    
            self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                     (trade.pnl, trade.pnlcomm))
    
    
    cerebro = bt.Cerebro()
    data = CustomDataLoader(dataname= df5, plot=True)
    cerebro.addstrategy(TestStrategy)
    cerebro.adddata(data)
    cerebro.broker.setcash(100000.0)
    cerebro.addsizer(bt.sizers.FixedSize, stake=10)
    cerebro.broker.setcommission(commission=0.001)
    cerebro.run()
    cerebro.broker.getvalue()
    
            
    

    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:

    5821658823529

    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(
            self.datas[0].datetime.date[0],  
            self.datas[0].open[0],
            self.datas[0].high[0],
            self.datas[0].low[0],
            self.datas[0].close[0],
    )
    

    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

    Thanks


Log in to reply
 

});