def get_order_book(self, contract): if contract not in self.markets: raise WebserverException("exceptions/webserver/no-such-ticker", contract) if contract not in self.books: log("Warning: %s not in books" % contract) # TODO: Get book from engine self.books[contract] = {'contract': contract, 'bids': [], 'asks': []} result = yield succeed(self.books[contract]) returnValue(result)
def get_trade_history(self, contract, start_timestamp=None, end_timestamp=None): if contract not in self.markets: raise WebserverException("exceptions/webserver/no-such-ticker", contract) now = util.dt_to_timestamp(datetime.utcnow()) start = start_timestamp or int(now - 3.6e9) # delta 1 hour end = end_timestamp or now history = yield succeed([entry for entry in self.trade_history.get(contract, []) \ if start <= entry["timestamp"] <= end]) returnValue(history)
def place_order(self, order, username=None): order["timestamp"] = util.dt_to_timestamp(datetime.datetime.utcnow()) order['username'] = username # Check for zero price or quantity if order["price"] == 0 or order["quantity"] == 0: raise WebserverException( "exceptions/webserver/invalid_price_quantity") # check tick size and lot size in the accountant, not here result = yield self.accountant.proxy.place_order(username, order) returnValue(result)
def request_withdrawal(self, contract, amount, address, totp=None, username=None): """ Makes a note in the database that a withdrawal needs to be processed :param contract: the currency to process the withdrawal in :param amount: the amount of money to withdraw :param address: the address to which the withdrawn money is to be sent :returns: bool, Deferred - if an invalid amount, just return False, otherwise return a deferred """ if amount <= 0: raise WebserverException( "exceptions/webserver/invalid-withdrawal-amount") totp_result = yield self.administrator.proxy.check_totp(username, totp) if totp_result: result = yield self.accountant.proxy.request_withdrawal( username, contract, amount, address) returnValue(result) else: raise WebserverException("exceptions/webserver/invalid_otp")
def get_ohlcv_history(self, contract, period=None, start_timestamp=None, end_timestamp=None): if contract not in self.markets: raise WebserverException("exceptions/webserver/no-such-ticker", contract) now = util.dt_to_timestamp(datetime.utcnow()) start = start_timestamp or int(now - 5.184e12) # delta 60 days end = end_timestamp or now period = period or "day" data = self.ohlcv_history.get(contract, {}).get(period, {}) ohlcv = yield succeed({key: value for key, value in data.iteritems() \ if value["open_timestamp"] <= end and \ start <= value["close_timestamp"]}) returnValue(ohlcv)
def get_new_api_credentials(self, expiration=None, totp=None, username=None): if expiration is None: now = datetime.utcnow() expiration = util.dt_to_timestamp(now + timedelta(days=7)) # Check totp totp_result = yield self.administrator.proxy.check_totp(username, totp) if totp_result: r = yield self.administrator.proxy.get_new_api_credentials( username, expiration) returnValue(r) else: raise WebserverException("exceptions/webserver/invalid_otp")