def get_position(self): ''' current order position including open and close position, return None if there is no position 当前的仓位,如果没有的话,返回None ''' ret = u.retry(lambda: self.client.Position.Position_get(filter=json.dumps({"symbol": s.SYMBOL})).result()) if ret: return ret[0] else: return None
def get_market_price(self): ''' current close price for settings symbol 当前设置的交易对收盘价格 ''' if self.market_price: return self.market_price else: return u.retry(lambda: self.client.Instrument.Instrument_get(symbol=s.SYMBOL).result())[0]["lastPrice"]
def cancel_all(self): """ cancel all orders, including stop orders """ orders = u.retry(lambda: self.client.Order.Order_cancelAll().result()) for order in orders: logger.info(f"Cancel Order : (orderID, orderType, side, orderQty, limit, stop) = " f"({order['clOrdID']}, {order['ordType']}, {order['side']}, {order['orderQty']}, " f"{order['price']}, {order['stopPx']})") logger.info(f"Cancel All Order")
def get_open_orders(self): """ fetch my all open orders """ open_orders = u.retry(lambda: self.client .Order.Order_getOrders(filter=json.dumps({"symbol": s.SYMBOL, "open": True})) .result()) open_orders = [o for o in open_orders if o["clOrdID"].startswith('Daxiang')] if len(open_orders) > 0: return open_orders else: return None
def order(self, orderQty, stop=0): ''' This is 'Market' order 'buy' if orderQty is positive 'sell' if orderQty is nagative ''' clOrdID = 'Daxiang_' + u.random_str() side = 'Buy' if orderQty>0 else 'Sell' if stop == 0: # market order orderType = 'Market' u.retry(lambda: self.client.Order.Order_new(symbol=s.SYMBOL, ordType=orderType, clOrdID=clOrdID, side=side, orderQty=orderQty).result()) u.logging_order(id=clOrdID, type=orderType, side=side, qty=orderQty, price=self.get_market_price()) else: # stop order orderType = 'Stop' u.retry(lambda: self.client.Order.Order_new(symbol=s.SYMBOL, ordType=orderType, clOrdID=clOrdID, side=side, orderQty=orderQty, stopPx=stop).result()) u.logging_order(id=clOrdID, type=orderType, side=side, qty=orderQty, stop=stop)
def fetch_ohlcv(self, start_time, end_time): ''' get data for open-high-low-close-volumn array 获取open-high-low-close-volumn数组数据 ''' left_time = start_time right_time = end_time data = u.to_data_frame([]) while True: source = u.retry(lambda: self.client.Trade.Trade_getBucketed(symbol=s.SYMBOL, binSize="1m", startTime=left_time, endTime=right_time, count=500, partial=False).result()) if len(source) == 0: break source = u.to_data_frame(source) data = pd.concat([data, source]) if right_time > source.iloc[-1].name + timedelta(minutes=1): left_time = source.iloc[-1].name + timedelta(minutes=1) time.sleep(1) else: break return data
def get_margin(self): ''' account balance summary 当前的账户余额相关信息 ''' return u.retry(lambda: self.client.User.User_getMargin(currency="XBt").result())
def set_leverage(self, leverage): ''' set leverage to position ''' return u.retry(lambda: self.client.Position.Position_updateLeverage(symbol=s.SYMBOL, leverage=leverage).result())