當前位置: 首頁>>代碼示例>>Python>>正文


Python EventEngine.set_ws方法代碼示例

本文整理匯總了Python中eventEngine.EventEngine.set_ws方法的典型用法代碼示例。如果您正苦於以下問題:Python EventEngine.set_ws方法的具體用法?Python EventEngine.set_ws怎麽用?Python EventEngine.set_ws使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在eventEngine.EventEngine的用法示例。


在下文中一共展示了EventEngine.set_ws方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from eventEngine import EventEngine [as 別名]
# 或者: from eventEngine.EventEngine import set_ws [as 別名]
class MainEngine:
    """主引擎,負責對API的調度"""

    #----------------------------------------------------------------------
    def __init__(self, ws, account, justCopySignal=False):
        """Constructor
        :type self: object
        """
        self.ee = EventEngine(account,ws)         # 創建事件驅動引擎
        self.justCopySignal = justCopySignal

        self.userid = account['userid']
        self.password = account['password']
        self.brokerid = account['brokerid']
        self.mdaddress = account['mdAddress']
        self.tdaddress = account['tdAddress']

        self.md = DemoMdApi(self.ee, self.mdaddress, self.userid, self.password, self.brokerid)    # 創建API接口
        self.td = DemoTdApi(self.ee, self.tdaddress, self.userid, self.password, self.brokerid)

        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_ws(self,ws):self.ee.set_ws(ws)
    def get_order(self,event):
        _data = event.dict_['data']
        if _data['OrderStatus'] == '5':
            self.__retry += 1
            if int(_data['OrderRef']) in self.__orders:
                _saved = self.__orders.pop(int(_data['OrderRef']))
            else:
                self.__orders = {}
                return 0
            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:
#.........這裏部分代碼省略.........
開發者ID:wangdkchina,項目名稱:web_ctp,代碼行數:103,代碼來源:demoEngine.py


注:本文中的eventEngine.EventEngine.set_ws方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。