Ejemplo n.º 1
0
def sell(amount=1.0, price="", totalAmount=0, avgPrice=0):
    orderId = None
    try:
        if price:
            matchPrice = "0"
        else:
            matchPrice = "1"
        orderId = yield okexFuture.trade(pairs,
                                         price=price,
                                         amount=str(round(amount)),
                                         tradeType="2",
                                         matchPrice=matchPrice)
    except Exception as err:
        failure = Failure(err)
        return failure

    try:
        orders = yield okexFuture.getOrder(pairs, status="1", orderId="-1")
    except Exception as err:
        log = Logger('action')
        failure = Failure(err)
        log.info("{failure}", failure=failure)

    else:
        if orders is not None:
            for order in orders:
                orderType = order['type']
                status = order['status']
                pId = order['order_id']
                if (orderType == 2 or orderType == 4) and (status == 0
                                                           or status == 1):
                    log = Logger('sellpc')
                    log.info("{pId}", pId=pId)
                    try:
                        result, data = yield okexFuture.cancel(pairs,
                                                               orderId=pId)
                    except Exception as err:
                        failure = Failure(err)

    if orderId:
        try:
            order = yield okexFuture.getOrder(pairs, orderId=orderId)
        except Exception as err:
            failure = Failure(err)
            return failure
        else:
            price = order[0]['price']
            sellInfo = (orderId, price, float(amount))
            avgPrice = avgPrice * totalAmount + price * round(float(amount))
            totalAmount += round(float(amount))
            avgPrice = avgPrice / totalAmount
            sellpInfos = yield doP(totalAmount,
                                   avgPrice,
                                   round(float(amount)),
                                   price,
                                   profitRates,
                                   leverage,
                                   pType=1)
            # sellpInfo = None
            return (sellInfo, sellpInfos)
Ejemplo n.º 2
0
def sellp(amount, price=""):
    orderId = None
    try:
        if price == "":
            matchPrice = "1"
        else:
            matchPrice = "0"
        orderId = yield okexFuture.trade(pairs,
                                         price=price,
                                         amount=str(round(amount)),
                                         tradeType="4",
                                         matchPrice=matchPrice)
    except Exception as err:
        failure = Failure(err)
        return failure

    if orderId:
        try:
            order = yield okexFuture.getOrder(pairs, orderId=orderId)
        except Exception as err:
            failure = Failure(err)
            return failure
        else:
            price = order[0]['price']
            return (orderId, price, float(amount))
Ejemplo n.º 3
0
def buyp(amount, price="", sellAmount=0):
    global state
    global buys
    global buypId
    orderId = None
    try:
        if price == "":
            matchPrice = "1"
        else:
            matchPrice = "0"
        orderId = yield okexFuture.trade(pairs,
                                         price=price,
                                         amount=str(round(amount)),
                                         tradeType="3",
                                         matchPrice=matchPrice)
    except Exception as err:
        failure = Failure(err)
        print(failure.getBriefTraceback())

    if orderId:
        print("SUCCESSFULLY BUYP:", orderId)

        try:
            order = yield okexFuture.getOrder(pairs, orderId=orderId)
        except Exception as err:
            failure = Failure(err)
            print(failure.getBriefTraceback())
        else:
            print(order)

    buypId = orderId
    if state == 'PPP':
        if sellAmount > 0:
            state = 'PPPsell'
            reactor.callWhenRunning(sellp, amount=sellAmount)
        else:
            state = 'STOP'
    else:
        state = 'BUYPCHECK'
Ejemplo n.º 4
0
def buy(amount=1.0, price=""):
    global state
    global buys
    global lastBuyAmount
    orderId = None
    try:
        if price:
            matchPrice = "0"
        else:
            matchPrice = "1"
        orderId = yield okexFuture.trade(pairs,
                                         price=price,
                                         amount=str(round(amount)),
                                         tradeType="1",
                                         matchPrice=matchPrice)
        print(orderId)
    except Exception as err:
        failure = Failure(err)
        print(failure.getBriefTraceback())

    if orderId:
        print("SUCCESSFULLY BUY:", orderId)
        try:
            order = yield okexFuture.getOrder(pairs, orderId=orderId)
        except Exception as err:
            failure = Failure(err)
            print(failure.getBriefTraceback())
        else:
            print(order)
            price = order[0]['price']
            buys.append((price, float(amount)))
            data = shelve.open(dataFile)
            data['buys'] = buys
            lastBuyAmount = amount
            data.close()
            # time.sleep(1)

    state = 'GO'
Ejemplo n.º 5
0
def sellp(amount, price=""):
    global state
    global sells
    global sellpId
    orderId = None
    try:
        if price == "":
            matchPrice = "1"
        else:
            matchPrice = "0"
        orderId = yield okexFuture.trade(pairs,
                                         price=price,
                                         amount=str(round(amount)),
                                         tradeType="4",
                                         matchPrice=matchPrice)
        print(orderId)
    except Exception as err:
        failure = Failure(err)
        print(failure.getBriefTraceback())

    if orderId:
        print("SUCCESSFULLY SELLP:", orderId)
        try:
            order = yield okexFuture.getOrder(pairs, orderId=orderId)
        except Exception as err:
            failure = Failure(err)
            print(failure.getBriefTraceback())
        else:
            print(order)

    sellpId = orderId

    if state == 'PPPsell':
        state = 'STOP'
    else:
        state = 'SELLPCHECK'
Ejemplo n.º 6
0
def cancle_p(lastAmount,
             lastPrice,
             pType="buy",
             avgPrice=0.0,
             totalAmount=0.0,
             must=False):
    orders = None
    try:
        orders = yield okexFuture.getOrder(pairs, status="1", orderId="-1")
    except Exception as err:
        print('cp1')
        log = Logger('cancle_p')
        failure = Failure(err)
        log.info("{failure}", failure=failure)
    else:
        if pType == 'buy':
            pType = 3
        elif pType == 'sell':
            pType = 4

        print(orders)
        if orders:
            for order in orders:
                orderType = order['type']
                status = order['status']
                pId = order['order_id']

                if orderType == pType and (status == 0 or status == 1):
                    log = Logger('pc')
                    log.info("{pId}", pId=pId)
                    try:
                        result, data = yield okexFuture.cancel(pairs,
                                                               orderId=pId)
                    except Exception as err:
                        failure = Failure(err)

        pInfos = None
        try:
            if pType == 3:
                if not must:
                    pInfos = yield doP(round(float(totalAmount)),
                                       avgPrice,
                                       round(float(lastAmount)),
                                       lastPrice,
                                       profitRates,
                                       leverage,
                                       pType=0)
                else:
                    pInfos = []
                    pInfo = yield buyp(amount=round(float(totalAmount)))
                    pInfos.append(pInfo)
            elif pType == 4:
                if not must:
                    pInfos = yield doP(round(float(totalAmount)),
                                       avgPrice,
                                       round(float(lastAmount)),
                                       lastPrice,
                                       profitRates,
                                       leverage,
                                       pType=1)
                else:
                    pInfos = []
                    pInfo = yield sellp(amount=round(float(totalAmount)))
                    pInfos.append(pInfo)
        except Exception as err:
            log = Logger('cancle_p')
            failure = Failure(err)
            log.info("{failure}", failure=failure)
            return failure
        else:
            return pInfos