class AccModelHandler(QAWebSocketHandler): port=QA_Portfolio() def open(self): self.client.add(self) self.write_message('realtime socket start') def on_message(self, message): #assert isinstance(message,str) try: if message =='create_account': account=self.port.new_account() #account = self.port.add_account() self.write_message('CREATE ACCOUNT: {}'.format(account.account_cookie)) except Exception as e: print(e) def on_close(self): print('connection close')
class AccModelHandler(QAWebSocketHandler): port = QA_Portfolio() def open(self): self.write_message('realtime socket start') def on_message(self, message): try: message = message.split('_') self.write_message({'input_param': message}) if message[0] == 'create': if message[1] == 'account': self.account = self.port.new_account() self.write_message('CREATE ACCOUNT: {}'.format( self.account.account_cookie)) self.write_message(self.account.init_assets) elif message[0] == 'query': if message[1] == 'portfolio': self.write_message( {'result': list(self.port.accounts.keys())}) elif message[1] == 'history': self.write_message({'result': self.account.history}) elif message[0] == 'trade': """code/price/amount/towards/time """ self.account.receive_simpledeal(code=str(message[1]), trade_price=float(message[2]), trade_amount=int(message[3]), trade_towards=int(message[4]), trade_time=str(message[5])) except Exception as e: print(e) def on_close(self): print('connection close')
class AccModelHandler(QAWebSocketHandler): port = QA_Portfolio() broker = ['haitong', 'ths_moni', 'tdx_moni', 'quantaxis_backtest', 'ctp', 'ctp_min'] Broker = QA_BacktestBroker() systime = False def open(self): self.write_message({ 'data': 'QUANTAXIS BACKEND: realtime socket start', 'topic': 'open', 'status': 200}) def on_message(self, message): """ 返回值需要带上 1. topic 2. status 3. mes 用于客户端记录log """ try: message = message.split('$') # self.write_message({'topic':'receive', 'status': 304, 'input_param': message}) if message[0] == 'create': if message[1] == 'account': self.account = self.port.new_account() self.write_message( 'CREATE ACCOUNT: {}'.format(self.account.account_cookie)) self.write_message(self.account.init_assets) elif message[0] == 'query': if message[1] == 'portfolio': self.write_message( {'result': list(self.port.accounts.keys())}) elif message[1] == 'history': self.write_message({ 'topic': 'history', 'status': 200, 'data': self.port.get_account_by_cookie(message[2]).history}) elif message[1] == 'filled_order': self.write_message({ 'topic': 'filled_orders', 'status': 200 }) elif message[1] == 'available_account': self.write_message({'status': 200, 'topic': 'query_account', 'data': list(self.port.accounts.keys())}) elif message[1] == 'info': ac = self.port.get_account_by_cookie(message[2]) self.write_message({ 'topic': 'account_info', 'status': 200, 'data': {'hold': ac.hold.to_dict(), 'cash': ac.cash_available}, 'mes': 'QAT: get account {} info successfully'.format(ac.account_cookie) }) elif message[0] == 'login': """ login$account$broker$password$tpassword$serverip """ account, broker, password, tpassword, serverip = message[ 1], message[2], message[3], message[4], message[5] if broker == 'quantaxis_backtest': self.account = self.port.new_account( account_cookie=account) self.write_message({'topic': 'login', 'status': 200, 'account_cookie': self.account.account_cookie, 'mes': 'QAT: success login QUANTAXIS_BACKTEST welcome {}'.format(self.account.account_cookie)}) elif broker in ['ths_moni', 'tdx_moni']: self.account = self.port.new_account( account_cookie=account ) elif message[0] == 'trade': """account/code/price/amount/towards/time 先给个简易版本 """ print(message) ac = self.port.get_account_by_cookie(message[1]) self.systime= self.systime if self.systime else str(message[6]) if self.systime < str(message[6]): ac.settle() order = ac.send_order( code=str(message[2]), time=str(message[6]), amount=int(message[4]), towards=int(message[5]), price=float(message[3]), order_model=ORDER_MODEL.MARKET, amount_model=AMOUNT_MODEL.BY_AMOUNT ) try: self.Broker.receive_order(QA_Event(order=order)) trade_mes = self.Broker.query_orders( ac.account_cookie, 'filled') # print(trade_mes) res = trade_mes.loc[order.account_cookie, order.realorder_id] order.trade(res.trade_id, res.trade_price, res.trade_amount, res.trade_time) # TODO: market_engine self.write_message({ 'topic': 'trade', 'status': 200, 'order_id': order.realorder_id, 'mes': 'trade success TradeID: {} | Trade_Price: {} | Trade_Amount: {} | Trade_Time: | {}'.format(res.trade_id, res.trade_price, res.trade_amount, res.trade_time), }) except Exception as e: self.write_message({ 'topic':'trade', 'status': 400, 'mes': str(e) }) except Exception as e: print(e) def on_close(self): print('connection close')
class AccModelHandler(QAWebSocketHandler): port = QA_Portfolio(portfolio_cookie='trade_special') broker = [ 'haitong', 'ths_moni', 'tdx_moni', 'quantaxis_backtest', 'ctp', 'ctp_min' ] Broker = QA_BacktestBroker() systime = False def open(self): self.write_message({ 'data': 'QUANTAXIS BACKEND: realtime socket start', 'topic': 'open', 'mes': 'QUANTAXIS BACKEND: realtime socket start', 'status': 200 }) def on_message(self, message): """ 返回值需要带上 1. topic 2. status 3. mes 用于客户端记录log """ try: message = eval(message) # self.write_message({'topic':'receive', 'status': 304, 'input_param': message}) if message['topic'] == 'query': """ { 'topic' : 'query', 'subtopic': 'xxxx', } """ if message['subtopic'] == 'portfolio': self.write_message({ 'topic': 'query_portfolio', 'status': 200, 'mes': 'QAT: get_query_portfolio', 'result': list(self.port.accounts.keys()), }) elif message['subtopic'] == 'history': self.write_message({ 'topic': 'history', 'status': 200, 'mes': 'QAT: get_query_history', 'data': self.port.get_account_by_cookie( message['account_cookie']).history }) elif message['subtopic'] == 'filled_order': self.write_message({ 'topic': 'filled_orders', 'mes': 'QAT: get_filled_order_query', 'status': 200 }) elif message['subtopic'] == 'available_account': self.write_message({ 'status': 200, 'topic': 'query_account', 'mes': 'QAT: get_query_account_command', 'data': list(self.port.accounts.keys()) }) elif message['subtopic'] == 'info': ac = self.port.get_account_by_cookie( message['account_cookie']) self.write_message({ 'topic': 'account_info', 'status': 200, 'data': { 'hold': ac.hold.to_dict(), 'cash': ac.cash_available }, 'mes': 'QAT: get account {} info successfully'.format( ac.account_cookie) }) elif message['topic'] == 'login': """ login$account$broker$password$tpassword$serverip """ account, broker, password, tpassword, serverip = message[ 'account_cookie'], message['broker'], message[ 'password'], message['tpassword'], message['server_ip'] if broker == 'quantaxis_backtest': self.account = self.port.new_account( account_cookie=account) print(self.account.account_cookie) z = { 'topic': 'login', 'status': 200, 'account_cookie': self.account.account_cookie, 'mes': 'QAT: success login QUANTAXIS_BACKTEST welcome {}'. format(self.account.account_cookie) } print(z) self.write_message(z) print('fin write') elif broker in ['ths_moni', 'tdx_moni']: self.account = self.port.new_account( account_cookie=account) elif broker in ['simnow']: pass elif message['topic'] == 'trade': """account/code/price/amount/towards/time 先给个简易版本 websocket 请求 { 'topic': 'trade', 'code' : code, 'account': xxxx, 'price': price, 'amount' : amount, 'time' : time, 'towards': towards } topic: trade status: 200/304/404/500 mes: xxxx data: xxxx """ self.write_message({ 'topic': 'mes', 'status': 200, 'mes': 'QAtrader:get_{}_{}_{}_{}_{}'.format( message['account'], message['code'], message['price'], message['amount'], message['time']) }) ac = self.port.get_account_by_cookie(message['account']) self.systime = self.systime if self.systime else str( message['time']) if self.systime < str(message['time']): ac.settle() order = ac.send_order(code=str(message['code']), time=str(message['time']), amount=int(message['amount']), towards=int(message['towards']), price=float(message['price']), order_model=ORDER_MODEL.MARKET, amount_model=AMOUNT_MODEL.BY_AMOUNT) if order: try: self.Broker.receive_order(QA_Event(order=order)) trade_mes = self.Broker.query_orders( ac.account_cookie, 'filled') # print(trade_mes) res = trade_mes.loc[order.account_cookie, order.realorder_id] order.trade(res.trade_id, res.trade_price, res.trade_amount, res.trade_time) # TODO: market_engine self.write_message({ 'topic': 'trade', 'status': 200, 'order_id': order.realorder_id, 'mes': 'trade success TradeID: {} | Trade_Price: {} | Trade_Amount: {} | Trade_Time: | {}' .format(res.trade_id, res.trade_price, res.trade_amount, res.trade_time), }) except Exception as e: self.write_message({ 'topic': 'trade', 'status': 400, 'mes': str(e) }) else: self.write_message({ 'topic': 'trade', 'status': 500, 'mes': 'QATrader: Failed to create order' }) except Exception as e: print(e) def on_close(self): print('connection close')