Skip to content

Commit 66838e1

Browse files
feat: currency conversion monitor
1 parent 236f344 commit 66838e1

File tree

15 files changed

+531
-172
lines changed

15 files changed

+531
-172
lines changed

internal/asset/asset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func GetAssets(ctx c.Context, assetGroupQuote c.AssetGroupQuote) ([]c.Asset, Hol
3030

3131
for i, assetQuote := range assetGroupQuote.AssetQuotes {
3232

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

3535
holding := getHoldingFromAssetQuote(assetQuote, holdingsBySymbol, currencyRateByUse)
3636
holdingSummary = addHoldingToHoldingSummary(holdingSummary, holding, currencyRateByUse)

internal/cli/cli.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/achannarasappa/ticker/v4/internal/cli/symbol"
99
c "github.com/achannarasappa/ticker/v4/internal/common"
10-
"github.com/achannarasappa/ticker/v4/internal/quote"
1110
yahooClient "github.com/achannarasappa/ticker/v4/internal/quote/yahoo/client"
1211
"github.com/achannarasappa/ticker/v4/internal/ui/util"
1312

@@ -138,10 +137,11 @@ func readConfig(fs afero.Fs, configPathOption string) (c.Config, error) {
138137

139138
func getReference(config c.Config, assetGroups []c.AssetGroup, client *resty.Client) (c.Reference, error) {
140139

141-
currencyRates, err := quote.GetAssetGroupsCurrencyRates(client, assetGroups, config.Currency)
142-
if err != nil {
143-
return c.Reference{}, err
144-
}
140+
var err error
141+
// currencyRates, err := quote.GetAssetGroupsCurrencyRates(client, assetGroups, config.Currency)
142+
// if err != nil {
143+
// return c.Reference{}, err
144+
// }
145145

146146
styles := util.GetColorScheme(config.ColorScheme)
147147

@@ -150,8 +150,8 @@ func getReference(config c.Config, assetGroups []c.AssetGroup, client *resty.Cli
150150
}
151151

152152
return c.Reference{
153-
CurrencyRates: currencyRates,
154-
Styles: styles,
153+
// CurrencyRates: currencyRates,
154+
Styles: styles,
155155
}, err
156156

157157
}

internal/common/common.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ type Monitor interface {
8787
Stop() error
8888
}
8989

90+
type MonitorCurrencyRate interface {
91+
Start() error
92+
SetTargetCurrency(targetCurrency string)
93+
Stop() error
94+
}
95+
9096
// Lot represents a cost basis lot
9197
type Lot struct {
9298
Symbol string `yaml:"symbol"`
@@ -257,3 +263,9 @@ type MessageUpdate[T any] struct {
257263
Sequence int64
258264
Nonce int
259265
}
266+
267+
type MessageRequest[T any] struct {
268+
Data T
269+
ID string
270+
Nonce int
271+
}

internal/currency/currency.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,53 @@ type CurrencyRateByUse struct { //nolint:golint,revive
1414
}
1515

1616
// GetCurrencyRateFromContext reads currency rates from the context and sets the conversion rate for each use case
17-
func GetCurrencyRateFromContext(ctx c.Context, fromCurrency string) CurrencyRateByUse {
17+
func GetCurrencyRateFromContext(ctx c.Context, fromCurrency string, toCurrency string, rate float64) CurrencyRateByUse {
1818

19-
// If currency is convertible
20-
if currencyRate, ok := ctx.Reference.CurrencyRates[fromCurrency]; ok {
21-
22-
currencyRateCost := currencyRate.Rate
23-
24-
if ctx.Config.CurrencyDisableUnitCostConversion {
25-
currencyRateCost = 1.0
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,
2626
}
27+
}
2728

28-
// Convert only the summary currency to the configured currency
29-
if ctx.Config.Currency != "" && ctx.Config.CurrencyConvertSummaryOnly {
30-
return CurrencyRateByUse{
31-
ToCurrencyCode: fromCurrency,
32-
QuotePrice: 1.0,
33-
PositionCost: 1.0,
34-
SummaryValue: currencyRate.Rate,
35-
SummaryCost: currencyRateCost,
36-
}
37-
}
29+
currencyRateCost := rate
3830

39-
// Convert all quotes and positions to target currency and implicitly convert summary currency (i.e. no conversion since underlying values are already converted)
40-
if ctx.Config.Currency != "" {
41-
return CurrencyRateByUse{
42-
ToCurrencyCode: currencyRate.ToCurrency,
43-
QuotePrice: currencyRate.Rate,
44-
PositionCost: currencyRateCost,
45-
SummaryValue: 1.0,
46-
SummaryCost: 1.0,
47-
}
48-
}
31+
if ctx.Config.CurrencyDisableUnitCostConversion {
32+
currencyRateCost = 1.0
33+
}
4934

50-
// Convert only the summary currency to the default currency (USD) when currency conversion is not enabled
35+
// Convert only the summary currency to the configured currency
36+
if ctx.Config.Currency != "" && ctx.Config.CurrencyConvertSummaryOnly {
5137
return CurrencyRateByUse{
52-
ToCurrencyCode: currencyRate.ToCurrency,
38+
ToCurrencyCode: fromCurrency,
5339
QuotePrice: 1.0,
5440
PositionCost: 1.0,
55-
SummaryValue: currencyRate.Rate,
41+
SummaryValue: rate,
5642
SummaryCost: currencyRateCost,
5743
}
44+
}
5845

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+
}
5955
}
6056

57+
// Convert only the summary currency to the default currency (USD) when currency conversion is not enabled
6158
return CurrencyRateByUse{
62-
ToCurrencyCode: fromCurrency,
59+
ToCurrencyCode: toCurrency,
6360
QuotePrice: 1.0,
6461
PositionCost: 1.0,
65-
SummaryValue: 1.0,
66-
SummaryCost: 1.0,
62+
SummaryValue: rate,
63+
SummaryCost: currencyRateCost,
6764
}
65+
6866
}

internal/currency/currency_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var _ = Describe("Currency", func() {
2828
},
2929
},
3030
}
31-
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "EUR")
31+
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "EUR", "USD", 0)
3232
Expect(outputCurrencyRateByUse.QuotePrice).To(Equal(1.0))
3333
Expect(outputCurrencyRateByUse.PositionCost).To(Equal(1.0))
3434
Expect(outputCurrencyRateByUse.SummaryValue).To(Equal(1.0))
@@ -58,7 +58,7 @@ var _ = Describe("Currency", func() {
5858
},
5959
},
6060
}
61-
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD")
61+
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD", "EUR", 0)
6262
Expect(outputCurrencyRateByUse.QuotePrice).To(Equal(1.25))
6363
Expect(outputCurrencyRateByUse.PositionCost).To(Equal(1.25))
6464
Expect(outputCurrencyRateByUse.SummaryValue).To(Equal(1.0))
@@ -88,7 +88,7 @@ var _ = Describe("Currency", func() {
8888
},
8989
},
9090
}
91-
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD")
91+
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD", "EUR", 0)
9292
Expect(outputCurrencyRateByUse.QuotePrice).To(Equal(1.0))
9393
Expect(outputCurrencyRateByUse.PositionCost).To(Equal(1.0))
9494
Expect(outputCurrencyRateByUse.SummaryValue).To(Equal(1.25))
@@ -119,7 +119,7 @@ var _ = Describe("Currency", func() {
119119
},
120120
},
121121
}
122-
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD")
122+
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD", "EUR", 0)
123123
Expect(outputCurrencyRateByUse.QuotePrice).To(Equal(1.0))
124124
Expect(outputCurrencyRateByUse.PositionCost).To(Equal(1.0))
125125
Expect(outputCurrencyRateByUse.SummaryValue).To(Equal(1.25))
@@ -150,7 +150,7 @@ var _ = Describe("Currency", func() {
150150
},
151151
},
152152
}
153-
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD")
153+
outputCurrencyRateByUse := GetCurrencyRateFromContext(inputCtx, "USD", "EUR", 0)
154154
Expect(outputCurrencyRateByUse.SummaryCost).To(Equal(1.0))
155155
Expect(outputCurrencyRateByUse.PositionCost).To(Equal(1.0))
156156
})

0 commit comments

Comments
 (0)