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

Cannot send buy order to Binance



  • Re: Anyone use backtrader to do live trading on Bitcoin exchange?

    Hi @Ed-Bartosh,
    First of all, thanks for your excellent work so far.

    I tried to implement this simple so called "Asian" strategy:

    • open 1 long position at 2 p.m.
    • close this position at 1 a.m.
    • repeat this process on a daily basis
    class Asian(bt.Strategy):
        """ Asian statrategy.
    
        Hypothesis
        ----------
        Americans are eager to buy crypto currencies at NY open and Asians want to to sell when Tokyo opens.
        As a result, prices are supposed to surge during NY opening hours and fall back once Tokyo exchange opens its doors.
        
        Base strategy
        -------------
        - symbol: any symbol (get started with ETH/BTC and ETH/USDT)
        - granularity: 1H bars
        - market buy @ 2 p.m. UCT
        - close position @ 1 a.m. UCT
        
        Parameters
        ----------
        - buy -time of each day    (default: 2 p.m. UCT)
        - close -time of each day  (default: 1 a.m. UCT)
        """
        
        params = (
            ('buy_at',  '14:00:00'),
            ('sell_at', '01:00:00'),
        )
        
        def log(self, txt, dt=None):
            """ Logging function for this strategy
            """
            dt = dt or self.datas[0].datetime.date(0)
            time = self.datas[0].datetime.time()
            print('%s - %s, %s' % (dt.isoformat(), time, txt))
        
        
        def start(self):
            self.buy_time = datetime.strptime(self.p.buy_at, "%H:%M:%S").time()
            self.sell_time = datetime.strptime(self.p.sell_at, "%H:%M:%S").time()
            self.has_traded = False
            print('START')
            
        def __init__(self):
            self.dataclose = self.data.close
        
        def notify_order(self, order):
            if order.status in [order.Margin, order.Rejected]:
                self.log("ORDER REJECTED")
    
            if order.status == order.Submitted:
                self.log("ORDER SUBMITTED")
            
            if order.status == order.Accepted:
                self.log("ORDER ACCEPTED")
            
            if order.status == order.Cancelled:
                self.log("ORDER CANCELLED")
            
            if order.status == order.Completed:
                self.log("ORDER COMPLETED")
    
                
        def next(self):
            current_time =  self.datetime.time(ago=0)
            past_time = self.datetime.time(ago=-1)
            print('*' * 5,
                  'NEXT:', bt.num2date(self.data0.datetime[0]),
                  'Symbol: ',self.data0._name,
                  'Open: ', self.data0.open[0],
                  'High: ', self.data0.high[0],
                  'Low: ', self.data0.low[0],
                  'Close: ', self.data0.close[0],
                  'Volume: ', self.data0.volume[0],
                   bt.TimeFrame.getname(self.data0._timeframe),
                  len(self.data0))
            
            if (current_time >= self.buy_time) and (past_time < self.buy_time):
                size = 1.0 / self.dataclose
                self.log("BUY! -- {}".format(size))
                o1 = self.buy(self.data0, 
                              exectype=bt.Order.Market,
                              size=size
                             )
                self.pos = True
                self.has_traded = True
            
            if (self.has_traded) and (current_time >= self.sell_time) and (past_time < self.sell_time):
                self.log("SELL!")
                self.close()
                self.pos = False
    

    When running the strategy, the output looks promising.
    It looks like the Buy market order is accepted by Binance:

    START
    ***** NEXT: 2018-03-28 13:58:00 Symbol:  eth_usdt_min Open:  446.6 High:  447.3 Low:  446.45 Close:  446.68 Volume:  36.90053 Minute 1
    ***** NEXT: 2018-03-28 13:59:00 Symbol:  eth_usdt_min Open:  447.21 High:  447.21 Low:  446.47 Close:  447.21 Volume:  1.41311 Minute 2
    ***** NEXT: 2018-03-28 14:00:00 Symbol:  eth_usdt_min Open:  447.26 High:  447.26 Low:  446.61 Close:  446.61 Volume:  0.05101 Minute 3
    2018-03-28 - 14:00:00, BUY! -- 0.0022390900338102593
    2018-03-28 - 14:01:00, ORDER SUBMITTED
    2018-03-28 - 14:01:00, ORDER ACCEPTED
    2018-03-28 - 14:01:00, ORDER COMPLETED
    ***** NEXT: 2018-03-28 14:57:00 Symbol:  eth_usdt_min Open:  446.59 High:  446.59 Low:  446.59 Close:  446.59 Volume:  0.0 Minute 4
    

    Unfortunately, it turns out that the buy order is not submitted, accepted nor completed within the Binance exchange.

    What am I doing wrong here?
    All hints are highly appreciated!