本文整理匯總了Python中swigibpy.EPosixClientSocket.isConnected方法的典型用法代碼示例。如果您正苦於以下問題:Python EPosixClientSocket.isConnected方法的具體用法?Python EPosixClientSocket.isConnected怎麽用?Python EPosixClientSocket.isConnected使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類swigibpy.EPosixClientSocket
的用法示例。
在下文中一共展示了EPosixClientSocket.isConnected方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: IBBroker
# 需要導入模塊: from swigibpy import EPosixClientSocket [as 別名]
# 或者: from swigibpy.EPosixClientSocket import isConnected [as 別名]
class IBBroker(Broker):
cid = 0 # connection id
oid = 0 # order id
tid = 0 # tick id (for fetching quotes)
tws = None # Trader WorkStation
wrapper = None # instance of EWrapper
sid_to_tid = {} # map of security id to tick id
def __init__(self, wrapper=None):
Broker.__init__(self)
# initialize a default wrapper
if wrapper:
self.wrapper = wrapper
else:
self.wrapper = WrapperDefault()
# initialize the wrapper's portfolio object
self.wrapper.portfolio = IBPortfolio()
# get next order id
def get_next_oid(self):
IBBroker.oid += 1
return IBBroker.oid
# get next connection id
def get_next_cid(self):
IBBroker.cid += 1
return IBBroker.cid
# get next tick request id (for getting quotes)
def get_next_tid(self):
self.tid += 1
return self.tid
# connect to TWS
def connect(self, port=7496):
if self.is_connected():
self.disconnect()
cid = self.get_next_cid()
self.tws = EPosixClientSocket(self.wrapper)
self.tws.eConnect('', port, cid)
# disconnect from TWS
def disconnect(self):
self.tws.eDisconnect()
# check if TWS is connected
def is_connected(self):
if self.tws is None:
return False
return self.tws.isConnected()
# Convert Zipline order signs into IB action strings
def order_action(self, iSign):
if iSign > 0:
return 'BUY'
elif iSign < 0:
return 'SELL'
raise Exception('Order of zero shares has no IB side: %i' % iSign)
# get an IB contract by ticker
def get_contract_by_sid(self, sid):
contract = Contract()
contract.symbol = sid
contract.secType = 'STK'
contract.exchange = 'SMART'
contract.currency = 'USD'
return contract
# get a default IB market order
def get_market_order(self, sid, amt):
order = Order();
order.action = self.order_action(amt)
order.totalQuantity = abs(amt)
order.orderType = 'MKT'
order.tif = 'DAY'
order.outsideRth = False
return order
# get a default IB limit order
def get_limit_order(self, sid, amt, lmtPrice):
order = Order();
order.action = self.order_action(amt)
order.totalQuantity = abs(amt)
order.orderType = 'LMT'
order.tif = 'DAY'
order.outsideRth = False
order.lmtPrice = lmtPrice
return order
# send the IB (contract, order) order to TWS
def place_order(self, contract, order):
oid = self.get_next_oid()
self.tws.placeOrder(oid, contract, order)
return oid
# send order with Zipline style order arguments
# <TODO> stop_price is not implemented
#.........這裏部分代碼省略.........