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

Order Getting Rejected, can't find a reason



  • I used this code successfully in a different data a couple of months ago. But now, its not working. All my orders are getting rejected and I don't know why.

    My dataframe looks like this:

     	Time 	                Open 	        Close         percentage_12hour    percentage_1day
    0 	2019-07-01 00:00:00 	10854.10 	10854.10 	6.677368 	     6.183376
    1 	2019-07-01 00:30:00 	11007.65 	11007.65 	6.677368 	     6.183376
    2 	2019-07-01 01:00:00 	11091.06 	11091.06 	6.677368 	     6.183376
    3 	2019-07-01 01:30:00 	11043.72 	11043.72 	6.677368 	     6.183376
    4 	2019-07-01 02:00:00 	10981.15 	10981.15 	6.677368 	     6.183376
    

    percentage_12hour and percentage_1day are custom variables I made manually. I use them to determine if i should trade. So, I have defined a custom Pandas feed

    class PandasData_Custom(bt.feeds.PandasData):
        lines = ('percentage_12hour','percentage_1day',)
        params = (
            ('datetime', 0),
            ('open', 1),
            ('high', None),
            ('low', None),
            ('close', 2),
            ('volume', None),
            ('percentage_12hour', 2),
            ('percentage_1day', 2),
        )
    

    And I have a trivial logic to test the working:

    class tradeStrategy(bt.Strategy):
        def __init__(self):
            self.dataopen = self.datas[0].open
            self.dataclose = self.datas[0].close
            self.twelve_hour = self.datas[0].percentage_12hour
            self.one_day = self.datas[0].percentage_1day
            self.buy_percentage = 0
            self.order=None
            self.buyprice=None
            self.buycomm=None
            
            self.trades = io.StringIO()
            self.trades_writer = csv.writer(self.trades)
            
            self.operations = io.StringIO()
            self.operations_writer = csv.writer(self.operations)
            
            self.portfolioValue = io.StringIO()
            self.portfolioValue_writer = csv.writer(self.portfolioValue)
            
        def log(self, txt, dt=None):
            dt = dt or self.datas[0].datetime.datetime(0)
            print("Datetime: {} Message: {} Percentage: {}".format(dt, txt, self.buy_percentage))
            
        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                return
            
            if order.status in [order.Completed]:
                if order.isbuy():
                    ordertype = "BUY"
                    #self.log("BUY EXECUTED, Price: {}, Cost: {}, Comm: {}".format(order.executed.price, order.executed.value, order.executed.comm))
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                else:
                    ordertype = "SELL"
                    #self.log("SELL EXECUTED, Price: {}, Cost: {}, Comm: {}".format(order.executed.price, order.executed.value, order.executed.comm))
    
                self.trades_writer.writerow([self.datas[0].datetime.datetime(0), ordertype, order.executed.price, order.executed.value, order.executed.comm])
            
                self.bar_executed = len(self)
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log("Order Canceled/Margin/Rejected")
                self.log(order.Rejected)
                self.trades_writer.writerow([self.datas[0].datetime.datetime(0) , 'Rejection', 0, 0, 0])
                
            self.order = None
        
        def notify_trade(self, trade):
            if not trade.isclosed:
                return
            
            self.log('OPERATION PROFIT, GROSS: {}, NET: {}'.format(trade.pnl, trade.pnlcomm))
            self.operations_writer.writerow([self.datas[0].datetime.datetime(0), trade.pnlcomm])
            
        def notify_data(self, data, status, *args, **kwargs):
            print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args)
            if status == data.LIVE:
                self.datastatus = 1
            
        def notify_store(self, msg, *args, **kwargs):
            print('*' * 5, 'STORE NOTIF:', msg)
        
        def get_logs(self):
            '''
            Returns:
            ________
            portfolioValue (df):
            Date and Value of portfolio
            
            trades (df):
            'Date', 'Type', 'Price', 'Total Spent', 'Comission'
            
            operations (df):
            'Date', 'Profit'
            '''
            self.portfolioValue.seek(0)
            portfolioValueDf = pd.read_csv(self.portfolioValue, names=['Date', 'Value'])
            
            portfolioValueDf['Date'] = pd.to_datetime(portfolioValueDf['Date'])
            portfolioValueDf = portfolioValueDf.set_index('Date')
            portfolioValueDf = portfolioValueDf.resample('1D').agg({'Date': lambda x: x.iloc[0], 'Value': lambda x: x.iloc[-1]})['Date']
            
            self.trades.seek(0)
            tradesDf = pd.read_csv(self.trades, names=['Date', 'Type', 'Price', 'Total Spent', 'Comission'])
            
            self.operations.seek(0)
            operationsDf = pd.read_csv(self.operations, names=['Date', 'Profit'])
            
            return portfolioValueDf.reset_index(), tradesDf, operationsDf
        
        
        def next(self):
            self.portfolioValue_writer.writerow([self.datas[0].datetime.datetime(0), self.broker.get_cash()])
            
            if self.order:
                return
            
            if not self.position: #buy if value is much greater than the one earlier
                if self.twelve_hour[-6] -  self.twelve_hour[0] > 1:
                    self.log("BUY CREATE {}".format(self.dataopen[0]))
                    self.order = self.buy()
                    self.buy_percentage = self.twelve_hour[-6] -  self.twelve_hour[0]
            else: #use better logic ofc
                if self.twelve_hour[-6] -  self.twelve_hour[0] < self.buy_percentage:
                    self.log("SELL CREATE {}".format(self.dataopen[0]))
                    self.order = self.sell()
    

    However, when I run it, all my trades are getting rejected

    data = PandasData_Custom(dataname=features)
    
    cerebro = bt.Cerebro()
    cerebro.adddata(data)
    
    cerebro.addstrategy(tradeStrategy)
    
    cerebro.addsizer(bt.sizers.SizerFix, stake=100)
    
    cerebro.broker.setcash(10000)
    cerebro.broker.setcommission(0.1/100)
    
    run = cerebro.run()
    
    portfolioValue, trades, operations = run[0].get_logs()
    

    My output looks like this:

    Datetime: 2019-07-01 06:30:00 Message: BUY CREATE 11063.89 Percentage: 0
    Datetime: 2019-07-01 07:00:00 Message: Order Canceled/Margin/Rejected Percentage: 65.44000000000051
    Datetime: 2019-07-01 07:00:00 Message: 8 Percentage: 65.44000000000051
    Datetime: 2019-07-01 07:00:00 Message: BUY CREATE 10960.4 Percentage: 65.44000000000051
    Datetime: 2019-07-01 07:30:00 Message: Order Canceled/Margin/Rejected Percentage: 178.97000000000116
    Datetime: 2019-07-01 07:30:00 Message: 8 Percentage: 178.97000000000116
    Datetime: 2019-07-01 07:30:00 Message: BUY CREATE 10945.96 Percentage: 178.97000000000116
    Datetime: 2019-07-01 08:00:00 Message: Order Canceled/Margin/Rejected Percentage: 159.6200000000008
    Datetime: 2019-07-01 08:00:00 Message: 8 Percentage: 159.6200000000008
    Datetime: 2019-07-01 08:00:00 Message: BUY CREATE 10983.7 Percentage: 159.6200000000008
    Datetime: 2019-07-01 08:30:00 Message: Order Canceled/Margin/Rejected Percentage: 160.90999999999985
    Datetime: 2019-07-01 08:30:00 Message: 8 Percentage: 160.90999999999985
    Datetime: 2019-07-01 08:30:00 Message: BUY CREATE 10998.0 Percentage: 160.90999999999985
    Datetime: 2019-07-01 09:00:00 Message: Order Canceled/Margin/Rejected Percentage: 165.35000000000036
    Datetime: 2019-07-01 09:00:00 Message: 8 Percentage: 165.35000000000036
    Datetime: 2019-07-01 11:30:00 Message: BUY CREATE 10964.0 Percentage: 165.35000000000036
    Datetime: 2019-07-01 12:00:00 Message: Order Canceled/Margin/Rejected Percentage: 34.0
    Datetime: 2019-07-01 12:00:00 Message: 8 Percentage: 34.0
    Datetime: 2019-07-01 12:00:00 Message: BUY CREATE 11020.87 Percentage: 34.0
    Datetime: 2019-07-01 12:30:00 Message: Order Canceled/Margin/Rejected Percentage: 157.2599999999984
    Datetime: 2019-07-01 12:30:00 Message: 8 Percentage: 157.2599999999984
    Datetime: 2019-07-01 12:30:00 Message: BUY CREATE 10748.3 Percentage: 157.2599999999984
    Datetime: 2019-07-01 13:00:00 Message: Order Canceled/Margin/Rejected Percentage: 342.3299999999999
    Datetime: 2019-07-01 13:00:00 Message: 8 Percentage: 342.3299999999999
    Datetime: 2019-07-01 13:00:00 Message: BUY CREATE 10665.04 Percentage: 342.3299999999999
    Datetime: 2019-07-01 13:30:00 Message: Order Canceled/Margin/Rejected Percentage: 442.1299999999992
    Datetime: 2019-07-01 13:30:00 Message: 8 Percentage: 442.1299999999992
    Datetime: 2019-07-01 13:30:00 Message: BUY CREATE 10708.9 Percentage: 442.1299999999992
    Datetime: 2019-07-01 14:00:00 Message: Order Canceled/Margin/Rejected Percentage: 295.77000000000044
    Datetime: 2019-07-01 14:00:00 Message: 8 Percentage: 295.77000000000044
    Datetime: 2019-07-01 14:00:00 Message: BUY CREATE 10578.21 Percentage: 295.77000000000044
    Datetime: 2019-07-01 14:30:00 Message: Order Canceled/Margin/Rejected Percentage: 416.2400000000016
    Datetime: 2019-07-01 14:30:00 Message: 8 Percentage: 416.2400000000016
    Datetime: 2019-07-01 14:30:00 Message: BUY CREATE 10606.07 Percentage: 416.2400000000016
    Datetime: 2019-07-01 15:00:00 Message: Order Canceled/Margin/Rejected Percentage: 357.9300000000003
    Datetime: 2019-07-01 15:00:00 Message: 8 Percentage: 357.9300000000003
    Datetime: 2019-07-01 15:00:00 Message: BUY CREATE 10294.35 Percentage: 357.9300000000003
    Datetime: 2019-07-01 15:30:00 Message: Order Canceled/Margin/Rejected Percentage: 726.5200000000004
    Datetime: 2019-07-01 15:30:00 Message: 8 Percentage: 726.5200000000004
    Datetime: 2019-07-01 15:30:00 Message: BUY CREATE 10109.56 Percentage: 726.5200000000004
    Datetime: 2019-07-01 16:00:00 Message: Order Canceled/Margin/Rejected Percentage: 638.7399999999998
    Datetime: 2019-07-01 16:00:00 Message: 8 Percentage: 638.7399999999998
    Datetime: 2019-07-01 16:00:00 Message: BUY CREATE 10390.17 Percentage: 638.7399999999998
    Datetime: 2019-07-01 16:30:00 Message: Order Canceled/Margin/Rejected Percentage: 274.8700000000008
    Datetime: 2019-07-01 16:30:00 Message: 8 Percentage: 274.8700000000008
    Datetime: 2019-07-01 16:30:00 Message: BUY CREATE 10451.0 Percentage: 274.8700000000008
    Datetime: 2019-07-01 17:00:00 Message: Order Canceled/Margin/Rejected Percentage: 257.89999999999964
    Datetime: 2019-07-01 17:00:00 Message: 8 Percentage: 257.89999999999964
    Datetime: 2019-07-01 17:00:00 Message: BUY CREATE 10317.18 Percentage: 257.89999999999964
    Datetime: 2019-07-01 17:30:00 Message: Order Canceled/Margin/Rejected Percentage: 261.02999999999884
    

    My trades are getting rejected but notify_store does not notify anything. It is written here. "The reason will be notified via the notify_store method of the strategy". But it seems I am not getting notified. What am I doing wrong?


  • administrators

    @warproxxx said in Order Getting Rejected, can't find a reason:

    I used this code successfully

    @warproxxx said in Order Getting Rejected, can't find a reason:

    in a different data

    @warproxxx said in Order Getting Rejected, can't find a reason:

    cerebro.broker.setcash(10000)

    @warproxxx said in Order Getting Rejected, can't find a reason:

    Datetime: 2019-07-01 06:30:00 Message: BUY CREATE 11063.89 Percentage: 0



  • Thank you. This was really foolish of me.


Log in to reply
 

});