mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* Initial currency overhaul before service system implementation * Remove redundant currency string in orderbook.Base Unexport lastupdated field in orderbook.Base as it was being instantiated multiple times Add error handling for process orderbook * Remove redundant currency string in ticker.Price Unexport lastupdated field in ticker.Price Add error handling for process ticker function and fix tests * Phase Two Update * Update translations to use map type - thankyou to kempeng for spotting this * Change pair method name from Display -> Format for better readability * Fixes misspelling and tests * Implement requested changes from GloriousCode * Remove reduntant function and streamlined return in currency_translation.go * Revert pair method naming conventions * Change currency naming conventions * Changed code type to exported Item type with underlying string to reduce complexity * Added interim orderbook process method to orderbook.Base type * Changed feebuilder struct field to currency.Pair * Adds fall over system for backup fx providers * deprecate function and children and fix linter issue with btcmarkets * Fixed requested changes * Fix bug and move mtx for rates * Fixed after rebase oopsies * Fix linter issues * Fixes race conditions in testing functions * Final phase coinmarketcap update * fix linter issues * Implement requested changes * Adds configuration variables to increase/decrease time durations between updating currency file and fetching new currency rates * Add a collection of tests to improve codecov * After rebase oopsy fixes for btse * Fix requested changes * fix after rebase oopsies and add more efficient comparison checks within currency pair * Fix linter issues
167 lines
3.8 KiB
Go
167 lines
3.8 KiB
Go
package base
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/thrasher-/gocryptotrader/common"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
|
)
|
|
|
|
// global vars contain staged update data that will be sent to the communication
|
|
// mediums
|
|
var (
|
|
TickerStaged map[string]map[string]map[string]ticker.Price
|
|
OrderbookStaged map[string]map[string]map[string]Orderbook
|
|
PortfolioStaged Portfolio
|
|
SettingsStaged Settings
|
|
ServiceStarted time.Time
|
|
m sync.Mutex
|
|
)
|
|
|
|
// Orderbook holds the minimal orderbook details to be sent to a communication
|
|
// medium
|
|
type Orderbook struct {
|
|
CurrencyPair string
|
|
AssetType string
|
|
TotalAsks float64
|
|
TotalBids float64
|
|
LastUpdated string
|
|
}
|
|
|
|
// Ticker holds the minimal orderbook details to be sent to a communication
|
|
// medium
|
|
type Ticker struct {
|
|
CurrencyPair string
|
|
LastUpdated string
|
|
}
|
|
|
|
// Portfolio holds the minimal portfolio details to be sent to a communication
|
|
// medium
|
|
type Portfolio struct {
|
|
ProfitLoss string
|
|
}
|
|
|
|
// Settings holds the minimal setting details to be sent to a communication
|
|
// medium
|
|
type Settings struct {
|
|
EnabledExchanges string
|
|
EnabledCommunications string
|
|
}
|
|
|
|
// Base enforces standard variables across communication packages
|
|
type Base struct {
|
|
Name string
|
|
Enabled bool
|
|
Verbose bool
|
|
Connected bool
|
|
}
|
|
|
|
// Event is a generalise event type
|
|
type Event struct {
|
|
Type string
|
|
GainLoss string
|
|
TradeDetails string
|
|
}
|
|
|
|
// IsEnabled returns if the comms package has been enabled in the configuration
|
|
func (b *Base) IsEnabled() bool {
|
|
return b.Enabled
|
|
}
|
|
|
|
// IsConnected returns if the package is connected to a server and/or ready to
|
|
// send
|
|
func (b *Base) IsConnected() bool {
|
|
return b.Connected
|
|
}
|
|
|
|
// GetName returns a package name
|
|
func (b *Base) GetName() string {
|
|
return b.Name
|
|
}
|
|
|
|
// GetTicker returns staged ticker data
|
|
func (b *Base) GetTicker(exchangeName string) string {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
|
|
tickerPrice, ok := TickerStaged[exchangeName]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
|
|
var tickerPrices []ticker.Price
|
|
for x := range tickerPrice {
|
|
for y := range tickerPrice[x] {
|
|
tickerPrices = append(tickerPrices, tickerPrice[x][y])
|
|
}
|
|
}
|
|
|
|
var packagedTickers []string
|
|
for i := range tickerPrices {
|
|
packagedTickers = append(packagedTickers, fmt.Sprintf(
|
|
"Currency Pair: %s Ask: %f, Bid: %f High: %f Last: %f Low: %f ATH: %f Volume: %f",
|
|
tickerPrices[i].Pair,
|
|
tickerPrices[i].Ask,
|
|
tickerPrices[i].Bid,
|
|
tickerPrices[i].High,
|
|
tickerPrices[i].Last,
|
|
tickerPrices[i].Low,
|
|
tickerPrices[i].PriceATH,
|
|
tickerPrices[i].Volume))
|
|
}
|
|
return common.JoinStrings(packagedTickers, "\n")
|
|
}
|
|
|
|
// GetOrderbook returns staged orderbook data
|
|
func (b *Base) GetOrderbook(exchangeName string) string {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
|
|
orderbook, ok := OrderbookStaged[exchangeName]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
|
|
var orderbooks []Orderbook
|
|
for _, x := range orderbook {
|
|
for _, y := range x {
|
|
orderbooks = append(orderbooks, y)
|
|
}
|
|
}
|
|
|
|
var packagedOrderbooks []string
|
|
for i := range orderbooks {
|
|
packagedOrderbooks = append(packagedOrderbooks, fmt.Sprintf(
|
|
"Currency Pair: %s AssetType: %s, LastUpdated: %s TotalAsks: %f TotalBids: %f",
|
|
orderbooks[i].CurrencyPair,
|
|
orderbooks[i].AssetType,
|
|
orderbooks[i].LastUpdated,
|
|
orderbooks[i].TotalAsks,
|
|
orderbooks[i].TotalBids))
|
|
}
|
|
return common.JoinStrings(packagedOrderbooks, "\n")
|
|
}
|
|
|
|
// GetPortfolio returns staged portfolio info
|
|
func (b *Base) GetPortfolio() string {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
return fmt.Sprintf("%v", PortfolioStaged)
|
|
}
|
|
|
|
// GetSettings returns stage setting info
|
|
func (b *Base) GetSettings() string {
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
return fmt.Sprintf("%v", SettingsStaged)
|
|
}
|
|
|
|
// GetStatus returns status data
|
|
func (b *Base) GetStatus() string {
|
|
return `
|
|
GoCryptoTrader Service: Online
|
|
Service Started: ` + ServiceStarted.String()
|
|
}
|