def jumpBuy(env, buyPrice, jumpQueueModel, transactionModel, index): try: symbol = transactionModel.symbol orderId = jumpQueueModel.orderId buyModel = modelUtil.getBuyModelByOrderId(orderId) if buyModel is None: logUtil.info("orderId is null", orderId) return newAmount = round( float(transactionModel.everyExpense) / buyPrice, int(transactionModel.precision)) # 已经不存在这个交易对的买入 说明是第一次买入 需要降低买入的花费 if modelUtil.getBuyModelBySymbolAndStatus(transactionModel.symbol, 0) is None: newAmount = round( float(transactionModel.everyExpense) * 0.6 / buyPrice, int(transactionModel.precision)) if "pro" == env: result = huobi.send_order(newAmount, "api", symbol, "buy-limit", buyPrice) logUtil.info("buy result", result, symbol, newAmount, buyPrice) if result['status'] == 'ok': orderId = result['data'] else: return False else: huobi.send_order_dev(newAmount, 1, buyPrice) newBuyModel = BuyModel(buyModel.id, buyModel.symbol, buyPrice, buyModel.oriPrice, index, newAmount, orderId, transactionModel.minIncome, buyPrice, 0) if modelUtil.modBuyModel(newBuyModel): modelUtil.delJumpModelById(jumpQueueModel.id) jumpProfit = (float(jumpQueueModel.oriPrice) - buyPrice) * float(newAmount) huobi.jumpProfit = huobi.jumpProfit + jumpProfit jumpQueueModel.orderId = orderId modelUtil.insertHistoryJumpQueueModel(jumpQueueModel, buyPrice, newAmount) modelUtil.insertBuySellReocrd(buyModel.symbol, 1, buyPrice, 0, orderId, 0, newAmount, index) else: logUtil.error("BiTradeUtil--jumpBuy modBuyModel 0 orderId=", orderId) return True except Exception as err: logUtil.error("BiTradeUtil--jumpBuy" + err) 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 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 stopLossJumpBuy(env, buyPrice, jumpQueueModel, transactionModel, index): try: symbol = transactionModel.symbol orderId = jumpQueueModel.orderId stopLossModel = modelUtil.getStopLossModelByOrderId(orderId) if stopLossModel is None: logUtil.error("jumpQueueModel=", jumpQueueModel, " is none") return buyModel = modelUtil.getBuyModelByOrderId(stopLossModel.oriOrderId) minAmount = round(5.1 / buyPrice, int(transactionModel.precision)) buyAmount = float(buyModel.amount) oldAmount = float(buyModel.amount) if minAmount > oldAmount: buyAmount = minAmount if "pro" == env: result = huobi.send_order(buyAmount, "api", symbol, "buy-limit", buyPrice) logUtil.info("buy result", result, symbol, buyAmount, buyPrice) if result['status'] == 'ok': orderId = result['data'] else: return False else: huobi.send_order_dev(buyAmount, 1, buyPrice) # 计算新的价格 length = transactionModel.pricePrecision #原本用掉的钱 - (止损卖出等到的钱 - 这次买回来花费的钱)/目前的数量 newPrice = round((float(buyModel.price) * oldAmount - (float(stopLossModel.sellPrice) * oldAmount - float(buyPrice) * buyAmount)) / buyAmount, length) newBuyModel = BuyModel(buyModel.id, buyModel.symbol, newPrice, buyModel.oriPrice, buyModel.index, buyModel.amount, buyModel.orderId, transactionModel.minIncome, buyPrice, 0) if modelUtil.modBuyModel(newBuyModel): if modelUtil.delStopLossModelById(stopLossModel.id): modelUtil.delJumpModelById(jumpQueueModel.id) modelUtil.insertHistoryJumpQueueModel(jumpQueueModel, buyPrice, buyModel.amount) jumpProfit = (float(jumpQueueModel.oriPrice) - buyPrice) * float(buyModel.amount) huobi.jumpProfit = huobi.jumpProfit + jumpProfit # 记录止损买日志 modelUtil.insertStopLossHistoryReocrd(symbol, 1, buyPrice, stopLossModel.oriPrice, stopLossModel.oriAmount, stopLossModel.oriOrderId, orderId) else: logUtil.error("BiTradeUtil--delStopLossModelById 0 =", stopLossModel) else: logUtil.error("BiTradeUtil--stopLossJumpBuy 0 orderId=", buyModel.orderId) except Exception as err: logUtil.error("BiTradeUtil--stopLossJumpBuy" + err)