Skip to content

Commit

Permalink
Add fiat display currency setting, defaults to USD
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Sep 14, 2017
1 parent bc85f11 commit 04d1de9
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 42 deletions.
13 changes: 9 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ type Config struct {
Cryptocurrencies string
CurrencyExchangeProvider string
CurrencyPairFormat *CurrencyPairFormatConfig `json:"CurrencyPairFormat"`
Portfolio portfolio.Base `json:"PortfolioAddresses"`
SMS SMSGlobalConfig `json:"SMSGlobal"`
Webserver WebserverConfig `json:"Webserver"`
Exchanges []ExchangeConfig `json:"Exchanges"`
FiatDisplayCurrency string
Portfolio portfolio.Base `json:"PortfolioAddresses"`
SMS SMSGlobalConfig `json:"SMSGlobal"`
Webserver WebserverConfig `json:"Webserver"`
Exchanges []ExchangeConfig `json:"Exchanges"`
}

// ExchangeConfig holds all the information needed for each enabled Exchange.
Expand Down Expand Up @@ -454,6 +455,10 @@ func (c *Config) LoadConfig(configPath string) error {
}
}

if c.FiatDisplayCurrency == "" {
c.FiatDisplayCurrency = "USD"
}

return nil
}

Expand Down
125 changes: 125 additions & 0 deletions currency/symbol/symbol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package symbol

import "errors"

// symbols map holds the currency name and symbol mappings
var symbols = map[string]string{
"ALL": "Lek",
"AFN": "؋",
"ARS": "$",
"AWG": "ƒ",
"AUD": "$",
"AZN": "ман",
"BSD": "$",
"BBD": "$",
"BYN": "Br",
"BZD": "BZ$",
"BMD": "$",
"BOB": "$b",
"BAM": "KM",
"BWP": "P",
"BGN": "лв",
"BRL": "R$",
"BND": "$",
"KHR": "៛",
"CAD": "$",
"KYD": "$",
"CLP": "$",
"CNY": "¥",
"COP": "$",
"CRC": "₡",
"HRK": "kn",
"CUP": "₱",
"CZK": "Kč",
"DKK": "kr",
"DOP": "RD$",
"XCD": "$",
"EGP": "£",
"SVC": "$",
"EUR": "€",
"FKP": "£",
"FJD": "$",
"GHS": "¢",
"GIP": "£",
"GTQ": "Q",
"GGP": "£",
"GYD": "$",
"HNL": "L",
"HKD": "$",
"HUF": "Ft",
"ISK": "kr",
"INR": "₹",
"IDR": "Rp",
"IRR": "﷼",
"IMP": "£",
"ILS": "₪",
"JMD": "J$",
"JPY": "¥",
"JEP": "£",
"KZT": "лв",
"KPW": "₩",
"KRW": "₩",
"KGS": "лв",
"LAK": "₭",
"LBP": "£",
"LRD": "$",
"MKD": "ден",
"MYR": "RM",
"MUR": "₨",
"MXN": "$",
"MNT": "₮",
"MZN": "MT",
"NAD": "$",
"NPR": "₨",
"ANG": "ƒ",
"NZD": "$",
"NIO": "C$",
"NGN": "₦",
"NOK": "kr",
"OMR": "﷼",
"PKR": "₨",
"PAB": "B/.",
"PYG": "Gs",
"PEN": "S/.",
"PHP": "₱",
"PLN": "zł",
"QAR": "﷼",
"RON": "lei",
"RUB": "₽",
"SHP": "£",
"SAR": "﷼",
"RSD": "Дин.",
"SCR": "₨",
"SGD": "$",
"SBD": "$",
"SOS": "S",
"ZAR": "R",
"LKR": "₨",
"SEK": "kr",
"CHF": "CHF",
"SRD": "$",
"SYP": "£",
"TWD": "NT$",
"THB": "฿",
"TTD": "TT$",
"TRY": "₺",
"TVD": "$",
"UAH": "₴",
"GBP": "£",
"USD": "$",
"UYU": "$U",
"UZS": "лв",
"VEF": "Bs",
"VND": "₫",
"YER": "﷼",
"ZWD": "Z$",
}

// GetSymbolByCurrencyName returns a currency symbol
func GetSymbolByCurrencyName(currency string) (string, error) {
result, ok := symbols[currency]
if !ok {
return "", errors.New("currency symbol not found")
}
return result, nil
}
21 changes: 21 additions & 0 deletions currency/symbol/symbol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package symbol

import "testing"

func TestGetSymbolByCurrencyName(t *testing.T) {
expected := "₩"
actual, err := GetSymbolByCurrencyName("KPW")
if err != nil {
t.Errorf("Test failed. TestGetSymbolByCurrencyName error: %s", err)
}

if actual != expected {
t.Errorf("Test failed. TestGetSymbolByCurrencyName differing values")
}

_, err = GetSymbolByCurrencyName("BLAH")
if err == nil {
t.Errorf("Test failed. TestGetSymbolByCurrencyNam returned nil on non-existant currency")
}

}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func main() {
}

log.Printf("Bot '%s' started.\n", bot.config.Name)
log.Printf("Fiat display currency: %s.", bot.config.FiatDisplayCurrency)
AdjustGoMaxProcs()

if bot.config.SMS.Enabled {
Expand Down
148 changes: 124 additions & 24 deletions routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,158 @@ import (
"log"
"time"

"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency/symbol"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/stats"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)

func printCurrencyFormat(price float64) string {
displaySymbol, err := symbol.GetSymbolByCurrencyName(bot.config.FiatDisplayCurrency)
if err != nil {
log.Printf("Failed to get display symbol: %s", err)
}

return fmt.Sprintf("%s%.8f", displaySymbol, price)
}

func printConvertCurrencyFormat(origCurrency string, origPrice float64) string {
displayCurrency := bot.config.FiatDisplayCurrency
conv, err := currency.ConvertCurrency(origPrice, origCurrency, displayCurrency)
if err != nil {
log.Printf("Failed to convert currency: %s", err)
}

displaySymbol, err := symbol.GetSymbolByCurrencyName(displayCurrency)
if err != nil {
log.Printf("Failed to get display symbol: %s", err)
}

origSymbol, err := symbol.GetSymbolByCurrencyName(origCurrency)
if err != nil {
log.Printf("Failed to get original currency symbol: %s", err)
}

return fmt.Sprintf("%s%.2f %s (%s%.2f %s)",
displaySymbol,
conv,
displayCurrency,
origSymbol,
origPrice,
origCurrency,
)
}

func printSummary(result ticker.Price, p pair.CurrencyPair, assetType, exchangeName string, err error) {
if err != nil {
log.Printf("failed to get %s %s ticker. Error: %s",
log.Printf("Failed to get %s %s ticker. Error: %s",
p.Pair().String(),
exchangeName,
err)
return
}

stats.Add(exchangeName, p, assetType, result.Last, result.Volume)
log.Printf("%s %s %s: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
result.Last,
result.Ask,
result.Bid,
result.High,
result.Low,
result.Volume)
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.String() != bot.config.FiatDisplayCurrency {
origCurrency := p.SecondCurrency.Upper().String()
log.Printf("%s %s %s: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
printConvertCurrencyFormat(origCurrency, result.Last),
printConvertCurrencyFormat(origCurrency, result.Ask),
printConvertCurrencyFormat(origCurrency, result.Bid),
printConvertCurrencyFormat(origCurrency, result.High),
printConvertCurrencyFormat(origCurrency, result.Low),
result.Volume)
} else {
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.Upper().String() == bot.config.FiatDisplayCurrency {
log.Printf("%s %s %s: Last %s Ask %s Bid %s High %s Low %s Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
printCurrencyFormat(result.Last),
printCurrencyFormat(result.Ask),
printCurrencyFormat(result.Bid),
printCurrencyFormat(result.High),
printCurrencyFormat(result.Low),
result.Volume)
} else {
log.Printf("%s %s %s: Last %.8f Ask %.8f Bid %.8f High %.8f Low %.8f Volume %.8f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
result.Last,
result.Ask,
result.Bid,
result.High,
result.Low,
result.Volume)
}
}
}

func printOrderbookSummary(result orderbook.Base, p pair.CurrencyPair, assetType, exchangeName string, err error) {
if err != nil {
log.Printf("failed to get %s %s orderbook. Error: %s",
log.Printf("Failed to get %s %s orderbook. Error: %s",
p.Pair().String(),
exchangeName,
err)
return
}

bidsAmount, bidsValue := result.CalculateTotalBids()
asksAmount, asksValue := result.CalculateTotalAsks()

log.Printf("%s %s %s: Orderbook Bids len: %d amount: %f total value: %f Asks len: %d amount: %f total value: %f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
bidsValue,
len(result.Asks),
asksAmount,
asksValue,
)
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.String() != bot.config.FiatDisplayCurrency {
origCurrency := p.SecondCurrency.Upper().String()
log.Printf("%s %s %s: Orderbook Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
p.FirstCurrency.String(),
printConvertCurrencyFormat(origCurrency, bidsValue),
len(result.Asks),
asksAmount,
p.FirstCurrency.String(),
printConvertCurrencyFormat(origCurrency, asksValue),
)
} else {
if currency.IsFiatCurrency(p.SecondCurrency.String()) && p.SecondCurrency.Upper().String() == bot.config.FiatDisplayCurrency {
log.Printf("%s %s %s: Orderbook Bids len: %d Amount: %f %s. Total value: %s Asks len: %d Amount: %f %s. Total value: %s",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
p.FirstCurrency.String(),
printCurrencyFormat(bidsValue),
len(result.Asks),
asksAmount,
p.FirstCurrency.String(),
printCurrencyFormat(asksValue),
)
} else {
log.Printf("%s %s %s: Orderbook Bids len: %d Amount: %f %s. Total value: %f Asks len: %d Amount: %f %s. Total value: %f",
exchangeName,
exchange.FormatCurrency(p).String(),
assetType,
len(result.Bids),
bidsAmount,
p.FirstCurrency.String(),
bidsValue,
len(result.Asks),
asksAmount,
p.FirstCurrency.String(),
asksValue,
)
}
}

}

func relayWebsocketEvent(result interface{}, event, assetType, exchangeName string) {
Expand Down
6 changes: 5 additions & 1 deletion testdata/configtest.dat
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"Name": "Skynet",
"EncryptConfig": 0,
"Cryptocurrencies": "BTC,LTC,ETH,XRP,NMC,NVC,PPC,XBT,DOGE,DASH",
"CurrencyExchangeProvider": "fixer",
"CurrencyPairFormat": {
"Uppercase": true,
"Delimiter": "-"
},
"FiatDisplayCurrency": "USD",
"PortfolioAddresses": {
"Addresses": [
{
Expand Down Expand Up @@ -50,7 +52,9 @@
"Enabled": false,
"AdminUsername": "admin",
"AdminPassword": "Password",
"ListenAddress": ":9050"
"ListenAddress": ":9050",
"WebsocketConnectionLimit": 1,
"WebsocketAllowInsecureOrigin": false
},
"Exchanges": [
{
Expand Down
Loading

0 comments on commit 04d1de9

Please sign in to comment.