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))
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)
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'
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'
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'