Example #1
0
    def on_update(self, bin_size, strategy):
        """
        Register the strategy function
        bind functions with webosocket data streams        
        :param strategy: strategy
        """       
        logger.info(f"pair: {self.pair}")  
        logger.info(f"timeframes: {bin_size}")  
        self.bin_size = bin_size
        self.strategy = strategy       

        if self.is_running:
            self.ws = BitMexWs(account=self.account, pair=self.pair, test=self.demo)
            
            #if len(self.bin_size) > 1:   
                #self.minute_granularity=True  

            #if self.minute_granularity==True and '1m' not in self.bin_size:
                #self.bin_size.append('1m')      

            #self.ws.bind('1m' if self.minute_granularity else allowed_range[bin_size[0]][0] \
                        #, self.__update_ohlcv)     

            if len(self.bin_size) > 0: 
                for t in self.bin_size:                                        
                    self.ws.bind(allowed_range_minute_granularity[t][0] if self.minute_granularity else allowed_range[t][0] \
                        , self.__update_ohlcv)                              
            self.ws.bind('instrument', self.__on_update_instrument)
            self.ws.bind('wallet', self.__on_update_wallet)
            self.ws.bind('position', self.__on_update_position)
            self.ws.bind('order', self.__on_update_order)
            self.ws.bind('margin', self.__on_update_margin)
            self.ob = OrderBook(self.ws)
Example #2
0
    def test_subscribe_margin(self):
        ws = BitMexWs(account=self.account, pair=self.pair)

        def subscribe(x):
            print(x)
            self.complete()

        ws.bind('margin', subscribe)

        self.wait_complete()
        ws.close()
Example #3
0
 def on_update(self, bin_size, strategy):
     """
     戦略の関数を登録する。
     :param strategy:
     """
     self.bin_size = bin_size
     self.strategy = strategy
     if self.is_running:
         self.ws = BitMexWs(test=self.demo)
         self.ws.bind(allowed_range[bin_size][0], self.__update_ohlcv)
         self.ws.bind('instrument', self.__on_update_instrument)
         self.ws.bind('wallet', self.__on_update_wallet)
         self.ws.bind('position', self.__on_update_position)
         self.ws.bind('margin', self.__on_update_margin)
         self.ob = OrderBook(self.ws)
Example #4
0
 def test_setup(self):
     ws = BitMexWs(account=self.account, pair=self.pair)
     ws.close()
Example #5
0
            if action == "partial" or \
                    action == "insert":
                orders[ordId] = v
            elif action == "update" and ordId in orders:
                orders[ordId]["size"] = v['size']
            elif action == "delete" and ordId in orders:
                del orders[ordId]

        bid_prices = sorted([v['price'] for v in self.asks.values()])
        ask_prices = sorted([v['price'] for v in self.bids.values()])

        if len(ask_prices) > 0:
            self.ask_max_price = ask_prices[-1]
        if len(bid_prices) > 0:
            self.bid_min_price = bid_prices[0]
        if len(ask_prices) > 0:
            self.best_bid_price = bid_prices[-1]
        if len(ask_prices) > 0:
            self.best_ask_price = ask_prices[0]

    def get_prices(self):
        return self.best_bid_price, self.best_ask_price


if __name__ == '__main__':
    ws = BitMexWs(account=BitMexWs.account, pair=BitMexWs.pair)
    ob = OrderBook(ws)
    while True:
        sys.stdout.write(f"\r{ob.get_prices()}")
        sys.stdout.flush()
Example #6
0
        for v in values:
            ordId = v['id']
            side = v['side']
            orders = self.asks if side == "Buy" else self.bids
            if action == "partial" or \
                    action == "insert":
                orders[ordId] = v
            elif action == "update" and ordId in orders:
                orders[ordId]["size"] = v['size']
            elif action == "delete" and ordId in orders:
                del orders[ordId]

        ask_prices = sorted([v['price'] for v in self.asks.values()])
        bid_prices = sorted([v['price'] for v in self.bids.values()])

        if len(ask_prices) > 0:
            self.ask_max_price = ask_prices[-1]
        if len(bid_prices) > 0:
            self.bid_min_price = bid_prices[0]

    def get_prices(self):
        return self.bid_min_price, self.ask_max_price


if __name__ == '__main__':
    ws = BitMexWs()
    ob = OrderBook(ws)
    while True:
        sys.stdout.write(f"\r{ob.get_prices()}")
        sys.stdout.flush()