Пример #1
0
def buy(buyPrice, amount, index, transactionModel):
    try:

        symbol = transactionModel.symbol
        minIncome = transactionModel.minIncome
        decimalLength = transactionModel.pricePrecision

        orderId = commonUtil.getRandomOrderId()

        # 已经不存在这个交易对的买入 说明是第一次买入 需要降低买入的花费
        if modelUtil.getBuyModelBySymbolAndStatus(transactionModel.symbol,
                                                  0) is None:
            amount = round(
                float(transactionModel.everyExpense) * 0.6 / buyPrice,
                int(transactionModel.precision))

        buyModel = BuyModel(0, symbol, buyPrice, buyPrice, index, amount,
                            orderId, minIncome, buyPrice, 2)
        newJumpModel = JumpQueueModel(0, symbol, 1, orderId,
                                      round(buyPrice * 1.005, decimalLength),
                                      round(buyPrice * 1.008, decimalLength),
                                      round(buyPrice * 0.995, decimalLength),
                                      0, buyPrice)

        if modelUtil.insertBuyModel(buyModel):

            modelUtil.insertJumpQueueModel(newJumpModel)

        else:
            logUtil.error("BiTradeUtil--buy insert 0")

        return True
    except Exception as err:
        logUtil.error("BiTradeUtil--buy" + err)
    return False
Пример #2
0
def getBuyModel(symbol, env):
    models = []

    if "dev" == env:
        if myCache.buyModelIsNone:
            return models

        if myCache.buyModelCache.get(symbol, None):
            return myCache.buyModelCache.get(symbol, None)

    mysql = MySqlConn()
    sql = "select * from tb_buy_record where symbol=%s"
    results = mysql.getAll(sql, (symbol))
    mysql.dispose()
    if results:
        for row in results:
            buyModel = BuyModel(row['id'], row['symbol'], row['price'],
                                row['ori_price'], row['buy_index'],
                                row['amount'], row['order_id'],
                                row['min_income'], row['last_price'],
                                row['status'])
            models.append(buyModel)

        myCache.buyModelIsNone = False
        myCache.buyModelCache[symbol] = models
    else:
        myCache.buyModelIsNone = True
    return models
Пример #3
0
def sell(env, sellPrice, sellIndex, buyModel, transactionModel):
    try:

        symbol = transactionModel.symbol
        decimalLength = transactionModel.pricePrecision

        if "pro" == env:
            sellPrice = round(
                float(buyModel.price) * (1 + float(buyModel.minIncome)),
                decimalLength)

        newBuyModel = BuyModel(buyModel.id, buyModel.symbol, buyModel.price,
                               buyModel.oriPrice, buyModel.index,
                               buyModel.amount, buyModel.orderId,
                               buyModel.minIncome, buyModel.lastPrice, 3)
        # newJumpModel = JumpQueueModel(2, buyModel.orderId, round(sellPrice * 0.992, decimalLength),
        #                               round(sellPrice * 0.995, decimalLength), round(sellPrice * 1.01, decimalLength), 0,
        #                               time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sellIndex)),sellPrice)

        #统计发现,因为提高了最低收入值,因此不大可能会再次触发跳跃
        newJumpModel = JumpQueueModel(0, symbol, 2, buyModel.orderId,
                                      sellPrice, sellPrice,
                                      round(sellPrice * 1.01,
                                            decimalLength), 0, sellPrice)

        if modelUtil.modBuyModel(newBuyModel):
            modelUtil.insertJumpQueueModel(newJumpModel)
        else:
            logUtil.error("BiTradeUtil--sell modBuyModel 0 orderId=",
                          buyModel.orderId)

    except Exception as err:
        logUtil.error("BiTradeUtil--sell" + err)
Пример #4
0
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
Пример #5
0
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)
Пример #6
0
def getBuyModelBySymbolAndStatus(symbol, status):
    sql = "select * from tb_buy_record where symbol=%s and status=%s"
    mysql = MySqlConn()
    result = mysql.getOne(sql, (symbol, status))
    mysql.dispose()
    if result:
        buyModel = BuyModel(result['id'], result['symbol'], result['price'],
                            result['ori_price'], result['buy_index'],
                            result['amount'], result['order_id'],
                            result['min_income'], result['last_price'],
                            result['status'])
        return buyModel

    return None
Пример #7
0
def doMerge(env, list, transactionModel):

    if len(list) < 1:
        return None

    decimallength = transactionModel.pricePrecision
    price = 0.0
    amount = 0.0
    index = 0
    orderId = 0
    shouldDel = []

    for buyModel in list:
        if checkOrderIsFilled(env, buyModel.orderId):
            logUtil.info("doMerge=", buyModel)
            shouldDel.append(buyModel)
            price = price + float(buyModel.price) * float(buyModel.amount)
            amount = amount + float(buyModel.amount)
            index = buyModel.index  #这两个随便
            orderId = buyModel.orderId

    if len(shouldDel) < 2:
        return None

    amount = round(amount, int(transactionModel.precision))
    avgPrice = round(price / amount, decimallength)

    newBuyModel = BuyModel(0, list[0].symbol, avgPrice, avgPrice, index,
                           amount, orderId, transactionModel.minIncome,
                           avgPrice, 0)

    for shouldDelBuyModel in shouldDel:
        modelUtil.delBuyModel(shouldDelBuyModel.id)

    id = modelUtil.insertBuyModel(newBuyModel)
    newBuyModel.id = id
    return newBuyModel
Пример #8
0
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)
Пример #9
0
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)