本文整理匯總了Python中eventEngine.EventEngine.start方法的典型用法代碼示例。如果您正苦於以下問題:Python EventEngine.start方法的具體用法?Python EventEngine.start怎麽用?Python EventEngine.start使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類eventEngine.EventEngine
的用法示例。
在下文中一共展示了EventEngine.start方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from eventEngine import EventEngine [as 別名]
# 或者: from eventEngine.EventEngine import start [as 別名]
class MainEngine:
#----------------------------------------------------------------------
def __init__(self, account, _plus_path, bg):
self.ee = EventEngine(account) # 創建事件驅動引擎
self.bridge = bg
self.__lock = Lock()
self.userid = str(account['userid'])
self.password = str(account['password'])
self.brokerid = str(account['brokerid'])
self.mdaddress = str(account['mdfront'])
self.tdaddress = str(account['tdfront'])
self.instrument = account['instrument'] # sub list str
self.pluspath = _plus_path
self.dictInstrument = {} # 字典(保存合約查詢數據)
self.dictProduct = {} # 字典(保存合約查詢數據)
self.dictExchange= {}
self.tmpInstrument = {} # 字典(保存合約查詢數據)
self.tmpProduct = {} # 字典(保存合約查詢數據)
self.tmpExchange= {}
self.dictUpdate = None
self.subInstrument = set()
self.subedInstrument = set()
self.master = {} # 記錄主力合約對應關係
self.masterSubed = False
self.subedMaster = {}
self.tickpass = set()
self.now = datetime.now()
self.socket = None
self.coreServer = str(account['zmqserver'])
self.corefunc = passit
if int(account['usezmq'])>0:
if self.coreServer[:4] == 'tcp:':
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(self.coreServer)
self.socket = socket
self.corefunc = tcpfunc
elif self.coreServer[:5] == 'http:':
self.socket = True
self.corefunc = httpfunc
self.ee.start() # 啟動事件驅動引擎
self.som = {}
self.lastError = 0
self.lastTodo = 0
self.eq = 0
# 循環查詢持倉和賬戶相關
self.countGet = 0 # 查詢延時計數
self.lastGet = 'Account' # 上次查詢的性質
self.ee.register(EVENT_TDLOGIN, self.initGet,True) # 登錄成功後開始初始化查詢
self.__timer = time()+3
self.__readySubscribe = {}
# 合約儲存相關
self.get_instrument()
self.get_subscribe(self.instrument)
self.ee.register(EVENT_MDLOGIN, self.ready_subscribe,True)
self.ee.register(EVENT_TDLOGIN, self.ready_subscribe,True)
self.ee.register(EVENT_ERROR, self.get_error,False)
self.ee.register(EVENT_INSTRUMENT, self.insertInstrument,True)
self.ee.register(EVENT_TIMER, self.getAccountPosition,False)
self.ee.register(EVENT_TRADE, self.get_trade,False)
self.ee.register(EVENT_ORDER, self.get_order,False)
self.ee.register(EVENT_TICK, self.get_tick,True)
self.ee.register(EVENT_POSITION, self.get_position,False)
self.ee.register(EVENT_ACCOUNT, self.get_account,False)
self.ee.register(EVENT_TICK, self.check_timer,False)
self.ee.register(EVENT_ORDER, self.check_timer,False)
import eventType
for k,v in eventType.__dict__.items():
if 'EVENT_' in k and v[0]!='_':
self.ee.register(v,self.websocket_send,False)
self.md = ctpMdApi(self, self.mdaddress, self.userid, self.password, self.brokerid, plus_path=_plus_path) # 創建API接口
self.td = ctpTdApi(self, self.tdaddress, self.userid, self.password, self.brokerid, plus_path=_plus_path)
def get_subscribe(self,_inst):
if '#' in _inst:
_instlist = [ (v.get('_vol_',0),k) for k,v in self.dictInstrument.items()]
_instlist.sort(reverse=True)
_only = set()
for v,_instrumentid in _instlist[:10]:
_product = self.dictInstrument[_instrumentid]['ProductID']
if _product not in _only:
_exchangeid = self.dictInstrument.get(_instrumentid,{}).get("ExchangeID",'')
self.subInstrument.add((_instrumentid,_exchangeid))
self.subedMaster[_instrumentid] = 1
self.tickpass.add(_instrumentid)
_only.add(_product)
for _productid in ['IF','IH','IC']:
if _productid in self.dictProduct and _productid not in _only:
#.........這裏部分代碼省略.........
示例2: __init__
# 需要導入模塊: from eventEngine import EventEngine [as 別名]
# 或者: from eventEngine.EventEngine import start [as 別名]
class MainEngine:
"""主引擎,負責對API的調度"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.ee = EventEngine() # 創建事件驅動引擎
self.md = DemoMdApi(self.ee) # 創建API接口
self.td = DemoTdApi(self.ee)
self.ee.start() # 啟動事件驅動引擎
# 循環查詢持倉和賬戶相關
self.countGet = 0 # 查詢延時計數
self.lastGet = 'Account' # 上次查詢的性質
self.ee.register(EVENT_TDLOGIN, self.initGet) # 登錄成功後開始初始化查詢
# 合約儲存相關
self.dictInstrument = {} # 字典(保存合約查詢數據)
self.ee.register(EVENT_INSTRUMENT, self.insertInstrument)
#----------------------------------------------------------------------
def login(self, userid, password, brokerid, mdAddress, tdAddress):
"""登陸"""
self.md.login(mdAddress, userid, password, brokerid)
self.td.login(tdAddress, userid, password, brokerid)
#----------------------------------------------------------------------
def subscribe(self, instrumentid, exchangeid):
"""訂閱合約"""
self.md.subscribe(instrumentid, exchangeid)
#----------------------------------------------------------------------
def getAccount(self):
"""查詢賬戶"""
self.td.getAccount()
#----------------------------------------------------------------------
def getInvestor(self):
"""查詢投資者"""
self.td.getInvestor()
#----------------------------------------------------------------------
def getPosition(self):
"""查詢持倉"""
self.td.getPosition()
#----------------------------------------------------------------------
def getInstrument(self):
"""獲取合約"""
event = Event(type_=EVENT_LOG)
log = u'查詢合約信息'
event.dict_['log'] = log
self.ee.put(event)
self.td.getInstrument()
#----------------------------------------------------------------------
def sendOrder(self, instrumentid, exchangeid, price, pricetype, volume, direction, offset):
"""發單"""
ref = self.td.sendOrder(instrumentid, exchangeid, price, pricetype, volume, direction, offset)
return str(ref)
#----------------------------------------------------------------------
def cancelOrder(self, instrumentid, exchangeid, orderref, frontid, sessionid):
"""撤單"""
self.td.cancelOrder(instrumentid, exchangeid, orderref, frontid, sessionid)
#----------------------------------------------------------------------
def getAccountPosition(self, event):
"""循環查詢賬戶和持倉"""
self.countGet = self.countGet + 1
# 每5秒發一次查詢
if self.countGet > 5:
self.countGet = 0 # 清空計數
if self.lastGet == 'Account':
self.getPosition()
self.lastGet = 'Position'
else:
self.getAccount()
self.lastGet = 'Account'
#----------------------------------------------------------------------
def initGet(self, event):
"""在交易服務器登錄成功後,開始初始化查詢"""
# 打開設定文件setting.vn
f = shelve.open('setting.vn')
# 嘗試讀取設定字典,若該字典不存在,則發出查詢請求
try:
d = f['instrument']
# 如果本地保存的合約數據是今日的,則載入,否則發出查詢請求
today = date.today()
if d['date'] == today:
self.dictInstrument = d['dictInstrument']
#.........這裏部分代碼省略.........
示例3: __init__
# 需要導入模塊: from eventEngine import EventEngine [as 別名]
# 或者: from eventEngine.EventEngine import start [as 別名]
class MainEngine:
"""主引擎,負責對API的調度"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor
:type self: object
"""
self.ee = EventEngine() # 創建事件驅動引擎
self.md = DemoMdApi(self.ee) # 創建API接口
self.td = DemoTdApi(self.ee)
self.ee.start() # 啟動事件驅動引擎
self.havedposi = False
self.position = {}
self.todayposition = {}
self.__orders = {}
self.__retry = 0
# 循環查詢持倉和賬戶相關
self.countGet = 0 # 查詢延時計數
self.lastGet = 'Account' # 上次查詢的性質
self.ee.register(EVENT_TDLOGIN, self.initGet) # 登錄成功後開始初始化查詢
# 合約儲存相關
self.dictInstrument = {} # 字典(保存合約查詢數據)
self.ee.register(EVENT_INSTRUMENT, self.insertInstrument)
self.ee.register(EVENT_TRADE_DATA, self.get_trade)
self.ee.register(EVENT_ORDER_DATA, self.get_order)
self.ee.register(EVENT_ERROR, self.get_error)
self.ee.register(EVENT_MARKETDATA_DATA, self.get_data)
self.ee.register(EVENT_POSITION_DATA, self.get_position)
def set_app(self,_app):self.app = _app
def get_order(self,event):
_data = event.dict_['data']
if _data['OrderStatus'] == '5':
self.__retry += 1
_saved = self.__orders.pop(int(_data['OrderRef']))
if self.__retry>5:
self.__retry = 0
return 0
print("Plus...Order")
if _saved[6] == defineDict['THOST_FTDC_OF_Open']:
_tr = 1
elif _saved[6] == defineDict['THOST_FTDC_OF_Close']:
_tr = -1
else:
_tr = 0
if _saved[5] == defineDict["THOST_FTDC_D_Buy"]:
_kr = 1
elif _saved[5] == defineDict["THOST_FTDC_D_Sell"]:
_kr = -1
else:
_kr = 0
if _tr*_kr>0:
price = float(_saved[2])+0.2
else:
price = float(_saved[2])-0.2
_ref = self.td.sendOrder(_saved[0],_saved[1],price,_saved[3],_saved[4],_saved[5],_saved[6])
self.__orders[_ref] = (_saved[0],_saved[1],price,_saved[3],_saved[4],_saved[5],_saved[6])
def get_trade(self,event):
_data = event.dict_['data']
print('get_trade',_data['OrderRef'])
_done = _data['Volume']
if int(_data['OrderRef']) in self.__orders:
_saved = self.__orders.pop(int(_data['OrderRef']))
_goon = _saved[4] - _done
else:
_goon = 0
if _goon != 0:
self.__retry += 1
if self.__retry>5:
self.__retry = 0
return 0
print("Plus...Trade")
if _saved[6] == defineDict['THOST_FTDC_OF_Open']:
_tr = 1
elif _saved[6] == defineDict['THOST_FTDC_OF_Close']:
_tr = -1
else:
_tr = 0
if _saved[5] == defineDict["THOST_FTDC_D_Buy"]:
_kr = 1
elif _saved[5] == defineDict["THOST_FTDC_D_Sell"]:
_kr = -1
else:
_kr = 0
if _tr*_kr>0:
price = float(_saved[2])+0.2
else:
price = float(_saved[2])-0.2
_ref = self.td.sendOrder(_saved[0],_saved[1],price,_saved[3],_goon,_saved[5],_saved[6])
self.__orders[_ref] = (_saved[0],_saved[1],price,_saved[3],_goon,_saved[5],_saved[6])
def set_symbol(self,_s):
self.symbol = _s
def set_socket(self,_s):
self.socket = _s
#.........這裏部分代碼省略.........
示例4: __init__
# 需要導入模塊: from eventEngine import EventEngine [as 別名]
# 或者: from eventEngine.EventEngine import start [as 別名]
class MainEngine:
"""主引擎,負責對API的調度"""
#----------------------------------------------------------------------
def __init__(self, ws, account, _plus_path, justCopySignal=False, useZmq = False, zmqServer = "tcp://localhost:9999"):
"""Constructor
:type self: object
"""
self.ee = EventEngine(account) # 創建事件驅動引擎
self.justCopySignal = justCopySignal
self.userid = str(account['userid'])
self.password = str(account['password'])
self.brokerid = str(account['brokerid'])
self.mdaddress = str(account['mdfront'])
self.tdaddress = str(account['tdfront'])
self.pluspath = _plus_path
self.symbol = None
self.socket = None
self.websocket = ws # websocket list to send msg
if useZmq:
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(zmqServer)
self.socket = socket
self.ee.start() # 啟動事件驅動引擎
self.havedposi = False
self.position = {}
self.todayposition = {}
self.lastError = 0
self.lastTodo = 0
self.__timer = time()+60
self.__orders = {}
self.__retry = 0
self.__maxRetry = 5
# 循環查詢持倉和賬戶相關
self.countGet = 0 # 查詢延時計數
self.lastGet = 'Account' # 上次查詢的性質
self.ee.register(EVENT_TDLOGIN, self.initGet) # 登錄成功後開始初始化查詢
# 合約儲存相關
self.dictInstrument = {} # 字典(保存合約查詢數據)
self.dictProduct = {} # 字典(保存合約查詢數據)
self.dictExchange= {}
self.volInstrument = {}
self.subInstrument = set()
self.subedInst = set()
self.price = {} # 存儲報價,分品種
self.todo = 0
self.ee.register(EVENT_ERROR, self.get_error)
self.ee.register(EVENT_INSTRUMENT, self.insertInstrument)
self.ee.register(EVENT_TIMER, self.getAccountPosition)
self.ee.register(EVENT_TRADE, self.get_trade)
self.ee.register(EVENT_ORDER, self.get_order)
self.ee.register(EVENT_TICK, self.get_tick)
self.ee.register(EVENT_POSITION, self.get_position)
import eventType
for k,v in eventType.__dict__.items():
if 'EVENT_' in k and v[0]!='_':
self.ee.register(v,self.websocket_send)
self.md = DemoMdApi(self.ee, self.mdaddress, self.userid, self.password, self.brokerid,plus_path=_plus_path) # 創建API接口
self.td = DemoTdApi(self.ee, self.tdaddress, self.userid, self.password, self.brokerid,plus_path=_plus_path)
def set_ws(self,ws):
self.websocket = ws
def websocket_send(self,event):
try:
_data = json.dumps(event.dict_,ensure_ascii=False)
for _ws in self.websocket:
try:
_ws.send(_data)
except Exception,e:
print(_data,e)
except Exception,e:
print(event.dict_,e)
示例5: __init__
# 需要導入模塊: from eventEngine import EventEngine [as 別名]
# 或者: from eventEngine.EventEngine import start [as 別名]
class MainEngine:
"""主引擎,負責對API的調度"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.ee = EventEngine() # 創建事件驅動引擎
self.dsa = DemoStockApi(('192.168.16.59', 9991), self.ee) # 現貨報單
self.dma = DemoMarketApi(('10.10.1.186', 9101), self.ee) # 現貨報單
# self.md = DemoL2Api(self.ee) # 如果使用L2行情就改為這行
# self.td = DemoTdApi(self.ee)
self.ee.start() # 啟動事件驅動引擎
# 循環查詢持倉和賬戶相關
self.countGet = 0 # 查詢延時計數
self.lastGet = 'Account' # 上次查詢的性質
# self.ee.register(EVENT_TDLOGIN, self.initGet) # 登錄成功後開始初始化查詢
# 合約儲存相關
self.dictInstrument = {} # 字典(保存合約查詢數據)
# self.ee.register(EVENT_INSTRUMENT, self.insertInstrument)
#----------------------------------------------------------------------
def login(self, userid, mdPassword, tdPassword, brokerid, mdAddress, tdAddress):
"""登陸"""
self.dsa.login_req()
# self.md.login(mdAddress, userid, mdPassword, brokerid)
# self.td.login(tdAddress, userid, tdPassword, brokerid)
#----------------------------------------------------------------------
def subscribe(self, instrumentid, exchangeid):
"""訂閱合約"""
# self.md.subscribe(instrumentid, exchangeid)
#----------------------------------------------------------------------
def getAccount(self):
"""查詢賬戶"""
# self.td.getAccount()
#----------------------------------------------------------------------
def getInvestor(self):
"""查詢投資者"""
# self.td.getInvestor()
#----------------------------------------------------------------------
def getPosition(self):
"""查詢持倉"""
# self.td.getPosition()
#----------------------------------------------------------------------
def getInstrument(self):
"""獲取合約"""
# event = Event(type_=EVENT_LOG)
# log = u'查詢合約信息'
# event.dict_['log'] = log
# self.ee.put(event)
# self.td.getInstrument()
#----------------------------------------------------------------------
def sendOrder(self, instrumentid, exchangeid, price, pricetype, volume, direction, offset):
"""發單"""
# self.td.sendOrder(instrumentid, exchangeid, price, pricetype, volume, direction, offset)
#----------------------------------------------------------------------
def cancelOrder(self, instrumentid, exchangeid, orderref, frontid, sessionid):
"""撤單"""
# self.td.cancelOrder(instrumentid, exchangeid, orderref, frontid, sessionid)
#----------------------------------------------------------------------
def getAccountPosition(self, event):
"""循環查詢賬戶和持倉"""
# self.countGet = self.countGet + 1
#
# # 每5秒發一次查詢
# if self.countGet > 5:
# self.countGet = 0 # 清空計數
#
# if self.lastGet == 'Account':
# self.getPosition()
# self.lastGet = 'Position'
# else:
# self.getAccount()
# self.lastGet = 'Account'
#----------------------------------------------------------------------
def initGet(self, event):
"""在交易服務器登錄成功後,開始初始化查詢"""
# 打開設定文件setting.vn
# f = shelve.open('setting.vn')
#
# # 嘗試讀取設定字典,若該字典不存在,則發出查詢請求
# try:
# d = f['instrument']
#
# # 如果本地保存的合約數據是今日的,則載入,否則發出查詢請求
#.........這裏部分代碼省略.........