Skip to content

Commit 42d0104

Browse files
test: fix failing tests and move currency use cases to asset
1 parent b86223f commit 42d0104

File tree

11 files changed

+662
-362
lines changed

11 files changed

+662
-362
lines changed

internal/asset/asset.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package asset
22

33
import (
44
c "github.com/achannarasappa/ticker/v4/internal/common"
5-
"github.com/achannarasappa/ticker/v4/internal/currency"
65
)
76

87
// AggregatedLot represents a cost basis lot of an asset grouped by symbol
@@ -26,11 +25,11 @@ func GetAssets(ctx c.Context, assetGroupQuote c.AssetGroupQuote) ([]c.Asset, Hol
2625

2726
var holdingSummary HoldingSummary
2827
assets := make([]c.Asset, 0)
29-
holdingsBySymbol := getLots(assetGroupQuote.AssetGroup.Holdings)
28+
holdingsBySymbol := getLots(assetGroupQuote.AssetGroup.ConfigAssetGroup.Holdings)
3029

3130
for i, assetQuote := range assetGroupQuote.AssetQuotes {
3231

33-
currencyRateByUse := currency.GetCurrencyRateFromContext(ctx, assetQuote.Currency.FromCurrencyCode, assetQuote.Currency.ToCurrencyCode, assetQuote.Currency.Rate)
32+
currencyRateByUse := getCurrencyRateByUse(ctx, assetQuote.Currency.FromCurrencyCode, assetQuote.Currency.ToCurrencyCode, assetQuote.Currency.Rate)
3433

3534
holding := getHoldingFromAssetQuote(assetQuote, holdingsBySymbol, currencyRateByUse)
3635
holdingSummary = addHoldingToHoldingSummary(holdingSummary, holding, currencyRateByUse)
@@ -63,7 +62,7 @@ func GetAssets(ctx c.Context, assetGroupQuote c.AssetGroupQuote) ([]c.Asset, Hol
6362

6463
}
6564

66-
func addHoldingToHoldingSummary(holdingSummary HoldingSummary, holding c.Holding, currencyRateByUse currency.CurrencyRateByUse) HoldingSummary {
65+
func addHoldingToHoldingSummary(holdingSummary HoldingSummary, holding c.Holding, currencyRateByUse currencyRateByUse) HoldingSummary {
6766

6867
if holding.Cost == 0 || holding.Value == 0 {
6968
return holdingSummary
@@ -104,7 +103,7 @@ func updateHoldingWeights(assets []c.Asset, holdingSummary HoldingSummary) []c.A
104103

105104
}
106105

107-
func getHoldingFromAssetQuote(assetQuote c.AssetQuote, lotsBySymbol map[string]AggregatedLot, currencyRateByUse currency.CurrencyRateByUse) c.Holding {
106+
func getHoldingFromAssetQuote(assetQuote c.AssetQuote, lotsBySymbol map[string]AggregatedLot, currencyRateByUse currencyRateByUse) c.Holding {
108107

109108
if aggregatedLot, ok := lotsBySymbol[assetQuote.Symbol]; ok {
110109
value := aggregatedLot.Quantity * assetQuote.QuotePrice.Price * currencyRateByUse.QuotePrice

internal/asset/asset_test.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,23 @@ var _ = Describe("Asset", func() {
8484
Config: c.Config{
8585
Currency: "EUR",
8686
},
87-
Reference: c.Reference{
88-
// CurrencyRates: map[string]c.CurrencyRate{
89-
// "USD": {
90-
// FromCurrency: "USD",
91-
// ToCurrency: "EUR",
92-
// Rate: 1.5,
93-
// },
94-
// "GBP": {
95-
// FromCurrency: "GBP",
96-
// ToCurrency: "EUR",
97-
// Rate: 2,
98-
// },
99-
// },
100-
},
87+
Reference: c.Reference{},
10188
}
10289
inputAssetGroupQuote := fixtureAssetGroupQuote
10390
inputAssetGroupQuote.AssetQuotes = []c.AssetQuote{
10491
{
10592
Name: "ThoughtWorks",
10693
Symbol: "TWKS",
10794
Class: c.AssetClassStock,
108-
Currency: c.Currency{FromCurrencyCode: "USD"},
95+
Currency: c.Currency{FromCurrencyCode: "USD", ToCurrencyCode: "EUR", Rate: 1.5},
10996
QuotePrice: c.QuotePrice{Price: 110.0, PricePrevClose: 100.0, PriceOpen: 100.0, PriceDayHigh: 110.0, PriceDayLow: 90.0, Change: 10.0, ChangePercent: 10.0},
11097
QuoteExtended: c.QuoteExtended{FiftyTwoWeekHigh: 150, FiftyTwoWeekLow: 50, MarketCap: 1000000},
11198
},
11299
{
113100
Name: "Microsoft Inc",
114101
Symbol: "MSFT",
115102
Class: c.AssetClassStock,
116-
Currency: c.Currency{FromCurrencyCode: "GBP"},
103+
Currency: c.Currency{FromCurrencyCode: "GBP", ToCurrencyCode: "EUR", Rate: 2.0},
117104
QuotePrice: c.QuotePrice{Price: 220.0, PricePrevClose: 200.0, PriceOpen: 200.0, PriceDayHigh: 220.0, PriceDayLow: 180.0, Change: 20.0, ChangePercent: 10.0},
118105
},
119106
}
@@ -213,20 +200,14 @@ var _ = Describe("Asset", func() {
213200
When("and there is a holding with a non-US currency", func() {
214201

215202
inputContext := c.Context{
216-
Reference: c.Reference{
217-
// CurrencyRates: map[string]c.CurrencyRate{
218-
// "EUR": {
219-
// FromCurrency: "EUR",
220-
// ToCurrency: "USD",
221-
// Rate: 0.5,
222-
// },
223-
// },
224-
},
203+
Reference: c.Reference{},
225204
}
226205
inputAssetGroupQuote := fixtureAssetGroupQuote
227206
inputAssetQuotes := make([]c.AssetQuote, len(fixtureAssetGroupQuote.AssetQuotes))
228207
copy(inputAssetQuotes, fixtureAssetGroupQuote.AssetQuotes)
229208
inputAssetQuotes[0].Currency.FromCurrencyCode = "EUR"
209+
inputAssetQuotes[0].Currency.ToCurrencyCode = "USD"
210+
inputAssetQuotes[0].Currency.Rate = 0.5
230211
inputAssetGroupQuote.AssetQuotes = inputAssetQuotes
231212
inputAssetGroupQuote.AssetGroup.ConfigAssetGroup.Holdings = []c.Lot{
232213
{

internal/asset/currency.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,69 @@ package asset
22

33
import (
44
c "github.com/achannarasappa/ticker/v4/internal/common"
5-
"github.com/achannarasappa/ticker/v4/internal/currency"
65
)
76

8-
func convertAssetQuotePriceCurrency(currencyRateByUse currency.CurrencyRateByUse, quotePrice c.QuotePrice) c.QuotePrice {
7+
// currencyRateByUse represents the currency conversion rate for each use case
8+
type currencyRateByUse struct { //nolint:golint,revive
9+
ToCurrencyCode string
10+
QuotePrice float64
11+
PositionCost float64
12+
SummaryValue float64
13+
SummaryCost float64
14+
}
15+
16+
// getCurrencyRateByUse reads currency rates from the context and sets the conversion rate for each use case
17+
func getCurrencyRateByUse(ctx c.Context, fromCurrency string, toCurrency string, rate float64) currencyRateByUse {
18+
19+
if rate == 0 {
20+
return currencyRateByUse{
21+
ToCurrencyCode: fromCurrency,
22+
QuotePrice: 1.0,
23+
PositionCost: 1.0,
24+
SummaryValue: 1.0,
25+
SummaryCost: 1.0,
26+
}
27+
}
28+
29+
currencyRateCost := rate
30+
31+
if ctx.Config.CurrencyDisableUnitCostConversion {
32+
currencyRateCost = 1.0
33+
}
34+
35+
// Convert only the summary currency to the configured currency
36+
if ctx.Config.Currency != "" && ctx.Config.CurrencyConvertSummaryOnly {
37+
return currencyRateByUse{
38+
ToCurrencyCode: fromCurrency,
39+
QuotePrice: 1.0,
40+
PositionCost: 1.0,
41+
SummaryValue: rate,
42+
SummaryCost: currencyRateCost,
43+
}
44+
}
45+
46+
// Convert all quotes and positions to target currency and implicitly convert summary currency (i.e. no conversion since underlying values are already converted)
47+
if ctx.Config.Currency != "" {
48+
return currencyRateByUse{
49+
ToCurrencyCode: toCurrency,
50+
QuotePrice: rate,
51+
PositionCost: currencyRateCost,
52+
SummaryValue: 1.0,
53+
SummaryCost: 1.0,
54+
}
55+
}
56+
57+
// Convert only the summary currency to the default currency (USD) when currency conversion is not enabled
58+
return currencyRateByUse{
59+
ToCurrencyCode: toCurrency,
60+
QuotePrice: 1.0,
61+
PositionCost: 1.0,
62+
SummaryValue: rate,
63+
SummaryCost: currencyRateCost,
64+
}
65+
}
66+
67+
func convertAssetQuotePriceCurrency(currencyRateByUse currencyRateByUse, quotePrice c.QuotePrice) c.QuotePrice {
968
return c.QuotePrice{
1069
Price: quotePrice.Price * currencyRateByUse.QuotePrice,
1170
PricePrevClose: quotePrice.PricePrevClose * currencyRateByUse.QuotePrice,
@@ -17,7 +76,7 @@ func convertAssetQuotePriceCurrency(currencyRateByUse currency.CurrencyRateByUse
1776
}
1877
}
1978

20-
func convertAssetQuoteExtendedCurrency(currencyRateByUse currency.CurrencyRateByUse, quoteExtended c.QuoteExtended) c.QuoteExtended {
79+
func convertAssetQuoteExtendedCurrency(currencyRateByUse currencyRateByUse, quoteExtended c.QuoteExtended) c.QuoteExtended {
2180
return c.QuoteExtended{
2281
FiftyTwoWeekHigh: quoteExtended.FiftyTwoWeekHigh * currencyRateByUse.QuotePrice,
2382
FiftyTwoWeekLow: quoteExtended.FiftyTwoWeekLow * currencyRateByUse.QuotePrice,

0 commit comments

Comments
 (0)