1
+ import datetime
2
+ import unittest
3
+
4
+ from fava_portfolio_returns .returns .simple import SimpleReturns
5
+ from fava_portfolio_returns .test .test import BEANGROW_CONFIG_CORP
6
+ from fava_portfolio_returns .test .test import load_portfolio_str
7
+
8
+ from fava_portfolio_returns .test .test import approx2
9
+
10
+ class CurrenciesTest (unittest .TestCase ):
11
+ def test_indirect_currency_conversion (self ):
12
+ p = load_portfolio_str (
13
+ """
14
+ plugin "beancount.plugins.auto_accounts"
15
+ plugin "beancount.plugins.implicit_prices"
16
+
17
+ 2020-01-01 commodity CORP
18
+ name: "Example Stock"
19
+
20
+ 2020-01-01 commodity CURRENCY_BASE
21
+ 2020-01-01 commodity CURRENCY_TARGET
22
+
23
+ 2020-01-01 price CURRENCY_BASE 2 CURRENCY_TARGET
24
+
25
+ 2020-01-01 * "Buy 100 CORP @ 2 CURRENCY_BASE"
26
+ Assets:Cash -100.00 CURRENCY_BASE
27
+ Assets:CORP 50 CORP {2 CURRENCY_BASE}
28
+
29
+ 2020-01-04 price CURRENCY_BASE 3 CURRENCY_TARGET
30
+
31
+ 2020-02-01 price CORP 3 CURRENCY_BASE
32
+ 2020-03-03 price CURRENCY_BASE 5 CURRENCY_TARGET
33
+ """ ,
34
+ BEANGROW_CONFIG_CORP ,
35
+ )
36
+ p .target_currency = 'CURRENCY_TARGET'
37
+
38
+ # Cost basis of 100 CURRENCY_BASE = 200 CURRENCY_TARGET
39
+ assert p .cash_at (datetime .date (2020 , 1 , 1 )) == 200
40
+ # Cost basis of 100 CURRENCY_BASE = 300 CURRENCY_TARGET
41
+ assert p .cash_at (datetime .date (2020 , 1 , 5 )) == 300
42
+ # Cost basis of 100 CURRENCY_BASE = 300 CURRENCY_TARGET
43
+ assert p .cash_at (datetime .date (2020 , 2 , 1 )) == 300
44
+ # Cost basis of 100 CURRENCY_BASE = 500 CURRENCY_TARGET
45
+ assert p .cash_at (datetime .date (2020 , 3 , 3 )) == 500
46
+
47
+ returns = SimpleReturns ().series (p , datetime .date (2020 , 1 , 1 ), datetime .date (2020 , 4 , 1 ))
48
+ assert returns == [
49
+ # 200 CURRENCY_TARGET = 100 CURRENCY_BASE invested
50
+ (datetime .date (2020 , 1 , 1 ), 0.0 ),
51
+ # currency rate changing doesn't change the cost basis
52
+ # (300 CURRENCY_TARGET invested)
53
+ (datetime .date (2020 , 1 , 4 ), 0.0 ),
54
+ # investment has acrually grown
55
+ # (300 CURRENCY_TARGET invested, 50 CORP valued at 150 CURRENCY_BASE = 450 CURRENCY_TARGET)
56
+ (datetime .date (2020 , 2 , 1 ), 0.5 ),
57
+ # currency rate changing again doesn't change the cost basis (500 CURRENCY_TARGET invested)
58
+ (datetime .date (2020 , 3 , 3 ), 0.5 ),
59
+ ]
0 commit comments