Engine QA - Order update (#372)

Engine QA - Order update
This commit is contained in:
Ryan O'Hara-Reid
2019-10-30 14:06:48 +11:00
committed by Adrian Gallagher
parent 242b02c382
commit 63a9e9fcb1
89 changed files with 3077 additions and 2876 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
@@ -120,9 +121,9 @@ func (c *COINUT) NewOrder(instrumentID int64, quantity, price float64, buy bool,
params["price"] = fmt.Sprintf("%v", price)
}
params["qty"] = fmt.Sprintf("%v", quantity)
params["side"] = exchange.BuyOrderSide.ToString()
params["side"] = order.Buy.String()
if !buy {
params["side"] = exchange.SellOrderSide.ToString()
params["side"] = order.Sell.String()
}
params["client_ord_id"] = orderID

View File

@@ -9,6 +9,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
)
@@ -262,8 +263,8 @@ func TestGetActiveOrders(t *testing.T) {
c.SetDefaults()
TestSetup(t)
var getOrdersRequest = exchange.GetOrdersRequest{
OrderType: exchange.AnyOrderType,
var getOrdersRequest = order.GetOrdersRequest{
OrderType: order.AnyType,
}
_, err := c.GetActiveOrders(&getOrdersRequest)
@@ -276,8 +277,8 @@ func TestGetOrderHistory(t *testing.T) {
c.SetDefaults()
TestSetup(t)
var getOrdersRequest = exchange.GetOrdersRequest{
OrderType: exchange.AnyOrderType,
var getOrdersRequest = order.GetOrdersRequest{
OrderType: order.AnyType,
Currencies: []currency.Pair{currency.NewPair(currency.BTC,
currency.LTC)},
}
@@ -302,13 +303,13 @@ func TestSubmitOrder(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
var orderSubmission = &exchange.OrderSubmission{
var orderSubmission = &order.Submit{
Pair: currency.Pair{
Base: currency.BTC,
Quote: currency.USD,
},
OrderSide: exchange.BuyOrderSide,
OrderType: exchange.LimitOrderType,
OrderSide: order.Buy,
OrderType: order.Limit,
Price: 1,
Amount: 1,
ClientID: "meowOrder",
@@ -332,7 +333,7 @@ func TestCancelExchangeOrder(t *testing.T) {
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
var orderCancellation = &exchange.OrderCancellation{
var orderCancellation = &order.Cancel{
OrderID: "1",
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
AccountID: "1",
@@ -359,7 +360,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
var orderCancellation = &exchange.OrderCancellation{
var orderCancellation = &order.Cancel{
OrderID: "1",
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
AccountID: "1",
@@ -375,8 +376,8 @@ func TestCancelAllExchangeOrders(t *testing.T) {
t.Errorf("Could not cancel orders: %v", err)
}
if len(resp.OrderStatus) > 0 {
t.Errorf("%v orders failed to cancel", len(resp.OrderStatus))
if len(resp.Status) > 0 {
t.Errorf("%v orders failed to cancel", len(resp.Status))
}
}
@@ -395,7 +396,7 @@ func TestGetAccountInfo(t *testing.T) {
}
func TestModifyOrder(t *testing.T) {
_, err := c.ModifyOrder(&exchange.ModifyOrder{})
_, err := c.ModifyOrder(&order.Modify{})
if err == nil {
t.Error("ModifyOrder() Expected error")
}
@@ -480,7 +481,7 @@ func TestWsAuthSubmitOrder(t *testing.T) {
Currency: currency.NewPair(currency.LTC, currency.BTC),
OrderID: 1,
Price: 1,
Side: exchange.BuyOrderSide,
Side: order.Buy,
}
_, err := c.wsSubmitOrder(&order)
if err != nil {
@@ -499,14 +500,14 @@ func TestWsAuthSubmitOrders(t *testing.T) {
Currency: currency.NewPair(currency.LTC, currency.BTC),
OrderID: 1,
Price: 1,
Side: exchange.BuyOrderSide,
Side: order.Buy,
}
order2 := WsSubmitOrderParameters{
Amount: 3,
Currency: currency.NewPair(currency.LTC, currency.BTC),
OrderID: 2,
Price: 2,
Side: exchange.BuyOrderSide,
Side: order.Buy,
}
_, err := c.wsSubmitOrders([]WsSubmitOrderParameters{order1, order2})
if err != nil {

View File

@@ -5,7 +5,7 @@ import (
"sync"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
// GenericResponse is the generic response you will get from coinut
@@ -477,7 +477,7 @@ type WsSubmitOrderRequest struct {
// WsSubmitOrderParameters ws request parameters
type WsSubmitOrderParameters struct {
Currency currency.Pair
Side exchange.OrderSide
Side order.Side
Amount, Price float64
OrderID int64
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wsorderbook"
@@ -305,7 +306,7 @@ func (c *COINUT) WsProcessOrderbookUpdate(update *WsOrderbookUpdate) error {
UpdateID: update.TransID,
AssetType: asset.Spot,
}
if strings.EqualFold(update.Side, exchange.BuyOrderSide.ToLower().ToString()) {
if strings.EqualFold(update.Side, order.Buy.Lower()) {
bufferUpdate.Bids = []orderbook.Item{{Price: update.Price, Amount: update.Volume}}
} else {
bufferUpdate.Asks = []orderbook.Item{{Price: update.Price, Amount: update.Volume}}

View File

@@ -13,6 +13,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
@@ -430,19 +431,15 @@ func (c *COINUT) GetExchangeHistory(p currency.Pair, assetType asset.Item) ([]ex
}
// SubmitOrder submits a new order
func (c *COINUT) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitOrderResponse, error) {
var submitOrderResponse exchange.SubmitOrderResponse
if order == nil {
return submitOrderResponse, exchange.ErrOrderSubmissionIsNil
}
if err := order.Validate(); err != nil {
func (c *COINUT) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
var submitOrderResponse order.SubmitResponse
if err := s.Validate(); err != nil {
return submitOrderResponse, err
}
var APIresponse interface{}
isBuyOrder := order.OrderSide == exchange.BuyOrderSide
clientIDInt, err := strconv.ParseUint(order.ClientID, 0, 32)
isBuyOrder := s.OrderSide == order.Buy
clientIDInt, err := strconv.ParseUint(s.ClientID, 0, 32)
if err != nil {
return submitOrderResponse, err
}
@@ -456,32 +453,40 @@ func (c *COINUT) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitOr
}
}
currencyID := c.instrumentMap.LookupID(c.FormatExchangeCurrency(order.Pair,
currencyID := c.instrumentMap.LookupID(c.FormatExchangeCurrency(s.Pair,
asset.Spot).String())
if currencyID == 0 {
return submitOrderResponse, errLookupInstrumentID
}
switch order.OrderType {
case exchange.LimitOrderType:
APIresponse, err = c.NewOrder(currencyID, order.Amount, order.Price,
isBuyOrder, clientIDUint)
case exchange.MarketOrderType:
APIresponse, err = c.NewOrder(currencyID, order.Amount, 0, isBuyOrder,
switch s.OrderType {
case order.Limit:
APIresponse, err = c.NewOrder(currencyID,
s.Amount,
s.Price,
isBuyOrder,
clientIDUint)
case order.Market:
APIresponse, err = c.NewOrder(currencyID,
s.Amount,
0,
isBuyOrder,
clientIDUint)
}
switch apiResp := APIresponse.(type) {
case OrdersBase:
orderResult := apiResp
submitOrderResponse.OrderID = fmt.Sprintf("%v", orderResult.OrderID)
submitOrderResponse.OrderID = strconv.FormatInt(orderResult.OrderID, 10)
case OrderFilledResponse:
orderResult := apiResp
submitOrderResponse.OrderID = fmt.Sprintf("%v", orderResult.Order.OrderID)
submitOrderResponse.OrderID = strconv.FormatInt(orderResult.Order.OrderID, 10)
case OrderRejectResponse:
orderResult := apiResp
submitOrderResponse.OrderID = fmt.Sprintf("%v", orderResult.OrderID)
err = fmt.Errorf("orderID: %v was rejected: %v", orderResult.OrderID, orderResult.Reasons)
submitOrderResponse.OrderID = strconv.FormatInt(orderResult.OrderID, 10)
err = fmt.Errorf("orderID: %d was rejected: %v",
orderResult.OrderID,
orderResult.Reasons)
}
if err == nil {
@@ -493,12 +498,12 @@ func (c *COINUT) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitOr
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (c *COINUT) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
func (c *COINUT) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (c *COINUT) CancelOrder(order *exchange.OrderCancellation) error {
func (c *COINUT) CancelOrder(order *order.Cancel) error {
orderIDInt, err := strconv.ParseInt(order.OrderID, 10, 64)
if err != nil {
return err
@@ -523,13 +528,14 @@ func (c *COINUT) CancelOrder(order *exchange.OrderCancellation) error {
}
// CancelAllOrders cancels all orders associated with a currency pair
func (c *COINUT) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
func (c *COINUT) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, error) {
// TODO, this is a terrible implementation. Requires DB to improve
// Coinut provides no way of retrieving orders without a currency
// So we need to retrieve all currencies, then retrieve orders for each currency
// Then cancel. Advisable to never use this until DB due to performance
cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{
OrderStatus: make(map[string]string),
// So we need to retrieve all currencies, then retrieve orders for each
// currency then cancel. Advisable to never use this until DB due to
// performance.
cancelAllOrdersResponse := order.CancelAllResponse{
Status: make(map[string]string),
}
if !c.instrumentMap.IsLoaded() {
@@ -566,7 +572,7 @@ func (c *COINUT) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cancel
for _, order := range resp.Results {
if order.Status != "OK" {
cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(order.OrderID, 10)] = order.Status
cancelAllOrdersResponse.Status[strconv.FormatInt(order.OrderID, 10)] = order.Status
}
}
}
@@ -575,8 +581,8 @@ func (c *COINUT) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cancel
}
// GetOrderInfo returns information on a current open order
func (c *COINUT) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
return exchange.OrderDetail{}, common.ErrNotYetImplemented
func (c *COINUT) GetOrderInfo(orderID string) (order.Detail, error) {
return order.Detail{}, common.ErrNotYetImplemented
}
// GetDepositAddress returns a deposit address for a specified currency
@@ -617,7 +623,7 @@ func (c *COINUT) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error)
}
// GetActiveOrders retrieves any orders that are active/open
func (c *COINUT) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
func (c *COINUT) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
if !c.instrumentMap.IsLoaded() {
err := c.SeedInstruments()
if err != nil {
@@ -626,9 +632,9 @@ func (c *COINUT) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([
}
var instrumentsToUse []int64
if len(getOrdersRequest.Currencies) > 0 {
for x := range getOrdersRequest.Currencies {
currency := c.FormatExchangeCurrency(getOrdersRequest.Currencies[x],
if len(req.Currencies) > 0 {
for x := range req.Currencies {
currency := c.FormatExchangeCurrency(req.Currencies[x],
asset.Spot).String()
instrumentsToUse = append(instrumentsToUse,
c.instrumentMap.LookupID(currency))
@@ -641,7 +647,7 @@ func (c *COINUT) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([
return nil, errors.New("no instrument IDs to use")
}
var orders []exchange.OrderDetail
var orders []order.Detail
for x := range instrumentsToUse {
openOrders, err := c.GetOpenOrders(instrumentsToUse[x])
if err != nil {
@@ -652,9 +658,9 @@ func (c *COINUT) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([
p := currency.NewPairFromFormattedPairs(curr,
c.GetEnabledPairs(asset.Spot),
c.GetPairFormat(asset.Spot, true))
orderSide := exchange.OrderSide(strings.ToUpper(openOrders.Orders[y].Side))
orderSide := order.Side(strings.ToUpper(openOrders.Orders[y].Side))
orderDate := time.Unix(openOrders.Orders[y].Timestamp, 0)
orders = append(orders, exchange.OrderDetail{
orders = append(orders, order.Detail{
ID: strconv.FormatInt(openOrders.Orders[y].OrderID, 10),
Amount: openOrders.Orders[y].Quantity,
Price: openOrders.Orders[y].Price,
@@ -666,14 +672,14 @@ func (c *COINUT) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([
}
}
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
order.FilterOrdersByTickRange(&orders, req.StartTicks, req.EndTicks)
order.FilterOrdersBySide(&orders, req.OrderSide)
return orders, nil
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (c *COINUT) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
func (c *COINUT) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
if !c.instrumentMap.IsLoaded() {
err := c.SeedInstruments()
if err != nil {
@@ -682,9 +688,9 @@ func (c *COINUT) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([
}
var instrumentsToUse []int64
if len(getOrdersRequest.Currencies) > 0 {
for x := range getOrdersRequest.Currencies {
currency := c.FormatExchangeCurrency(getOrdersRequest.Currencies[x],
if len(req.Currencies) > 0 {
for x := range req.Currencies {
currency := c.FormatExchangeCurrency(req.Currencies[x],
asset.Spot).String()
instrumentsToUse = append(instrumentsToUse,
c.instrumentMap.LookupID(currency))
@@ -697,7 +703,7 @@ func (c *COINUT) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([
return nil, errors.New("no instrument IDs to use")
}
var allOrders []exchange.OrderDetail
var allOrders []order.Detail
for x := range instrumentsToUse {
orders, err := c.GetTradeHistory(instrumentsToUse[x], -1, -1)
if err != nil {
@@ -708,10 +714,9 @@ func (c *COINUT) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([
p := currency.NewPairFromFormattedPairs(curr,
c.GetEnabledPairs(asset.Spot),
c.GetPairFormat(asset.Spot, true))
orderSide := exchange.OrderSide(
strings.ToUpper(orders.Trades[y].Order.Side))
orderSide := order.Side(strings.ToUpper(orders.Trades[y].Order.Side))
orderDate := time.Unix(orders.Trades[y].Order.Timestamp, 0)
allOrders = append(allOrders, exchange.OrderDetail{
allOrders = append(allOrders, order.Detail{
ID: strconv.FormatInt(orders.Trades[y].Order.OrderID, 10),
Amount: orders.Trades[y].Order.Quantity,
Price: orders.Trades[y].Order.Price,
@@ -724,9 +729,8 @@ func (c *COINUT) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([
}
exchange.FilterOrdersByTickRange(&allOrders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
exchange.FilterOrdersBySide(&allOrders, getOrdersRequest.OrderSide)
order.FilterOrdersByTickRange(&allOrders, req.StartTicks, req.EndTicks)
order.FilterOrdersBySide(&allOrders, req.OrderSide)
return allOrders, nil
}