def test__compute_correctly_in_other_currencies(): okid10_usd = lib.portfolio_asset(name=__asset_name, end_period='2018-12', currency='usd') okid10_rub = lib.portfolio_asset(name=__asset_name, end_period='2018-12', currency='rub') okid10_currency_rate = okid10_usd.close() / okid10_rub.close() vs_rub = lib.portfolio_asset(name='cbr/RUB', start_period=okid10_currency_rate.start_period, end_period=okid10_currency_rate.end_period, currency='usd').close() np.testing.assert_almost_equal(okid10_currency_rate.values, vs_rub.values, decimal_places)
def test__currency_conversion_should_be_inversive(currency1: Currency, currency2: Currency): vs1 = lib.portfolio_asset(name='cbr/' + currency1.name, start_period='2015-1', end_period='2016-12', currency=currency2.name).close() vs2 = lib.portfolio_asset(name='cbr/' + currency2.name, start_period='2015-1', end_period='2016-12', currency=currency1.name).close() np.testing.assert_almost_equal(vs1.values * vs2.values, 1.)
def test__currency_should_not_be_converted_to_itself_inside_datatable( currency: Currency): vs = lib.portfolio_asset(name='cbr/' + currency.name, start_period='2015-1', end_period='2017-1', currency=currency.name).close() np.testing.assert_equal(vs.values, 1.)
def test__currency_should_be_converted_other_currency(): vs = lib.portfolio_asset(name='cbr/EUR', start_period='2015-1', end_period='2017-1', currency='USD').close() assert np.all(vs.values > 1.05)
def test__have_valid_selected_period_range(): start_period = pd.Period('2013-1', freq='M') end_period = pd.Period('2015-3', freq='M') okid10 = lib.portfolio_asset(name=__asset_name, start_period=str(start_period), end_period=str(end_period)) assert okid10.close().start_period == start_period assert okid10.close().end_period == end_period
def test__asset_should_be_converted_correctly(): vs_eur = lib.portfolio_asset(name='mut_ru/0890-94127385', start_period='2011-1', end_period='2017-2', currency='EUR').close() vs_usd = lib.portfolio_asset(name='mut_ru/0890-94127385', start_period='2011-1', end_period='2017-2', currency='USD').close() vs_curr = lib.portfolio_asset(name='cbr/USD', start_period='2011-1', end_period='2017-2', currency='EUR').close() np.testing.assert_almost_equal((vs_eur / vs_usd).values, vs_curr.values)
def test__us_data_source_values(): asset = lib.portfolio_asset(name='us/MSFT', start_period='2017-11', end_period='2018-2', currency='usd') period_range_expected = pd.period_range(start='2017-11', end='2017-12', freq='M') assert asset.close().period_range() == list(period_range_expected)
def test__support_all_types_of_currency_conversions(currency_from: Currency, currency_to: Currency): vs = lib.portfolio_asset(name='cbr/' + currency_from.name, start_period='2015-1', end_period='2016-12', currency=currency_to.name).close() assert vs.size == 2 * 12 assert np.all(vs.values > 0.)
def test__default_periods(): asset = lib.portfolio_asset(name='micex/SBER') assert asset.close().start_period >= pd.Period('1900-1', freq='M') assert pd.Period.now(freq='M') >= asset.close().end_period assert asset.currency.value == Currency.RUB portfolio = lib.portfolio(assets={'micex/SBER': 1.}, currency='rub') assert portfolio.get_return().start_period >= pd.Period('1900-1', freq='M') assert pd.Period.now(freq='M') >= portfolio.get_return().end_period
def test__cagr_should_be_full_when_it_has_period_equal_to_ror(): start_period = pd.Period('2011-01', freq='M') years_amount = 5 end_period = start_period + years_amount * 12 asset = lib.portfolio_asset(name=__asset_name, start_period=str(start_period), end_period=str(end_period), currency='usd') cagr1 = asset.cagr() assert_that(cagr1.value, close_to(-.1426, delta)) cagr2 = asset.cagr(years_ago=years_amount) assert_that(cagr2.value, close_to(cagr1.value, delta))
def test__risk(): short_asset = lib.portfolio_asset(name=__asset_name, start_period='2016-8', end_period='2016-12', currency='USD') assert_that( calling(short_asset.risk).with_args(period='year'), raises(Exception)) assert __asset.risk().kind == TimeSeriesKind.REDUCED_VALUE assert __asset.risk(period='year').kind == TimeSeriesKind.REDUCED_VALUE assert __asset.risk(period='month').kind == TimeSeriesKind.REDUCED_VALUE assert_that(__asset.risk().value, close_to(.2860, delta)) assert_that(__asset.risk(period='year').value, close_to(.2860, delta)) assert_that(__asset.risk(period='month').value, close_to(.0823, delta))
def test__get_cagr_invariants(): start_period = pd.Period('2009-3', freq='M') end_period = pd.Period('2019-3', freq='M') asset = lib.portfolio_asset(name=__asset_name, start_period=str(start_period), end_period=str(end_period), currency='rub') years_ago = 10 months_ago = years_ago * _MONTHS_PER_YEAR cagr = asset.cagr() cagr10 = asset.cagr(years_ago=years_ago) assert cagr.start_period == cagr10.start_period == start_period + 1 assert cagr.end_period == cagr10.end_period == end_period assert cagr.period_size == months_ago ror_c = asset.get_return(kind='cumulative') ror_from_cagr10 = (cagr10 + 1.)**years_ago - 1. ror_from_cagr = (cagr + 1.)**years_ago - 1. assert_that(ror_from_cagr.value, close_to(ror_c[-1].value, delta=delta)) assert_that(ror_from_cagr10.value, close_to(ror_c[-1].value, delta=delta))
def test__return_empty_list_or_none_if_symbol_doesnt_exist(): nonexisting_id = 'mut_ru/xxx' asset = lib.portfolio_asset(name=nonexisting_id) assert_that(asset, none()) assets = lib.portfolio_asset(names=[nonexisting_id]) assert_that(assets, empty()) assets = lib.portfolio_asset(names=['micex/FXRU', nonexisting_id]) assert_that(assets, has_length(1)) portfolio = lib.portfolio(assets={ 'micex/FXRU': 1., nonexisting_id: 1. }, currency='USD') assert len(portfolio.assets) == 1 nonexisting_namespace = 'yyy/FXRU' asset = lib.portfolio_asset(name=nonexisting_namespace) assert_that(asset, none()) assets = lib.portfolio_asset(names=[nonexisting_namespace]) assert_that(assets, empty()) assets = lib.portfolio_asset(names=['micex/FXRU', nonexisting_namespace]) assert_that(assets, has_length(1)) portfolio = lib.portfolio(assets={ 'micex/FXRU': 1., nonexisting_namespace: 1. }, currency='USD') assert_that(portfolio.assets, has_length(1))
def test__have_correct_values(): okid10 = lib.portfolio_asset(name=__asset_name, end_period='2018-12') np.testing.assert_almost_equal(okid10.close()[:5].values, [100., 100.9854, 101.9356, 102.8515, 103.7328], decimal_places) np.testing.assert_almost_equal(okid10.close()[-5:].values, [212.0694, 213.2737, 214.4767, 215.6832, 216.8961], decimal_places)
def test__have_valid_max_period_range(): okid10 = lib.portfolio_asset(name=__asset_name) cbr_top10 = lib.information(name='cbr/TOP_rates') assert okid10.close().start_period == cbr_top10.start_period + _MONTHS_PER_YEAR assert (cbr_top10.end_period - okid10.close().end_period).n < 2
import numpy as np import pandas as pd from hamcrest import assert_that, close_to, calling, raises import cifrum as lib from conftest import decimal_places, delta from cifrum._settings import _MONTHS_PER_YEAR from cifrum.common.time_series import TimeSeriesKind __asset_name = 'mut_ru/0890-94127385' __portfolio_period_start = pd.Period('2011-1', freq='M') __portfolio_period_end = pd.Period('2017-2', freq='M') __asset = lib.portfolio_asset(name=__asset_name, start_period=str(__portfolio_period_start), end_period=str(__portfolio_period_end), currency='USD') def test__cumulative_get_return(): arors = __asset.get_return(kind='cumulative').values assert_that(arors.max(), close_to(.0924, delta)) assert_that(arors.min(), close_to(-.5464, delta)) arors_real = __asset.get_return(kind='cumulative', real=True).values assert_that(arors_real.max(), close_to(.0765, delta)) assert_that(arors_real.min(), close_to(-.5725, delta)) def test__ytd_get_return(): ror_ytd = __asset.get_return(kind='ytd') assert ror_ytd.start_period == pd.Period('2012-1', freq='M')
def test__handle_asset_with_dash_in_name(): asset = lib.portfolio_asset(name='us/BRK-B') assert_that(asset, not_none()) assert_that(asset.close(), is_not(empty()))