mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Package currency
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -211,6 +213,55 @@ func (c *Config) CheckWebserverConfigValues() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) RetrieveConfigCurrencyPairs() {
|
||||
cryptoCurrencies := common.SplitStrings(c.Cryptocurrencies, ",")
|
||||
fiatCurrencies := common.SplitStrings(currency.DEFAULT_CURRENCIES, ",")
|
||||
|
||||
for _, exchange := range c.Exchanges {
|
||||
if exchange.Enabled {
|
||||
baseCurrencies := common.SplitStrings(exchange.BaseCurrencies, ",")
|
||||
enabledCurrencies := common.SplitStrings(exchange.EnabledPairs, ",")
|
||||
|
||||
for _, currencyPair := range enabledCurrencies {
|
||||
ok, separator := currency.ContainsSeparator(currencyPair)
|
||||
if ok {
|
||||
pair := common.SplitStrings(currencyPair, separator)
|
||||
for _, x := range pair {
|
||||
ok, _ = currency.ContainsBaseCurrencyIndex(baseCurrencies, x)
|
||||
if !ok {
|
||||
cryptoCurrencies = currency.CheckAndAddCurrency(cryptoCurrencies, x)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ok, idx := currency.ContainsBaseCurrencyIndex(baseCurrencies, currencyPair)
|
||||
if ok {
|
||||
curr := strings.Replace(currencyPair, idx, "", -1)
|
||||
|
||||
if currency.ContainsBaseCurrency(baseCurrencies, curr) {
|
||||
fiatCurrencies = currency.CheckAndAddCurrency(fiatCurrencies, curr)
|
||||
} else {
|
||||
cryptoCurrencies = currency.CheckAndAddCurrency(cryptoCurrencies, curr)
|
||||
}
|
||||
|
||||
if currency.ContainsBaseCurrency(baseCurrencies, idx) {
|
||||
fiatCurrencies = currency.CheckAndAddCurrency(fiatCurrencies, idx)
|
||||
} else {
|
||||
cryptoCurrencies = currency.CheckAndAddCurrency(cryptoCurrencies, idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currency.BaseCurrencies = common.JoinStrings(fiatCurrencies, ",")
|
||||
if common.StringContains(currency.BaseCurrencies, "RUR") {
|
||||
currency.BaseCurrencies = strings.Replace(currency.BaseCurrencies, "RUR", "RUB", -1)
|
||||
}
|
||||
c.Cryptocurrencies = common.JoinStrings(cryptoCurrencies, ",")
|
||||
currency.CryptoCurrencies = c.Cryptocurrencies
|
||||
}
|
||||
|
||||
func (c *Config) ReadConfig() error {
|
||||
_, err := common.ReadFile(OLD_CONFIG_FILE)
|
||||
if err == nil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package currency
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -41,11 +41,13 @@ const (
|
||||
YAHOO_YQL_URL = "http://query.yahooapis.com/v1/public/yql"
|
||||
YAHOO_DATABASE = "store://datatables.org/alltableswithkeys"
|
||||
DEFAULT_CURRENCIES = "USD,AUD,EUR,CNY"
|
||||
DEFAULT_CRYPTOCURRENCIES = "BTC,LTC,ETH,DOGE,DASH,XRP,XMR"
|
||||
)
|
||||
|
||||
var (
|
||||
CurrencyStore map[string]Rate
|
||||
BaseCurrencies string
|
||||
CryptoCurrencies string
|
||||
ErrCurrencyDataNotFetched = errors.New("Yahoo currency data has not been fetched yet.")
|
||||
ErrCurrencyNotFound = errors.New("Unable to find specified currency.")
|
||||
ErrQueryingYahoo = errors.New("Unable to query Yahoo currency values.")
|
||||
@@ -56,12 +58,16 @@ func IsDefaultCurrency(currency string) bool {
|
||||
return common.StringContains(DEFAULT_CURRENCIES, common.StringToUpper(currency))
|
||||
}
|
||||
|
||||
func IsDefaultCryptocurrency(currency string) bool {
|
||||
return common.StringContains(DEFAULT_CRYPTOCURRENCIES, common.StringToUpper(currency))
|
||||
}
|
||||
|
||||
func IsFiatCurrency(currency string) bool {
|
||||
return common.StringContains(BaseCurrencies, common.StringToUpper(currency))
|
||||
}
|
||||
|
||||
func IsCryptocurrency(currency string) bool {
|
||||
return common.StringContains(bot.config.Cryptocurrencies, common.StringToUpper(currency))
|
||||
return common.StringContains(CryptoCurrencies, common.StringToUpper(currency))
|
||||
}
|
||||
|
||||
func ContainsSeparator(input string) (bool, string) {
|
||||
@@ -106,58 +112,17 @@ func CheckAndAddCurrency(input []string, check string) []string {
|
||||
return input
|
||||
}
|
||||
|
||||
func RetrieveConfigCurrencyPairs() error {
|
||||
cryptoCurrencies := common.SplitStrings(bot.config.Cryptocurrencies, ",")
|
||||
fiatCurrencies := common.SplitStrings(DEFAULT_CURRENCIES, ",")
|
||||
|
||||
for _, exchange := range bot.config.Exchanges {
|
||||
if exchange.Enabled {
|
||||
baseCurrencies := common.SplitStrings(exchange.BaseCurrencies, ",")
|
||||
enabledCurrencies := common.SplitStrings(exchange.EnabledPairs, ",")
|
||||
|
||||
for _, currencyPair := range enabledCurrencies {
|
||||
ok, separator := ContainsSeparator(currencyPair)
|
||||
if ok {
|
||||
pair := common.SplitStrings(currencyPair, separator)
|
||||
for _, x := range pair {
|
||||
ok, _ = ContainsBaseCurrencyIndex(baseCurrencies, x)
|
||||
if !ok {
|
||||
cryptoCurrencies = CheckAndAddCurrency(cryptoCurrencies, x)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ok, idx := ContainsBaseCurrencyIndex(baseCurrencies, currencyPair)
|
||||
if ok {
|
||||
currency := strings.Replace(currencyPair, idx, "", -1)
|
||||
|
||||
if ContainsBaseCurrency(baseCurrencies, currency) {
|
||||
fiatCurrencies = CheckAndAddCurrency(fiatCurrencies, currency)
|
||||
} else {
|
||||
cryptoCurrencies = CheckAndAddCurrency(cryptoCurrencies, currency)
|
||||
}
|
||||
|
||||
if ContainsBaseCurrency(baseCurrencies, idx) {
|
||||
fiatCurrencies = CheckAndAddCurrency(fiatCurrencies, idx)
|
||||
} else {
|
||||
cryptoCurrencies = CheckAndAddCurrency(cryptoCurrencies, idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
func SeedCurrencyData(fiatCurrencies string) error {
|
||||
if fiatCurrencies == "" {
|
||||
fiatCurrencies = DEFAULT_CURRENCIES
|
||||
}
|
||||
|
||||
BaseCurrencies = common.JoinStrings(fiatCurrencies, ",")
|
||||
BaseCurrencies = strings.Replace(BaseCurrencies, "RUR", "RUB", -1)
|
||||
bot.config.Cryptocurrencies = common.JoinStrings(cryptoCurrencies, ",")
|
||||
|
||||
err := QueryYahooCurrencyValues(BaseCurrencies)
|
||||
err := QueryYahooCurrencyValues(fiatCurrencies)
|
||||
|
||||
if err != nil {
|
||||
return ErrQueryingYahoo
|
||||
}
|
||||
|
||||
log.Println("Fetched currency value data.")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -177,6 +142,12 @@ func MakecurrencyPairs(supportedCurrencies string) string {
|
||||
}
|
||||
|
||||
func ConvertCurrency(amount float64, from, to string) (float64, error) {
|
||||
if len(CurrencyStore) == 0 {
|
||||
err := SeedCurrencyData(common.StringToUpper(from) + "," + common.StringToUpper(to))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
currency := common.StringToUpper(from + to)
|
||||
if common.StringContains(currency, "RUB") {
|
||||
currency = strings.Replace(currency, "RUB", "RUR", -1)
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -20,17 +21,16 @@ func (b *BTCMarkets) Run() {
|
||||
|
||||
for b.Enabled {
|
||||
for _, x := range b.EnabledPairs {
|
||||
currency := x
|
||||
curr := x
|
||||
go func() {
|
||||
ticker, err := b.GetTickerPrice(currency)
|
||||
ticker, err := b.GetTickerPrice(curr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
//BTCMarketsLastUSD, _ := ConvertCurrency(ticker.Last, "AUD", "USD")
|
||||
//BTCMarketsBestBidUSD, _ := ConvertCurrency(ticker.Bid, "AUD", "USD")
|
||||
//BTCMarketsBestAskUSD, _ := ConvertCurrency(ticker.Ask, "AUD", "USD")
|
||||
//log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", currency, BTCMarketsLastUSD, ticker.Last, BTCMarketsBestBidUSD, ticker.Bid, BTCMarketsBestAskUSD, ticker.Ask)
|
||||
log.Printf("BTC Markets %s: Last %f Bid %f Ask %f \n", currency, ticker.Last, ticker.Bid, ticker.Ask)
|
||||
BTCMarketsLastUSD, _ := currency.ConvertCurrency(ticker.Last, "AUD", "USD")
|
||||
BTCMarketsBestBidUSD, _ := currency.ConvertCurrency(ticker.Bid, "AUD", "USD")
|
||||
BTCMarketsBestAskUSD, _ := currency.ConvertCurrency(ticker.Ask, "AUD", "USD")
|
||||
log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", curr, BTCMarketsLastUSD, ticker.Last, BTCMarketsBestBidUSD, ticker.Bid, BTCMarketsBestAskUSD, ticker.Ask)
|
||||
//AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, 0)
|
||||
//AddExchangeInfo(b.GetName(), currency[0:3], "USD", BTCMarketsLastUSD, 0)
|
||||
}()
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -26,22 +27,19 @@ func (h *HUOBI) Run() {
|
||||
|
||||
for h.Enabled {
|
||||
for _, x := range h.EnabledPairs {
|
||||
currency := common.StringToLower(x[0:3])
|
||||
curr := common.StringToLower(x[0:3])
|
||||
go func() {
|
||||
ticker, err := h.GetTickerPrice(currency)
|
||||
ticker, err := h.GetTickerPrice(curr)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
/*
|
||||
HuobiLastUSD, _ := ConvertCurrency(ticker.Last, "CNY", "USD")
|
||||
HuobiHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD")
|
||||
HuobiLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD")
|
||||
log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Volume)
|
||||
AddExchangeInfo(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:]), ticker.Last, ticker.Volume)
|
||||
AddExchangeInfo(h.GetName(), common.StringToUpper(currency[0:3]), "USD", HuobiLastUSD, ticker.Volume)
|
||||
*/
|
||||
log.Printf("Huobi %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
|
||||
HuobiLastUSD, _ := currency.ConvertCurrency(ticker.Last, "CNY", "USD")
|
||||
HuobiHighUSD, _ := currency.ConvertCurrency(ticker.High, "CNY", "USD")
|
||||
HuobiLowUSD, _ := currency.ConvertCurrency(ticker.Low, "CNY", "USD")
|
||||
log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", curr, HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Volume)
|
||||
// AddExchangeInfo(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:]), ticker.Last, ticker.Volume)
|
||||
// AddExchangeInfo(h.GetName(), common.StringToUpper(currency[0:3]), "USD", HuobiLastUSD, ticker.Volume)
|
||||
}()
|
||||
}
|
||||
time.Sleep(time.Second * h.RESTPollingDelay)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
@@ -26,41 +27,40 @@ func (o *OKCoin) Run() {
|
||||
|
||||
for o.Enabled {
|
||||
for _, x := range o.EnabledPairs {
|
||||
currency := common.StringToLower(x[0:3] + "_" + x[3:])
|
||||
curr := common.StringToLower(x[0:3] + "_" + x[3:])
|
||||
if o.APIUrl == OKCOIN_API_URL {
|
||||
for _, y := range o.FuturesValues {
|
||||
futuresValue := y
|
||||
go func() {
|
||||
ticker, err := o.GetFuturesTicker(currency, futuresValue)
|
||||
ticker, err := o.GetFuturesTicker(curr, futuresValue)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Printf("OKCoin Intl Futures %s (%s): Last %f High %f Low %f Volume %f\n", currency, futuresValue, ticker.Last, ticker.High, ticker.Low, ticker.Vol)
|
||||
log.Printf("OKCoin Intl Futures %s (%s): Last %f High %f Low %f Volume %f\n", curr, futuresValue, ticker.Last, ticker.High, ticker.Low, ticker.Vol)
|
||||
//AddExchangeInfo(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:]), ticker.Last, ticker.Vol)
|
||||
}()
|
||||
}
|
||||
go func() {
|
||||
ticker, err := o.GetTickerPrice(currency)
|
||||
ticker, err := o.GetTickerPrice(curr)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
|
||||
log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", curr, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
|
||||
//AddExchangeInfo(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:]), ticker.Last, ticker.Volume)
|
||||
}()
|
||||
} else {
|
||||
go func() {
|
||||
ticker, err := o.GetTickerPrice(currency)
|
||||
ticker, err := o.GetTickerPrice(curr)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
//tickerLastUSD, _ := ConvertCurrency(ticker.Last, "CNY", "USD")
|
||||
//tickerHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD")
|
||||
//tickerLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD")
|
||||
//log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Volume)
|
||||
log.Printf("OKCoin China %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
|
||||
tickerLastUSD, _ := currency.ConvertCurrency(ticker.Last, "CNY", "USD")
|
||||
tickerHighUSD, _ := currency.ConvertCurrency(ticker.High, "CNY", "USD")
|
||||
tickerLowUSD, _ := currency.ConvertCurrency(ticker.Low, "CNY", "USD")
|
||||
log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", curr, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Volume)
|
||||
//AddExchangeInfo(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:]), ticker.Last, ticker.Volume)
|
||||
//AddExchangeInfo(o.GetName(), common.StringToUpper(currency[0:3]), "USD", tickerLastUSD, ticker.Volume)
|
||||
}()
|
||||
|
||||
8
main.go
8
main.go
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/anx"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/bitfinex"
|
||||
@@ -135,12 +136,15 @@ func main() {
|
||||
|
||||
setupBotExchanges()
|
||||
|
||||
err = RetrieveConfigCurrencyPairs()
|
||||
bot.config.RetrieveConfigCurrencyPairs()
|
||||
|
||||
err = currency.SeedCurrencyData(currency.BaseCurrencies)
|
||||
if err != nil {
|
||||
log.Fatalf("Fatal error retrieving config currency AvailablePairs. Error: ", err)
|
||||
log.Fatalf("Fatal error retrieving config currencies. Error: ", err)
|
||||
}
|
||||
|
||||
log.Println("Successfully retrieved config currencies.")
|
||||
|
||||
go StartPortfolioWatcher()
|
||||
|
||||
if bot.config.Webserver.Enabled {
|
||||
|
||||
4
stats.go
4
stats.go
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
)
|
||||
|
||||
type ExchangeInfo struct {
|
||||
@@ -43,7 +45,7 @@ func (this ByVolume) Swap(i, j int) {
|
||||
}
|
||||
|
||||
func AddExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
|
||||
if !IsFiatCurrency(fiat) {
|
||||
if !currency.IsFiatCurrency(fiat) {
|
||||
return
|
||||
}
|
||||
if len(ExchInfo) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user