代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Defines Sender class to handle outbound requests.
#
# Sender instances defer failed attribute lookup to their
# EClientSocket member objects.
#
##
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import time
from functools import wraps
from ib.ext.EClientSocket import EClientSocket
from ib.lib import toTypeName
from ib.opt.message import registry, clientSocketMethods
class Sender(object):
""" Encapsulates an EClientSocket instance, and proxies attribute
lookup to it.
"""
client = None
def __init__(self, dispatcher):
""" Initializer.
@param dispatcher message dispatcher instance
"""
self.dispatcher = dispatcher
self.clientMethodNames = [m[0] for m in clientSocketMethods]
# print("self.clientMethodNames = ", self.clientMethodNames)
def connect(self, host, port, clientId, handler, clientType=EClientSocket):
""" Creates a TWS client socket and connects it.
@param host name of host for connection; default is localhost
@param port port number for connection; default is 7496
@param clientId client identifier to send when connected
@param handler object to receive reader messages
@keyparam clientType=EClientSocket callable producing socket client
@return True if connected, False otherwise
"""
self.client = clientType(handler)
self.client.eConnect(host, port, clientId)
while (not self.client.isConnected()):
time.sleep(15)
print("connect to tws api, failed, try again, one seconds later")
self.reconnect(host, port, clientId)
print("connect to tws api success")
return True
def reconnect(self, host, port, clientId):
self.client.eConnect(host, port, clientId)
return self.client.isConnected()
def disconnect(self):
""" Disconnects the client.
@return True if disconnected, False otherwise
"""
client = self.client
if client and client.isConnected():
client.eDisconnect()
return not client.isConnected()
return False
def __getattr__(self, name):
""" x.__getattr__('name') <==> x.name
@return named attribute from EClientSocket object
"""
try:
value = getattr(self.client, name)
except (AttributeError, ):
raise
if name not in self.clientMethodNames:
return value
return value
preName, postName = name+'Pre', name+'Post'
preType, postType = registry[preName], registry[postName]
@wraps(value)
def wrapperMethod(*args):
mapping = dict(list(zip(preType.__slots__, args)))
results = self.dispatcher(preName, mapping)
if not all(results):
return # raise exception instead?
result = value(*args)
self.dispatcher(postName, mapping)
return result # or results?
return wrapperMethod
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。