Esempio n. 1
0
    def test_future_ticker_to_contract_mapping(self):
        """ Test mapping of future tickers onto IB contracts. """
        mapping = {
            BloombergFutureTicker('Copper', 'HG{} Comdty', 1, 1, 250):
            IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
        }
        contract_ticker_mapper = IBContractTickerMapper(
            mapping, self.data_provider)

        # Map a specific ticker to contract
        ticker = BloombergTicker("HGH20 Comdty", SecurityType.FUTURE, 250)
        actual_contract = contract_ticker_mapper.ticker_to_contract(ticker)
        expected_contract = IBContract("HG", SecurityType.FUTURE,
                                       "NYMEX", "25000", "USD",
                                       str_to_date("2020-03-20"))
        self.assertEqual(actual_contract, expected_contract)

        # Map a future ticker to contract
        future_ticker = Mock(spec=BloombergFutureTicker)
        future_ticker.get_current_specific_ticker.return_value = BloombergTicker(
            "HGN20 Comdty", SecurityType.FUTURE, 250)
        actual_contract = contract_ticker_mapper.ticker_to_contract(
            future_ticker)
        expected_contract = IBContract("HG", SecurityType.FUTURE,
                                       "NYMEX", "25000", "USD",
                                       str_to_date("2020-06-20"))
        self.assertEqual(actual_contract, expected_contract)
Esempio n. 2
0
    def test_duplicate_stock_tickers(self):
        bbg_ib_symbols_mapping = {
            BloombergTicker("Example Index"):
            IBContract("EXAMPLE", SecurityType.STOCK, "EXAMPLE_EXCHANGE"),
            BloombergTicker("Example 2 Index"):
            IBContract("EXAMPLE", SecurityType.STOCK, "EXAMPLE_EXCHANGE"),
        }

        with self.assertRaises(Exception):
            IBContractTickerMapper(bbg_ib_symbols_mapping)
Esempio n. 3
0
    def test_contract_ticker_mapping_multiple_tickers_matching_contract(self):
        """ Test  contract to ticker mapping in case if many tickers correspond to the same contract. """
        mapping = {
            BloombergFutureTicker('Copper1', 'HG{} Comdty', 1, 1, 250):
            IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
            BloombergFutureTicker('Copper2', 'HG{} Comdty', 1, 2, 250):
            IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
        }

        with self.assertRaises(AssertionError):
            IBContractTickerMapper(mapping, self.data_provider)
Esempio n. 4
0
 def test_invalid_contract_to_specific_future_ticker_mapping(self):
     """ Test futures contract to ticker mapping in case if the last trade date is invalid. """
     mapping = {
         BloombergFutureTicker('Copper', 'HG{} Comdty', 1, 1, 250):
         IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
     }
     contract_ticker_mapper = IBContractTickerMapper(
         mapping, self.data_provider)
     contract = IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000",
                           "USD", str_to_date("2020-06-25"))
     with self.assertRaises(ValueError):
         contract_ticker_mapper.contract_to_ticker(contract)
Esempio n. 5
0
    def test_futures_contract_to_ticker_invalid_last_trade_date(self):
        """ Test contract to ticker mapping, in case if the last trade date in the IBContract is invalid. """
        mapping = {
            BloombergTicker("PAH20 Comdty", SecurityType.FUTURE, 100):
            IBContract("PA", SecurityType.FUTURE, "NYMEX", "100", "",
                       str_to_date("2020-03-27"))
        }
        contract_ticker_mapper = IBContractTickerMapper(mapping)
        contract = IBContract("PA", SecurityType.FUTURE, "NYMEX", "100", "",
                              str_to_date("2020-03-10"))

        with self.assertRaises(ValueError):
            contract_ticker_mapper.contract_to_ticker(contract)
Esempio n. 6
0
 def test_contract_to_specific_future_ticker_mapping(self):
     mapping = {
         BloombergFutureTicker('Copper', 'HG{} Comdty', 1, 1, 250):
         IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
     }
     contract_ticker_mapper = IBContractTickerMapper(
         mapping, self.data_provider)
     contract = IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000",
                           "USD", str_to_date("2020-06-20"))
     actual_ticker = contract_ticker_mapper.contract_to_ticker(contract)
     expected_ticker = BloombergTicker("HGN20 Comdty", SecurityType.FUTURE,
                                       250)
     self.assertEqual(actual_ticker, expected_ticker)
Esempio n. 7
0
    def test_contract_ticker_mapping_many_future_tickers(self):
        """ Test futures contract to ticker mapping in case if many future tickers correspond to the given ticker. """
        mapping = {
            BloombergFutureTicker('Copper', 'HG{} Comdty', 1, 1, 250):
            IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
            BloombergFutureTicker('Copper', 'HG{} Comdty', 1, 17, 250):
            IBContract("HG", SecurityType.FUTURE, "NYMEX", "250", "USD"),
        }
        contract_ticker_mapper = IBContractTickerMapper(
            mapping, self.data_provider)

        with self.assertRaises(ValueError):
            ticker = BloombergTicker("HGH20 Comdty", SecurityType.FUTURE, 250)
            contract_ticker_mapper.ticker_to_contract(ticker)
Esempio n. 8
0
    def test_ticker_to_futures_contract_mapping(self):
        """ Test contract to ticker mapping, in case of a futures contract when the mapping between a specific futures
                ticker and an IBContract was provided. """
        mapping = {
            BloombergTicker("PAH20 Comdty", SecurityType.FUTURE, 100):
            IBContract("PA", SecurityType.FUTURE, "NYMEX", "100", "",
                       str_to_date("2020-03-27"))
        }
        contract_ticker_mapper = IBContractTickerMapper(mapping)
        ticker = BloombergTicker("PAH20 Comdty", SecurityType.FUTURE, 100)
        actual_contract = contract_ticker_mapper.ticker_to_contract(ticker)

        expected_contract = IBContract("PA", SecurityType.FUTURE, "NYMEX",
                                       "100", "", str_to_date("2020-03-27"))
        self.assertEqual(expected_contract, actual_contract)
Esempio n. 9
0
    def test_no_data_provider(self):
        fut_ticker = BloombergFutureTicker("Copper", "HG{} Comdty", 1, 4)

        bbg_ib_symbols_mapping = {
            fut_ticker: IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000")
        }
        with self.assertRaises(ValueError):
            contract_ticker_mapper = IBContractTickerMapper(
                bbg_ib_symbols_mapping, data_provider=None)
            contract_ticker_mapper.contract_to_ticker(
                IBContract("HG",
                           SecurityType.FUTURE,
                           "NYMEX",
                           "25000",
                           last_trade_date=datetime(2021, 9, 9)))
Esempio n. 10
0
 def test_contract_to_ticker_no_valid_mapping(self):
     """ Test contract to ticker mapping if no mapping for the ticker exists. """
     contract_ticker_mapper = IBContractTickerMapper({})
     contract = IBContract("PA", SecurityType.FUTURE, "NYMEX", "100", "",
                           str_to_date("2020-03-27"))
     with self.assertRaises(ValueError):
         contract_ticker_mapper.contract_to_ticker(contract)
Esempio n. 11
0
    def test_duplicate_future_tickers(self):
        future_ticker_1 = BloombergFutureTicker("Copper", "HG{} Comdty", 1, 4)
        future_ticker_2 = BloombergFutureTicker("Example", "Example{} Comdty",
                                                1, 4)

        bbg_ib_symbols_mapping = {
            future_ticker_1:
            IBContract("EXAMPLE", SecurityType.FUTURE, "DIFFERENT_EXCHANGE",
                       "25000"),
            future_ticker_2:
            IBContract("EXAMPLE", SecurityType.FUTURE, "EXAMPLE_EXCHANGE",
                       "25000"),
        }

        with self.assertRaises(Exception):
            IBContractTickerMapper(bbg_ib_symbols_mapping, self.data_provider)
Esempio n. 12
0
    def test_duplicate_future_and_equity_tickers(self):
        future_ticker_1 = BloombergFutureTicker("Copper", "HG{} Comdty", 1, 4)

        bbg_ib_symbols_mapping = {
            future_ticker_1:
            IBContract("EXAMPLE", SecurityType.FUTURE, "DIFFERENT_EXCHANGE",
                       "25000"),
            BloombergTicker("Example Index"):
            IBContract("EXAMPLE", SecurityType.STOCK, "EXAMPLE_EXCHANGE"),
        }

        contract_ticker_mapper = IBContractTickerMapper(
            bbg_ib_symbols_mapping, self.data_provider)
        contract = IBContract("EXAMPLE", SecurityType.STOCK,
                              "EXAMPLE_EXCHANGE")
        ticker = contract_ticker_mapper.contract_to_ticker(contract)
        self.assertEqual(ticker.as_string(), "Example Index")
def map_future_ticker_to_contract():
    mapping = {
        BloombergFutureTicker('Copper', 'HG{} Comdty', 1, 1, 250):
        IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD"),
        BloombergTicker("PAH20 Comdty", SecurityType.FUTURE, 100):
        IBContract("PA", SecurityType.FUTURE, "NYMEX", "100", "",
                   str_to_date("2020-03-27"))
    }
    data_provider = container.resolve(BloombergDataProvider)

    contract_ticker_mapper = IBContractTickerMapper(mapping, data_provider)
    current_time = str_to_date("2020-12-01")

    for future_ticker in mapping.keys():
        if isinstance(future_ticker, BloombergFutureTicker):
            future_ticker.initialize_data_provider(SettableTimer(current_time),
                                                   data_provider)

    print("\nMapping PAH20 Comdty ticker to IB contract")
    ticker = BloombergTicker("PAH20 Comdty", SecurityType.FUTURE, 100)
    contract = contract_ticker_mapper.ticker_to_contract(ticker)
    print(f"Ticker mapped onto the following contract: {contract}")

    print("\nMapping IBContract onto ticker")
    contract = IBContract("PA", SecurityType.FUTURE, "NYMEX", "100", "",
                          str_to_date("2020-03-27"))
    ticker = contract_ticker_mapper.contract_to_ticker(contract)
    print(f"Contract mapped onto the following ticker: {ticker}")

    print(
        "\nMapping HGH20 Comdty ticker - Copper, March 2020 futures contracts")
    ticker = BloombergTicker("HGH20 Comdty", SecurityType.FUTURE, 250)
    contract = contract_ticker_mapper.ticker_to_contract(ticker)
    print(f"Ticker mapped onto the following contract: {contract}")

    print("\nMapping IBContract onto ticker")
    contract = IBContract("HG", SecurityType.FUTURE, "NYMEX", "25000", "USD",
                          str_to_date("2021-01-27"))
    ticker = contract_ticker_mapper.contract_to_ticker(contract)
    print(f"Contract mapped onto the following ticker: {ticker}")