Пример #1
0
def get_contract(symbol: str, date, date_roll=False):
    contract = Contract()
    contract.symbol = symbol
    #contract.symbol = "ES"
    # contract.secType = 'CONTFUT'
    # contract.secType = 'FUT+CONTFUT'
    contract.secType = "FUT"
    contract.lastTradeDateOrContractMonth = determine_expiry(
        pd.to_datetime(date), date_roll)
    print(contract.lastTradeDateOrContractMonth)
    # contract.lastTradeDateOrContractMonth = "201803"
    contract.exchange = "GLOBEX"
    contract.currency = "USD"
    contract.includeExpired = 1
    return contract
Пример #2
0
    def from_string(cls, contract_str: str) -> "IBContract":
        """ Create IBContract object from the corresponding string. """

        params, combo_legs = contract_str.split("combo:")
        ib_contract = Contract()
        [ib_contract.conId, ib_contract.symbol, ib_contract.secType, ib_contract.lastTradeDateOrContractMonth,
         ib_contract.strike, ib_contract.right, ib_contract.multiplier, ib_contract.exchange,
         ib_contract.primaryExchange, ib_contract.currency, ib_contract.localSymbol, ib_contract.tradingClass,
         ib_contract.includeExpired, ib_contract.secIdType, ib_contract.secId] = params.split(",")

        ib_contract.conId = int(ib_contract.conId)
        ib_contract.strike = float(ib_contract.strike)
        ib_contract.includeExpired = bool(ib_contract.includeExpired == "True")

        combo_legs = combo_legs.split(";")
        combo_legs = [c for c in combo_legs if len(c) > 0]

        if len(combo_legs) > 0:
            if len(combo_legs[-1].split(",")) == 3:
                delta_neutral_contract = combo_legs[-1].split(",")
                combo_legs = combo_legs[:-1]

                ib_contract.deltaNeutralContract = DeltaNeutralContract()
                ib_contract.deltaNeutralContract.conId = int(delta_neutral_contract[0])
                ib_contract.deltaNeutralContract.delta = float(delta_neutral_contract[1])
                ib_contract.deltaNeutralContract.price = float(delta_neutral_contract[2])

            ib_contract.comboLegs = [] if len(combo_legs) > 0 else None
            if ib_contract.comboLegs is not None:
                for params in combo_legs:
                    params = params.split(",")
                    combo_leg = ComboLeg()
                    combo_leg.conId = int(params[0])
                    combo_leg.ratio = int(params[1])
                    combo_leg.action = params[2]
                    combo_leg.exchange = params[3]
                    combo_leg.openClose = int(params[4])
                    combo_leg.shortSaleSlot = int(params[5])
                    combo_leg.designatedLocation = params[6]
                    combo_leg.exemptCode = int(params[7])
                    ib_contract.comboLegs.append(combo_leg)

        return cls.from_ib_contract(ib_contract)
Пример #3
0
    def start(self):

        items = [('VIX', 'IND', 'CBOE', '', '', 'VIX')]

        nxt = self.__start
        while nxt < self.__end:
            contract = self.GetContract(nxt)
            items.append(contract)
            nxt = nxt + relativedelta(months=1)

        for sym, typ, exch, tc, exp, loc in items:

            validated = Contract()
            validated.symbol = sym
            validated.secType = typ
            validated.exchange = exch
            validated.tradingClass = tc
            validated.lastTradeDateOrContractMonth = exp
            if typ == 'FUT':
                validated.includeExpired = True
            validated.localSymbol = loc

            hId = self.nextReqId()
            self.historicalLookup[hId] = validated.localSymbol
            self.requestedHistoricalData[hId] = validated
            if exp != '':
                expiry = datetime.datetime.strptime(exp, '%Y%m%d')
                end = expiry.strftime('%Y%m%d %H:%M:%S')
                duration = "30 D"
            else:
                end = self.__end.strftime('%Y%m%d %H:%M:%S')
                duration = "%s M" % self.months
            self.Logger.info(
                'ReqId: %s. Requesting Historical %s %s %s %s %s %s' %
                (hId, sym, typ, exch, tc, exp, loc))
            self.reqHistoricalData(hId, validated, end, duration, "1 day",
                                   "TRADES", 1, 1, False, list("XYZ"))
Пример #4
0
    @iswrapper
    def cancelHistoricalData(self, reqId: TickerId):
        """
        msgHandler must >>
        """
        super().cancelHistoricalData(reqId)


if __name__ == "__main__":
    # Create a new contract object for the E-mini NASDAQ-100
    contract = Contract()
    contract.symbol = "ES"
    contract.secType = "FUT"
    contract.exchange = "GLOBEX"
    contract.currency = "USD"
    contract.includeExpired = True

    # contract.symbol = "SPY"
    # contract.secType = "STK"
    # contract.exchange = "SMART"
    # # contract.primaryExchange = "ARCA"
    # contract.currency = "USD"

    # paper = True
    port = 7496
    # if paper == True:
    # port = 7497

    tApp = IBAPI('127.0.0.1', port, 0)
    timePassed = 0
    while not (tApp.isConnected()):