def getStopLossBuyModel(price, symbol, stopLoss, env): stopLossPackage = [] stopLoss = float(stopLoss) try: buyPackage = modelUtil.getBuyModel(symbol, env) # 查询购买历史 for buyModel in buyPackage: buyModelPrice = float(buyModel.price) status = int(buyModel.status) lastPrice = float(buyModel.lastPrice) stopLosssTemp = stopLoss if status != 0 or buyModelPrice <= price: continue stoplossCount = modelUtil.getStopLossModelCountByOrderId( buyModel.orderId) #判断有没有进行过止损 if stoplossCount > 0: if price < lastPrice: stopLosssTemp = stopLoss / 2 buyModelPrice = lastPrice else: #与上一次买的价差率 lastGap = price - lastPrice lastGap = lastGap / lastPrice #与当前价格的价差率 nowGap = buyModelPrice - price nowGap = nowGap / buyModelPrice if lastGap < 0.02 or nowGap < stopLoss: continue gap = buyModelPrice - price gap = gap / buyModelPrice if gap >= stopLosssTemp: if "pro" == env: result = huobi.order_info(buyModel.orderId) data = result['data'] state = data['state'] logUtil.info("order_info result", buyModel.orderId, result, symbol) if state != 'filled': continue stopLossPackage.append(buyModel) except Exception as err: logUtil.error("commonUtil--getStopLossBuyModel" + err) return stopLossPackage
def checkOrderIsFilled(env, orderId): if "pro" == env: result = huobi.order_info(orderId) data = result['data'] state = data['state'] if state == 'filled': return True else: return True return False
def jumpSell(env, sellPrice, jumpQueueModel, transactionModel, index): try: sellOrderId = commonUtil.getRandomOrderId() symbol = transactionModel.symbol orderId = jumpQueueModel.orderId buyModel = modelUtil.getBuyModelByOrderId(orderId) if "pro" == env: #{'status': 'ok', 'data': {'symbol': 'xrpusdt', 'source': 'api', 'field-cash-amount': '0.0', 'price': '0.1972', # 'canceled-at': 0, 'field-amount': '0.0', 'type': 'sell-limit', 'state': 'submitted', 'client-order-id': '', 'field-fees': '0.0', # 'created-at': 1587721600720, 'account-id': account-id, 'id': 82495000363, 'amount': '26.0000', 'finished-at': 0}} result = huobi.order_info(buyModel.orderId) data = result['data'] state = data['state'] logUtil.info("order_info result", result, symbol) if state == 'filled': #{'status': 'ok', 'data': 'orderId'} result = huobi.send_order(buyModel.amount, "api", symbol, 'sell-limit', sellPrice) logUtil.info("send_order result", result, symbol) if result['status'] != 'ok': return sellOrderId = result['data'] else: return else: huobi.send_order_dev(buyModel.amount, 0, sellPrice) sellOrderModel = SellOrderModel(0, buyModel.symbol, buyModel.price, sellPrice, buyModel.index, index, buyModel.orderId, sellOrderId, buyModel.amount) newBuyModel = BuyModel(buyModel.id, buyModel.symbol, buyModel.price, buyModel.oriPrice, buyModel.index, buyModel.amount, buyModel.orderId, buyModel.minIncome, buyModel.lastPrice, 4) if modelUtil.modBuyModel(newBuyModel): modelUtil.delJumpModelById(jumpQueueModel.id) modelUtil.insertHistoryJumpQueueModel(jumpQueueModel, sellPrice, buyModel.amount) modelUtil.insertSellOrderReocrd(sellOrderModel) else: logUtil.error("BiTradeUtil--sell jumpSell 0 orderId=", buyModel.orderId) except Exception as err: logUtil.error("BiTradeUtil--jumpSell" + err)
def chechOrder(self, orderId): try: if "pro" == self.env: result = huobi.order_info(orderId) data = result['data'] state = data['state'] logUtil.info("chechOrder", result) if state == 'filled': return True else: return True except Exception as err: logUtil.error('chechOrder error', err) return False
def stopLossSell(env, sellPrice, buyModel, symbol): try: orderId = commonUtil.getRandomOrderId() if "pro" == env: result = huobi.order_info(buyModel.orderId) data = result['data'] state = data['state'] logUtil.info("stopLossSell result", result, symbol) if state == 'filled': result = huobi.send_order(buyModel.amount, "api", symbol, 'sell-limit', sellPrice) if result['status'] == 'ok': orderId = result['data'] else: return else: return else: huobi.send_order_dev(buyModel.amount, 0, sellPrice) newBuyModel = BuyModel(buyModel.id, buyModel.symbol, buyModel.price, buyModel.oriPrice, buyModel.index, buyModel.amount, buyModel.orderId, buyModel.minIncome, buyModel.lastPrice, 1) if modelUtil.modBuyModel(newBuyModel): #在{什么时候} 以 {什么价格} 卖出 {原价是什么} 的 {多少个} {原来的orderId} {这次的orderId} {状态} insertResult = modelUtil.insertStopLossReocrd( symbol, sellPrice, buyModel.price, buyModel.amount, buyModel.orderId, orderId, 0) if insertResult is False: logUtil.error("insertResult is false", buyModel.orderId) #记录日志 modelUtil.insertStopLossHistoryReocrd(symbol, 0, sellPrice, buyModel.price, buyModel.amount, buyModel.orderId, orderId) else: logUtil.error("BiTradeUtil--sell stopLossSell 0 orderId=", buyModel.orderId, " id=", buyModel.id) except Exception as err: logUtil.error("BiTradeUtil--stopLossSell" + err)
def getOrderStatus(orderId): result = huobi.order_info(orderId) data = result['data'] state = data['state'] return state
# -*- coding: utf-8 -*- import HuobiService as huobi import globalUtil as constant if __name__ == '__main__': # test = huobi.get_kline('eosusdt', '1min', 1000) # test = aa.test0 # test['data'].reverse() # test = client.getKline(1200,"eos_usdt") # openOrders = huobi.open_orders(1057795,"eosusdt") print(huobi.order_info(28677414736))