Ejemplo n.º 1
0
 def __init__(self, currency):
     self.name = self.__class__.__name__
     self.currency = currency
     self.depth_updated = 0
     self.update_rate = 1
     self.fc = FiatConverter()
     self.fc.update()
Ejemplo n.º 2
0
class Market(object):
    def __init__(self, currency):
        self.name = self.__class__.__name__
        self.currency = currency
        self.pair = config.pair
        pair_names = str.split(self.pair, "_")
        self.pair1_name = str.upper(pair_names[0])
        self.pair2_name = str.upper(pair_names[1])
        self.depth_updated = 0
        self.update_rate = 60
        self.fc = FiatConverter()
        self.fc.update()

    def get_depth(self):
        timediff = time.time() - self.depth_updated
        if timediff > self.update_rate:
            self.ask_update_depth()
        timediff = time.time() - self.depth_updated
        if timediff > config.market_expiration_time:
            logging.warn('Market: %s order book is expired' % self.name)
            self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
                {'price': 0, 'amount': 0}]}
        return self.depth

    def convert_to_usd(self):
        if self.currency == "USD":
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency, "USD")

    def ask_update_depth(self):
        try:
            self.update_depth()
            self.convert_to_usd()
            self.depth_updated = time.time()
        except (urllib.error.HTTPError, urllib.error.URLError) as e:
            logging.error("HTTPError, can't update market: %s code=%d reason=%s headers=%s" % (self.name, e.code, e.reason, e.headers))
            log_exception(logging.DEBUG)
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))
            log_exception(logging.DEBUG)

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0],
                   'bid': depth['bids'][0]}
        return res

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass
Ejemplo n.º 3
0
 def __init__(self):
     self.name = self.__class__.__name__
     self.depth_updated = 0
     self.update_rate = 1
     self.fc = FiatConverter()
     self.fc.update()
     self.is_terminated = False
Ejemplo n.º 4
0
    def __init__(self):
        self.name = self.__class__.__name__
        self.btc_balance = 0.
        self.eur_balance = 0.
        self.usd_balance = 0.
        self.cny_balance = 0.

        self.fc = FiatConverter()
Ejemplo n.º 5
0
class Market(object):
    def __init__(self, currency):
        self.name = self.__class__.__name__
        self.currency = currency
        self.depth_updated = 0
        self.update_rate = 60
        self.trade_fee = 0
        self.fc = FiatConverter()
        self.fc.update()

    def get_depth(self):
        timediff = time.time() - self.depth_updated
        if timediff > self.update_rate:
            self.ask_update_depth()
        timediff = time.time() - self.depth_updated
        if timediff > config.market_expiration_time:
            logging.warning('Market: %s order book is expired' % self.name)
            self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [{'price': 0, 'amount': 0}]}
        if self.depth['bids'][0]['price'] > self.depth['asks'][0]['price']:
            logging.warning('Market: %s order book is invalid (bid>ask : quotation is stopped ?)' % self.name)
            self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [{'price': 0, 'amount': 0}]}
        return self.depth

    def convert_to_usd(self):
        if self.currency == "USD":
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency, "USD")

    def ask_update_depth(self):
        try:
            self.update_depth()
            self.convert_to_usd()
            self.depth_updated = time.time()
        except (urllib.error.HTTPError, urllib.error.URLError) as e:
            logging.error("HTTPError, can't update market: %s" % self.name)
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0],
                   'bid': depth['bids'][0]}
        return res

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass
Ejemplo n.º 6
0
 def __init__(self, exchange):
     self.name = exchange.id
     self.exchange = exchange
     # self.pair_code = pair_code
     self.depth_updated = 0
     self.update_rate = 1
     self.fc = FiatConverter()
     self.fc.update()
     self.is_terminated = False
     self.request_timeout = 5 #5s
Ejemplo n.º 7
0
 def __init__(self):
     self.name = self.__class__.__name__
     self.eur_bal2ance = 0.
     self.usd_balance = 0.
     self.btc_balance = 0.
     self.btc_frozen = 0.
     self.cny_balance = 0.
     self.cny_frozen = 0.
     self.fc = FiatConverter()
     self.market = None
Ejemplo n.º 8
0
 def __init__(self, currency):
     # print("I HAVE INSTANTIATED MARKET.PY, self:", self.depth, "; CURRENCY: ", currency)
     self.name = self.__class__.__name__
     self.currency = currency
     self.depth_updated = 0
     self.update_rate = 60
     # self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
     #     {'price': 0, 'amount': 0}]}
     self.fc = FiatConverter()
     self.fc.update()
     print("Market.py, __init completed")
Ejemplo n.º 9
0
    def __init__(self, base_currency, market_currency, pair_code):
        self.name = self.__class__.__name__
        self.base_currency = base_currency
        self.market_currency = market_currency
        self.pair_code = pair_code

        self.depth_updated = 0
        self.update_rate = 1
        self.fc = FiatConverter()
        self.fc.update()
        self.is_terminated = False
        self.request_timeout = 5 #5s
Ejemplo n.º 10
0
class Market:
    def __init__(self):
        self.name = self.__class__.__name__
        self.btc_balance = 0.
        self.eur_balance = 0.
        self.usd_balance = 0.
        self.pair = config.pair
        pair_names = str.split(self.pair, "_")
        self.pair1_name = str.upper(pair_names[0])
        self.pair2_name = str.upper(pair_names[1])
        self.pair1_balance = 0.
        self.pair2_balance = 0.
        self.fc = FiatConverter()

    def __str__(self):
        return "%s: %s" % (self.name, str({"btc_balance": self.btc_balance,
                                           "eur_balance": self.eur_balance,
                                           "usd_balance": self.usd_balance,
                                           "pair1_balance": self.pair1_balance,
                                           "pair2_balance": self.pair2_balance}))

    def buy(self, amount, price):
        """Orders are always priced in USD"""
        local_currency_price = self.fc.convert(price, "USD", self.currency)
        logging.info("Buy %f BTC at %f %s (%f USD) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))
        self._buy(amount, local_currency_price)

    def sell(self, amount, price):
        """Orders are always priced in USD"""
        local_currency_price = self.fc.convert(price, "USD", self.currency)
        logging.info("Sell %f BTC at %f %s (%f USD) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))
        self._sell(amount, local_currency_price)

    def _buy(self, amount, price):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def _sell(self, amount, price):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def deposit(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def withdraw(self, amount, address):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def get_info(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)
Ejemplo n.º 11
0
 def __init__(self, currency):
     self.name = self.__class__.__name__
     self.currency = currency
     self.depth_updated = 0
     self.update_rate = 60
     self.fc = FiatConverter()
     self.fc.update()
Ejemplo n.º 12
0
 def __init__(self, exchanges):
     self.__name__ = 'TraderBot'
     # may not need exchanges
     if type(exchanges) == str:
         exchanges = ast.literal_eval(exchanges)
     # self.clients = exchanges
     self.exchanges = exchanges
     # print("observer, traderbot, TraderBot self.exchanges (the exchnages) : ", self.exchanges)
     self.fc = FiatConverter()
     self.trade_wait = 20  # in seconds
     self.last_trade = 0
     self.potential_trades = []
     self.current_pair = ''
     self.ask_market = []
     self.bid_market = []
     self.base_coin = {}
     self.quote_coin = {}
     self.volume = 0
     self.deposit_address = {}
     self.sellprice = 0
     self.buyprice = 0
     self.trade_fee = 0
     self.withdraw_fee = 0
     self.pre_trade_base_balance = 0
     self.pre_trade_quote_balance = 0
     self.pre_withdraw_bid_balance = 0
     self.base_coin_to_sell = 0
     self.min_tx_volume = 0
     self.max_tx_volume = 0
Ejemplo n.º 13
0
 def __init__(self):
     self.name = self.__class__.__name__
     self.btc_balance = 0.
     self.eur_balance = 0.
     self.usd_balance = 0.
     self.gbp_balance = 0.
     self.fc = FiatConverter()
Ejemplo n.º 14
0
 def __init__(self, currency):
     self.name = self.__class__.__name__
     self.currency = currency
     self.depth_updated = 0
     self.update_rate = 60
     self.fc = FiatConverter()
     self.fc.update()
     self.converted_depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
         {'price': 0, 'amount': 0}]}
Ejemplo n.º 15
0
 def __init__(self):
     self.clients = {
         # TODO: move that to the config file
         # "BitstampUSD": bitstampusd.PrivateBitstampUSD(),
     }
     self.fc = FiatConverter()
     self.trade_wait = 120  # in seconds
     self.last_trade = 0
     self.potential_trades = []
Ejemplo n.º 16
0
 def __init__(self):
     self.name = self.__class__.__name__
     self.btc_balance = 0.
     self.eur_balance = 0.
     self.usd_balance = 0.
     self.cny_balance = 0.
     self.btc_frozen = 0.
     self.cny_frozen = 0.
     self.fc = FiatConverter()
     self.market = None
Ejemplo n.º 17
0
 def __init__(self):
     self.clients = {
         "MtGoxEUR": mtgoxeur.PrivateMtGoxEUR(),
         "MtGoxUSD": mtgoxusd.PrivateMtGoxUSD(),
         "BitstampUSD": bitstampusd.PrivateBitstampUSD(),
     }
     self.fc = FiatConverter()
     self.trade_wait = 120  # in seconds
     self.last_trade = 0
     self.potential_trades = []
Ejemplo n.º 18
0
class Market:
    def __init__(self):
        self.name = self.__class__.__name__
        self.btc_balance = 0.
        self.eur_balance = 0.
        self.usd_balance = 0.
        self.fc = FiatConverter()

    def __str__(self):
        return "%s: %s" % (self.name, str({"btc_balance": self.btc_balance,
                                           "eur_balance": self.eur_balance,
                                           "usd_balance": self.usd_balance}))

    def buy(self, amount, price):
        """Orders are always priced in USD"""
        local_currency_price = self.fc.convert(price, "USD", self.currency)
        logging.info("Buy %f BTC at %f %s (%f USD) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))
        self._buy(amount, local_currency_price)


    def sell(self, amount, price):
        """Orders are always priced in USD"""
        local_currency_price = self.fc.convert(price, "USD", self.currency)
        logging.info("Sell %f BTC at %f %s (%f USD) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))
        self._sell(amount, local_currency_price)

    def _buy(self, amount, price):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def _sell(self, amount, price):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def deposit(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def withdraw(self, amount, address):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def get_info(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)
Ejemplo n.º 19
0
 def __init__(self, currency):
     self.name = self.__class__.__name__
     self.currency = currency
     self.pair = config.pair
     pair_names = str.split(self.pair, "_")
     self.pair1_name = str.upper(pair_names[0])
     self.pair2_name = str.upper(pair_names[1])
     self.depth_updated = 0
     self.update_rate = 60
     self.fc = FiatConverter()
     self.fc.update()
Ejemplo n.º 20
0
 def __init__(self):
     self.name = self.__class__.__name__
     self.btc_balance = 0.
     self.eur_balance = 0.
     self.usd_balance = 0.
     self.pair = config.pair
     pair_names = str.split(self.pair, "_")
     self.pair1_name = str.upper(pair_names[0])
     self.pair2_name = str.upper(pair_names[1])
     self.pair1_balance = 0.
     self.pair2_balance = 0.
     self.fc = FiatConverter()
Ejemplo n.º 21
0
class Market:
    def __init__(self):
        self.name = self.__class__.__name__
        self.btc_balance = 0.
        self.eur_balance = 0.
        self.usd_balance = 0.
        self.cny_balance = 0.
        self.btc_frozen = 0.
        self.cny_frozen = 0.
        self.fc = FiatConverter()

    def __str__(self):
        return "%s: %s" % (self.name, str({"btc_balance": self.btc_balance,
                                           "cny_balance": self.cny_balance}))

    def buy(self, amount, price):
        """Orders are always priced in CNY"""
        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        logging.verbose("Buy %f BTC at %f %s (%f CNY) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))
        return self._buy(amount, local_currency_price)


    def sell(self, amount, price):
        """Orders are always priced in CNY"""
        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        logging.verbose("Sell %f BTC at %f %s (%f CNY) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))
        return self._sell(amount, local_currency_price)

    def buy_maker(self, amount, price):
        """Orders are always priced in CNY"""

        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        local_currency_price = int(local_currency_price)
        logging.verbose("Buy maker %f BTC at %d %s (%d CNY) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))

        return self._buy_maker(amount, local_currency_price)


    def sell_maker(self, amount, price):
        """Orders are always priced in CNY"""
        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        local_currency_price = int(local_currency_price)

        logging.verbose("Sell maker %f BTC at %d %s (%d CNY) @%s" % (amount,
                     local_currency_price, self.currency, price, self.name))

        return self._sell_maker(amount, local_currency_price)

    def get_order(self, order_id):
        return self._get_order(order_id)

    def cancel_order(self, order_id):
        return self._cancel_order(order_id)

    def cancel_all(self):
        return self._cancel_all()


    def _buy(self, amount, price):
        raise NotImplementedError("%s.buy(self, amount, price)" % self.name)

    def _sell(self, amount, price):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def _buy_maker(self, amount, price):
        raise NotImplementedError("%s.buy_maker(self, amount, price)" % self.name)

    def _sell_maker(self, amount, price):
        raise NotImplementedError("%s.sell_maker(self, amount, price)" % self.name)


    def _get_order(self, order_id):
        raise NotImplementedError("%s.get_order(self, order_id)" % self.name)

    def _cancel_order(self, order_id):
        raise NotImplementedError("%s.cancel_order(self, order_id)" % self.name)

    def _cancel_all(self):
        raise NotImplementedError("%s.cancel_all(self)" % self.name)

    def deposit(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def withdraw(self, amount, address):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def get_info(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)
Ejemplo n.º 22
0
class Market(object):
    registered_market = {}

    def __init__(self):
        self.name = self.__class__.__name__
        self.depth_updated = 0
        self.update_rate = 1
        self.fc = FiatConverter()
        self.fc.update()
        self.is_terminated = False

    def terminate(self):
        self.is_terminated = True  #seems not used anywhere

    def get_depth(self):
        timediff = time.time() - self.depth_updated
        if timediff > self.update_rate:
            self.ask_update_depth()

        #order book is in valide if it updated time long time ago.
        timediff = time.time() - self.depth_updated
        if timediff > config.market_expiration_time:
            logging.warn('Market: %s order book is expired' % self.name)
            self.depth = {
                'asks': [{
                    'price': 0,
                    'amount': 0
                }],
                'bids': [{
                    'price': 0,
                    'amount': 0
                }]
            }
        return self.depth

    def gen_id(self, lu, currency_pair):
        return hex(int(lu.timestamp() * 1e6)) + currency_pair

    def convert_to_cny(self):
        if self.currency == "CNY":
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency,
                                                 "CNY")

    def start_websocket_depth(self):
        if config.SUPPORT_WEBSOCKET:
            t = threading.Thread(target=self.websocket_depth)
            t.start()

    def websocket_depth(self):  #??
        import json
        from socketIO_client import SocketIO

        def on_message(data):
            data = data.decode('utf8')
            if data[0] != '2':
                return

            data = json.loads(data[1:])
            depth = data[1]

            logging.debug("depth coming: %s", depth['market'])
            self.depth_updated = int(depth['timestamp'] / 1000)
            self.depth = self.format_depth(depth)

        def on_connect():
            logging.info('[Connected]')

            socketIO.emit('land', {
                'app': 'haobtcnotify',
                'events': [self.event]
            })

        with SocketIO(config.WEBSOCKET_HOST,
                      port=config.WEBSOCKET_PORT) as socketIO:

            socketIO.on('connect', on_connect)
            socketIO.on('message', on_message)

            socketIO.wait()

    def ask_update_depth(self):
        try:
            self.update_depth()
            # self.convert_to_usd()
            self.depth_updated = time.time()
        except (urllib.error.HTTPError, urllib.error.URLError) as e:
            logging.error("HTTPError, can't update market: %s" % self.name)
            traceback.print_exc()

            log_exception(logging.DEBUG)
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))
            log_exception(logging.DEBUG)
            traceback.print_exc()

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {
                'date': depth['date'].strftime('%Y.%m.%d'),
                'lu': depth['lu'],
                'ask': depth['asks'][0],
                'bid': depth['bids'][0]
            }
        return res

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass

    def sort_and_format(self, l, reverse=False):
        l.sort(key=lambda x: float(x[0]), reverse=reverse)
        r = []
        for i in l:
            r.append({'price': float(i[0]), 'amount': float(i[1])})
        return r

    def format_depth(self, depth):
        bids = self.sort_and_format(depth['bids'], True)
        asks = self.sort_and_format(depth['asks'], False)
        return {'asks': asks, 'bids': bids}

    @staticmethod
    def get_market(name):
        return Market.registered_market[name]()

    @staticmethod
    def get_market_list():
        return Market.registered_market.keys()

    @staticmethod
    def register_market(class_name):
        name = class_name.__name__
        createfn = class_name.create
        if name in Market.registered_market:
            logging.info('conflicted market name for ' + name +
                         ' will be skipped')
        else:
            Market.registered_market[name] = createfn
            logging.info(name + ' is registered')
Ejemplo n.º 23
0
class Market:
    def __init__(self):
        self.name = self.__class__.__name__
        self.btc_balance = 0.
        self.eur_balance = 0.
        self.usd_balance = 0.
        self.cny_balance = 0.
        self.btc_frozen = 0.
        self.cny_frozen = 0.
        self.fc = FiatConverter()

    def __str__(self):
        return "%s: %s" % (self.name,
                           str({
                               "btc_balance": self.btc_balance,
                               "cny_balance": self.cny_balance
                           }))

    def buy(self, amount, price):
        """Orders are always priced in CNY"""
        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        logging.verbose(
            "Buy %f BTC at %f %s (%f CNY) @%s" %
            (amount, local_currency_price, self.currency, price, self.name))
        return self._buy(amount, local_currency_price)

    def sell(self, amount, price):
        """Orders are always priced in CNY"""
        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        logging.verbose(
            "Sell %f BTC at %f %s (%f CNY) @%s" %
            (amount, local_currency_price, self.currency, price, self.name))
        return self._sell(amount, local_currency_price)

    def buy_maker(self, amount, price):
        """Orders are always priced in CNY"""

        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        local_currency_price = int(local_currency_price)
        logging.verbose(
            "Buy maker %f BTC at %d %s (%d CNY) @%s" %
            (amount, local_currency_price, self.currency, price, self.name))

        return self._buy_maker(amount, local_currency_price)

    def sell_maker(self, amount, price):
        """Orders are always priced in CNY"""
        local_currency_price = self.fc.convert(price, "CNY", self.currency)
        local_currency_price = int(local_currency_price)

        logging.verbose(
            "Sell maker %f BTC at %d %s (%d CNY) @%s" %
            (amount, local_currency_price, self.currency, price, self.name))

        return self._sell_maker(amount, local_currency_price)

    def get_order(self, order_id):
        return self._get_order(order_id)

    def cancel_order(self, order_id):
        return self._cancel_order(order_id)

    def cancel_all(self):
        return self._cancel_all()

    def _buy(self, amount, price):
        raise NotImplementedError("%s.buy(self, amount, price)" % self.name)

    def _sell(self, amount, price):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def _buy_maker(self, amount, price):
        raise NotImplementedError("%s.buy_maker(self, amount, price)" %
                                  self.name)

    def _sell_maker(self, amount, price):
        raise NotImplementedError("%s.sell_maker(self, amount, price)" %
                                  self.name)

    def _get_order(self, order_id):
        raise NotImplementedError("%s.get_order(self, order_id)" % self.name)

    def _cancel_order(self, order_id):
        raise NotImplementedError("%s.cancel_order(self, order_id)" %
                                  self.name)

    def _cancel_all(self):
        raise NotImplementedError("%s.cancel_all(self)" % self.name)

    def deposit(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def withdraw(self, amount, address):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)

    def get_info(self):
        raise NotImplementedError("%s.sell(self, amount, price)" % self.name)
Ejemplo n.º 24
0
class Market(object):
    def __init__(self, base_currency, market_currency, pair_code):
        self.name = self.__class__.__name__
        self.base_currency = base_currency
        self.market_currency = market_currency
        self.pair_code = pair_code

        self.depth_updated = 0
        self.update_rate = 1
        self.fc = FiatConverter()
        self.fc.update()
        self.is_terminated = False
        self.request_timeout = 5 #5s

    def terminate(self):
        self.is_terminated = True

    def get_depth(self):
        timediff = time.time() - self.depth_updated
        # logging.warn('Market: %s order book1:(%s>%s)', self.name, timediff, self.depth_updated)
        if timediff > self.update_rate:
            # print('should update...')
            self.ask_update_depth()
        

        timediff = time.time() - self.depth_updated
        # logging.warn('Market: %s order book2:(%s>%s)', self.name, timediff, self.depth_updated)

        if timediff > config.market_expiration_time:
            # logging.warn('Market: %s order book is expired(%s>%s)', self.name, timediff, config.market_expiration_time)
            self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
                {'price': 0, 'amount': 0}]}
        return self.depth

    def convert_to_cny(self):
        if self.currency == "CNY":
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency, "CNY")

    def subscribe_depth(self):
        if config.SUPPORT_ZMQ:
            t = threading.Thread(target = self.subscribe_zmq_depth)
            t.start()  
        elif config.SUPPORT_WEBSOCKET:
            t = threading.Thread(target = self.subscribe_websocket_depth)
            t.start()
        else:
            pass

    def subscribe_zmq_depth(self):
        import lib.push as push

        push_s = push.Push(config.ZMQ_PORT)
        push_s.msg_server()

    def subscribe_websocket_depth(self):
        import json
        from socketIO_client import SocketIO

        def on_message(data):
            data = data.decode('utf8')
            if data[0] != '2':
                return

            data = json.loads(data[1:])
            depth = data[1]

            logging.debug("depth coming: %s", depth['market'])
            self.depth_updated = int(depth['timestamp']/1000)
            self.depth = self.format_depth(depth)
        
        def on_connect():
            logging.info('[Connected]')

            socketIO.emit('land', {'app': 'haobtcnotify', 'events':[self.event]});

        with SocketIO(config.WEBSOCKET_HOST, port=config.WEBSOCKET_PORT) as socketIO:

            socketIO.on('connect', on_connect)
            socketIO.on('message', on_message)

            socketIO.wait()
    
    def ask_update_depth(self):
        try:
            self.update_depth()
            # self.convert_to_usd()
            self.depth_updated = time.time()
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))
            log_exception(logging.DEBUG)
            # traceback.print_exc()

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0],
                   'bid': depth['bids'][0]}
        return res

    def sort_and_format(self, l, reverse=False):
        l.sort(key=lambda x: float(x[0]), reverse=reverse)
        r = []
        for i in l:
            r.append({'price': float(i[0]), 'amount': float(i[1])})
        return r

    def format_depth(self, depth):
        bids = self.sort_and_format(depth['bids'], True)
        asks = self.sort_and_format(depth['asks'], False)
        return {'asks': asks, 'bids': bids}

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass
Ejemplo n.º 25
0
class Market(object):
    def __init__(self, currency):
        # print("I HAVE INSTANTIATED MARKET.PY, self:", self.depth, "; CURRENCY: ", currency)
        self.name = self.__class__.__name__
        self.currency = currency
        self.depth_updated = 0
        self.update_rate = 60
        # self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
        #     {'price': 0, 'amount': 0}]}
        self.fc = FiatConverter()
        self.fc.update()
        print("Market.py, __init completed")

    # def get_depth(self):
    #     timediff = time.time() - self.depth_updated
    #     if timediff > self.update_rate:
    #         print("3.1 market.py, get_depth, timediff: {}, self.update_rate: {};  ".format(timediff, self.update_rate))
    #         self.ask_update_depth()
    #     timediff = time.time() - self.depth_updated
    #     print("!!!!!! market.py, get_depth, time.time(): {}; self.depth.updated(): {}, config.market_expiration_time: {}".format(time.time(),
    #                                                                                             self.depth_updated, config.market_expiration_time))
    #     if timediff > config.market_expiration_time:
    #         print("3.2 market.py, get_depth, timediff > config.market_expiration_time")
    #         logging.warning('Market: %s order book is expired' % self.name)
    #         # TODO: Issues is with setting self.depth here - self.depth should be set in __init__
    #         self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
    #             {'price': 0, 'amount': 0}]}
    #         print("3.3 market.py, get_depth, timediff > config.market_expiration_time; self.depth: ", self.depth)
    #     return self.depth

    def convert_to_usd(self):
        # print("arbitrage, public_markets, market.py, convert_to_usd()")
        if self.currency == "USD":
            print(
                "arbitrage, public_markets, convert_to_usd, self.currency = USD"
            )
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                # print("arbitrage, public_markets, market.py, convert_to_usd(), for loop nested")
                order["price"] = self.fc.convert(order["price"], self.currency,
                                                 "USD")

    # def ask_update_depth(self):
    #     print("4.0 arbitrage, public_markets, market.py, ask_update_depth()")
    #     try:
    #         # TODO: Market' object has no attribute 'depth' (occurs here)
    #         import pdb; pdb.set_trace()
    #         print("market.py, ask_update_depth, after 1st pdb")
    #         self.update_depth()
    #         print("market.py, ask_update_depth, self.update_depth just finished")
    #         self.convert_to_usd()
    #         self.depth_updated = time.time()
    #     except (urllib.error.HTTPError, urllib.error.URLError) as e:
    #         logging.error("4.1 arbitrage, public_markets, market.py, ask_update_depth HTTPError, can't update market: %s" % self.name)
    #         log_exception(logging.DEBUG)
    #     except Exception as e:
    #         logging.error("4.2 arbitrage, public_markets, market.py, ask_update_depth(); Can't update market: %s - %s" % (self.name, str(e)))
    #         log_exception(logging.DEBUG)

    def get_ticker(self):
        # print("!!!!!!!!!!!!!!3.0 market.py, get_ticker")
        depth = self.get_depth()
        # print("3.0 market.py, get_ticker, depth: ", depth)
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0], 'bid': depth['bids'][0]}
        return res

    ## Abstract methods
    def update_depth(self):
        # import pdb; pdb.set_trace()
        # print("arbitrage, market.py, update_depth, passing (self): ", self)
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass
Ejemplo n.º 26
0
class Market(object):
    def __init__(self, currency):
        self.name = self.__class__.__name__
        self.currency = currency
        self.depth_updated = 0
        self.update_rate = 1
        self.fc = FiatConverter()
        self.fc.update()

    def get_depth(self):
        timediff = time.time() - self.depth_updated
        if timediff > self.update_rate:
            self.ask_update_depth()

        timediff = time.time() - self.depth_updated
        if timediff > config.market_expiration_time:
            logging.warn('Market: %s order book is expired' % self.name)
            self.depth = {
                'asks': [{
                    'price': 0,
                    'amount': 0
                }],
                'bids': [{
                    'price': 0,
                    'amount': 0
                }]
            }
        return self.depth

    def convert_to_cny(self):
        if self.currency == "CNY":
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency,
                                                 "CNY")

    def ask_update_depth(self):
        try:
            self.update_depth()
            # self.convert_to_usd()
            self.depth_updated = time.time()
        except (urllib.error.HTTPError, urllib.error.URLError) as e:
            logging.error("HTTPError, can't update market: %s" % self.name)
            log_exception(logging.DEBUG)
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))
            log_exception(logging.DEBUG)

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0], 'bid': depth['bids'][0]}
        return res

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass
Ejemplo n.º 27
0
class Market(object):
    def __init__(self, currency):
        self.name = self.__class__.__name__
        self.currency = currency
        self.depth_updated = 0
        self.update_rate = 60
        self.fc = FiatConverter()
        self.fc.update()
        self.converted_depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
            {'price': 0, 'amount': 0}]}

    def get_depth(self):
        # note that self.ask_update_depth converts the result to USD
        timediff = time.time() - self.depth_updated
        if timediff > self.update_rate:
            self.ask_update_depth()
        timediff = time.time() - self.depth_updated
        if timediff > config.market_expiration_time:
            logging.warn('Market: %s order book is expired' % self.name)
            self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
                {'price': 0, 'amount': 0}]}
            self.converted = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
                {'price': 0, 'amount': 0}]}
        return self.depth

    def get_converted_depth (self, currency="USD"):
        self.get_depth()
        self.convert_to_currency(currency)
        return self.converted_depth
    
    def convert_to_usd(self):
        if self.currency == "USD":
            return
        #dont get confused here, direction will take either "asks" or "bids"
        self.converted_depth = self.depth
        for direction in ("asks", "bids"):
            for order in self.converted_depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency, "USD")
                
    def convert_to_currency(self, currency="USD"):
        if self.currency == currency:
            return
        #dont get confused here, direction will take either "asks" or "bids"
        self.converted_depth = self.depth
        for direction in ("asks", "bids"):
            for order in self.converted_depth[direction]:
                order["price"] = self.fc.convert(order["price"], self.currency, currency)
                           
    def ask_update_depth(self):
        try:
            self.update_depth()
            #self.convert_to_usd()
            self.depth_updated = time.time()
        
        #update urllib in original code to urllib2
        except (urllib2.HTTPError, urllib2.URLError) as e:
            logging.error("HTTPError, can't update market: %s" % self.name)
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0],
                   'bid': depth['bids'][0]}
        return res
    
    def get_converted_ticker(self, currency = "USD"):
        depth = self.get_converted_depth(currency)
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0],
                   'bid': depth['bids'][0]}
        return res

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass