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

@@ -10,6 +10,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"
log "github.com/thrasher-corp/gocryptotrader/logger"
@@ -89,11 +90,15 @@ func (o *OKGroup) UpdateOrderbook(p currency.Pair, assetType asset.Item) (resp o
for x := range orderbookNew.Bids {
amount, convErr := strconv.ParseFloat(orderbookNew.Bids[x][1], 64)
if convErr != nil {
log.Errorf(log.ExchangeSys, "Could not convert %v to float64", orderbookNew.Bids[x][1])
log.Errorf(log.ExchangeSys,
"Could not convert %v to float64",
orderbookNew.Bids[x][1])
}
price, convErr := strconv.ParseFloat(orderbookNew.Bids[x][0], 64)
if convErr != nil {
log.Errorf(log.ExchangeSys, "Could not convert %v to float64", orderbookNew.Bids[x][0])
log.Errorf(log.ExchangeSys,
"Could not convert %v to float64",
orderbookNew.Bids[x][0])
}
resp.Bids = append(resp.Bids, orderbook.Item{
Amount: amount,
@@ -104,11 +109,15 @@ func (o *OKGroup) UpdateOrderbook(p currency.Pair, assetType asset.Item) (resp o
for x := range orderbookNew.Asks {
amount, convErr := strconv.ParseFloat(orderbookNew.Asks[x][1], 64)
if convErr != nil {
log.Errorf(log.ExchangeSys, "Could not convert %v to float64", orderbookNew.Asks[x][1])
log.Errorf(log.ExchangeSys,
"Could not convert %v to float64",
orderbookNew.Asks[x][1])
}
price, convErr := strconv.ParseFloat(orderbookNew.Asks[x][0], 64)
if convErr != nil {
log.Errorf(log.ExchangeSys, "Could not convert %v to float64", orderbookNew.Asks[x][0])
log.Errorf(log.ExchangeSys,
"Could not convert %v to float64",
orderbookNew.Asks[x][0])
}
resp.Asks = append(resp.Asks, orderbook.Item{
Amount: amount,
@@ -134,20 +143,25 @@ func (o *OKGroup) GetAccountInfo() (resp exchange.AccountInfo, err error) {
currencies, err := o.GetSpotTradingAccounts()
currencyAccount := exchange.Account{}
for _, curr := range currencies {
hold, err := strconv.ParseFloat(curr.Hold, 64)
for i := range currencies {
hold, err := strconv.ParseFloat(currencies[i].Hold, 64)
if err != nil {
log.Errorf(log.ExchangeSys, "Could not convert %v to float64", curr.Hold)
log.Errorf(log.ExchangeSys,
"Could not convert %v to float64",
currencies[i].Hold)
}
totalValue, err := strconv.ParseFloat(curr.Balance, 64)
totalValue, err := strconv.ParseFloat(currencies[i].Balance, 64)
if err != nil {
log.Errorf(log.ExchangeSys, "Could not convert %v to float64", curr.Balance)
log.Errorf(log.ExchangeSys,
"Could not convert %v to float64",
currencies[i].Balance)
}
currencyAccount.Currencies = append(currencyAccount.Currencies, exchange.AccountCurrencyInfo{
CurrencyName: currency.NewCode(curr.Currency),
Hold: hold,
TotalValue: totalValue,
})
currencyAccount.Currencies = append(currencyAccount.Currencies,
exchange.AccountCurrencyInfo{
CurrencyName: currency.NewCode(currencies[i].Currency),
Hold: hold,
TotalValue: totalValue,
})
}
resp.Accounts = append(resp.Accounts, currencyAccount)
@@ -161,9 +175,9 @@ func (o *OKGroup) GetFundingHistory() (resp []exchange.FundHistory, err error) {
if err != nil {
return
}
for _, deposit := range accountDepositHistory {
for x := range accountDepositHistory {
orderStatus := ""
switch deposit.Status {
switch accountDepositHistory[x].Status {
case 0:
orderStatus = "waiting"
case 1:
@@ -173,12 +187,12 @@ func (o *OKGroup) GetFundingHistory() (resp []exchange.FundHistory, err error) {
}
resp = append(resp, exchange.FundHistory{
Amount: deposit.Amount,
Currency: deposit.Currency,
Amount: accountDepositHistory[x].Amount,
Currency: accountDepositHistory[x].Currency,
ExchangeName: o.Name,
Status: orderStatus,
Timestamp: deposit.Timestamp,
TransferID: deposit.TransactionID,
Timestamp: accountDepositHistory[x].Timestamp,
TransferID: accountDepositHistory[x].TransactionID,
TransferType: "deposit",
})
}
@@ -203,25 +217,21 @@ func (o *OKGroup) GetExchangeHistory(p currency.Pair, assetType asset.Item) ([]e
}
// SubmitOrder submits a new order
func (o *OKGroup) SubmitOrder(order *exchange.OrderSubmission) (resp exchange.SubmitOrderResponse, err error) {
if order == nil {
return resp, exchange.ErrOrderSubmissionIsNil
}
err = order.Validate()
func (o *OKGroup) SubmitOrder(s *order.Submit) (resp order.SubmitResponse, err error) {
err = s.Validate()
if err != nil {
return resp, err
}
request := PlaceSpotOrderRequest{
ClientOID: order.ClientID,
InstrumentID: o.FormatExchangeCurrency(order.Pair, asset.Spot).String(),
Side: strings.ToLower(order.OrderSide.ToString()),
Type: strings.ToLower(order.OrderType.ToString()),
Size: strconv.FormatFloat(order.Amount, 'f', -1, 64),
ClientOID: s.ClientID,
InstrumentID: o.FormatExchangeCurrency(s.Pair, asset.Spot).String(),
Side: strings.ToLower(s.OrderSide.String()),
Type: strings.ToLower(s.OrderType.String()),
Size: strconv.FormatFloat(s.Amount, 'f', -1, 64),
}
if order.OrderType == exchange.LimitOrderType {
request.Price = strconv.FormatFloat(order.Price, 'f', -1, 64)
if s.OrderType == order.Limit {
request.Price = strconv.FormatFloat(s.Price, 'f', -1, 64)
}
orderResponse, err := o.PlaceSpotOrder(&request)
@@ -236,12 +246,12 @@ func (o *OKGroup) SubmitOrder(order *exchange.OrderSubmission) (resp exchange.Su
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (o *OKGroup) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
func (o *OKGroup) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (o *OKGroup) CancelOrder(orderCancellation *exchange.OrderCancellation) (err error) {
func (o *OKGroup) CancelOrder(orderCancellation *order.Cancel) (err error) {
orderID, err := strconv.ParseInt(orderCancellation.OrderID, 10, 64)
if err != nil {
return
@@ -252,21 +262,22 @@ func (o *OKGroup) CancelOrder(orderCancellation *exchange.OrderCancellation) (er
OrderID: orderID,
})
if !orderCancellationResponse.Result {
err = fmt.Errorf("order %v failed to be cancelled", orderCancellationResponse.OrderID)
err = fmt.Errorf("order %d failed to be cancelled",
orderCancellationResponse.OrderID)
}
return
}
// CancelAllOrders cancels all orders associated with a currency pair
func (o *OKGroup) CancelAllOrders(orderCancellation *exchange.OrderCancellation) (resp exchange.CancelAllOrdersResponse, err error) {
func (o *OKGroup) CancelAllOrders(orderCancellation *order.Cancel) (resp order.CancelAllResponse, err error) {
orderIDs := strings.Split(orderCancellation.OrderID, ",")
resp.OrderStatus = make(map[string]string)
resp.Status = make(map[string]string)
var orderIDNumbers []int64
for _, i := range orderIDs {
orderIDNumber, strConvErr := strconv.ParseInt(i, 10, 64)
for i := range orderIDs {
orderIDNumber, strConvErr := strconv.ParseInt(orderIDs[i], 10, 64)
if strConvErr != nil {
resp.OrderStatus[i] = strConvErr.Error()
resp.Status[orderIDs[i]] = strConvErr.Error()
continue
}
orderIDNumbers = append(orderIDNumbers, orderIDNumber)
@@ -281,9 +292,9 @@ func (o *OKGroup) CancelAllOrders(orderCancellation *exchange.OrderCancellation)
return
}
for _, orderMap := range cancelOrdersResponse {
for _, cancelledOrder := range orderMap {
resp.OrderStatus[fmt.Sprintf("%v", cancelledOrder.OrderID)] = fmt.Sprintf("%v", cancelledOrder.Result)
for x := range cancelOrdersResponse {
for y := range cancelOrdersResponse[x] {
resp.Status[strconv.FormatInt(cancelOrdersResponse[x][y].OrderID, 10)] = strconv.FormatBool(cancelOrdersResponse[x][y].Result)
}
}
@@ -291,29 +302,29 @@ func (o *OKGroup) CancelAllOrders(orderCancellation *exchange.OrderCancellation)
}
// GetOrderInfo returns information on a current open order
func (o *OKGroup) GetOrderInfo(orderID string) (resp exchange.OrderDetail, err error) {
order, err := o.GetSpotOrder(GetSpotOrderRequest{OrderID: orderID})
func (o *OKGroup) GetOrderInfo(orderID string) (resp order.Detail, err error) {
mOrder, err := o.GetSpotOrder(GetSpotOrderRequest{OrderID: orderID})
if err != nil {
return
}
resp = exchange.OrderDetail{
Amount: order.Size,
CurrencyPair: currency.NewPairDelimiter(order.InstrumentID,
resp = order.Detail{
Amount: mOrder.Size,
CurrencyPair: currency.NewPairDelimiter(mOrder.InstrumentID,
o.GetPairFormat(asset.Spot, false).Delimiter),
Exchange: o.Name,
OrderDate: order.Timestamp,
ExecutedAmount: order.FilledSize,
Status: order.Status,
OrderSide: exchange.OrderSide(order.Side),
OrderDate: mOrder.Timestamp,
ExecutedAmount: mOrder.FilledSize,
Status: order.Status(mOrder.Status),
OrderSide: order.Side(mOrder.Side),
}
return
}
// GetDepositAddress returns a deposit address for a specified currency
func (o *OKGroup) GetDepositAddress(p currency.Code, accountID string) (_ string, err error) {
func (o *OKGroup) GetDepositAddress(p currency.Code, accountID string) (string, error) {
wallet, err := o.GetAccountDepositAddressForCurrency(p.Lower().String())
if err != nil || len(wallet) == 0 {
return
return "", err
}
return wallet[0].Address, nil
}
@@ -333,10 +344,13 @@ func (o *OKGroup) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.CryptoWi
return "", err
}
if !withdrawal.Result {
return fmt.Sprintf("%v", withdrawal.WithdrawalID), fmt.Errorf("could not withdraw currency %v to %v, no error specified", withdrawRequest.Currency.String(), withdrawRequest.Address)
return strconv.FormatInt(withdrawal.WithdrawalID, 10),
fmt.Errorf("could not withdraw currency %s to %s, no error specified",
withdrawRequest.Currency,
withdrawRequest.Address)
}
return fmt.Sprintf("%v", withdrawal.WithdrawalID), nil
return strconv.FormatInt(withdrawal.WithdrawalID, 10), nil
}
// WithdrawFiatFunds returns a withdrawal ID when a
@@ -352,27 +366,27 @@ func (o *OKGroup) WithdrawFiatFundsToInternationalBank(withdrawRequest *exchange
}
// GetActiveOrders retrieves any orders that are active/open
func (o *OKGroup) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) (resp []exchange.OrderDetail, err error) {
for _, currency := range getOrdersRequest.Currencies {
func (o *OKGroup) GetActiveOrders(req *order.GetOrdersRequest) (resp []order.Detail, err error) {
for x := range req.Currencies {
spotOpenOrders, err := o.GetSpotOpenOrders(GetSpotOpenOrdersRequest{
InstrumentID: o.FormatExchangeCurrency(currency,
InstrumentID: o.FormatExchangeCurrency(req.Currencies[x],
asset.Spot).String(),
})
if err != nil {
return resp, err
}
for i := range spotOpenOrders {
resp = append(resp, exchange.OrderDetail{
resp = append(resp, order.Detail{
ID: spotOpenOrders[i].OrderID,
Price: spotOpenOrders[i].Price,
Amount: spotOpenOrders[i].Size,
CurrencyPair: currency,
CurrencyPair: req.Currencies[x],
Exchange: o.Name,
OrderSide: exchange.OrderSide(spotOpenOrders[i].Side),
OrderType: exchange.OrderType(spotOpenOrders[i].Type),
OrderSide: order.Side(spotOpenOrders[i].Side),
OrderType: order.Type(spotOpenOrders[i].Type),
ExecutedAmount: spotOpenOrders[i].FilledSize,
OrderDate: spotOpenOrders[i].Timestamp,
Status: spotOpenOrders[i].Status,
Status: order.Status(spotOpenOrders[i].Status),
})
}
}
@@ -382,28 +396,28 @@ func (o *OKGroup) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) (
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (o *OKGroup) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) (resp []exchange.OrderDetail, err error) {
for _, currency := range getOrdersRequest.Currencies {
func (o *OKGroup) GetOrderHistory(req *order.GetOrdersRequest) (resp []order.Detail, err error) {
for x := range req.Currencies {
spotOpenOrders, err := o.GetSpotOrders(GetSpotOrdersRequest{
Status: strings.Join([]string{"filled", "cancelled", "failure"}, "|"),
InstrumentID: o.FormatExchangeCurrency(currency,
InstrumentID: o.FormatExchangeCurrency(req.Currencies[x],
asset.Spot).String(),
})
if err != nil {
return resp, err
}
for i := range spotOpenOrders {
resp = append(resp, exchange.OrderDetail{
resp = append(resp, order.Detail{
ID: spotOpenOrders[i].OrderID,
Price: spotOpenOrders[i].Price,
Amount: spotOpenOrders[i].Size,
CurrencyPair: currency,
CurrencyPair: req.Currencies[x],
Exchange: o.Name,
OrderSide: exchange.OrderSide(spotOpenOrders[i].Side),
OrderType: exchange.OrderType(spotOpenOrders[i].Type),
OrderSide: order.Side(spotOpenOrders[i].Side),
OrderType: order.Type(spotOpenOrders[i].Type),
ExecutedAmount: spotOpenOrders[i].FilledSize,
OrderDate: spotOpenOrders[i].Timestamp,
Status: spotOpenOrders[i].Status,
Status: order.Status(spotOpenOrders[i].Status),
})
}
}