Skip to content

Commit

Permalink
Refactor stats and link up to ticker routine
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Sep 14, 2017
1 parent 87633c2 commit 69aa445
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 132 deletions.
9 changes: 9 additions & 0 deletions currency/pair/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ func (c CurrencyPair) Display(delimiter string, uppercase bool) CurrencyItem {
return pair.Lower()
}

// Equal compares two currency pairs and returns whether or not they are equal
func (c CurrencyPair) Equal(p CurrencyPair) bool {
if c.FirstCurrency.Upper() == p.FirstCurrency.Upper() &&
c.SecondCurrency.Upper() == p.SecondCurrency.Upper() {
return true
}
return false
}

// NewCurrencyPairDelimiter splits the desired currency string at delimeter,
// the returns a CurrencyPair struct
func NewCurrencyPairDelimiter(currency, delimiter string) CurrencyPair {
Expand Down
24 changes: 24 additions & 0 deletions currency/pair/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ func TestDisplay(t *testing.T) {
}
}

func TestEqual(t *testing.T) {
t.Parallel()
pair := NewCurrencyPair("BTC", "USD")
secondPair := NewCurrencyPair("btc", "uSd")
actual := pair.Equal(secondPair)
expected := true
if actual != expected {
t.Errorf(
"Test failed. Equal(): %v was not equal to expected value: %v",
actual, expected,
)
}

secondPair.SecondCurrency = "ETH"
actual = pair.Equal(secondPair)
expected = false
if actual != expected {
t.Errorf(
"Test failed. Equal(): %v was not equal to expected value: %v",
actual, expected,
)
}
}

func TestNewCurrencyPair(t *testing.T) {
t.Parallel()
pair := NewCurrencyPair("BTC", "USD")
Expand Down
2 changes: 0 additions & 2 deletions exchanges/btce/btce_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/stats"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)

Expand Down Expand Up @@ -45,7 +44,6 @@ func (b *BTCE) Run() {
x = common.StringToUpper(x[0:3] + x[4:])
log.Printf("BTC-e %s: Last %f High %f Low %f Volume %f\n", x, y.Last, y.High, y.Low, y.Vol_cur)
b.Ticker[x] = y
stats.AddExchangeInfo(b.GetName(), common.StringToUpper(x[0:3]), common.StringToUpper(x[4:]), y.Last, y.Vol_cur)
}
}()
time.Sleep(time.Second * b.RESTPollingDelay)
Expand Down
131 changes: 71 additions & 60 deletions exchanges/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,124 @@ package stats
import (
"sort"

"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/currency/pair"
)

type ExchangeInfo struct {
Exchange string
FirstCurrency string
FiatCurrency string
Price float64
Volume float64
// Item holds various fields for storing currency pair stats
type Item struct {
Exchange string
Pair pair.CurrencyPair
AssetType string
Price float64
Volume float64
}

var ExchInfo []ExchangeInfo
// Items var array
var Items []Item

type ByPrice []ExchangeInfo
// ByPrice allows sorting by price
type ByPrice []Item

func (this ByPrice) Len() int {
return len(this)
func (b ByPrice) Len() int {
return len(b)
}

func (this ByPrice) Less(i, j int) bool {
return this[i].Price < this[j].Price
func (b ByPrice) Less(i, j int) bool {
return b[i].Price < b[j].Price
}

func (this ByPrice) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
func (b ByPrice) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}

type ByVolume []ExchangeInfo
// ByVolume allows sorting by volume
type ByVolume []Item

func (this ByVolume) Len() int {
return len(this)
func (b ByVolume) Len() int {
return len(b)
}

func (this ByVolume) Less(i, j int) bool {
return this[i].Volume < this[j].Volume
func (b ByVolume) Less(i, j int) bool {
return b[i].Volume < b[j].Volume
}

func (this ByVolume) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
func (b ByVolume) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}

func AddExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
if currency.BaseCurrencies == "" {
currency.BaseCurrencies = currency.DefaultCurrencies
}

if !currency.IsFiatCurrency(fiat) {
// Add adds or updates the item stats
func Add(exchange string, p pair.CurrencyPair, assetType string, price, volume float64) {
if exchange == "" || assetType == "" || price == 0 || volume == 0 || p.FirstCurrency == "" || p.SecondCurrency == "" {
return
}
AppendExchangeInfo(exchange, crypto, fiat, price, volume)

Append(exchange, p, assetType, price, volume)
}

func AppendExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
if ExchangeInfoAlreadyExists(exchange, crypto, fiat, price, volume) {
// Append adds or updates the item stats for a specific
// currency pair and asset type
func Append(exchange string, p pair.CurrencyPair, assetType string, price, volume float64) {
if AlreadyExists(exchange, p, assetType, price, volume) {
return
}

exch := ExchangeInfo{}
exch.Exchange = exchange
exch.FirstCurrency = crypto
exch.FiatCurrency = fiat
exch.Price = price
exch.Volume = volume
ExchInfo = append(ExchInfo, exch)
i := Item{
Exchange: exchange,
Pair: p,
AssetType: assetType,
Price: price,
Volume: volume,
}

Items = append(Items, i)
}

func ExchangeInfoAlreadyExists(exchange, crypto, fiat string, price, volume float64) bool {
for i := range ExchInfo {
if ExchInfo[i].Exchange == exchange && ExchInfo[i].FirstCurrency == crypto && ExchInfo[i].FiatCurrency == fiat {
ExchInfo[i].Price, ExchInfo[i].Volume = price, volume
// AlreadyExists checks to see if item info already exists
// for a specific currency pair and asset type
func AlreadyExists(exchange string, p pair.CurrencyPair, assetType string, price, volume float64) bool {
for i := range Items {
if Items[i].Exchange == exchange && Items[i].Pair.Equal(p) && Items[i].AssetType == assetType {
Items[i].Price, Items[i].Volume = price, volume
return true
}
}
return false
}

func SortExchangesByVolume(crypto, fiat string, reverse bool) []ExchangeInfo {
info := []ExchangeInfo{}

for _, x := range ExchInfo {
if x.FirstCurrency == crypto && x.FiatCurrency == fiat {
info = append(info, x)
// SortExchangesByVolume sorts item info by volume for a specific
// currency pair and asset type. Reverse will reverse the order from lowest to
// highest
func SortExchangesByVolume(p pair.CurrencyPair, assetType string, reverse bool) []Item {
var result []Item
for x := range Items {
if Items[x].Pair.Equal(p) && Items[x].AssetType == assetType {
result = append(result, Items[x])
}
}

if reverse {
sort.Sort(sort.Reverse(ByVolume(info)))
sort.Sort(sort.Reverse(ByVolume(result)))
} else {
sort.Sort(ByVolume(info))
sort.Sort(ByVolume(result))
}
return info
return result
}

func SortExchangesByPrice(crypto, fiat string, reverse bool) []ExchangeInfo {
info := []ExchangeInfo{}

for _, x := range ExchInfo {
if x.FirstCurrency == crypto && x.FiatCurrency == fiat {
info = append(info, x)
// SortExchangesByPrice sorts item info by volume for a specific
// currency pair and asset type. Reverse will reverse the order from lowest to
// highest
func SortExchangesByPrice(p pair.CurrencyPair, assetType string, reverse bool) []Item {
var result []Item
for x := range Items {
if Items[x].Pair.Equal(p) && Items[x].AssetType == assetType {
result = append(result, Items[x])
}
}

if reverse {
sort.Sort(sort.Reverse(ByPrice(info)))
sort.Sort(sort.Reverse(ByPrice(result)))
} else {
sort.Sort(ByPrice(info))
sort.Sort(ByPrice(result))
}
return info
return result
}
Loading

0 comments on commit 69aa445

Please sign in to comment.