Feature: Websocket order handling (#446)

* Initial changes, removing exchange name as an arg and puts it in the pointer struct. Adds case to ws routines

* Adds CancelAllOrders func, adds GetByExchangeAndID. Adds modify handler in routines.go

* initial poor attempts to have bitmex work with new datahandler handlers. fixes ordersides

* bitmex Completes new order

* Better bitmex handling, but not complete. Begins a gargantuan task of unifying order data structs. Sometimes an order update will contain lot's of information, so its best to be able to update all fields of our orders, rather than just an arbitrary subset. As a result, everything will be broken for the foreseeable future :glitch_crab:

* Removes old order handler which did nothing. Updates order properties for everything everywhere - now consistent. Changes order status. Adds asset type and wallet address to all order types

* Adds order updater to update only relevant fields since the object is generic, we don't know what fields are passed from what exchanges. Adds "lastupdated" field to order.Detail. Expands order cancellation for engine orders.

* Ensures that new orders are added to the ordermanager's order store. Saaa many comments. Internalises orderStore get func. Adds internalOrderID to orderdetail and adds websocket support for it

* Fixes a cancelAllOrders oopsie doopsie

* Adds potential func to update orderdetails from an orderdetail struct. Unsure if will keep.

* Begins btcmarkets implementation. Expands order "stringToOrder" funcs to allow for some more flexible string coversions. Removes order.Submit via websocket as it would cause unlimited order place issues :D

* Finishes btc markets without testing

* Adds untested ws auth func to btse

* Finises btse, fixes btcmarkets bug

* Adds coinbasepro support

* Fixes a few more fields in coinbase pro and readds the extra subs

* Begins work on coinbene. Plus theyve added a new ws connection yeee

* Wasted a bunch of time adding support to an additional websocket that isn't needed ;_; Fixed a bug in coinbasepro. Fully kitted out coinbene support. Updates order types with all fields

* Removes extra websocket connection ;_;

* Finishes gemini. Fixes order side unknown

* Adds okgroup support. Moves byte reading to another function to allow for unit testing. Updates routines to use pointers. Updates date update handling for order details

* Finishes order data for okgroup websocket, but starts the STRANGE process of converting all other websocket endpoints to be a little less silly

* Cleans up okroup websocket implementation. Fixes bug in Gemini

* Adds poloniex support. Updates ws order handling

* new bitmex support. Adds some tests now that its all in its own func. Fixes poloniex bug

* Begins work on authenticated binance websocket

* Attempts to track user data via binance websocket

* Maybe finishes Binance websocket support

* Begins adding test coverage to orders.go. Updates names of script properties to match updated

* Begins an experiment with code coverage. Fixes more rebase issues

* Completes orders coverage. Botches a few other things though. Fixes more scripting stuff

* All tests in engine package pass

* Adds some loevely routine tests

* Moves ordermanager to test Bot ordermanager
Adds lovely routine tests to ensure things that get sent to be handled the data handler are handled by the data handler by handling them

* Replaces "wsHandleData" with "wsReadData" as that's what its going to do now.

* Splits all wsHandleData into wsReadData and wsHandleData to allow for easy testing via sending []byte json examples to test proper functionality. Breaks so many tests

* Fixes majority of test issues. But data races which are tough on the engine package

* "Fixes" test by removing shutdown test. It interferes with too many things. Requires some thought

* Tests all the binance websocket points

* Adds better bitfinex websocket support.

* Adds testing for bitfinex, bitstamp and btcmarkets. Fixes websocket bugs encountered

* Adds BTSE ws tests. Fixes bugs in ws

* Adds coinbase pro tests. Fixes any issues

* Coinbene tests

* Starts to handle coinut. Runs into a problem conceptually regarding websocket roundtrip and orders. Both events need to happen without impacting eachother/racing

* Addresses a data race issue regarding websocket and bot order management submission - order submission locks at an earlier point to prevent routines.go from creating an order before order submission creates it. Updates rpcserver to use order management bot to submit orders.

* Finishes the hectic coinut testing

* Adds tests for gateio

* Fixes rebase issues. Updates tests to work without being overloaded

* Begins testing of gemini. fixes up minor issues

* ginishes gemini tests and fixes

* Adds hitbtc tests. Fixes all the many issues with hitbtc websocket

* Adds remaining tests. Increases default test channel limit again

* Begins work towards huobi tests

* Finishes huobi tests

* Fixed all mythical rebase adventures

* Begins kraken transformation

* Finishes kraken. Fixes coinbene leverage now that its changed

* Begins okgroup testing

* Adds okgroup ws tests

* Does some poloniex

* Fixes basic curreny issue by extracting to func

* Begins redesign of poloniex websocket datahandling. Completes authenticated handling, now onto unauth

* Finishes poloniex revision

* Finishes ZB additions

* Fixes data races

* Fixes rebase issues. Fixes bad kraken logic

* Fixes after reviewing code

* lint everywhere

* Fixes lingering lints

* lint

* Adds test coverage to order detail and modify updating

* Fixes linting

* Fixes huge int, fixes date tests

* Adds GetByExchange, adds test for it. Protects fakepass echange. Renames DisplayQty to DisplayQuantity. Removes verbose. Adds some websocket properties.  Updates bitmex asset type in test

* Addresses timestamps, type abbreviations, verbosity. Expands binance kline switch cases. Updates some websocket capabilities.

* Adds coverage to the stringToOrderType/Status functions introduced in PR

* Minor fixes addressing some time, error text and use of StringDataCompareInsensitive

* Introduces shiny new system which checks if there is an awaiting ID, if found, processes via wrapper method, else, goes through wsHandleData method. Removes weird locking system from wrapper/websocket data race. Updates bitfinex to properly handle websocket order requests and notifications

* Moves fakePassingExchange to test_helper. Fixes some order side implementations for trades. Botches a new error type

* Adds new error type to track and handle order classification errors separately

* Fully fleshes out ClassificationError for all instances of status conversion. Even in order trades and some wrapper functions

* Introduces common.SimpleTimeFormat for "2006-01-02 15:04:05". Fixes binance and bitfinex issues with auth endpoint use, map casting. Expands more order.ClassificationError usage. Fixes some more generic websocket response errors

* Future proofs order updating by utilising asset types. Expands testing to accomodate. Adds shiny new time type. Expands wrapper websocket functionality definitions

* minty linty

* Broken end of day code addressing basic nits on comments, returns and currency conversion

* Adds testing to btcmarkets websocket. Also updates websocket orderbook to use update instead

* Fixes fun rebase fun fun so fun

* Addresses minor nits regarding changed interface and comments

* Creates new function `GetRequestFormattedPairAndAssetType` to retrieve a currency pair and asset type based on a string. It will iterate over enabled pairs and compare them to formatted pairs and then return that pair if found.

* Fixes test

* Adds a single line to the end of the file, because that would be really bad if it wasn't there

* Updates fakepassexchange to not use params, updates test params, uses fatal in some tests where its important, updates order manager to have a rwmutex, removes some returns, improves ws key test for binance, updates properties to reflect their actual values, adds some more websocket properties

* Addresses binance switch linting

* Updates leverage property to int64

* Fixes what was broken
This commit is contained in:
Scott
2020-03-03 13:32:14 +11:00
committed by GitHub
parent 9d49184bc6
commit b686cf2e0e
142 changed files with 13867 additions and 6043 deletions

View File

@@ -71,11 +71,11 @@ func TestValidate(t *testing.T) {
for x := range tester {
s := Submit{
Pair: tester[x].Pair,
OrderSide: tester[x].Side,
OrderType: tester[x].Type,
Amount: tester[x].Amount,
Price: tester[x].Price,
Pair: tester[x].Pair,
Side: tester[x].Side,
Type: tester[x].Type,
Amount: tester[x].Amount,
Price: tester[x].Price,
}
if err := s.Validate(); err != tester[x].ExpectedErr {
t.Errorf("Unexpected result. Got: %s, want: %s", err, tester[x].ExpectedErr)
@@ -115,10 +115,10 @@ func TestFilterOrdersByType(t *testing.T) {
var orders = []Detail{
{
OrderType: ImmediateOrCancel,
Type: ImmediateOrCancel,
},
{
OrderType: Limit,
Type: Limit,
},
}
@@ -143,10 +143,10 @@ func TestFilterOrdersBySide(t *testing.T) {
var orders = []Detail{
{
OrderSide: Buy,
Side: Buy,
},
{
OrderSide: Sell,
Side: Sell,
},
{},
}
@@ -172,13 +172,13 @@ func TestFilterOrdersByTickRange(t *testing.T) {
var orders = []Detail{
{
OrderDate: time.Unix(100, 0),
Date: time.Unix(100, 0),
},
{
OrderDate: time.Unix(110, 0),
Date: time.Unix(110, 0),
},
{
OrderDate: time.Unix(111, 0),
Date: time.Unix(111, 0),
},
}
@@ -208,13 +208,13 @@ func TestFilterOrdersByCurrencies(t *testing.T) {
var orders = []Detail{
{
CurrencyPair: currency.NewPair(currency.BTC, currency.USD),
Pair: currency.NewPair(currency.BTC, currency.USD),
},
{
CurrencyPair: currency.NewPair(currency.LTC, currency.EUR),
Pair: currency.NewPair(currency.LTC, currency.EUR),
},
{
CurrencyPair: currency.NewPair(currency.DOGE, currency.RUB),
Pair: currency.NewPair(currency.DOGE, currency.RUB),
},
}
@@ -275,26 +275,26 @@ func TestSortOrdersByDate(t *testing.T) {
orders := []Detail{
{
OrderDate: time.Unix(0, 0),
Date: time.Unix(0, 0),
}, {
OrderDate: time.Unix(1, 0),
Date: time.Unix(1, 0),
}, {
OrderDate: time.Unix(2, 0),
Date: time.Unix(2, 0),
},
}
SortOrdersByDate(&orders, false)
if orders[0].OrderDate.Unix() != time.Unix(0, 0).Unix() {
if orders[0].Date.Unix() != time.Unix(0, 0).Unix() {
t.Errorf("Expected: '%v', received: '%v'",
time.Unix(0, 0).Unix(),
orders[0].OrderDate.Unix())
orders[0].Date.Unix())
}
SortOrdersByDate(&orders, true)
if orders[0].OrderDate.Unix() != time.Unix(2, 0).Unix() {
if orders[0].Date.Unix() != time.Unix(2, 0).Unix() {
t.Errorf("Expected: '%v', received: '%v'",
time.Unix(2, 0).Unix(),
orders[0].OrderDate.Unix())
orders[0].Date.Unix())
}
}
@@ -303,40 +303,40 @@ func TestSortOrdersByCurrency(t *testing.T) {
orders := []Detail{
{
CurrencyPair: currency.NewPairWithDelimiter(currency.BTC.String(),
Pair: currency.NewPairWithDelimiter(currency.BTC.String(),
currency.USD.String(),
"-"),
}, {
CurrencyPair: currency.NewPairWithDelimiter(currency.DOGE.String(),
Pair: currency.NewPairWithDelimiter(currency.DOGE.String(),
currency.USD.String(),
"-"),
}, {
CurrencyPair: currency.NewPairWithDelimiter(currency.BTC.String(),
Pair: currency.NewPairWithDelimiter(currency.BTC.String(),
currency.RUB.String(),
"-"),
}, {
CurrencyPair: currency.NewPairWithDelimiter(currency.LTC.String(),
Pair: currency.NewPairWithDelimiter(currency.LTC.String(),
currency.EUR.String(),
"-"),
}, {
CurrencyPair: currency.NewPairWithDelimiter(currency.LTC.String(),
Pair: currency.NewPairWithDelimiter(currency.LTC.String(),
currency.AUD.String(),
"-"),
},
}
SortOrdersByCurrency(&orders, false)
if orders[0].CurrencyPair.String() != currency.BTC.String()+"-"+currency.RUB.String() {
if orders[0].Pair.String() != currency.BTC.String()+"-"+currency.RUB.String() {
t.Errorf("Expected: '%v', received: '%v'",
currency.BTC.String()+"-"+currency.RUB.String(),
orders[0].CurrencyPair.String())
orders[0].Pair.String())
}
SortOrdersByCurrency(&orders, true)
if orders[0].CurrencyPair.String() != currency.LTC.String()+"-"+currency.EUR.String() {
if orders[0].Pair.String() != currency.LTC.String()+"-"+currency.EUR.String() {
t.Errorf("Expected: '%v', received: '%v'",
currency.LTC.String()+"-"+currency.EUR.String(),
orders[0].CurrencyPair.String())
orders[0].Pair.String())
}
}
@@ -345,28 +345,28 @@ func TestSortOrdersByOrderSide(t *testing.T) {
orders := []Detail{
{
OrderSide: Buy,
Side: Buy,
}, {
OrderSide: Sell,
Side: Sell,
}, {
OrderSide: Sell,
Side: Sell,
}, {
OrderSide: Buy,
Side: Buy,
},
}
SortOrdersBySide(&orders, false)
if !strings.EqualFold(orders[0].OrderSide.String(), Buy.String()) {
if !strings.EqualFold(orders[0].Side.String(), Buy.String()) {
t.Errorf("Expected: '%v', received: '%v'",
Buy,
orders[0].OrderSide)
orders[0].Side)
}
SortOrdersBySide(&orders, true)
if !strings.EqualFold(orders[0].OrderSide.String(), Sell.String()) {
if !strings.EqualFold(orders[0].Side.String(), Sell.String()) {
t.Errorf("Expected: '%v', received: '%v'",
Sell,
orders[0].OrderSide)
orders[0].Side)
}
}
@@ -375,28 +375,28 @@ func TestSortOrdersByOrderType(t *testing.T) {
orders := []Detail{
{
OrderType: Market,
Type: Market,
}, {
OrderType: Limit,
Type: Limit,
}, {
OrderType: ImmediateOrCancel,
Type: ImmediateOrCancel,
}, {
OrderType: TrailingStop,
Type: TrailingStop,
},
}
SortOrdersByType(&orders, false)
if !strings.EqualFold(orders[0].OrderType.String(), ImmediateOrCancel.String()) {
if !strings.EqualFold(orders[0].Type.String(), ImmediateOrCancel.String()) {
t.Errorf("Expected: '%v', received: '%v'",
ImmediateOrCancel,
orders[0].OrderType)
orders[0].Type)
}
SortOrdersByType(&orders, true)
if !strings.EqualFold(orders[0].OrderType.String(), TrailingStop.String()) {
if !strings.EqualFold(orders[0].Type.String(), TrailingStop.String()) {
t.Errorf("Expected: '%v', received: '%v'",
TrailingStop,
orders[0].OrderType)
orders[0].Type)
}
}
@@ -420,7 +420,7 @@ var stringsToOrderSide = []struct {
{"any", AnySide, nil},
{"ANY", AnySide, nil},
{"aNy", AnySide, nil},
{"woahMan", Buy, errors.New("woahMan not recognised as side type")},
{"woahMan", Buy, errors.New("woahMan not recognised as order side")},
}
func TestStringToOrderSide(t *testing.T) {
@@ -453,16 +453,18 @@ var stringsToOrderType = []struct {
{"immediate_or_cancel", ImmediateOrCancel, nil},
{"IMMEDIATE_OR_CANCEL", ImmediateOrCancel, nil},
{"iMmEdIaTe_Or_CaNcEl", ImmediateOrCancel, nil},
{"iMmEdIaTe Or CaNcEl", ImmediateOrCancel, nil},
{"stop", Stop, nil},
{"STOP", Stop, nil},
{"sToP", Stop, nil},
{"trailingstop", TrailingStop, nil},
{"TRAILINGSTOP", TrailingStop, nil},
{"tRaIlInGsToP", TrailingStop, nil},
{"trailing_stop", TrailingStop, nil},
{"TRAILING_STOP", TrailingStop, nil},
{"tRaIlInG_sToP", TrailingStop, nil},
{"tRaIlInG sToP", TrailingStop, nil},
{"any", AnyType, nil},
{"ANY", AnyType, nil},
{"aNy", AnyType, nil},
{"woahMan", Unknown, errors.New("woahMan not recognised as order type")},
{"woahMan", UnknownType, errors.New("woahMan not recognised as order type")},
}
func TestStringToOrderType(t *testing.T) {
@@ -516,7 +518,13 @@ var stringsToOrderStatus = []struct {
{"hidden", Hidden, nil},
{"HIDDEN", Hidden, nil},
{"hIdDeN", Hidden, nil},
{"woahMan", UnknownStatus, errors.New("woahMan not recognised as order STATUS")},
{"market_unavailable", MarketUnavailable, nil},
{"MARKET_UNAVAILABLE", MarketUnavailable, nil},
{"mArKeT_uNaVaIlAbLe", MarketUnavailable, nil},
{"insufficient_balance", InsufficientBalance, nil},
{"INSUFFICIENT_BALANCE", InsufficientBalance, nil},
{"iNsUfFiCiEnT_bAlAnCe", InsufficientBalance, nil},
{"woahMan", UnknownStatus, errors.New("woahMan not recognised as order status")},
}
func TestStringToOrderStatus(t *testing.T) {
@@ -534,3 +542,375 @@ func TestStringToOrderStatus(t *testing.T) {
})
}
}
func TestUpdateOrderFromModify(t *testing.T) {
var leet = "1337"
od := Detail{
ImmediateOrCancel: false,
HiddenOrder: false,
FillOrKill: false,
PostOnly: false,
Leverage: "",
Price: 0,
Amount: 0,
LimitPriceUpper: 0,
LimitPriceLower: 0,
TriggerPrice: 0,
TargetAmount: 0,
ExecutedAmount: 0,
RemainingAmount: 0,
Fee: 0,
Exchange: "",
ID: "1",
AccountID: "",
ClientID: "",
WalletAddress: "",
Type: "",
Side: "",
Status: "",
AssetType: "",
Date: time.Time{},
LastUpdated: time.Time{},
Pair: currency.Pair{},
Trades: nil,
}
updated := time.Now()
om := Modify{
ImmediateOrCancel: true,
HiddenOrder: true,
FillOrKill: true,
PostOnly: true,
Leverage: "1",
Price: 1,
Amount: 1,
LimitPriceUpper: 1,
LimitPriceLower: 1,
TriggerPrice: 1,
TargetAmount: 1,
ExecutedAmount: 1,
RemainingAmount: 1,
Fee: 1,
Exchange: "1",
InternalOrderID: "1",
ID: "1",
AccountID: "1",
ClientID: "1",
WalletAddress: "1",
Type: "1",
Side: "1",
Status: "1",
AssetType: "1",
LastUpdated: updated,
Pair: currency.NewPairFromString("BTCUSD"),
Trades: []TradeHistory{},
}
od.UpdateOrderFromModify(&om)
if od.InternalOrderID == "1" {
t.Error("Should not be able to update the internal order ID")
}
if !od.ImmediateOrCancel {
t.Error("Failed to update")
}
if !od.HiddenOrder {
t.Error("Failed to update")
}
if !od.FillOrKill {
t.Error("Failed to update")
}
if !od.PostOnly {
t.Error("Failed to update")
}
if od.Leverage != "1" {
t.Error("Failed to update")
}
if od.Price != 1 {
t.Error("Failed to update")
}
if od.Amount != 1 {
t.Error("Failed to update")
}
if od.LimitPriceLower != 1 {
t.Error("Failed to update")
}
if od.LimitPriceUpper != 1 {
t.Error("Failed to update")
}
if od.TriggerPrice != 1 {
t.Error("Failed to update")
}
if od.TargetAmount != 1 {
t.Error("Failed to update")
}
if od.ExecutedAmount != 1 {
t.Error("Failed to update")
}
if od.RemainingAmount != 1 {
t.Error("Failed to update")
}
if od.Fee != 1 {
t.Error("Failed to update")
}
if od.Exchange != "" {
t.Error("Should not be able to update exchange via modify")
}
if od.ID != "1" {
t.Error("Failed to update")
}
if od.ClientID != "1" {
t.Error("Failed to update")
}
if od.WalletAddress != "1" {
t.Error("Failed to update")
}
if od.Type != "1" {
t.Error("Failed to update")
}
if od.Side != "1" {
t.Error("Failed to update")
}
if od.Status != "1" {
t.Error("Failed to update")
}
if od.AssetType != "1" {
t.Error("Failed to update")
}
if od.LastUpdated != updated {
t.Error("Failed to update")
}
if od.Pair.String() != "BTCUSD" {
t.Error("Failed to update")
}
if od.Trades != nil {
t.Error("Failed to update")
}
om.Trades = append(om.Trades, TradeHistory{TID: "1"}, TradeHistory{TID: "2"})
od.UpdateOrderFromModify(&om)
if len(od.Trades) != 2 {
t.Error("Failed to add trades")
}
om.Trades[0].Exchange = leet
om.Trades[0].Price = 1337
om.Trades[0].Fee = 1337
om.Trades[0].IsMaker = true
om.Trades[0].Timestamp = updated
om.Trades[0].Description = leet
om.Trades[0].Side = UnknownSide
om.Trades[0].Type = UnknownType
om.Trades[0].Amount = 1337
od.UpdateOrderFromModify(&om)
if od.Trades[0].Exchange == leet {
t.Error("Should not be able to update exchange from update")
}
if od.Trades[0].Price != 1337 {
t.Error("Failed to update trades")
}
if od.Trades[0].Fee != 1337 {
t.Error("Failed to update trades")
}
if !od.Trades[0].IsMaker {
t.Error("Failed to update trades")
}
if od.Trades[0].Timestamp != updated {
t.Error("Failed to update trades")
}
if od.Trades[0].Description != leet {
t.Error("Failed to update trades")
}
if od.Trades[0].Side != UnknownSide {
t.Error("Failed to update trades")
}
if od.Trades[0].Type != UnknownType {
t.Error("Failed to update trades")
}
if od.Trades[0].Amount != 1337 {
t.Error("Failed to update trades")
}
}
func TestUpdateOrderFromDetail(t *testing.T) {
var leet = "1337"
od := Detail{
ImmediateOrCancel: false,
HiddenOrder: false,
FillOrKill: false,
PostOnly: false,
Leverage: "",
Price: 0,
Amount: 0,
LimitPriceUpper: 0,
LimitPriceLower: 0,
TriggerPrice: 0,
TargetAmount: 0,
ExecutedAmount: 0,
RemainingAmount: 0,
Fee: 0,
Exchange: "",
ID: "1",
AccountID: "",
ClientID: "",
WalletAddress: "",
Type: "",
Side: "",
Status: "",
AssetType: "",
Date: time.Time{},
LastUpdated: time.Time{},
Pair: currency.Pair{},
Trades: nil,
}
updated := time.Now()
om := Detail{
ImmediateOrCancel: true,
HiddenOrder: true,
FillOrKill: true,
PostOnly: true,
Leverage: "1",
Price: 1,
Amount: 1,
LimitPriceUpper: 1,
LimitPriceLower: 1,
TriggerPrice: 1,
TargetAmount: 1,
ExecutedAmount: 1,
RemainingAmount: 1,
Fee: 1,
Exchange: "1",
InternalOrderID: "1",
ID: "1",
AccountID: "1",
ClientID: "1",
WalletAddress: "1",
Type: "1",
Side: "1",
Status: "1",
AssetType: "1",
LastUpdated: updated,
Pair: currency.NewPairFromString("BTCUSD"),
Trades: []TradeHistory{},
}
od.UpdateOrderFromDetail(&om)
if od.InternalOrderID == "1" {
t.Error("Should not be able to update the internal order ID")
}
if !od.ImmediateOrCancel {
t.Error("Failed to update")
}
if !od.HiddenOrder {
t.Error("Failed to update")
}
if !od.FillOrKill {
t.Error("Failed to update")
}
if !od.PostOnly {
t.Error("Failed to update")
}
if od.Leverage != "1" {
t.Error("Failed to update")
}
if od.Price != 1 {
t.Error("Failed to update")
}
if od.Amount != 1 {
t.Error("Failed to update")
}
if od.LimitPriceLower != 1 {
t.Error("Failed to update")
}
if od.LimitPriceUpper != 1 {
t.Error("Failed to update")
}
if od.TriggerPrice != 1 {
t.Error("Failed to update")
}
if od.TargetAmount != 1 {
t.Error("Failed to update")
}
if od.ExecutedAmount != 1 {
t.Error("Failed to update")
}
if od.RemainingAmount != 1 {
t.Error("Failed to update")
}
if od.Fee != 1 {
t.Error("Failed to update")
}
if od.Exchange != "" {
t.Error("Should not be able to update exchange via modify")
}
if od.ID != "1" {
t.Error("Failed to update")
}
if od.ClientID != "1" {
t.Error("Failed to update")
}
if od.WalletAddress != "1" {
t.Error("Failed to update")
}
if od.Type != "1" {
t.Error("Failed to update")
}
if od.Side != "1" {
t.Error("Failed to update")
}
if od.Status != "1" {
t.Error("Failed to update")
}
if od.AssetType != "1" {
t.Error("Failed to update")
}
if od.LastUpdated != updated {
t.Error("Failed to update")
}
if od.Pair.String() != "BTCUSD" {
t.Error("Failed to update")
}
if od.Trades != nil {
t.Error("Failed to update")
}
om.Trades = append(om.Trades, TradeHistory{TID: "1"}, TradeHistory{TID: "2"})
od.UpdateOrderFromDetail(&om)
if len(od.Trades) != 2 {
t.Error("Failed to add trades")
}
om.Trades[0].Exchange = leet
om.Trades[0].Price = 1337
om.Trades[0].Fee = 1337
om.Trades[0].IsMaker = true
om.Trades[0].Timestamp = updated
om.Trades[0].Description = leet
om.Trades[0].Side = UnknownSide
om.Trades[0].Type = UnknownType
om.Trades[0].Amount = 1337
od.UpdateOrderFromDetail(&om)
if od.Trades[0].Exchange == leet {
t.Error("Should not be able to update exchange from update")
}
if od.Trades[0].Price != 1337 {
t.Error("Failed to update trades")
}
if od.Trades[0].Fee != 1337 {
t.Error("Failed to update trades")
}
if !od.Trades[0].IsMaker {
t.Error("Failed to update trades")
}
if od.Trades[0].Timestamp != updated {
t.Error("Failed to update trades")
}
if od.Trades[0].Description != leet {
t.Error("Failed to update trades")
}
if od.Trades[0].Side != UnknownSide {
t.Error("Failed to update trades")
}
if od.Trades[0].Type != UnknownType {
t.Error("Failed to update trades")
}
if od.Trades[0].Amount != 1337 {
t.Error("Failed to update trades")
}
}

View File

@@ -2,30 +2,14 @@ package order
import (
"errors"
"fmt"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
const (
limitOrder = iota
marketOrder
)
// Orders variable holds an array of pointers to order structs
var Orders []*Order
// Order struct holds order values
type Order struct {
OrderID int
Exchange string
Type int
Amount float64
Price float64
}
// vars related to orders
// var error definitions
var (
ErrSubmissionIsNil = errors.New("order submission is nil")
ErrPairIsEmpty = errors.New("order pair is empty")
@@ -35,16 +19,39 @@ var (
ErrPriceMustBeSetIfLimitOrder = errors.New("order price must be set if limit order type is desired")
)
// Submit contains the order submission data
// Submit contains all properties of an order that may be required
// for an order to be created on an exchange
// Each exchange has their own requirements, so not all fields
// are required to be populated
type Submit struct {
Pair currency.Pair
OrderType Type
OrderSide Side
TriggerPrice float64
TargetAmount float64
Price float64
Amount float64
ClientID string
ImmediateOrCancel bool
HiddenOrder bool
FillOrKill bool
PostOnly bool
Leverage string
Price float64
Amount float64
LimitPriceUpper float64
LimitPriceLower float64
TriggerPrice float64
TargetAmount float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
Exchange string
InternalOrderID string
ID string
AccountID string
ClientID string
WalletAddress string
Type Type
Side Side
Status Status
AssetType asset.Item
Date time.Time
LastUpdated time.Time
Pair currency.Pair
Trades []TradeHistory
}
// SubmitResponse is what is returned after submitting an order to an exchange
@@ -54,20 +61,39 @@ type SubmitResponse struct {
OrderID string
}
// Modify is an order modifyer
// Modify contains all properties of an order
// that may be updated after it has been created
// Each exchange has their own requirements, so not all fields
// are required to be populated
type Modify struct {
OrderID string
Type
Side
Price float64
Amount float64
LimitPriceUpper float64
LimitPriceLower float64
CurrencyPair currency.Pair
ImmediateOrCancel bool
HiddenOrder bool
FillOrKill bool
PostOnly bool
Leverage string
Price float64
Amount float64
LimitPriceUpper float64
LimitPriceLower float64
TriggerPrice float64
TargetAmount float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
Exchange string
InternalOrderID string
ID string
AccountID string
ClientID string
WalletAddress string
Type Type
Side Side
Status Status
AssetType asset.Item
Date time.Time
LastUpdated time.Time
Pair currency.Pair
Trades []TradeHistory
}
// ModifyResponse is an order modifying return type
@@ -75,12 +101,113 @@ type ModifyResponse struct {
OrderID string
}
// CancelAllResponse returns the status from attempting to cancel all orders on
// an exchagne
// Detail contains all properties of an order
// Each exchange has their own requirements, so not all fields
// are required to be populated
type Detail struct {
ImmediateOrCancel bool
HiddenOrder bool
FillOrKill bool
PostOnly bool
Leverage string
Price float64
Amount float64
LimitPriceUpper float64
LimitPriceLower float64
TriggerPrice float64
TargetAmount float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
Exchange string
InternalOrderID string
ID string
AccountID string
ClientID string
WalletAddress string
Type Type
Side Side
Status Status
AssetType asset.Item
Date time.Time
LastUpdated time.Time
Pair currency.Pair
Trades []TradeHistory
}
// Cancel contains all properties that may be required
// to cancel an order on an exchange
// Each exchange has their own requirements, so not all fields
// are required to be populated
type Cancel struct {
Price float64
Amount float64
Exchange string
ID string
AccountID string
ClientID string
WalletAddress string
Type Type
Side Side
Status Status
AssetType asset.Item
Date time.Time
Pair currency.Pair
Trades []TradeHistory
}
// CancelAllResponse returns the status from attempting to
// cancel all orders on an exchange
type CancelAllResponse struct {
Status map[string]string
}
// TradeHistory holds exchange history data
type TradeHistory struct {
Price float64
Amount float64
Fee float64
Exchange string
TID string
Description string
Type Type
Side Side
Timestamp time.Time
IsMaker bool
}
// GetOrdersRequest used for GetOrderHistory and GetOpenOrders wrapper functions
type GetOrdersRequest struct {
Type Type
Side Side
StartTicks time.Time
EndTicks time.Time
// Currencies Empty array = all currencies. Some endpoints only support
// singular currency enquiries
Pairs []currency.Pair
}
// Status defines order status types
type Status string
// All order status types
const (
AnyStatus Status = "ANY"
New Status = "NEW"
Active Status = "ACTIVE"
PartiallyCancelled Status = "PARTIALLY_CANCELLED"
PartiallyFilled Status = "PARTIALLY_FILLED"
Filled Status = "FILLED"
Cancelled Status = "CANCELLED"
PendingCancel Status = "PENDING_CANCEL"
InsufficientBalance Status = "INSUFFICIENT_BALANCE"
MarketUnavailable Status = "MARKET_UNAVAILABLE"
Rejected Status = "REJECTED"
Expired Status = "EXPIRED"
Hidden Status = "HIDDEN"
UnknownStatus Status = "UNKNOWN"
)
// Type enforces a standard for order types across the code base
type Type string
@@ -91,8 +218,8 @@ const (
Market Type = "MARKET"
ImmediateOrCancel Type = "IMMEDIATE_OR_CANCEL"
Stop Type = "STOP"
TrailingStop Type = "TRAILINGSTOP"
Unknown Type = "UNKNOWN"
TrailingStop Type = "TRAILING_STOP"
UnknownType Type = "UNKNOWN"
)
// Side enforces a standard for order sides across the code base
@@ -105,78 +232,7 @@ const (
Sell Side = "SELL"
Bid Side = "BID"
Ask Side = "ASK"
SideUnknown Side = "SIDEUNKNOWN"
)
// Detail holds order detail data
type Detail struct {
Exchange string
AccountID string
ID string
CurrencyPair currency.Pair
OrderSide Side
OrderType Type
OrderDate time.Time
Status Status
Price float64
Amount float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
Trades []TradeHistory
}
// TradeHistory holds exchange history data
type TradeHistory struct {
Timestamp time.Time
TID string
Price float64
Amount float64
Exchange string
Type Type
Side Side
Fee float64
Description string
}
// Cancel type required when requesting to cancel an order
type Cancel struct {
AccountID string
OrderID string
CurrencyPair currency.Pair
AssetType asset.Item
WalletAddress string
Side Side
}
// GetOrdersRequest used for GetOrderHistory and GetOpenOrders wrapper functions
type GetOrdersRequest struct {
OrderType Type
OrderSide Side
StartTicks time.Time
EndTicks time.Time
// Currencies Empty array = all currencies. Some endpoints only support
// singular currency enquiries
Currencies []currency.Pair
}
// Status defines order status types
type Status string
// All order status types
const (
AnyStatus Status = "ANY"
New Status = "NEW"
Active Status = "ACTIVE"
PartiallyCancelled Status = "PARTIALLY_CANCELLED"
PartiallyFilled Status = "PARTIALLY_FILLED"
Filled Status = "FILLED"
Cancelled Status = "CANCELLED"
PendingCancel Status = "PENDING_CANCEL"
Rejected Status = "REJECTED"
Expired Status = "EXPIRED"
Hidden Status = "HIDDEN"
UnknownStatus Status = "UNKNOWN"
UnknownSide Side = "UNKNOWN"
)
// ByPrice used for sorting orders by price
@@ -193,3 +249,23 @@ type ByDate []Detail
// ByOrderSide used for sorting orders by order side (buy sell)
type ByOrderSide []Detail
// ClassificationError returned when an order status
// side or type cannot be recognised
type ClassificationError struct {
Exchange string
OrderID string
Err error
}
func (o *ClassificationError) Error() string {
if o.OrderID != "" {
return fmt.Sprintf("%s - OrderID: %s classification error: %v",
o.Exchange,
o.OrderID,
o.Err)
}
return fmt.Sprintf("%s - classification error: %v",
o.Exchange,
o.Err)
}

View File

@@ -1,7 +1,7 @@
package order
import (
"fmt"
"errors"
"sort"
"strings"
"time"
@@ -9,57 +9,6 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
)
// NewOrder creates a new order and returns a an orderID
func NewOrder(exchangeName string, amount, price float64) int {
ord := &Order{}
if len(Orders) == 0 {
ord.OrderID = 0
} else {
ord.OrderID = len(Orders)
}
ord.Exchange = exchangeName
ord.Amount = amount
ord.Price = price
Orders = append(Orders, ord)
return ord.OrderID
}
// DeleteOrder deletes orders by ID and returns state
func DeleteOrder(orderID int) bool {
for i := range Orders {
if Orders[i].OrderID == orderID {
Orders = append(Orders[:i], Orders[i+1:]...)
return true
}
}
return false
}
// GetOrdersByExchange returns order pointer grouped by exchange
func GetOrdersByExchange(exchange string) []*Order {
var orders []*Order
for i := range Orders {
if Orders[i].Exchange == exchange {
orders = append(orders, Orders[i])
}
}
if len(orders) > 0 {
return orders
}
return nil
}
// GetOrderByOrderID returns order pointer by ID
func GetOrderByOrderID(orderID int) *Order {
for i := range Orders {
if Orders[i].OrderID == orderID {
return Orders[i]
}
}
return nil
}
// Validate checks the supplied data and returns whether or not it's valid
func (s *Submit) Validate() error {
if s == nil {
@@ -70,14 +19,14 @@ func (s *Submit) Validate() error {
return ErrPairIsEmpty
}
if s.OrderSide != Buy &&
s.OrderSide != Sell &&
s.OrderSide != Bid &&
s.OrderSide != Ask {
if s.Side != Buy &&
s.Side != Sell &&
s.Side != Bid &&
s.Side != Ask {
return ErrSideIsInvalid
}
if s.OrderType != Market && s.OrderType != Limit {
if s.Type != Market && s.Type != Limit {
return ErrTypeIsInvalid
}
@@ -85,13 +34,311 @@ func (s *Submit) Validate() error {
return ErrAmountIsInvalid
}
if s.OrderType == Limit && s.Price <= 0 {
if s.Type == Limit && s.Price <= 0 {
return ErrPriceMustBeSetIfLimitOrder
}
return nil
}
// UpdateOrderFromDetail Will update an order detail (used in order management)
// by comparing passed in and existing values
func (d *Detail) UpdateOrderFromDetail(m *Detail) {
var updated bool
if d.ImmediateOrCancel != m.ImmediateOrCancel {
d.ImmediateOrCancel = m.ImmediateOrCancel
updated = true
}
if d.HiddenOrder != m.HiddenOrder {
d.HiddenOrder = m.HiddenOrder
updated = true
}
if d.FillOrKill != m.FillOrKill {
d.FillOrKill = m.FillOrKill
updated = true
}
if m.Price > 0 && m.Price != d.Price {
d.Price = m.Price
updated = true
}
if m.Amount > 0 && m.Amount != d.Amount {
d.Amount = m.Amount
updated = true
}
if m.LimitPriceUpper > 0 && m.LimitPriceUpper != d.LimitPriceUpper {
d.LimitPriceUpper = m.LimitPriceUpper
updated = true
}
if m.LimitPriceLower > 0 && m.LimitPriceLower != d.LimitPriceLower {
d.LimitPriceLower = m.LimitPriceLower
updated = true
}
if m.TriggerPrice > 0 && m.TriggerPrice != d.TriggerPrice {
d.TriggerPrice = m.TriggerPrice
updated = true
}
if m.TargetAmount > 0 && m.TargetAmount != d.TargetAmount {
d.TargetAmount = m.TargetAmount
updated = true
}
if m.ExecutedAmount > 0 && m.ExecutedAmount != d.ExecutedAmount {
d.ExecutedAmount = m.ExecutedAmount
updated = true
}
if m.Fee > 0 && m.Fee != d.Fee {
d.Fee = m.Fee
updated = true
}
if m.AccountID != "" && m.AccountID != d.AccountID {
d.AccountID = m.AccountID
updated = true
}
if m.PostOnly != d.PostOnly {
d.PostOnly = m.PostOnly
updated = true
}
if !m.Pair.IsEmpty() && m.Pair != d.Pair {
d.Pair = m.Pair
updated = true
}
if m.Leverage != "" && m.Leverage != d.Leverage {
d.Leverage = m.Leverage
updated = true
}
if m.ClientID != "" && m.ClientID != d.ClientID {
d.ClientID = m.ClientID
updated = true
}
if m.WalletAddress != "" && m.WalletAddress != d.WalletAddress {
d.WalletAddress = m.WalletAddress
updated = true
}
if m.Type != "" && m.Type != d.Type {
d.Type = m.Type
updated = true
}
if m.Side != "" && m.Side != d.Side {
d.Side = m.Side
updated = true
}
if m.Status != "" && m.Status != d.Status {
d.Status = m.Status
updated = true
}
if m.AssetType != "" && m.AssetType != d.AssetType {
d.AssetType = m.AssetType
updated = true
}
if m.Trades != nil {
for x := range m.Trades {
var found bool
for y := range d.Trades {
if d.Trades[y].TID != m.Trades[x].TID {
continue
}
found = true
if d.Trades[y].Fee != m.Trades[x].Fee {
d.Trades[y].Fee = m.Trades[x].Fee
updated = true
}
if m.Trades[y].Price != 0 && d.Trades[y].Price != m.Trades[x].Price {
d.Trades[y].Price = m.Trades[x].Price
updated = true
}
if d.Trades[y].Side != m.Trades[x].Side {
d.Trades[y].Side = m.Trades[x].Side
updated = true
}
if d.Trades[y].Type != m.Trades[x].Type {
d.Trades[y].Type = m.Trades[x].Type
updated = true
}
if d.Trades[y].Description != m.Trades[x].Description {
d.Trades[y].Description = m.Trades[x].Description
updated = true
}
if m.Trades[y].Amount != 0 && d.Trades[y].Amount != m.Trades[x].Amount {
d.Trades[y].Amount = m.Trades[x].Amount
updated = true
}
if d.Trades[y].Timestamp != m.Trades[x].Timestamp {
d.Trades[y].Timestamp = m.Trades[x].Timestamp
updated = true
}
if d.Trades[y].IsMaker != m.Trades[x].IsMaker {
d.Trades[y].IsMaker = m.Trades[x].IsMaker
updated = true
}
}
if !found {
d.Trades = append(d.Trades, m.Trades[x])
updated = true
}
m.RemainingAmount -= m.Trades[x].Amount
}
}
if m.RemainingAmount > 0 && m.RemainingAmount != d.RemainingAmount {
d.RemainingAmount = m.RemainingAmount
updated = true
}
if updated {
if d.LastUpdated == m.LastUpdated {
d.LastUpdated = time.Now()
} else {
d.LastUpdated = m.LastUpdated
}
}
}
// UpdateOrderFromModify Will update an order detail (used in order management)
// by comparing passed in and existing values
func (d *Detail) UpdateOrderFromModify(m *Modify) {
var updated bool
if d.ImmediateOrCancel != m.ImmediateOrCancel {
d.ImmediateOrCancel = m.ImmediateOrCancel
updated = true
}
if d.HiddenOrder != m.HiddenOrder {
d.HiddenOrder = m.HiddenOrder
updated = true
}
if d.FillOrKill != m.FillOrKill {
d.FillOrKill = m.FillOrKill
updated = true
}
if m.Price > 0 && m.Price != d.Price {
d.Price = m.Price
updated = true
}
if m.Amount > 0 && m.Amount != d.Amount {
d.Amount = m.Amount
updated = true
}
if m.LimitPriceUpper > 0 && m.LimitPriceUpper != d.LimitPriceUpper {
d.LimitPriceUpper = m.LimitPriceUpper
updated = true
}
if m.LimitPriceLower > 0 && m.LimitPriceLower != d.LimitPriceLower {
d.LimitPriceLower = m.LimitPriceLower
updated = true
}
if m.TriggerPrice > 0 && m.TriggerPrice != d.TriggerPrice {
d.TriggerPrice = m.TriggerPrice
updated = true
}
if m.TargetAmount > 0 && m.TargetAmount != d.TargetAmount {
d.TargetAmount = m.TargetAmount
updated = true
}
if m.ExecutedAmount > 0 && m.ExecutedAmount != d.ExecutedAmount {
d.ExecutedAmount = m.ExecutedAmount
updated = true
}
if m.Fee > 0 && m.Fee != d.Fee {
d.Fee = m.Fee
updated = true
}
if m.AccountID != "" && m.AccountID != d.AccountID {
d.AccountID = m.AccountID
updated = true
}
if m.PostOnly != d.PostOnly {
d.PostOnly = m.PostOnly
updated = true
}
if !m.Pair.IsEmpty() && m.Pair != d.Pair {
d.Pair = m.Pair
updated = true
}
if m.Leverage != "" && m.Leverage != d.Leverage {
d.Leverage = m.Leverage
updated = true
}
if m.ClientID != "" && m.ClientID != d.ClientID {
d.ClientID = m.ClientID
updated = true
}
if m.WalletAddress != "" && m.WalletAddress != d.WalletAddress {
d.WalletAddress = m.WalletAddress
updated = true
}
if m.Type != "" && m.Type != d.Type {
d.Type = m.Type
updated = true
}
if m.Side != "" && m.Side != d.Side {
d.Side = m.Side
updated = true
}
if m.Status != "" && m.Status != d.Status {
d.Status = m.Status
updated = true
}
if m.AssetType != "" && m.AssetType != d.AssetType {
d.AssetType = m.AssetType
updated = true
}
if m.Trades != nil {
for x := range m.Trades {
var found bool
for y := range d.Trades {
if d.Trades[y].TID != m.Trades[x].TID {
continue
}
found = true
if d.Trades[y].Fee != m.Trades[x].Fee {
d.Trades[y].Fee = m.Trades[x].Fee
updated = true
}
if m.Trades[y].Price != 0 && d.Trades[y].Price != m.Trades[x].Price {
d.Trades[y].Price = m.Trades[x].Price
updated = true
}
if d.Trades[y].Side != m.Trades[x].Side {
d.Trades[y].Side = m.Trades[x].Side
updated = true
}
if d.Trades[y].Type != m.Trades[x].Type {
d.Trades[y].Type = m.Trades[x].Type
updated = true
}
if d.Trades[y].Description != m.Trades[x].Description {
d.Trades[y].Description = m.Trades[x].Description
updated = true
}
if m.Trades[y].Amount != 0 && d.Trades[y].Amount != m.Trades[x].Amount {
d.Trades[y].Amount = m.Trades[x].Amount
updated = true
}
if d.Trades[y].Timestamp != m.Trades[x].Timestamp {
d.Trades[y].Timestamp = m.Trades[x].Timestamp
updated = true
}
if d.Trades[y].IsMaker != m.Trades[x].IsMaker {
d.Trades[y].IsMaker = m.Trades[x].IsMaker
updated = true
}
}
if !found {
d.Trades = append(d.Trades, m.Trades[x])
updated = true
}
m.RemainingAmount -= m.Trades[x].Amount
}
}
if m.RemainingAmount > 0 && m.RemainingAmount != d.RemainingAmount {
d.RemainingAmount = m.RemainingAmount
updated = true
}
if updated {
if d.LastUpdated == m.LastUpdated {
d.LastUpdated = time.Now()
} else {
d.LastUpdated = m.LastUpdated
}
}
}
// String implements the stringer interface
func (t Type) String() string {
return string(t)
@@ -126,7 +373,7 @@ func FilterOrdersBySide(orders *[]Detail, side Side) {
var filteredOrders []Detail
for i := range *orders {
if strings.EqualFold(string((*orders)[i].OrderSide), string(side)) {
if strings.EqualFold(string((*orders)[i].Side), string(side)) {
filteredOrders = append(filteredOrders, (*orders)[i])
}
}
@@ -143,7 +390,7 @@ func FilterOrdersByType(orders *[]Detail, orderType Type) {
var filteredOrders []Detail
for i := range *orders {
if strings.EqualFold(string((*orders)[i].OrderType), string(orderType)) {
if strings.EqualFold(string((*orders)[i].Type), string(orderType)) {
filteredOrders = append(filteredOrders, (*orders)[i])
}
}
@@ -163,8 +410,8 @@ func FilterOrdersByTickRange(orders *[]Detail, startTicks, endTicks time.Time) {
var filteredOrders []Detail
for i := range *orders {
if (*orders)[i].OrderDate.Unix() >= startTicks.Unix() &&
(*orders)[i].OrderDate.Unix() <= endTicks.Unix() {
if (*orders)[i].Date.Unix() >= startTicks.Unix() &&
(*orders)[i].Date.Unix() <= endTicks.Unix() {
filteredOrders = append(filteredOrders, (*orders)[i])
}
}
@@ -184,7 +431,7 @@ func FilterOrdersByCurrencies(orders *[]Detail, currencies []currency.Pair) {
for i := range *orders {
matchFound := false
for _, c := range currencies {
if !matchFound && (*orders)[i].CurrencyPair.EqualIncludeReciprocal(c) {
if !matchFound && (*orders)[i].Pair.EqualIncludeReciprocal(c) {
matchFound = true
}
}
@@ -223,7 +470,7 @@ func (b ByOrderType) Len() int {
}
func (b ByOrderType) Less(i, j int) bool {
return b[i].OrderType.String() < b[j].OrderType.String()
return b[i].Type.String() < b[j].Type.String()
}
func (b ByOrderType) Swap(i, j int) {
@@ -244,7 +491,7 @@ func (b ByCurrency) Len() int {
}
func (b ByCurrency) Less(i, j int) bool {
return b[i].CurrencyPair.String() < b[j].CurrencyPair.String()
return b[i].Pair.String() < b[j].Pair.String()
}
func (b ByCurrency) Swap(i, j int) {
@@ -265,7 +512,7 @@ func (b ByDate) Len() int {
}
func (b ByDate) Less(i, j int) bool {
return b[i].OrderDate.Unix() < b[j].OrderDate.Unix()
return b[i].Date.Unix() < b[j].Date.Unix()
}
func (b ByDate) Swap(i, j int) {
@@ -286,7 +533,7 @@ func (b ByOrderSide) Len() int {
}
func (b ByOrderSide) Less(i, j int) bool {
return b[i].OrderSide.String() < b[j].OrderSide.String()
return b[i].Side.String() < b[j].Side.String()
}
func (b ByOrderSide) Swap(i, j int) {
@@ -317,7 +564,7 @@ func StringToOrderSide(side string) (Side, error) {
case strings.EqualFold(side, AnySide.String()):
return AnySide, nil
default:
return Side(""), fmt.Errorf("%s not recognised as side type", side)
return UnknownSide, errors.New(side + " not recognised as order side")
}
}
@@ -329,16 +576,20 @@ func StringToOrderType(oType string) (Type, error) {
return Limit, nil
case strings.EqualFold(oType, Market.String()):
return Market, nil
case strings.EqualFold(oType, ImmediateOrCancel.String()):
case strings.EqualFold(oType, ImmediateOrCancel.String()),
strings.EqualFold(oType, "immediate or cancel"):
return ImmediateOrCancel, nil
case strings.EqualFold(oType, Stop.String()):
case strings.EqualFold(oType, Stop.String()),
strings.EqualFold(oType, "stop loss"),
strings.EqualFold(oType, "stop_loss"):
return Stop, nil
case strings.EqualFold(oType, TrailingStop.String()):
case strings.EqualFold(oType, TrailingStop.String()),
strings.EqualFold(oType, "trailing stop"):
return TrailingStop, nil
case strings.EqualFold(oType, AnyType.String()):
return AnyType, nil
default:
return Unknown, fmt.Errorf("%s not recognised as order type", oType)
return UnknownType, errors.New(oType + " not recognised as order type")
}
}
@@ -348,17 +599,27 @@ func StringToOrderStatus(status string) (Status, error) {
switch {
case strings.EqualFold(status, AnyStatus.String()):
return AnyStatus, nil
case strings.EqualFold(status, New.String()):
case strings.EqualFold(status, New.String()),
strings.EqualFold(status, "placed"):
return New, nil
case strings.EqualFold(status, Active.String()):
return Active, nil
case strings.EqualFold(status, PartiallyFilled.String()):
case strings.EqualFold(status, PartiallyFilled.String()),
strings.EqualFold(status, "partially matched"),
strings.EqualFold(status, "partially filled"):
return PartiallyFilled, nil
case strings.EqualFold(status, Filled.String()):
case strings.EqualFold(status, Filled.String()),
strings.EqualFold(status, "fully matched"),
strings.EqualFold(status, "fully filled"):
return Filled, nil
case strings.EqualFold(status, PartiallyCancelled.String()),
strings.EqualFold(status, "partially cancelled"):
return PartiallyCancelled, nil
case strings.EqualFold(status, Cancelled.String()):
return Cancelled, nil
case strings.EqualFold(status, PendingCancel.String()):
case strings.EqualFold(status, PendingCancel.String()),
strings.EqualFold(status, "pending cancel"),
strings.EqualFold(status, "pending cancellation"):
return PendingCancel, nil
case strings.EqualFold(status, Rejected.String()):
return Rejected, nil
@@ -366,7 +627,11 @@ func StringToOrderStatus(status string) (Status, error) {
return Expired, nil
case strings.EqualFold(status, Hidden.String()):
return Hidden, nil
case strings.EqualFold(status, InsufficientBalance.String()):
return InsufficientBalance, nil
case strings.EqualFold(status, MarketUnavailable.String()):
return MarketUnavailable, nil
default:
return UnknownStatus, fmt.Errorf("%s not recognised as order STATUS", status)
return UnknownStatus, errors.New(status + " not recognised as order status")
}
}

View File

@@ -1,37 +0,0 @@
package order
import (
"testing"
)
func TestNewOrder(t *testing.T) {
ID := NewOrder("OKEX", 2000, 20.00)
if ID != 0 {
t.Error("Orders_test.go NewOrder() - Error")
}
ID = NewOrder("BATMAN", 400, 25.00)
if ID != 1 {
t.Error("Orders_test.go NewOrder() - Error")
}
}
func TestDeleteOrder(t *testing.T) {
if value := DeleteOrder(0); !value {
t.Error("Orders_test.go DeleteOrder() - Error")
}
if value := DeleteOrder(100); value {
t.Error("Orders_test.go DeleteOrder() - Error")
}
}
func TestGetOrdersByExchange(t *testing.T) {
if value := GetOrdersByExchange("OKEX"); len(value) != 0 {
t.Error("Orders_test.go GetOrdersByExchange() - Error")
}
}
func TestGetOrderByOrderID(t *testing.T) {
if value := GetOrderByOrderID(69); value != nil {
t.Error("Orders_test.go GetOrdersByExchange() - Error")
}
}