Skip to content

Commit

Permalink
support buy/sell returned with broker entrust ID
Browse files Browse the repository at this point in the history
  • Loading branch information
moyuanz committed Jan 21, 2019
1 parent 6cbce74 commit e7ed327
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
6 changes: 5 additions & 1 deletion EventEngine/DyEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ class DyEventType:
stockPositionUpdate = 'eStockPositionUpdate_' # 账户持仓更新事件 + broker
stockCurEntrustsUpdate = 'eStockCurEntrustsUpdate_' # 账户当日委托更新事件 + broker
stockCurDealsUpdate = 'eStockCurDealsUpdate_' # 账户当日成交更新事件 + broker
stockEntrustUpdate = 'eStockEntrustUpdate_' # 账户单个委托更新事件 + broker, 如果网络或者其他什么原因,可能会导致委托无法挂到券商,这时用此事件通知
stockEntrustUpdate = 'eStockEntrustUpdate_' # 账户单个委托更新事件 + broker,
# 如果网络或者其他什么原因,可能会导致委托无法挂到券商,这时用此事件通知。
# 更新如下参数:
# 委托状态
# 券商委托号

# 主要为了UI的更新
stockCapitalTickUpdate = 'eStockCapitalTickUpdate_' # 账户资金状况每Tick更新事件 + broker(English name)
Expand Down
23 changes: 17 additions & 6 deletions Stock/Trade/AccountManager/DyStockAccountManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ def wrapper(self, *args, **kwargs):

@curWorkingCancelEntrustsWrapper
def _stockEntrustUpdateHandler(self, event):
"""
update below parameters for one entrust from broker:
- status
- brokerEntrustId
"""
updatedEntrust = event.data

entrusts = self._curEntrusts.get(updatedEntrust.code)
Expand All @@ -480,15 +485,21 @@ def _stockEntrustUpdateHandler(self, event):
if entrust.dyEntrustId != updatedEntrust.dyEntrustId:
continue

if entrust.status == updatedEntrust.status:
break
isUpdated = False
if entrust.status != updatedEntrust.status:
entrust.status = updatedEntrust.status
isUpdated = True

entrust.status = updatedEntrust.status
if entrust.brokerEntrustId != updatedEntrust.brokerEntrustId:
entrust.brokerEntrustId = updatedEntrust.brokerEntrustId
isUpdated = True

event = DyEvent(DyEventType.stockOnEntrust)
event.data = [copy.copy(entrust)]
if isUpdated: # @isUpdated should be True
event = DyEvent(DyEventType.stockOnEntrust)
event.data = [copy.copy(entrust)]

self._eventEngine.put(event)

self._eventEngine.put(event)
break

def _stockCapitalUpdateHandler(self, event):
Expand Down
37 changes: 35 additions & 2 deletions Stock/Trade/Broker/DyTrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,23 @@ def _unregisterEvent(self):

self._eventEngine.unregister(DyEventType.stockMarketTicks, self._stockMarketTicksHandler, DyStockTradeEventHandType.brokerEngine)

def _updateEntrustWithBrokerEntrustId(self, entrust, brokerEntrustId):
entrust = copy.copy(entrust)
entrust.brokerEntrustId = brokerEntrustId

event = DyEvent(DyEventType.stockEntrustUpdate + self.broker)
event.data = entrust

self._eventEngine.put(event)

def _stockBuyHandler(self, event):
entrust = event.data

if self.buy(entrust.code, entrust.name, entrust.price, entrust.totalVolume):
ret = self.buy(entrust.code, entrust.name, entrust.price, entrust.totalVolume)
if ret: # success
if ret is not True: # success with broker entrust ID
self._updateEntrustWithBrokerEntrustId(entrust, ret)

self._postfixEntrustAction()
else:
self._discardEntrust(entrust)
Expand All @@ -220,7 +233,11 @@ def _stockBuyHandler(self, event):
def _stockSellHandler(self, event):
entrust = event.data

if self.sell(entrust.code, entrust.name, entrust.price, entrust.totalVolume):
ret = self.sell(entrust.code, entrust.name, entrust.price, entrust.totalVolume)
if ret: # success
if ret is not True: # success with broker entrust ID
self._updateEntrustWithBrokerEntrustId(entrust, ret)

self._postfixEntrustAction()
else:
self._discardEntrust(entrust)
Expand Down Expand Up @@ -535,3 +552,19 @@ def _compareEntrusts(self, entrusts, newEntrusts):
allDone = False

return stateChange, allDone

def buy(self, code, name, price, volume):
"""
@return: True - success without broker entrust ID
False - failed
broker entrust ID - success with broker entrust ID
"""
raise NotImplementedError

def sell(self, code, name, price, volume):
"""
@return: True - success without broker entrust ID
False - failed
broker entrust ID - success with broker entrust ID
"""
raise NotImplementedError

0 comments on commit e7ed327

Please sign in to comment.