def func_broker(q_req): broker_stub_ind = broker_pb2_grpc.BrokerStub(CHANNEL_BROKER) while True: req = q_req.get(True) if req[0] == 0: broker_stub_ind.new_order(req[1]) else: broker_stub_ind.cancel_order(req[1])
def __init__(self, trader_id, trader_pin, channel): self.trader_id = trader_id self.trader_pin = trader_pin self.channel = grpc.insecure_channel(channel) self.stub = broker_pb2_grpc.BrokerStub(self.channel)
#multiprocessing.set_start_method('spawn') 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): """ 初始化策略。
def trading_B002(runtime): """ price_arr = np.array([[-0.15,-0.13,-0.11,-0.1,-0.07,-0.05], [ 0.05, 0.07, 0.1, 0.11, 0.13, 0.15]]) volume_arr = np.array([[10,6,2,2,4,6], [6,4,2,2,6,10]]) """ price_arr = np.array([[-0.07,-0.05], [ 0.05, 0.07]]) volume_arr = np.array([[10,12], [12,10]]) broker_stub_ind = broker_pb2_grpc.BrokerStub(CHANNEL_BROKER) while True: if runtime['isDigested_B002.PSE'] == 0: snapshot = getattr(runtime['snapshots'],'B002.PSE') runtime['isDigested_B002.PSE'] = 1 last_price = snapshot.last_price bid1 = snapshot.bid_levels[0].price ask1 = snapshot.ask_levels[0].price if runtime['net_B002.PSE'] == 1: buy_trm = dict(zip(price_arr[0,:] + bid1 - 0.02, volume_arr[0,:])) sell_trm = dict(zip(price_arr[1,:] + bid1 - 0.02, volume_arr[1,:])) elif runtime['net_B002.PSE'] == -1: buy_trm = dict(zip(price_arr[0,:] + ask1 + 0.02, volume_arr[0,:])) sell_trm = dict(zip(price_arr[1,:] + bid1 + 0.02, volume_arr[1,:])) else: buy_trm = dict(zip(price_arr[0,:] + last_price, volume_arr[0,:])) sell_trm = dict(zip(price_arr[1,:] + last_price, volume_arr[1,:])) curr_buy_order_info, curr_sell_order_info = func_order_info2level(runtime['order_info'].info['B002.PSE']) #//计算订单更新 order_to_cancel = [] buy_new_orders, tmp = func_calc_order_delta(curr_buy_order_info, buy_trm) order_to_cancel += tmp sell_new_orders, tmp = func_calc_order_delta(curr_sell_order_info, sell_trm) order_to_cancel += tmp buy_new_orders = sorted(buy_new_orders, key = lambda x:x[0], reverse = True) sell_new_orders = sorted(sell_new_orders, key = lambda x:x[0], reverse = True) #//计算下单 maxLen = max(len(buy_new_orders), len(sell_new_orders)) for i in range(maxLen): if runtime['isOpen_B002.PSE']: #//开平仓控制 try: buy_new_order = buy_new_orders[i] broker_stub_ind.new_order(api.TraderRequest_new_order('B002.PSE',0,0,buy_new_order[1],buy_new_order[0])) except: pass try: sell_new_order = sell_new_orders[i] broker_stub_ind.new_order(api.TraderRequest_new_order('B002.PSE',1,1,sell_new_order[1],sell_new_order[0])) except: pass else: try: buy_new_order = buy_new_orders[i] broker_stub_ind.new_order(api.TraderRequest_new_order('B002.PSE',0,1,buy_new_order[1],buy_new_order[0])) except: pass try: sell_new_order = sell_new_orders[i] broker_stub_ind.new_order(api.TraderRequest_new_order('B002.PSE',1,0,sell_new_order[1],sell_new_order[0])) except: pass #//进行撤单 for order_id in order_to_cancel: broker_stub_ind.cancel_order(api.TraderRequest_cancel_order(order_id)) print('B002.PSE完成一次操作循环!') runtime['loopCount_B002.PSE'] += 1
def __init__(self): channel = grpc.insecure_channel(broker_ip_port) self.stub = broker_pb2_grpc.BrokerStub(channel)
# used in circumstances in which the with statement does not fit the needs # of the code. # ============================================================================= # with grpc.insecure_channel('113.208.112.25:57600') as channel: # stub = broker_pb2_grpc.MarketDataStub(channel) # response = stub.list_instruments(common_pb2.Empty()) # print("Instruments received: " ) # print(response) # for data in stub.subscribe(common_pb2.Empty()): # # print("Subscription received: ") # print(data) # # ============================================================================= with grpc.insecure_channel('113.208.112.25:57500') as channel: stub = broker_pb2_grpc.BrokerStub(channel) response = stub.get_trader( broker_pb2.TraderRequest(trader_id=42, trader_pin='NrojE4oZH', request_type='FULL_INFO')) print("Instruments received: ") print(response) # ============================================================================= # for data in stub.subscribe(common_pb2.Empty()): # # ============================================================================= # # response=stub.subscribe(common_pb2.Empty()) # # ============================================================================= # print("Subscription received: ") # print(data) # # =============================================================================