예제 #1
0
def test_portfolio_to_dict_empty_portfolio():
    """
    Test 'portfolio_to_dict' method for an empty Portfolio.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    port = Portfolio(start_dt)
    port.subscribe_funds(start_dt, 100000.0)
    port_dict = port.portfolio_to_dict()
    assert port_dict == {}
예제 #2
0
def test_portfolio_to_dict_for_two_holdings():
    """
    Test portfolio_to_dict for two holdings.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    asset1_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    asset2_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    update_dt = pd.Timestamp('2017-10-08 08:00:00', tz=pytz.UTC)
    asset1 = Equity("AAA Inc.", "EQ:AAA", tax_exempt=False)
    asset2 = Equity("BBB Inc.", "EQ:BBB", tax_exempt=False)

    port = Portfolio(start_dt, portfolio_id='1234')
    port.subscribe_funds(start_dt, 100000.0)
    tn_asset1 = Transaction(asset=asset1.symbol,
                            quantity=100,
                            dt=asset1_dt,
                            price=567.0,
                            order_id=1,
                            commission=15.78)
    port.transact_asset(tn_asset1)

    tn_asset2 = Transaction(asset=asset2.symbol,
                            quantity=100,
                            dt=asset2_dt,
                            price=123.0,
                            order_id=2,
                            commission=7.64)
    port.transact_asset(tn_asset2)

    port.update_market_value_of_asset(asset2.symbol, 134.0, update_dt)
    test_holdings = {
        asset1.symbol: {
            "quantity": 100,
            "book_cost": 56715.78,
            "market_value": 56700.0,
            "gain": -15.78,
            "perc_gain": -0.027822944513854874
        },
        asset2.symbol: {
            "quantity": 100,
            "book_cost": 12307.64,
            "market_value": 13400.0,
            "gain": 1092.3600000000006,
            "perc_gain": 8.8754627207165679
        }
    }
    port_holdings = port.portfolio_to_dict()

    # This is needed because we're not using Decimal
    # datatypes and have to compare slightly differing
    # floating point representations
    for asset in (asset1.symbol, asset2.symbol):
        for key, val in test_holdings[asset].items():
            assert port_holdings[asset][key] == pytest.approx(
                test_holdings[asset][key])
예제 #3
0
def test_portfolio_to_dict_for_two_holdings():
    """
    Test portfolio_to_dict for two holdings.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    asset1_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    asset2_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    update_dt = pd.Timestamp('2017-10-08 08:00:00', tz=pytz.UTC)
    asset1 = 'EQ:AAA'
    asset2 = 'EQ:BBB'

    port = Portfolio(start_dt, portfolio_id='1234')
    port.subscribe_funds(start_dt, 100000.0)
    tn_asset1 = Transaction(asset=asset1,
                            quantity=100,
                            dt=asset1_dt,
                            price=567.0,
                            order_id=1,
                            commission=15.78)
    port.transact_asset(tn_asset1)

    tn_asset2 = Transaction(asset=asset2,
                            quantity=100,
                            dt=asset2_dt,
                            price=123.0,
                            order_id=2,
                            commission=7.64)
    port.transact_asset(tn_asset2)
    port.update_market_value_of_asset(asset2, 134.0, update_dt)
    test_holdings = {
        asset1: {
            "quantity": 100,
            "market_value": 56700.0,
            "unrealised_pnl": -15.78,
            "realised_pnl": 0.0,
            "total_pnl": -15.78
        },
        asset2: {
            "quantity": 100,
            "market_value": 13400.0,
            "unrealised_pnl": 1092.3600000000006,
            "realised_pnl": 0.0,
            "total_pnl": 1092.3600000000006
        }
    }
    port_holdings = port.portfolio_to_dict()

    # This is needed because we're not using Decimal
    # datatypes and have to compare slightly differing
    # floating point representations
    for asset in (asset1, asset2):
        for key, val in test_holdings[asset].items():
            assert port_holdings[asset][key] == pytest.approx(
                test_holdings[asset][key])