Engine improvements

This commit is contained in:
Adrian Gallagher
2019-06-10 20:02:09 +10:00
parent 04c7c4895f
commit f777e68716
88 changed files with 2037 additions and 1413 deletions

View File

@@ -1,56 +1,15 @@
package base
import (
"fmt"
"strings"
"sync"
"time"
"github.com/thrasher-/gocryptotrader/exchanges/assets"
"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[assets.AssetType]map[string]ticker.Price
OrderbookStaged map[string]map[assets.AssetType]map[string]Orderbook
PortfolioStaged Portfolio
SettingsStaged Settings
ServiceStarted time.Time
m sync.Mutex
ServiceStarted time.Time
)
// 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
@@ -61,9 +20,8 @@ type Base struct {
// Event is a generalise event type
type Event struct {
Type string
GainLoss string
TradeDetails string
Type string
Message string
}
// IsEnabled returns if the comms package has been enabled in the configuration
@@ -82,83 +40,6 @@ 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 strings.Join(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 strings.Join(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 `

View File

@@ -4,9 +4,6 @@ import (
"time"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/exchanges/assets"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
log "github.com/thrasher-/gocryptotrader/logger"
)
@@ -26,10 +23,7 @@ type ICommunicate interface {
// Setup sets up communication variables and intiates a connection to the
// communication mediums
func (c IComm) Setup() {
TickerStaged = make(map[string]map[assets.AssetType]map[string]ticker.Price)
OrderbookStaged = make(map[string]map[assets.AssetType]map[string]Orderbook)
ServiceStarted = time.Now()
for i := range c {
if c[i].IsEnabled() && !c[i].IsConnected() {
err := c[i].Connect()
@@ -46,8 +40,8 @@ func (c IComm) PushEvent(event Event) {
if c[i].IsEnabled() && c[i].IsConnected() {
err := c[i].PushEvent(event)
if err != nil {
log.Errorf("Communications error - PushEvent() in package %s with %v",
c[i].GetName(), event)
log.Errorf("Communications error - PushEvent() in package %s with %v. Err %s",
c[i].GetName(), event, err)
}
}
}
@@ -68,42 +62,3 @@ func (c IComm) GetEnabledCommunicationMediums() {
log.Warnf("Communications: No communication mediums are enabled.")
}
}
// StageTickerData stages updated ticker data for the communications package
func (c IComm) StageTickerData(exchangeName string, assetType assets.AssetType, tickerPrice *ticker.Price) {
m.Lock()
defer m.Unlock()
if _, ok := TickerStaged[exchangeName]; !ok {
TickerStaged[exchangeName] = make(map[assets.AssetType]map[string]ticker.Price)
}
if _, ok := TickerStaged[exchangeName][assetType]; !ok {
TickerStaged[exchangeName][assetType] = make(map[string]ticker.Price)
}
TickerStaged[exchangeName][assetType][tickerPrice.Pair.String()] = *tickerPrice
}
// StageOrderbookData stages updated orderbook data for the communications
// package
func (c IComm) StageOrderbookData(exchangeName string, assetType assets.AssetType, ob *orderbook.Base) {
m.Lock()
defer m.Unlock()
if _, ok := OrderbookStaged[exchangeName]; !ok {
OrderbookStaged[exchangeName] = make(map[assets.AssetType]map[string]Orderbook)
}
if _, ok := OrderbookStaged[exchangeName][assetType]; !ok {
OrderbookStaged[exchangeName][assetType] = make(map[string]Orderbook)
}
_, totalAsks := ob.TotalAsksAmount()
_, totalBids := ob.TotalBidsAmount()
OrderbookStaged[exchangeName][assetType][ob.Pair.String()] = Orderbook{
CurrencyPair: ob.Pair.String(),
TotalAsks: totalAsks,
TotalBids: totalBids}
}

View File

@@ -2,14 +2,10 @@ package base
import (
"testing"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
var (
b Base
i IComm
)
func TestStart(t *testing.T) {
@@ -39,41 +35,6 @@ func TestGetName(t *testing.T) {
}
}
func TestGetTicker(t *testing.T) {
v := b.GetTicker("ANX")
if v != "" {
t.Error("test failed - base GetTicker() error")
}
}
func TestGetOrderbook(t *testing.T) {
v := b.GetOrderbook("ANX")
if v != "" {
t.Error("test failed - base GetOrderbook() error")
}
}
func TestGetPortfolio(t *testing.T) {
v := b.GetPortfolio()
if v != "{}" {
t.Error("test failed - base GetPortfolio() error")
}
}
func TestGetSettings(t *testing.T) {
v := b.GetSettings()
if v != "{ }" {
t.Error("test failed - base GetSettings() error")
}
}
func TestGetStatus(t *testing.T) {
v := b.GetStatus()
if v == "" {
t.Error("test failed - base GetStatus() error")
}
}
type CommunicationProvider struct {
ICommunicate
@@ -166,58 +127,3 @@ func TestPushEvent(t *testing.T) {
}
}
}
func TestStageTickerData(t *testing.T) {
_, ok := TickerStaged["bitstamp"]["someAsset"]["BTCUSD"]
if ok {
t.Fatalf("key should not exists")
}
price := ticker.Price{}
var i IComm
i.Setup()
i.StageTickerData("bitstamp", "someAsset", &price)
_, ok = TickerStaged["bitstamp"]["someAsset"][price.Pair.String()]
if !ok {
t.Fatalf("key should exists")
}
}
func TestOrderbookData(t *testing.T) {
_, ok := OrderbookStaged["bitstamp"]["someAsset"]["someOrderbook"]
if ok {
t.Fatal("key should not exists")
}
ob := orderbook.Base{
Asks: []orderbook.Item{
{Amount: 1, Price: 2, ID: 3},
{Amount: 4, Price: 5, ID: 6},
},
}
var i IComm
i.Setup()
i.StageOrderbookData("bitstamp", "someAsset", &ob)
orderbook, ok := OrderbookStaged["bitstamp"]["someAsset"][ob.Pair.String()]
if !ok {
t.Fatal("key should exists")
}
if ob.Pair.String() != orderbook.CurrencyPair {
t.Fatal("currency missmatched")
}
_, totalAsks := ob.TotalAsksAmount()
if totalAsks != orderbook.TotalAsks {
t.Fatal("total asks missmatched")
}
_, totalBids := ob.TotalBidsAmount()
if totalBids != orderbook.TotalBids {
t.Fatal("total bids missmatched")
}
}