Skip to content

Commit

Permalink
Merge pull request ctpbee#151 from ctpbee/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
somewheve authored Jun 25, 2021
2 parents 09ba906 + 04bcafb commit b03a16e
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 649 deletions.
2 changes: 1 addition & 1 deletion ctpbee/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
for the future of life
"""
__version__ = '1.4.2'
__version__ = '1.4.3'
__status__ = 'release level'

# Here are pre import
Expand Down
4 changes: 1 addition & 3 deletions ctpbee/interface/ctp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from .md_api import BeeMdApi
from .md_api import BeeMdApiApp
from .td_api import BeeTdApi
from .td_api import BeeTdApiApp

__all__ = [BeeMdApiApp, BeeMdApi, BeeTdApi, BeeTdApiApp]
__all__ = [BeeMdApi, BeeTdApi]
173 changes: 0 additions & 173 deletions ctpbee/interface/ctp/md_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,176 +195,3 @@ def close(self):
"""
if self.connect_status:
self.exit()


class BeeMdApiApp(MdApiApp):
""""""

def __init__(self, app_signal):
"""Constructor"""
super(BeeMdApiApp, self).__init__()

self.gateway_name = "ctp"

self.reqid = 0
self.app_signal = app_signal
self.connect_status = False
self.login_status = False
self.subscribed = set()

self.userid = ""
self.password = ""
self.brokerid = 0

@property
def md_status(self):
return self.login_status

def on_event(self, type, data):
if type == EVENT_TICK:
event = Event(type=type, data=data)
signal = getattr(ctpbee.signals.common_signals, f"{type}_signal")
signal.send(event)
else:
event = Event(type=type, data=data)
signal = getattr(self.app_signal, f"{type}_signal")
signal.send(event)

def onFrontConnected(self):
"""
Callback when front server is connected.
"""
self.connect_status = True
self.on_event(type=EVENT_LOG, data="行情服务器连接成功")
self.login()

def onFrontDisconnected(self, reason: int):
"""
Callback when front server is disconnected.
"""
self.connect_status = False
self.login_status = False
self.on_event(type=EVENT_LOG, data=f"行情连接断开,原因{reason}")

def onRspUserLogin(self, data: dict, error: dict, reqid: int, last: bool):
"""
Callback when user is logged in.
"""
if not error["ErrorID"]:
self.login_status = True
self.on_event(type=EVENT_LOG, data="行情服务器登录成功")

for symbol in self.subscribed:
self.subscribeMarketData(symbol)
else:
error["detail"] = "行情登录失败"
self.on_event(type=EVENT_ERROR, data=error)

def onRspError(self, error: dict, reqid: int, last: bool):
"""
Callback when error occured.
"""
error['detail'] = "行情接口报错"
self.on_event(type=EVENT_ERROR, data=error)

def onRspSubMarketData(self, data: dict, error: dict, reqid: int, last: bool):
""""""
if not error or not error["ErrorID"]:
return
error['detail'] = "行情订阅失败"
self.on_event(type=EVENT_ERROR, data=error)

def onRtnDepthMarketData(self, data: dict):
"""
Callback of tick data update.
"""
symbol = data["InstrumentID"]
exchange = symbol_exchange_map.get(symbol, "")
if not exchange:
return

timestamp = f"{data['ActionDay']} {data['UpdateTime']}.{int(data['UpdateMillisec'] / 100)}"
try:
datetimed = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")
except ValueError as e:
datetimed = datetime.strptime(str(date.today()) + " " + timestamp, "%Y-%m-%d %H:%M:%S.%f")

tick = TickData(
symbol=symbol,
exchange=exchange,
datetime=datetimed,
name=symbol_name_map[symbol],
volume=data["Volume"],
last_price=data["LastPrice"],
limit_up=data["UpperLimitPrice"],
limit_down=data["LowerLimitPrice"],
open_interest=data['OpenInterest'],
open_price=data["OpenPrice"],
high_price=data["HighestPrice"],
low_price=data["LowestPrice"],
pre_close=data["PreClosePrice"],
bid_price_1=data["BidPrice1"],
ask_price_1=data["AskPrice1"],
bid_volume_1=data["BidVolume1"],
ask_volume_1=data["AskVolume1"],
average_price=data['AveragePrice'],
pre_settlement_price=data['PreSettlementPrice'],
gateway_name=self.gateway_name
)
self.on_event(type=EVENT_TICK, data=tick)

def connect(self, info: dict):
"""
Start connection to server.
"""
self.userid = info['userid']
self.password = info['password']
self.brokerid = info['brokerid']

# If not connected, then start connection first.
if not self.connect_status:
path = get_folder_path(self.gateway_name.lower() + f"/{self.userid}")
self.createFtdcMdApi(str(path) + "\\Md")
self.registerFront(info['md_address'])
self.init()
# If already connected, then login immediately.
elif not self.login_status:
self.login()

def login(self):
"""
Login onto server.
"""
req = {
"UserID": self.userid,
"Password": self.password,
"BrokerID": self.brokerid
}

self.reqid += 1
self.reqUserLogin(req, self.reqid)

def subscribe(self, symbol):
"""
Subscribe to tick data update.
"""
result = None
if self.login_status:
result = self.subscribeMarketData(symbol)
self.subscribed.add(symbol)
return result

def unsubscribe(self, symbol):
result = None
if self.login_status and symbol in self.subscribed:
result = self.unSubscribeMarketData(symbol)
if symbol in self.subscribed:
self.subscribed.remove(symbol)
return result

def close(self):
"""
Close the connection.
"""
if self.connect_status:
self.exit()
Loading

0 comments on commit b03a16e

Please sign in to comment.