def sell(self, symbol_name, **kwargs): self.trade_lock(symbol_name, self.strategy) try: assert kwargs['amount'] > kwargs['dollar'] # 获取交易数目精度 precision = self.precision_collection.find_one( {"symbol": symbol_name + "usdt"}, {"amount_precision": 1}) amount_precision = float(precision['amount_precision']) # 计算欲卖出的数目 sell_currency = retain_decimals( (float(kwargs['amount']) - float(kwargs['dollar'])) / 2 / float(kwargs['price']), amount_precision) if float(sell_currency) > TradeLimit.BTC: order_id = self.request_client.create_order( symbol_name + 'usdt', AccountType.SPOT, OrderType.SELL_MARKET, sell_currency, None) self.update_balance_and_dollar(order_id, Action.SELL, symbol_name) self.send_mail("触发卖出信号", f"动态平衡策略触发卖出{sell_currency}的{symbol_name}") else: self.logger.warning( f"当前欲卖出{sell_currency}美金的{symbol_name}, 不满足最低交易限制") except AssertionError: self.logger.error( f"AssertionError, symbol={symbol_name}, amount={kwargs['amount']}, " f"dollar={kwargs['dollar']}") finally: self.trade_unlock(symbol_name, self.strategy)
def buy(self, symbol_name, **kwargs): self.trade_lock(symbol_name, self.strategy) try: assert kwargs['dollar'] >= kwargs['amount'] buy_dollar = retain_decimals( (float(kwargs['dollar']) - float(kwargs['amount'])) / 2, 2) if float(buy_dollar) >= TradeLimit.USDT: # 买入 order_id = self.request_client.create_order( symbol_name + 'usdt', AccountType.SPOT, OrderType.BUY_MARKET, buy_dollar, None) self.update_balance_and_dollar(order_id, Action.BUY, symbol_name, buy_dollar=buy_dollar) self.send_mail("触发买入信号", f"动态平衡策略触发买入{buy_dollar}的{symbol_name}") else: self.logger.warning( f"当前欲买入{buy_dollar}美金的{symbol_name}, 不满足最低交易限制") except AssertionError: self.logger.error( f"AssertionError, symbol={symbol_name}, amount={kwargs['amount']}, " f"dollar={kwargs['dollar']}") finally: self.trade_unlock(symbol_name, self.strategy)
def update_balance_and_dollar(self, order_id, action, symbol_name, **kwargs): order_detail = self.request_client.get_order("symbol", order_id) # 验证订单是否执行完成 while not order_detail.state == OrderState.FILLED: time.sleep(1) self.logger.info(f"{order_id}还未执行完成, 休眠1秒等待执行完成") order_detail = self.request_client.get_order("symbol", order_id) # mongodb中减去已使用的美金 result = self.keep_balance_collection.find_one({"symbol": symbol_name}) dollar = float(result['amount']) if action == 'BUY': dollar -= float(kwargs['buy_dollar']) # mongodb中加上卖出获得的美金 elif action == 'SELL': dollar += float(order_detail.filled_cash_amount) # 格式化美金数 dollar = retain_decimals(dollar, 2) # mongodb更新操作 self.keep_balance_collection.update_one({"symbol": symbol_name}, {"$set": { "amount": dollar }}) self.logger.info(f"{symbol_name}美金数置为: {dollar}")
def get_account_amount(self, symbol_name, symbol_balance): price = self.get_price(symbol_name + "usdt") amount = str(float(symbol_balance) * float(price)) amount = retain_decimals(amount, 2) return price, amount