def test_records(account_checking, asset_krw): with Transaction.create() as t: record = Record.create( created_at=make_date('2016-03-14'), transaction=t, account=account_checking, asset=asset_krw, quantity=1000) # Make sure the record type has been set implictly assert 'deposit' == record.type with Transaction.create() as t: record = Record.create( created_at=make_date('2016-03-14'), transaction=t, account=account_checking, asset=asset_krw, quantity=-2000) # Make sure the record type has been set implictly assert 'withdraw' == record.type with Transaction.create() as t: record = Record.create( created_at=make_date('2016-03-14'), transaction=t, account=account_checking, asset=asset_krw, quantity=3000, type='balance_adjustment') # Make sure the record type has been set explicitly assert 'balance_adjustment' == record.type
def test_date_range(): start, end = make_date('2016-01-01'), make_date('2016-01-15') r = date_range(start, end) assert isinstance(r, types.GeneratorType) r = list(r) assert 14 == len(r) assert r[0] == make_date('2016-01-01') assert r[13] == make_date('2016-01-14')
def test_net_worth_2(account_checking, account_sp500, asset_krw, asset_sp500): AssetValue.create( evaluated_at=make_date('2016-02-25'), asset=asset_sp500, target_asset=asset_krw, granularity='1day', close=921.77) AssetValue.create( evaluated_at=make_date('2016-02-24'), asset=asset_sp500, target_asset=asset_krw, granularity='1day', close=932.00) AssetValue.create( evaluated_at=make_date('2016-02-23'), asset=asset_sp500, target_asset=asset_krw, granularity='1day', close=921.06) AssetValue.create( evaluated_at=make_date('2016-02-22'), asset=asset_sp500, target_asset=asset_krw, granularity='1day', close=921.76) with Transaction.create() as t: Record.create( created_at=make_date('2016-02-25'), transaction=t, account=account_sp500, asset=asset_sp500, quantity=1000) Record.create( created_at=make_date('2016-02-25'), transaction=t, account=account_checking, asset=asset_krw, quantity=-1000 * 921.77) assert 921770 == account_sp500.net_worth( evaluated_at=make_date('2016-02-25'), target_asset=asset_krw) assert 921770 == account_sp500.net_worth( evaluated_at=make_date('2016-03-01'), approximation=True, target_asset=asset_krw) with pytest.raises(AssetValueUnavailableException): account_sp500.net_worth(make_date('2016-03-01'), target_asset=asset_krw)
def import_sp500(): app = create_app(__name__) with app.app_context(): account_checking = Account.get(id=1001) account_sp500 = Account.get(id=7001) asset_krw = Asset.query.filter_by(name='KRW').first() asset_sp500 = Asset.query.filter_by(name='KB S&P500').first() with open('sample-data/sp500.csv') as fin: for line in fin: cols = line.split() if len(cols) != 5: continue date = parse_date(cols[0], '%Y.%m.%d') _type = cols[1] quantity_krw, quantity_sp500 = \ [int(extract_numbers(v)) for v in cols[2:4]] print(cols) withdraw = _type == '일반입금' with Transaction.create() as t: if withdraw: Record.create( created_at=date, account=account_checking, asset=asset_krw, quantity=-quantity_krw, transaction=t) Record.create( created_at=date, account=account_sp500, asset=asset_sp500, quantity=quantity_sp500, transaction=t) print(account_sp500.net_worth(make_date('2016-02-25'), target_asset=asset_krw))
def test_parse_8percent_data(): sample_file = os.path.join(PROJECT_PATH, 'sample-data', '8percent-829.html') with open(sample_file) as fin: raw = fin.read() stored_data = [ ('2016-04-11', 1694, 612, 160, 340), ('2016-05-11', 1916, 390, 90, 0), ('2016-06-13', 1920, 386, 90, 0), ('2016-07-11', 1982, 324, 80, 0), ('2016-08-10', 1963, 343, 80, 0), ('2016-09-12', 1979, 327, 80, 0), ('2016-10-10', 2005, 301, 70, 0), ('2016-11-14', 1992, 314, 70, 0), ('2016-12-12', 2054, 252, 60, 0), ('2017-01-10', 2044, 262, 60, 0), ('2017-02-13', 2053, 253, 60, 0), ('2017-03-13', 2099, 207, 50, 0), ('2017-04-10', 2101, 205, 50, 0), ('2017-05-15', 2098, 208, 50, 0), ('2017-06-12', 2145, 161, 40, 0), ('2017-07-10', 2151, 155, 30, 0), ('2017-08-14', 2153, 153, 30, 0), ('2017-09-11', 2188, 118, 20, 0), ('2017-10-11', 2198, 108, 20, 0), ('2017-11-13', 2216, 90, 20, 0), ('2017-12-11', 2238, 68, 10, 0), ('2018-01-10', 2251, 55, 10, 0), ('2018-02-12', 2270, 36, 0, 0), ('2018-03-12', 2290, 16, 0, 0), ] parsed_data = parse_8percent_data(raw) assert parsed_data['name'] assert parsed_data['grade'] assert isinstance(parsed_data['duration'], int) assert isinstance(parsed_data['annual_percentage_yield'], float) assert 0.0 < parsed_data['annual_percentage_yield'] <= 0.3 assert isinstance(parsed_data['amount'], int) assert 0 < parsed_data['amount'] flag = True for expected, actual in zip(stored_data, parsed_data['records']): assert len(expected) == len(actual) expected = list(expected) expected[0] = make_date(expected[0]) for exp, act in zip(expected, actual): flag = False assert exp == act if flag: pytest.fail('parse_8percent_data() did not return any data')
def import_sp500(): app = create_app(__name__) with app.app_context(): account_checking = Account.get(id=1001) account_sp500 = Account.get(id=7001) asset_krw = Asset.query.filter_by(name='KRW').first() asset_sp500 = Asset.query.filter_by(name='KB S&P500').first() with open('sample-data/sp500.csv') as fin: for line in fin: cols = line.split() if len(cols) != 5: continue date = parse_date(cols[0], '%Y.%m.%d') _type = cols[1] quantity_krw, quantity_sp500 = \ [int(extract_numbers(v)) for v in cols[2:4]] print(cols) withdraw = _type == '일반입금' with Transaction.create() as t: if withdraw: Record.create(created_at=date, account=account_checking, asset=asset_krw, quantity=-quantity_krw, transaction=t) Record.create(created_at=date, account=account_sp500, asset=asset_sp500, quantity=quantity_sp500, transaction=t) print( account_sp500.net_worth(make_date('2016-02-25'), target_asset=asset_krw))
def test_net_worth_1(account_checking, asset_krw): assert 0 == account_checking.net_worth( evaluated_at=make_date('2016-01-01'), target_asset=asset_krw) assert 0 == account_checking.net_worth( evaluated_at=make_date('2016-01-02'), target_asset=asset_krw) assert 0 == account_checking.net_worth( evaluated_at=make_date('2016-01-03'), target_asset=asset_krw) assert 0 == account_checking.net_worth( evaluated_at=make_date('2016-01-04'), target_asset=asset_krw) with Transaction.create() as t: Record.create( created_at=make_date('2016-01-01'), transaction=t, account=account_checking, asset=asset_krw, quantity=1000) assert 1000 == account_checking.net_worth( evaluated_at=make_date('2016-01-01'), target_asset=asset_krw) assert 1000 == account_checking.net_worth( evaluated_at=make_date('2016-01-02'), target_asset=asset_krw) assert 1000 == account_checking.net_worth( evaluated_at=make_date('2016-01-03'), target_asset=asset_krw) assert 1000 == account_checking.net_worth( evaluated_at=make_date('2016-01-04'), target_asset=asset_krw) with Transaction.create() as t: Record.create( created_at=make_date('2016-01-02'), transaction=t, account=account_checking, asset=asset_krw, quantity=2000) assert 1000 == account_checking.net_worth( evaluated_at=make_date('2016-01-01'), target_asset=asset_krw) assert 3000 == account_checking.net_worth( evaluated_at=make_date('2016-01-02'), target_asset=asset_krw) assert 3000 == account_checking.net_worth( evaluated_at=make_date('2016-01-03'), target_asset=asset_krw) assert 3000 == account_checking.net_worth( evaluated_at=make_date('2016-01-04'), target_asset=asset_krw) with Transaction.create() as t: Record.create( created_at=make_date('2016-01-03'), transaction=t, account=account_checking, asset=asset_krw, quantity=-1500) assert 1000 == account_checking.net_worth( evaluated_at=make_date('2016-01-01'), target_asset=asset_krw) assert 3000 == account_checking.net_worth( evaluated_at=make_date('2016-01-02'), target_asset=asset_krw) assert 1500 == account_checking.net_worth( evaluated_at=make_date('2016-01-03'), target_asset=asset_krw) assert 1500 == account_checking.net_worth( evaluated_at=make_date('2016-01-04'), target_asset=asset_krw)
def test_portfolio(account_hf, asset_hf1, account_checking, asset_krw): portfolio = Portfolio() portfolio.target_asset = asset_krw portfolio.add_accounts(account_hf, account_checking) with Transaction.create() as t: Record.create( created_at=make_date('2015-12-04'), transaction=t, account=account_checking, asset=asset_krw, quantity=500000) Record.create( created_at=make_date('2015-12-04'), transaction=t, account=account_checking, asset=asset_krw, quantity=-500000) Record.create( created_at=make_date('2015-12-04'), transaction=t, account=account_hf, asset=asset_hf1, quantity=1) net_worth = portfolio.net_worth(evaluated_at=make_date('2015-12-04'), granularity=Granularity.day) # The net asset value shall be initially zero assert 0 == net_worth # Initial asset value AssetValue.create( evaluated_at=make_date('2015-12-04'), asset=asset_hf1, target_asset=asset_krw, granularity='1day', close=500000) net_worth = portfolio.net_worth(evaluated_at=make_date('2015-12-04'), granularity=Granularity.day) assert 500000 == net_worth # 1st payment interest, tax, returned = 3923, 740, 30930 with Transaction.create() as t: Record.create( created_at=make_date('2016-01-08'), transaction=t, account=account_checking, asset=asset_krw, quantity=returned) # Remaining principle value after the 1st payment AssetValue.create( evaluated_at=make_date('2016-01-08'), asset=asset_hf1, target_asset=asset_krw, granularity='1day', close=472253) net_worth = portfolio.net_worth(evaluated_at=make_date('2016-01-08'), granularity=Granularity.day) assert 500000 + (interest - tax) == net_worth # 2nd payment with Transaction.create() as t: Record.create( created_at=make_date('2016-02-05'), transaction=t, account=account_checking, asset=asset_krw, quantity=25016) # Remaining principle value after the 2nd payment AssetValue.create( evaluated_at=make_date('2016-02-05'), asset=asset_hf1, target_asset=asset_krw, granularity='1day', close=450195) db.session.delete(portfolio) db.session.commit()