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

Backtrader Beginner Issue:



  • Hi! I have a trade system that buys/sells an asset at the D-0 closing price and it closes that same position on D+1 closing price. I don't know why some days its not executing my orders although the code is executed as you can see bellow. Could you please help me with that? Note: modelAI() output is always 1 or 2, so it's not that the issue. Thanks!

    def notify_trade(self, trade):
    if not trade.isclosed:
    return

        self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                 (trade.pnl, trade.pnlcomm))
        
    def next(self):
        current_position = self.getposition().size
        print("Open Position:", current_position)
      
        tamanho = 5000
        #print("Size", tamanho)
    
        if current_position > 0:
            self.log('Closing long position, %.2f' % self.dataclose[0])
            #self.order = self.sell(size=tamanho)   
            order1 = self.close(size=tamanho, price= self.dataclose[0]) 
            
        if current_position < 0:
            self.log('Closing short position, %.2f' % self.dataclose[0])
            #self.order = self.buy(size=tamanho)      
            order1 = self.close(size=tamanho, price= self.dataclose[0]) 
    
        
        if modelAI(self.datas[0].datetime.date(0)) == 1:
            self.log('BUY CREATE, %.2f' % self.dataclose[0])
            order2 = self.buy(size=tamanho, price= self.dataclose[0])
    
        if modelAI(self.datas[0].datetime.date(0)) == 2:
            self.log('SELL CREATE, %.2f' % self.dataclose[0])
            order2 = self.sell(size=tamanho, price= self.dataclose[0])  
    

    Open Position: 0
    2020-03-02, BUY CREATE, 26.53
    Open Position: 0
    2020-03-03, BUY CREATE, 26.05
    Open Position: 0
    2020-03-04, BUY CREATE, 26.89
    Open Position: 0
    2020-03-05, BUY CREATE, 25.29
    Open Position: 0
    2020-03-06, BUY CREATE, 22.83
    Open Position: 0
    2020-03-09, BUY CREATE, 16.05
    Open Position: 5000
    2020-03-10, Closing long position, 17.56
    2020-03-10, BUY CREATE, 17.56
    2020-03-11, OPERATION PROFIT, GROSS -6850.00, NET -7006.85
    Open Position: 5000
    2020-03-11, Closing long position, 15.85
    2020-03-11, BUY CREATE, 15.85
    2020-03-12, OPERATION PROFIT, GROSS -19600.00, NET -19730.40
    Open Position: 5000



  • my guess would be: you have 10k on you account and try to buy on 5000 x 26 = 13k. so bt rejects your orders since not enough funds is available.



  • @ab_trader That was the problem!!! Thanks!!!!


Log in to reply
 

});