Fixes to portfolio cost/profit

pull/243/head
Simon Roberts 3 years ago
parent 50316ff8dd
commit cf3dd176fd
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -12,7 +12,7 @@ import (
log "github.com/sirupsen/logrus"
)
// FiatCurrencyNames is a mpa of currency symbols to names.
// FiatCurrencyNames is a map of currency symbols to names.
// Keep these in alphabetical order.
var FiatCurrencyNames = map[string]string{
"AUD": "Australian Dollar",
@ -301,3 +301,23 @@ func CurrencySymbol(currency string) string {
return "?"
}
func (ct *Cointop) Convert(convertFrom string, convertTo string, amount float64) (float64, error) {
convertFrom = strings.ToLower(convertFrom)
convertTo = strings.ToLower(convertTo)
var rate float64
if convertFrom == convertTo {
rate = 1.0
} else if convertFrom == "usd" && convertTo == "aud" {
rate = 1.33 // TODO: cache exchange rates and remove this dodgy hack
log.Debugf("XXX DODGY HACK Convert(%s, %s, %f) = %f", convertFrom, convertTo, amount, rate*amount)
} else {
crate, err := ct.api.GetExchangeRate(convertFrom, convertTo)
if err != nil {
return 0, err
}
rate = crate
}
return rate * amount, nil
}

@ -341,13 +341,16 @@ func (ct *Cointop) GetPortfolioTable() *table.Table {
Text: text,
})
case "profit":
// TODO: finish
text := ""
if coin.BuyPrice > 0 && coin.BuyCurrency != "" {
// TODO: currency conversion
profit := coin.Holdings * (coin.Price - coin.BuyPrice)
profit = math.Round(100*profit) / 100 // cheesy round
text = ct.FormatPrice(profit)
costPrice, err := ct.Convert(coin.BuyCurrency, ct.State.currencyConversion, coin.BuyPrice)
if err == nil {
profit := (coin.Price - costPrice) * coin.Holdings
profit = math.Round(100*profit) / 100 // cheesy round
text = ct.FormatPrice(profit)
} else {
text = "?"
}
}
if ct.State.hidePortfolioBalances {
text = HiddenBalanceChars
@ -366,10 +369,11 @@ func (ct *Cointop) GetPortfolioTable() *table.Table {
case "profit_percent":
profitPercent := 0.0
if coin.BuyPrice > 0 && coin.BuyCurrency != "" {
// TODO: currency conversion
profitPercent = 100 * (coin.Price/coin.BuyPrice - 1)
costPrice, err := ct.Convert(coin.BuyCurrency, ct.State.currencyConversion, coin.BuyPrice)
if err == nil {
profitPercent = 100 * (coin.Price/costPrice - 1)
}
}
// text = ct.FormatPrice(profit)
colorProfit := ct.colorscheme.TableColumnChange
if profitPercent > 0 {
colorProfit = ct.colorscheme.TableColumnChangeUp

@ -430,3 +430,11 @@ func getChartInterval(start, end int64) string {
}
return interval
}
// GetExchangeRate gets the current excange rate between two currencies
func (s *Service) GetExchangeRate(convertFrom string, convertTo string) (float64, error) {
if convertFrom == convertTo {
return 1.0, nil
}
return 0, fmt.Errorf("unsupported currency conversion: %s => %s", convertFrom, convertTo)
}

@ -16,4 +16,5 @@ type Interface interface {
CoinLink(name string) string
SupportedCurrencies() []string
Price(name string, convert string) (float64, error)
GetExchangeRate(convertFrom string, convertTo string) (float64, error)
}

Loading…
Cancel
Save