def place_market_buy_order(self, amount, price, return_none=False, *args, **kwargs): """ Places a buy order in the market :param float | amount: Order amount in QUOTE :param float | price: Order price in BASE :param bool | return_none: :param args: :param kwargs: :return: """ symbol = self.market['base']['symbol'] precision = self.market['base']['precision'] base_amount = truncate(price * amount, precision) # Don't try to place an order of size 0 if not base_amount: self.log.critical('Trying to buy 0') self.disabled = True return None # Make sure we have enough balance for the order if self.returnOrderId and self.balance( self.market['base']) < base_amount: self.log.critical("Insufficient buy balance, needed {} {}".format( base_amount, symbol)) self.disabled = True return None self.log.info('Placing a buy order for {} {} @ {:.8f}'.format( base_amount, symbol, price)) # Place the order buy_transaction = self.retry_action(self.market.buy, price, Amount(amount=amount, asset=self.market["quote"]), account=self.account.name, expiration=self.expiration, returnOrderId=self.returnOrderId, fee_asset=self.fee_asset['id'], *args, **kwargs) self.log.debug('Placed buy order {}'.format(buy_transaction)) if self.returnOrderId: buy_order = self.get_order(buy_transaction['orderid'], return_none=return_none) if buy_order and buy_order['deleted']: # The API doesn't return data on orders that don't exist # We need to calculate the data on our own buy_order = self.calculate_order_data(buy_order, amount, price) self.recheck_orders = True return buy_order else: return True
def place_market_sell_order(self, amount, price, return_none=False, invert=False, *args, **kwargs): """ Places a sell order in the market. :param float | amount: Order amount in QUOTE :param float | price: Order price in BASE :param bool | return_none: :param bool | invert: True = return inverted sell order :param args: :param kwargs: :return: """ symbol = self._market['quote']['symbol'] precision = self._market['quote']['precision'] quote_amount = truncate(amount, precision) return_order_id = kwargs.pop('returnOrderId', self.returnOrderId) # Don't try to place an order of size 0 if not quote_amount: self.log.critical('Trying to sell 0') self.disabled = True return None # Make sure we have enough balance for the order if return_order_id and self.balance(self._market['quote']) < quote_amount: self.log.critical("Insufficient sell balance, needed {} {}".format(amount, symbol)) self.disabled = True return None self.log.info( 'Placing a sell order with {:.{prec}f} {} @ {:.8f}'.format(quote_amount, symbol, price, prec=precision) ) # Place the order sell_transaction = self.retry_action( self._market.sell, price, Amount(amount=amount, asset=self._market["quote"]), account=self._account.name, expiration=self.expiration, returnOrderId=return_order_id, fee_asset=self.fee_asset['id'], *args, **kwargs ) self.log.debug('Placed sell order {}'.format(sell_transaction)) if return_order_id: sell_order = self.get_order(sell_transaction['orderid'], return_none=return_none) if sell_order and sell_order['deleted']: # The API doesn't return data on orders that don't exist, we need to calculate the data on our own sell_order = self.calculate_order_data('sell', sell_order, amount, price) sell_order['id'] = sell_transaction['orderid'] self.recheck_orders = True if sell_order and invert: sell_order.invert() return sell_order else: return True
def convert_asset(from_value, from_asset, to_asset): """ Converts asset to another based on the latest market value :param float | from_value: Amount of the input asset :param string | from_asset: Symbol of the input asset :param string | to_asset: Symbol of the output asset :return: float Asset converted to another asset as float value """ market = Market('{}/{}'.format(from_asset, to_asset)) ticker = market.ticker() latest_price = ticker.get('latest', {}).get('price', None) precision = market['base']['precision'] return truncate((from_value * latest_price), precision)