def CancelOrderFull(self, order_id): try: order_deals = self.GetOrderDeals(order_id) type = order_deals["type"] cur1 = order_deals["in_currency"] cur2 = order_deals["out_currency"] pair = self._ToPair(cur1, cur2) if type == "buy" else self._ToPair(cur2, cur1) for trade in order_deals["trades"]: if type == "buy": if self.GetCurrentSellRate(pair) > float(trade["price"]): sell_quantity = float(trade["quantity"]) sell_quantity -= sell_quantity - float(trade["commission_amount"]) self.CreateOrder_SellMarket(pair, sell_quantity) else: error = c_errors.CannotCreateBuyOrder() error.SetDescription("current sell-rate is less than buy-rate for order-deal") raise error elif type == "sell": if self.GetCurrentBuyRate(pair) < float(trade["price"]): buy_cost = float(trade["amount"]) buy_cost -= float(trade["commission_amount"]) self.CreateOrder_BuyMarketTotal(pair, buy_cost) else: error = c_errors.CannotCreateSellOrder() error.SetDescription("current buy-rate is more than sell-rate for order-deal") raise error except c_errors.OrderIsNotFoundByID: pass return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "order_cancel", order_id=order_id))
def Query(cls, api_method, publick_key="STUMP", secret_key=bytes("STUMP", encoding="utf-8"), **params): connection.Wait.WaitingGlobal(1) def ComputeHash(data): """ computes hash-value from target https-request """ hash = hmac.new(key=secret_key, digestmod=hashlib.sha512) hash.update(data.encode('utf-8')) return hash.hexdigest() params["nonce"] = int(round(time.time() * 1000)) params = urllib.parse.urlencode(params) headers = { # type of data for sign "Content-type": "application/x-www-form-urlencoded", # publick-key for check data sign "Key": publick_key, # signed data for request "Sign": ComputeHash(params) # подписанные данные } conn = http.client.HTTPSConnection(cls.url) common.ExecuteOrRepeat(common.Lambda(conn.request, "POST", "/" + cls.api_version + "/" + api_method, params, headers)) response = conn.getresponse().read() conn.close() result = json.loads(response.decode('utf-8')) if "error" in result and result["error"]: error_message = result["error"] error = None if "Error 50304" in error_message: error = c_errors.OrderIsNotFoundByID() elif "Error 50052" in error_message: error = c_errors.InsufficientFundsForOrder() elif "Error 40016" in error_message: error = c_errors.MaintenanceWorkInProgres() # NOTE: in this case it is reasonable to wait at least one minute time.sleep(60) else: error = c_errors.QueryError() error.SetDescription(error_message) raise error return result
def GetUserDeals(self, pair, limit=100, offset=0): return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "user_trades", pair=pair, limit=limit, offset=offset))
def GetUserCancelledOrders(self, limit=100, offset=0): return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "user_cancelled_orders", limit=limit, offset=offset))
def GetUserOpenOrders(self): return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "user_open_orders"))
def GetUserInfo(self): return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "user_info"))
def _CreateOrder(self, **order_parameters): return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "order_create", **order_parameters))
def GetCurrencyList(cls): return common.ExecuteOrRepeat(common.Lambda(cls.Query, "currency"))
def GetPairSettings(cls): return common.ExecuteOrRepeat(common.Lambda(cls.Query, "pair_settings"))
def GetTicker(cls): return common.ExecuteOrRepeat(common.Lambda(cls.Query, "ticker"))
def GetOrderBook(cls, pair, limit=100): return common.ExecuteOrRepeat(common.Lambda(cls.Query, "order_book", pair=pair, limit=limit))
def GetTrades(cls, pair): return common.ExecuteOrRepeat(common.Lambda(cls.Query, "trades", pair=pair))
def CancelOrder(self, order_id): return common.ExecuteOrRepeat(common.Lambda(self._QueryPrivate, "order_cancel", order_id=order_id))