Skip to content

Commit

Permalink
Engine improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jun 10, 2019
1 parent 04c7c48 commit f777e68
Show file tree
Hide file tree
Showing 88 changed files with 2,037 additions and 1,413 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
deadline: 30s
deadline: 40s
issues-exit-code: 1
tests: true
skip-dirs:
Expand Down
10 changes: 9 additions & 1 deletion cmd/exchange_wrapper_coverage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ func testWrappers(e exchange.IBotExchange) []string {
funcs = append(funcs, "GetFundingHistory")
}

_, err = e.SubmitOrder(p, exchange.BuyOrderSide, exchange.LimitOrderType, 1000000, 10000000000, "meow")
s := &exchange.OrderSubmission{
Pair: p,
OrderSide: exchange.BuyOrderSide,
OrderType: exchange.LimitOrderType,
Amount: 1000000,
Price: 10000000000,
ClientID: "meow",
}
_, err = e.SubmitOrder(s)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "SubmitOrder")
}
Expand Down
47 changes: 47 additions & 0 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,53 @@ func disableExchange(c *cli.Context) error {
return nil
}

var getExchangeOTPCommand = cli.Command{
Name: "getexchangeotp",
Usage: "gets a specific exchanges otp code",
ArgsUsage: "<exchange>",
Action: getExchangeOTPCode,
Flags: []cli.Flag{
cli.StringFlag{
Name: "exchange",
Usage: "the exchange to get the otp code for",
},
},
}

func getExchangeOTPCode(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowCommandHelp(c, "getexchangeotp")
return nil
}

conn, err := setupClient()
if err != nil {
return err
}
defer conn.Close()

var exchangeName string
if c.IsSet("exchange") {
exchangeName = c.String("exchange")
} else {
exchangeName = c.Args().First()
}

client := gctrpc.NewGoCryptoTraderClient(conn)
result, err := client.GetExchangeOTPCode(context.Background(),
&gctrpc.GenericExchangeNameRequest{
Exchange: exchangeName,
},
)

if err != nil {
return err
}

jsonOutput(result)
return nil
}

var getExchangeInfoCommand = cli.Command{
Name: "getexchangeinfo",
Usage: "gets a specific exchanges info",
Expand Down
1 change: 1 addition & 0 deletions cmd/gctcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func main() {
getExchangesCommand,
enableExchangeCommand,
disableExchangeCommand,
getExchangeOTPCommand,
getExchangeInfoCommand,
getTickerCommand,
getTickersCommand,
Expand Down
File renamed without changes.
125 changes: 3 additions & 122 deletions communications/base/base.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 `
Expand Down
49 changes: 2 additions & 47 deletions communications/base/base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
Expand All @@ -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)
}
}
}
Expand All @@ -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}
}
Loading

0 comments on commit f777e68

Please sign in to comment.