当前位置: 首页>>代码示例>>Python>>正文


Python EPosixClientSocket.isConnected方法代码示例

本文整理汇总了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
#.........这里部分代码省略.........
开发者ID:Coding4ufn,项目名称:abund.com,代码行数:103,代码来源:broker.py


注:本文中的swigibpy.EPosixClientSocket.isConnected方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。