Ejemplo n.º 1
0
    def _get_fx_prices_with_ib_config(
            self, currency_code: str,
            ib_config_for_code: ibFXConfig) -> fxPrices:
        raw_fx_prices_as_series = self._get_raw_fx_prices(ib_config_for_code)

        if len(raw_fx_prices_as_series) == 0:
            self.log.warn(
                "No available IB prices for %s %s" %
                (currency_code, str(ib_config_for_code)),
                fx_code=currency_code,
            )
            return fxPrices.create_empty()

        if ib_config_for_code.invert:
            raw_fx_prices = 1.0 / raw_fx_prices_as_series
        else:
            raw_fx_prices = raw_fx_prices_as_series

        # turn into a fxPrices
        fx_prices = fxPrices(raw_fx_prices)

        self.log.msg("Downloaded %d prices" % len(fx_prices),
                     fx_code=currency_code)

        return fx_prices
Ejemplo n.º 2
0
    def _get_fx_prices(self, code: str) -> fxPrices:
        if not self.is_code_in_data(code):
            self.log.warn("Currency %s is missing from list of FX data" % code)

            return fxPrices.create_empty()

        data = self._get_fx_prices_without_checking(code)

        return data
Ejemplo n.º 3
0
    def _get_fx_prices_without_checking(self, currency_code: str) -> fxPrices:
        ib_config_for_code = self._get_config_info_for_code(currency_code)
        if ib_config_for_code is missing_instrument:
            self.warn("Can't get prices as missing IB config for %s" %
                      currency_code,
                      fx_code=currency_code)
            return fxPrices.create_empty()

        data = self._get_fx_prices_with_ib_config(currency_code,
                                                  ib_config_for_code)

        return data
    def _get_fx_prices_without_checking(self, currency_code):
        qcode = self._get_qcode(currency_code)
        try:
            fx_prices = quandl.get(qcode)
        except Exception as exception:
            self.log.warn("Can't get QUANDL data for %s error %s" %
                          (qcode, exception))
            return fxPrices.create_empty()

        fx_prices = fx_prices.Rate

        return fxPrices(fx_prices)
Ejemplo n.º 5
0
    def _get_fx_cross(self, fx_code: str) ->fxPrices:
        """
        Get a currency cross rate XXXYYY, eg not XXXUSD or USDXXX or XXXXXX

        :return: fxPrices
        """
        currency1, currency2 = get_fx_tuple_from_code(fx_code)
        currency1_vs_default = self._get_fx_prices_vs_default(currency1)
        currency2_vs_default = self._get_fx_prices_vs_default(currency2)

        if currency1_vs_default.empty or currency2_vs_default.empty:
            return fxPrices.create_empty()

        (aligned_c1, aligned_c2) = currency1_vs_default.align(
            currency2_vs_default, join="outer"
        )

        fx_rate_series = aligned_c1.ffill() / aligned_c2.ffill()

        return fx_rate_series
Ejemplo n.º 6
0
    def _get_fx_prices_without_checking(self, code: str) -> fxPrices:
        filename = self._filename_given_fx_code(code)
        config = self.config
        price_column = config.price_column
        date_column = config.date_column
        date_format = config.date_format

        try:
            fx_data = pd_readcsv(
                filename, date_format=date_format, date_index_name=date_column
            )
        except OSError:
            self.log.warn("Can't find currency price file %s" % filename, fx_code=code)
            return fxPrices.create_empty()

        fx_data = pd.Series(fx_data[price_column])

        fx_data = fxPrices(fx_data.sort_index())

        return fx_data