def main(): market_channel = grpc.insecure_channel('113.208.112.25:57600') market_stub = broker_pb2_grpc.MarketDataStub(market_channel) pool = Pool(processes=200) broker = Broker(25, 'qWC6G7nao', '113.208.112.25:57502') data = market_stub.subscribe(common_pb2.Empty()) try: i = 0 for item in data: # print('before') all_instrument_dict = instrument_parser( item, ['A001.PSE', 'A002.PSE', 'B001.PSE', 'B002.PSE']) for sym in ['A001.PSE', 'A002.PSE', 'B001.PSE', 'B002.PSE']: long_points_ave[sym].append( all_instrument_dict[sym]['deliver_price']) short_points_ave[sym].append( all_instrument_dict[sym]['deliver_price']) if i % 15 == 14: # print('in') account_detail = position_order_parser(broker.GET_TRADER()) tasks = [] for ticker in ['A001.PSE', 'A002.PSE', 'B001.PSE', 'B002.PSE']: long_ave = np.mean(long_points_ave[ticker]) short_ave = np.mean(short_points_ave[ticker]) tasks.extend( task_generator(account_detail, all_instrument_dict, ticker, long_ave, short_ave)) t0 = time.time() print(len(tasks)) if not tasks: pass else: results = pool.map_async(order_on_task, tasks) results.wait(timeout=1.5) # pool.close() print(time.time() - t0) print(account_detail['time_stamp']) i += 1 # print('out') except KeyboardInterrupt: print('Program stop by keyboardinterupt')
def __init__(self): channel = grpc.insecure_channel(market_ip_port) self.stub = broker_pb2_grpc.MarketDataStub(channel)
import os import numpy as np from time import ctime, sleep, time_ns import tradingObjects """ 配置背景参数。 """ ID = 3 PIN = 'jY0RyHyK9' CHANNEL_BROKER = grpc.insecure_channel('113.208.112.25:57503') CHANNEL_DATA = grpc.insecure_channel('113.208.112.25:57600') broker_stub = broker_pb2_grpc.BrokerStub(CHANNEL_BROKER) data_stub = broker_pb2_grpc.MarketDataStub(CHANNEL_DATA) #//创建客户端 data_stream = data_stub.subscribe( broker_pb2.TraderRequest(trader_id=ID, trader_pin=PIN)) #//获取行情流对象 api.set_stub(broker_stub, data_stub) api.set_stream(data_stream) #//设置api客户端 def initStrategy(runtime): """ 初始化策略。 """
""" Const """ ID = 33 PIN = 'jY0RyHyK9' """ 创建客户端 """ #Create channel broker_channel = grpc.insecure_channel('113.208.112.25:57503') data_channel = grpc.insecure_channel('113.208.112.25:57600') #create stub broker_stub = broker_pb2_grpc.BrokerStub(broker_channel) md_stub = broker_pb2_grpc.MarketDataStub(data_channel) api.set_stub(broker_stub, md_stub) """ Demo: 下单 """ TraderRequest_new_order = broker_pb2.TraderRequest(trader_id=ID, trader_pin=PIN, request_type=0, order_id=0, side=0, symbol='A000.PSE', volume=1, is_market=True, pos_type=1) TraderRequest_new_order_test = proto_wrapper.TraderRequest_new_order(