Skip to content

Commit

Permalink
feat: added currency conversion for summary line only
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed May 31, 2021
1 parent e18cf5a commit 5df8a74
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ show-separator: true
show-holdings: true
interval: 5
currency: USD
currency-convert-summary-only: false
watchlist:
- NET
- TEAM
Expand Down
25 changes: 13 additions & 12 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ type Context struct {
}

type Config struct {
RefreshInterval int `yaml:"interval"`
Watchlist []string `yaml:"watchlist"`
Lots []Lot `yaml:"lots"`
Separate bool `yaml:"show-separator"`
ExtraInfoExchange bool `yaml:"show-tags"`
ExtraInfoFundamentals bool `yaml:"show-fundamentals"`
ShowSummary bool `yaml:"show-summary"`
ShowHoldings bool `yaml:"show-holdings"`
Proxy string `yaml:"proxy"`
Sort string `yaml:"sort"`
Currency string `yaml:"currency"`
ColorScheme ConfigColorScheme `yaml:"colors"`
RefreshInterval int `yaml:"interval"`
Watchlist []string `yaml:"watchlist"`
Lots []Lot `yaml:"lots"`
Separate bool `yaml:"show-separator"`
ExtraInfoExchange bool `yaml:"show-tags"`
ExtraInfoFundamentals bool `yaml:"show-fundamentals"`
ShowSummary bool `yaml:"show-summary"`
ShowHoldings bool `yaml:"show-holdings"`
Proxy string `yaml:"proxy"`
Sort string `yaml:"sort"`
Currency string `yaml:"currency"`
CurrencyConvertSummaryOnly bool `yaml:"currency-convert-summary-only"`
ColorScheme ConfigColorScheme `yaml:"colors"`
}

type ConfigColorScheme struct {
Expand Down
8 changes: 8 additions & 0 deletions internal/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,18 @@ func GetCurrencyRates(client resty.Client, symbols []string, targetCurrency stri

func GetCurrencyRateFromContext(ctx c.Context, fromCurrency string) (float64, float64, string) {
if currencyRate, ok := ctx.Reference.CurrencyRates[fromCurrency]; ok {

// Convert only the summary currency to the configured currency
if ctx.Config.Currency != "" && ctx.Config.CurrencyConvertSummaryOnly {
return 1.0, currencyRate.Rate, fromCurrency
}

// Convert all quotes and positions to target currency and implicitly convert summary currency (i.e. no conversion since underlying values are already converted)
if ctx.Config.Currency != "" {
return currencyRate.Rate, 1.0, currencyRate.ToCurrency
}

// Convert only the summary currency to the default currency (USD) when currency conversion is not enabled
return 1.0, currencyRate.Rate, currencyRate.ToCurrency

}
Expand Down
31 changes: 30 additions & 1 deletion internal/currency/currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ var _ = Describe("Currency", func() {
})

When("the currency is not set", func() {
It("should return rate to convert", func() {
It("should return the conversion rate to convert only the summary line", func() {
inputCtx := c.Context{
Config: c.Config{
Currency: "",
Expand All @@ -223,4 +223,33 @@ var _ = Describe("Currency", func() {
Expect(outputCurrencyCode).To(Equal("EUR"))
})
})

When("the option to convert only the summary values is set", func() {
It("should return summary conversion rate", func() {
inputCtx := c.Context{
Config: c.Config{
Currency: "EUR",
CurrencyConvertSummaryOnly: true,
},
Reference: c.Reference{
CurrencyRates: c.CurrencyRates{
"USD": c.CurrencyRate{
FromCurrency: "USD",
ToCurrency: "EUR",
Rate: 1.25,
},
"GBP": c.CurrencyRate{
FromCurrency: "GBP",
ToCurrency: "EUR",
Rate: 2,
},
},
},
}
outputRate, outputDefaultRate, outputCurrencyCode := GetCurrencyRateFromContext(inputCtx, "USD")
Expect(outputRate).To(Equal(1.0))
Expect(outputDefaultRate).To(Equal(1.25))
Expect(outputCurrencyCode).To(Equal("USD"))
})
})
})

0 comments on commit 5df8a74

Please sign in to comment.