Add currency pair support

This commit is contained in:
Adrian Gallagher
2017-04-18 23:16:18 +10:00
parent 7457a7a209
commit d526e9f2a9
23 changed files with 484 additions and 320 deletions

View File

@@ -3,6 +3,8 @@ package orderbook
import (
"errors"
"time"
"github.com/thrasher-/gocryptotrader/currency/pair"
)
var (
@@ -19,16 +21,15 @@ type OrderbookItem struct {
}
type OrderbookBase struct {
FirstCurrency string `json:"first_currency"`
SecondCurrency string `json:"second_currency"`
CurrencyPair string `json:"currency_pair"`
Bids []OrderbookItem `json:"bids"`
Asks []OrderbookItem `json:"asks"`
LastUpdated time.Time `json:"last_updated"`
Pair pair.CurrencyPair `json:"pair"`
CurrencyPair string `json:"CurrencyPair"`
Bids []OrderbookItem `json:"bids"`
Asks []OrderbookItem `json:"asks"`
LastUpdated time.Time `json:"last_updated"`
}
type Orderbook struct {
Orderbook map[string]map[string]OrderbookBase
Orderbook map[pair.CurrencyItem]map[pair.CurrencyItem]OrderbookBase
ExchangeName string
}
@@ -58,21 +59,21 @@ func (o *OrderbookBase) Update(Bids, Asks []OrderbookItem) {
o.LastUpdated = time.Now()
}
func GetOrderbook(exchange, firstCurrency, secondCurrency string) (OrderbookBase, error) {
func GetOrderbook(exchange string, p pair.CurrencyPair) (OrderbookBase, error) {
orderbook, err := GetOrderbookByExchange(exchange)
if err != nil {
return OrderbookBase{}, err
}
if !FirstCurrencyExists(exchange, firstCurrency) {
if !FirstCurrencyExists(exchange, p.GetFirstCurrency()) {
return OrderbookBase{}, errors.New(ErrPrimaryCurrencyNotFound)
}
if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) {
if !SecondCurrencyExists(exchange, p) {
return OrderbookBase{}, errors.New(ErrSecondaryCurrencyNotFound)
}
return orderbook.Orderbook[firstCurrency][secondCurrency], nil
return orderbook.Orderbook[p.GetFirstCurrency()][p.GetSecondCurrency()], nil
}
func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
@@ -84,7 +85,7 @@ func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
return nil, errors.New(ErrOrderbookForExchangeNotFound)
}
func FirstCurrencyExists(exchange, currency string) bool {
func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
for _, y := range Orderbooks {
if y.ExchangeName == exchange {
if _, ok := y.Orderbook[currency]; ok {
@@ -95,11 +96,11 @@ func FirstCurrencyExists(exchange, currency string) bool {
return false
}
func SecondCurrencyExists(exchange, primary, secondary string) bool {
func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
for _, y := range Orderbooks {
if y.ExchangeName == exchange {
if _, ok := y.Orderbook[primary]; ok {
if _, ok := y.Orderbook[primary][secondary]; ok {
if _, ok := y.Orderbook[p.GetFirstCurrency()]; ok {
if _, ok := y.Orderbook[p.GetFirstCurrency()][p.GetSecondCurrency()]; ok {
return true
}
}
@@ -108,39 +109,39 @@ func SecondCurrencyExists(exchange, primary, secondary string) bool {
return false
}
func CreateNewOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) Orderbook {
func CreateNewOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew OrderbookBase) Orderbook {
orderbook := Orderbook{}
orderbook.ExchangeName = exchangeName
orderbook.Orderbook = make(map[string]map[string]OrderbookBase)
sMap := make(map[string]OrderbookBase)
sMap[secondCurrency] = orderbookNew
orderbook.Orderbook[firstCurrency] = sMap
orderbook.Orderbook = make(map[pair.CurrencyItem]map[pair.CurrencyItem]OrderbookBase)
sMap := make(map[pair.CurrencyItem]OrderbookBase)
sMap[p.GetSecondCurrency()] = orderbookNew
orderbook.Orderbook[p.GetFirstCurrency()] = sMap
Orderbooks = append(Orderbooks, orderbook)
return orderbook
}
func ProcessOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) {
orderbookNew.CurrencyPair = orderbookNew.FirstCurrency + orderbookNew.SecondCurrency
func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew OrderbookBase) {
orderbookNew.CurrencyPair = p.Pair().String()
if len(Orderbooks) == 0 {
CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew)
CreateNewOrderbook(exchangeName, p, orderbookNew)
return
} else {
orderbook, err := GetOrderbookByExchange(exchangeName)
if err != nil {
CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew)
CreateNewOrderbook(exchangeName, p, orderbookNew)
return
}
if FirstCurrencyExists(exchangeName, firstCurrency) {
if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) {
second := orderbook.Orderbook[firstCurrency]
second[secondCurrency] = orderbookNew
orderbook.Orderbook[firstCurrency] = second
if FirstCurrencyExists(exchangeName, p.GetFirstCurrency()) {
if !SecondCurrencyExists(exchangeName, p) {
second := orderbook.Orderbook[p.GetFirstCurrency()]
second[p.GetSecondCurrency()] = orderbookNew
orderbook.Orderbook[p.GetFirstCurrency()] = second
return
}
}
sMap := make(map[string]OrderbookBase)
sMap[secondCurrency] = orderbookNew
orderbook.Orderbook[firstCurrency] = sMap
sMap := make(map[pair.CurrencyItem]OrderbookBase)
sMap[p.GetSecondCurrency()] = orderbookNew
orderbook.Orderbook[p.GetFirstCurrency()] = sMap
}
}