def __init__(self, market_type, frequence,): self.user = QA_User() self.if_settled = False self.account = None self.portfolio = None self.market = QA_Market() self.market_type = market_type self.frequence = frequence self.broker = QA_SPEBroker() self.broker_name = 'shipane_broker' self.ingest_data = None
class TradeInfoHandler(QABaseHandler): """trade 信息查询句柄 Arguments: QABaseHandler {[type]} -- [description] ?func=ping ping 服务器 ?func=clients 查询当前的可用客户端 ?func=accounts 查询当前的账户 ?func=positions&account=xxx 查询账户持仓 ?func=orders&status 查询订单 下单/撤单功能不在此handler提供 """ broker = QA_SPEBroker() def funcs(self, func, account, *args, **kwargs): if func == 'ping': data = self.broker.query_clients() return data elif func == 'clients': data = self.broker.query_clients() return data elif func == 'accounts': data = self.broker.query_accounts(account) return data elif func == 'positions': data = self.broker.query_positions(account) if isinstance(data, dict): data['hold_available'] = data['hold_available'].to_dict() return data elif func == 'orders': status = self.get_argument('status', '') return self.broker.query_orders(account, status) elif func == 'cancel_order': orderid = self.get_argument('orderid') return self.broker.cancel_order(account, orderid) def get(self): func = self.get_argument('func', 'ping') account = self.get_argument('account', None) print(account) print(func) data = self.funcs(func, account) print(data) if isinstance(data, pd.DataFrame): self.write({'result': QA_util_to_json_from_pandas(data)}) else: self.write({'result': data})
class QATrade_Realtime(): def __init__(self, market_type, frequence,): self.user = QA_User() self.if_settled = False self.account = None self.portfolio = None self.market = QA_Market() self.market_type = market_type self.frequence = frequence self.broker = QA_SPEBroker() self.broker_name = 'shipane_broker' self.ingest_data = None @property def now(self): return datetime.datetime.now() def load_account(self, account): # 通过 broke名字 新建立一个 QAAccount 放在的中 session字典中 session 是 { 'cookie' , QAAccount } self.market.login(self.broker_name, account.account_cookie, account) def start_market(self): """ start the market thread and register backtest broker thread QAMarket 继承QATrader, QATrader 中有 trade_engine属性 , trade_engine类型是QA_Engine从 QA_Thread继承 """ # 启动 trade_engine 线程 self.market.start() # 注册 backtest_broker ,并且启动和它关联线程QAThread 存放在 kernels 词典中, { 'broker_name': QAThread } self.market.register(self.broker_name, self.broker) def run(self): """generator driven data flow """ # 如果出现了日期的改变 才会进行结算的事件 _date = None while QA_util_if_tradetime(self.now): for data in self.ingest_data: # 对于在ingest_data中的数据 # <class 'QUANTAXIS.QAData.QADataStruct.QA_DataStruct_Stock_day'> date = data.date[0] if self.market_type is MARKET_TYPE.STOCK_CN: # 如果是股票市场 if _date != date: # 如果新的date # 前一天的交易日已经过去 # 往 broker 和 account 发送 settle 事件 try: self.market.trade_engine.join() # time.sleep(2) self.market._settle(self.broker_name) except Exception as e: raise e # 基金 指数 期货 elif self.market_type in [MARKET_TYPE.FUND_CN, MARKET_TYPE.INDEX_CN, MARKET_TYPE.FUTURE_CN]: self.market._settle(self.broker_name) # print(data) self.broker.run( QA_Event(event_type=ENGINE_EVENT.UPCOMING_DATA, market_data=data)) # 生成 UPCOMING_DATA 事件放到 队列中去执行 self.market.upcoming_data(self.broker_name, data) self.market.trade_engine.join() _date = date