예제 #1
0
def test__return_empty_list_or_none_if_symbol_doesnt_exist():
    nonexisting_id = 'nlu/xxx'

    asset = y.portfolio_asset(name=nonexisting_id)
    assert_that(asset, none())

    assets = y.portfolio_asset(names=[nonexisting_id])
    assert_that(assets, empty())

    assets = y.portfolio_asset(names=['micex/FXRU', nonexisting_id])
    assert_that(assets, has_length(1))

    portfolio = y.portfolio(assets={'micex/FXRU': 1., nonexisting_id: 1.}, currency='USD')
    assert len(portfolio.assets) == 1

    nonexisting_namespace = 'yyy/FXRU'

    asset = y.portfolio_asset(name=nonexisting_namespace)
    assert_that(asset, none())

    assets = y.portfolio_asset(names=[nonexisting_namespace])
    assert_that(assets, empty())

    assets = y.portfolio_asset(names=['micex/FXRU', nonexisting_namespace])
    assert_that(assets, has_length(1))

    portfolio = y.portfolio(assets={'micex/FXRU': 1., nonexisting_namespace: 1.}, currency='USD')
    assert_that(portfolio.assets, has_length(1))
예제 #2
0
def test__usd_assets_that_is_older_than_rub():
    p_usd = y.portfolio(assets={'us/T': 1}, currency='usd')
    assert p_usd.get_return().start_period == pd.Period('1983-12', freq='M')

    p_rub = y.portfolio(assets={'us/T': 1}, currency='rub')
    assert p_rub.get_return().start_period == pd.Period('1992-08', freq='M')
    assert p_usd.get_return().end_period == p_rub.get_return().end_period
예제 #3
0
def test__create_portfolio_with_default_values():
    assets = {'micex/FXRU': 1., 'mut_ru/0890-94127385': 1.}
    period_start = pd.Period('2014-1', freq='M')
    period_now = pd.Period.now(freq='M')
    assert period_now == pd.Period('2019-2', freq='M')

    p1 = y.portfolio(assets=assets, currency='rub')
    assert_that(p1, not_none())
    assert_that(p1.assets, has_length(2))
    assert p1.get_return().start_period == period_start
    assert p1.get_return().end_period == period_now - 1

    sp2 = pd.Period('2017-1', freq='M')
    p2 = y.portfolio(assets=assets, start_period=str(sp2), currency='rub')
    assert_that(p2, not_none())
    assert_that(p2.assets, has_length(2))
    assert p2.get_return().start_period == sp2 + 1
    assert p2.get_return().end_period == period_now - 1

    ep3 = pd.Period('2018-3', freq='M')
    p3 = y.portfolio(assets=assets, end_period=str(ep3), currency='rub')
    assert_that(p3, not_none())
    assert_that(p3.assets, has_length(2))
    assert p3.get_return().start_period == period_start
    assert p3.get_return().end_period == ep3
 def portfolio():
     asset_names = {'nlu/xxxx': 1, 'micex/FXRU': 2, 'micex/FXMM': 3}
     portfolio = y.portfolio(assets=asset_names,
                             start_period=str(_portfolio_period_start),
                             end_period=str(_portfolio_period_end),
                             currency='USD')
     assert_that(portfolio.assets, has_length(2))
     return portfolio
예제 #5
0
def test__default_periods():
    asset = y.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 = y.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
예제 #6
0
def test__initial_data():
    assert_that(_portfolio.assets, has_length(3))

    p = y.portfolio(assets=_asset_names,
                    start_period=str(_portfolio_period_start),
                    end_period=str(_portfolio_period_end),
                    currency='RUB')
    assert_that(p.assets, has_length(3))

    assert_that(
        p.assets['mut_ru/0890-94127385'].close().values,
        contains(*[
            1055.64, 1094.14, 1080.76, 1074.51, 1091.37, 1151.83, 1103.9,
            1147.5, 1193.69, 1175.64, 1199.64, 1233.88, 1252.67, 1302.46,
            1268.7, 1269.31, 1334.93, 1351.45, 1357.41, 1366.64, 1444.08,
            1535.61, 1525.6, 1403.08, 1375.97, 1390.39, 1314.96
        ]))
    assert_that(
        p.assets['micex/FXRU'].close().values,
        contains(*[
            5070., 4670., 4830., 5110., 5650., 6130., 6160., 6140., 6330.,
            7020., 7230., 7250., 6720., 6540., 6710., 6630., 6760., 6770.,
            6550., 6610., 6570., 6260., 6310., 6110., 5970., 6110., 6190.
        ]))
    assert_that(
        p.assets['micex/FXMM'].close().values,
        contains(*[
            1097.2, 1110., 1121.7, 1135.5, 1146.5, 1155.7, 1164.4, 1174.3,
            1183.8, 1194.7, 1203.8, 1215.1, 1228.6, 1233.2, 1237.3, 1246.6,
            1253.7, 1265.5, 1273.4, 1282.7, 1292.1, 1302.2, 1308.6, 1317.2,
            1327.5, 1336.8, 1346.1
        ]))

    assert_that(
        np.round(_portfolio.get_return().values, 8).tolist(),
        contains(*[
            0.12229691, -0.01582228, -0.02737772, -0.01702011, -0.06406856,
            -0.01160388, 0.04782469, 0.00100095, -0.06237734, -0.0103524,
            0.01680251, 0.09387303, 0.06095007, -0.02852904, 0.02625182,
            -0.01216933, 0.04138359, 0.02000188, 0.01177389, -0.00741344,
            0.08587424, 0.00909396, -0.00811951, 0.01276804, 0.00324936,
            -0.0100885
        ]))

    assert_that(
        np.round(_portfolio.get_return(real=True).values, 8).tolist(),
        contains(*[
            0.12001991, -0.0208132, -0.03077293, -0.01708596, -0.0627414,
            -0.01006255, 0.04829642, 0.00311853, -0.05916249, -0.01198559,
            0.01596637, 0.08918301, 0.05594384, -0.03244377, 0.02289264,
            -0.01056843, 0.04042848, 0.01755568, 0.01051378, -0.00586757,
            0.08551928, 0.00324703, -0.01123018, 0.01194533, 0.00028252,
            -0.01093415
        ]))
def test__risk():
    short_portfolio = y.portfolio(assets=_asset_names,
                                  start_period='2016-8', end_period='2016-12', currency='USD')

    assert_that(calling(short_portfolio.risk).with_args(period='year'),
                raises(Exception))

    assert _portfolio.risk().kind == TimeSeriesKind.REDUCED_VALUE
    assert _portfolio.risk(period='year').kind == TimeSeriesKind.REDUCED_VALUE
    assert _portfolio.risk(period='month').kind == TimeSeriesKind.REDUCED_VALUE

    assert_that(_portfolio.risk(period='year').value, close_to(.1688, delta))
    assert_that(_portfolio.risk(period='month').value, close_to(.0432, delta))
    assert_that(_portfolio.risk().value, close_to(.1688, delta))
def test__normalize_weights():
    assert_that(np.sum(_portfolio.weights), close_to(1., delta))

    assert_that(_portfolio.assets['mut_ru/0890-94127385'].weight, close_to(.4444, delta))
    assert_that(_portfolio.assets['micex/FXRU'].weight, close_to(.3333, delta))
    assert_that(_portfolio.assets['micex/FXMM'].weight, close_to(.2222, delta))

    asset_names = {'mut_ru/xxxx-xxxxxxxx': 1, 'micex/FXRU': 2, 'micex/FXMM': 3}
    portfolio_misprint = y.portfolio(assets=asset_names,
                                     start_period=str(_portfolio_period_start),
                                     end_period=str(_portfolio_period_end),
                                     currency='USD')
    assert_that(np.sum(portfolio_misprint.weights), close_to(1., delta))
    assert_that(portfolio_misprint.assets['micex/FXRU'].weight, close_to(.4, delta))
    assert_that(portfolio_misprint.assets['micex/FXMM'].weight, close_to(.6, delta))
예제 #9
0
                            currency=Currency.RUB)
                    ]

            with Context(TestSymbolSources):
                yapo_instance = y._instance.Yapo()
                return yapo_instance

    return YapoInstanceFactory()


__portfolio = y.portfolio(assets={
    'mut_ru/0890-94127385': 1.,
    'micex/FXRU': 1.,
    'micex/FXMM': 1.,
    'cbr/USD': 1.,
    'cbr/EUR': 1.,
    'cbr/RUB': 1.
},
                          start_period='2015-3',
                          end_period='2017-5',
                          currency='USD')


@pytest.mark.parametrize('unsupported_id',
                         ['infl/RUB', 'infl/USD', 'infl/EUR', 'cbr/TOP_rates'])
def test__fail_if_asset_security_type_is_not_supported(unsupported_id):
    foo = calling(y.portfolio_asset).with_args(name=unsupported_id,
                                               start_period='2011-3',
                                               end_period='2015-5',
                                               currency='USD')
    assert_that(foo, raises(AssertionError))
def test__handle_assets_with_monthly_data_gaps(currency: Currency):
    p = y.portfolio(assets={'micex/KUBE': 1}, currency=currency.name)
    assert_that(p, not_none())
    assert_that(p.assets, has_length(1))
    assert_that(p.rate_of_return(), is_not(empty()))
def test__handle_portfolio_with_asset_with_dot_in_name(currency: Currency):
    p = y.portfolio(assets={'ny/BRK.B': 1}, currency=currency.name)
    assert_that(p, not_none())
    assert_that(p.assets, has_length(1))
    assert_that(p.rate_of_return(), is_not(empty()))
import numpy as np
import pandas as pd
import pytest
from hamcrest import assert_that, has_length, contains, not_none, close_to, calling, raises, is_not, empty

import yapo as y
from conftest import delta, decimal_places
from yapo.common.enums import Currency
from yapo.common.time_series import TimeSeriesKind

_portfolio_period_start = pd.Period('2015-3', freq='M')
_portfolio_period_end = pd.Period('2017-5', freq='M')
_asset_names = {'mut_ru/0890-94127385': 4, 'micex/FXRU': 3, 'micex/FXMM': 2}
_portfolio = y.portfolio(assets=_asset_names,
                         start_period=str(_portfolio_period_start),
                         end_period=str(_portfolio_period_end),
                         currency='USD')


def test__initial_data():
    assert_that(_portfolio.assets, has_length(3))

    p = y.portfolio(assets=_asset_names,
                    start_period=str(_portfolio_period_start),
                    end_period=str(_portfolio_period_end),
                    currency='RUB')
    assert_that(p.assets, has_length(3))

    assert_that(p.assets['mut_ru/0890-94127385'].close().values, contains(
        *[1055.64, 1094.14, 1080.76, 1074.51, 1091.37, 1151.83, 1103.9, 1147.5,
          1193.69, 1175.64, 1199.64, 1233.88, 1252.67, 1302.46, 1268.7,