mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-05 15:10:59 +00:00
Engine improvements
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/assets"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/request"
|
||||
)
|
||||
|
||||
@@ -51,12 +47,6 @@ const (
|
||||
Contact
|
||||
)
|
||||
|
||||
// SubmitOrderResponse is what is returned after submitting an order to an exchange
|
||||
type SubmitOrderResponse struct {
|
||||
IsOrderPlaced bool
|
||||
OrderID string
|
||||
}
|
||||
|
||||
// FeeBuilder is the type which holds all parameters required to calculate a fee
|
||||
// for an exchange
|
||||
type FeeBuilder struct {
|
||||
@@ -119,80 +109,6 @@ const (
|
||||
UnknownWithdrawalTypeText string = "UNKNOWN"
|
||||
)
|
||||
|
||||
// ModifyOrder is a an order modifyer
|
||||
// ModifyOrder is a an order modifyer
|
||||
type ModifyOrder struct {
|
||||
OrderID string
|
||||
OrderType
|
||||
OrderSide
|
||||
Price float64
|
||||
Amount float64
|
||||
LimitPriceUpper float64
|
||||
LimitPriceLower float64
|
||||
CurrencyPair currency.Pair
|
||||
|
||||
ImmediateOrCancel bool
|
||||
HiddenOrder bool
|
||||
FillOrKill bool
|
||||
PostOnly bool
|
||||
}
|
||||
|
||||
// ModifyOrderResponse is an order modifying return type
|
||||
type ModifyOrderResponse struct {
|
||||
OrderID string
|
||||
}
|
||||
|
||||
// CancelAllOrdersResponse returns the status from attempting to cancel all orders on an exchagne
|
||||
type CancelAllOrdersResponse struct {
|
||||
OrderStatus map[string]string
|
||||
}
|
||||
|
||||
// OrderType enforces a standard for Ordertypes across the code base
|
||||
type OrderType string
|
||||
|
||||
// OrderType ...types
|
||||
const (
|
||||
AnyOrderType OrderType = "ANY"
|
||||
LimitOrderType OrderType = "LIMIT"
|
||||
MarketOrderType OrderType = "MARKET"
|
||||
ImmediateOrCancelOrderType OrderType = "IMMEDIATE_OR_CANCEL"
|
||||
StopOrderType OrderType = "STOP"
|
||||
TrailingStopOrderType OrderType = "TRAILINGSTOP"
|
||||
UnknownOrderType OrderType = "UNKNOWN"
|
||||
)
|
||||
|
||||
// ToLower changes the ordertype to lower case
|
||||
func (o OrderType) ToLower() OrderType {
|
||||
return OrderType(strings.ToLower(string(o)))
|
||||
}
|
||||
|
||||
// ToString changes the ordertype to the exchange standard and returns a string
|
||||
func (o OrderType) ToString() string {
|
||||
return fmt.Sprintf("%v", o)
|
||||
}
|
||||
|
||||
// OrderSide enforces a standard for OrderSides across the code base
|
||||
type OrderSide string
|
||||
|
||||
// OrderSide types
|
||||
const (
|
||||
AnyOrderSide OrderSide = "ANY"
|
||||
BuyOrderSide OrderSide = "BUY"
|
||||
SellOrderSide OrderSide = "SELL"
|
||||
BidOrderSide OrderSide = "BID"
|
||||
AskOrderSide OrderSide = "ASK"
|
||||
)
|
||||
|
||||
// ToLower changes the ordertype to lower case
|
||||
func (o OrderSide) ToLower() OrderSide {
|
||||
return OrderSide(strings.ToLower(string(o)))
|
||||
}
|
||||
|
||||
// ToString changes the ordertype to the exchange standard and returns a string
|
||||
func (o OrderSide) ToString() string {
|
||||
return fmt.Sprintf("%v", o)
|
||||
}
|
||||
|
||||
// AccountInfo is a Generic type to hold each exchange's holdings in
|
||||
// all enabled currencies
|
||||
type AccountInfo struct {
|
||||
@@ -225,34 +141,6 @@ type TradeHistory struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
// OrderDetail holds order detail data
|
||||
type OrderDetail struct {
|
||||
Exchange string
|
||||
AccountID string
|
||||
ID string
|
||||
CurrencyPair currency.Pair
|
||||
OrderSide OrderSide
|
||||
OrderType OrderType
|
||||
OrderDate time.Time
|
||||
Status string
|
||||
Price float64
|
||||
Amount float64
|
||||
ExecutedAmount float64
|
||||
RemainingAmount float64
|
||||
Fee float64
|
||||
Trades []TradeHistory
|
||||
}
|
||||
|
||||
// OrderCancellation type required when requesting to cancel an order
|
||||
type OrderCancellation struct {
|
||||
AccountID string
|
||||
OrderID string
|
||||
CurrencyPair currency.Pair
|
||||
AssetType assets.AssetType
|
||||
WalletAddress string
|
||||
Side OrderSide
|
||||
}
|
||||
|
||||
// FundHistory holds exchange funding history data
|
||||
type FundHistory struct {
|
||||
ExchangeName string
|
||||
@@ -402,227 +290,6 @@ type API struct {
|
||||
}
|
||||
}
|
||||
|
||||
// GetOrdersRequest used for GetOrderHistory and GetOpenOrders wrapper functions
|
||||
type GetOrdersRequest struct {
|
||||
OrderType OrderType
|
||||
OrderSide OrderSide
|
||||
StartTicks time.Time
|
||||
EndTicks time.Time
|
||||
// Currencies Empty array = all currencies. Some endpoints only support singular currency enquiries
|
||||
Currencies []currency.Pair
|
||||
}
|
||||
|
||||
// OrderStatus defines order status types
|
||||
type OrderStatus string
|
||||
|
||||
// All OrderStatus types
|
||||
const (
|
||||
AnyOrderStatus OrderStatus = "ANY"
|
||||
NewOrderStatus OrderStatus = "NEW"
|
||||
ActiveOrderStatus OrderStatus = "ACTIVE"
|
||||
PartiallyFilledOrderStatus OrderStatus = "PARTIALLY_FILLED"
|
||||
FilledOrderStatus OrderStatus = "FILLED"
|
||||
CancelledOrderStatus OrderStatus = "CANCELED"
|
||||
PendingCancelOrderStatus OrderStatus = "PENDING_CANCEL"
|
||||
RejectedOrderStatus OrderStatus = "REJECTED"
|
||||
ExpiredOrderStatus OrderStatus = "EXPIRED"
|
||||
HiddenOrderStatus OrderStatus = "HIDDEN"
|
||||
UnknownOrderStatus OrderStatus = "UNKNOWN"
|
||||
)
|
||||
|
||||
// FilterOrdersBySide removes any OrderDetails that don't match the orderStatus provided
|
||||
func FilterOrdersBySide(orders *[]OrderDetail, orderSide OrderSide) {
|
||||
if orderSide == "" || orderSide == AnyOrderSide {
|
||||
return
|
||||
}
|
||||
|
||||
var filteredOrders []OrderDetail
|
||||
for i := range *orders {
|
||||
if strings.EqualFold(string((*orders)[i].OrderSide), string(orderSide)) {
|
||||
filteredOrders = append(filteredOrders, (*orders)[i])
|
||||
}
|
||||
}
|
||||
|
||||
*orders = filteredOrders
|
||||
}
|
||||
|
||||
// FilterOrdersByType removes any OrderDetails that don't match the orderType provided
|
||||
func FilterOrdersByType(orders *[]OrderDetail, orderType OrderType) {
|
||||
if orderType == "" || orderType == AnyOrderType {
|
||||
return
|
||||
}
|
||||
|
||||
var filteredOrders []OrderDetail
|
||||
for i := range *orders {
|
||||
if strings.EqualFold(string((*orders)[i].OrderType), string(orderType)) {
|
||||
filteredOrders = append(filteredOrders, (*orders)[i])
|
||||
}
|
||||
}
|
||||
|
||||
*orders = filteredOrders
|
||||
}
|
||||
|
||||
// FilterOrdersByTickRange removes any OrderDetails outside of the tick range
|
||||
func FilterOrdersByTickRange(orders *[]OrderDetail, startTicks, endTicks time.Time) {
|
||||
if startTicks.IsZero() || endTicks.IsZero() ||
|
||||
startTicks.Unix() == 0 || endTicks.Unix() == 0 || endTicks.Before(startTicks) {
|
||||
return
|
||||
}
|
||||
|
||||
var filteredOrders []OrderDetail
|
||||
for i := range *orders {
|
||||
if (*orders)[i].OrderDate.Unix() >= startTicks.Unix() && (*orders)[i].OrderDate.Unix() <= endTicks.Unix() {
|
||||
filteredOrders = append(filteredOrders, (*orders)[i])
|
||||
}
|
||||
}
|
||||
|
||||
*orders = filteredOrders
|
||||
}
|
||||
|
||||
// FilterOrdersByCurrencies removes any OrderDetails that do not match the provided currency list
|
||||
// It is forgiving in that the provided currencies can match quote or base currencies
|
||||
func FilterOrdersByCurrencies(orders *[]OrderDetail, currencies []currency.Pair) {
|
||||
if len(currencies) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var filteredOrders []OrderDetail
|
||||
for i := range *orders {
|
||||
matchFound := false
|
||||
for _, c := range currencies {
|
||||
if !matchFound && (*orders)[i].CurrencyPair.EqualIncludeReciprocal(c) {
|
||||
matchFound = true
|
||||
}
|
||||
}
|
||||
|
||||
if matchFound {
|
||||
filteredOrders = append(filteredOrders, (*orders)[i])
|
||||
}
|
||||
}
|
||||
|
||||
*orders = filteredOrders
|
||||
}
|
||||
|
||||
// ByPrice used for sorting orders by price
|
||||
type ByPrice []OrderDetail
|
||||
|
||||
func (b ByPrice) Len() int {
|
||||
return len(b)
|
||||
}
|
||||
|
||||
func (b ByPrice) Less(i, j int) bool {
|
||||
return b[i].Price < b[j].Price
|
||||
}
|
||||
|
||||
func (b ByPrice) Swap(i, j int) {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
// SortOrdersByPrice the caller function to sort orders
|
||||
func SortOrdersByPrice(orders *[]OrderDetail, reverse bool) {
|
||||
if reverse {
|
||||
sort.Sort(sort.Reverse(ByPrice(*orders)))
|
||||
} else {
|
||||
sort.Sort(ByPrice(*orders))
|
||||
}
|
||||
}
|
||||
|
||||
// ByOrderType used for sorting orders by order type
|
||||
type ByOrderType []OrderDetail
|
||||
|
||||
func (b ByOrderType) Len() int {
|
||||
return len(b)
|
||||
}
|
||||
|
||||
func (b ByOrderType) Less(i, j int) bool {
|
||||
return b[i].OrderType.ToString() < b[j].OrderType.ToString()
|
||||
}
|
||||
|
||||
func (b ByOrderType) Swap(i, j int) {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
// SortOrdersByType the caller function to sort orders
|
||||
func SortOrdersByType(orders *[]OrderDetail, reverse bool) {
|
||||
if reverse {
|
||||
sort.Sort(sort.Reverse(ByOrderType(*orders)))
|
||||
} else {
|
||||
sort.Sort(ByOrderType(*orders))
|
||||
}
|
||||
}
|
||||
|
||||
// ByCurrency used for sorting orders by order currency
|
||||
type ByCurrency []OrderDetail
|
||||
|
||||
func (b ByCurrency) Len() int {
|
||||
return len(b)
|
||||
}
|
||||
|
||||
func (b ByCurrency) Less(i, j int) bool {
|
||||
return b[i].CurrencyPair.String() < b[j].CurrencyPair.String()
|
||||
}
|
||||
|
||||
func (b ByCurrency) Swap(i, j int) {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
// SortOrdersByCurrency the caller function to sort orders
|
||||
func SortOrdersByCurrency(orders *[]OrderDetail, reverse bool) {
|
||||
if reverse {
|
||||
sort.Sort(sort.Reverse(ByCurrency(*orders)))
|
||||
} else {
|
||||
sort.Sort(ByCurrency(*orders))
|
||||
}
|
||||
}
|
||||
|
||||
// ByDate used for sorting orders by order date
|
||||
type ByDate []OrderDetail
|
||||
|
||||
func (b ByDate) Len() int {
|
||||
return len(b)
|
||||
}
|
||||
|
||||
func (b ByDate) Less(i, j int) bool {
|
||||
return b[i].OrderDate.Unix() < b[j].OrderDate.Unix()
|
||||
}
|
||||
|
||||
func (b ByDate) Swap(i, j int) {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
// SortOrdersByDate the caller function to sort orders
|
||||
func SortOrdersByDate(orders *[]OrderDetail, reverse bool) {
|
||||
if reverse {
|
||||
sort.Sort(sort.Reverse(ByDate(*orders)))
|
||||
} else {
|
||||
sort.Sort(ByDate(*orders))
|
||||
}
|
||||
}
|
||||
|
||||
// ByOrderSide used for sorting orders by order side (buy sell)
|
||||
type ByOrderSide []OrderDetail
|
||||
|
||||
func (b ByOrderSide) Len() int {
|
||||
return len(b)
|
||||
}
|
||||
|
||||
func (b ByOrderSide) Less(i, j int) bool {
|
||||
return b[i].OrderSide.ToString() < b[j].OrderSide.ToString()
|
||||
}
|
||||
|
||||
func (b ByOrderSide) Swap(i, j int) {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
// SortOrdersBySide the caller function to sort orders
|
||||
func SortOrdersBySide(orders *[]OrderDetail, reverse bool) {
|
||||
if reverse {
|
||||
sort.Sort(sort.Reverse(ByOrderSide(*orders)))
|
||||
} else {
|
||||
sort.Sort(ByOrderSide(*orders))
|
||||
}
|
||||
}
|
||||
|
||||
// Base stores the individual exchange information
|
||||
type Base struct {
|
||||
Name string
|
||||
|
||||
Reference in New Issue
Block a user