Utilising authenticated websocket functions in exchange wrappers (#384)

* Basic concept commit

* Initial changes to support bitfinex v2. Reverts linter changes as they suck. Exports bitfinex ws types

* Adds ticker, trade and orderbook support

* Candles sub that returns no data COMPLETE

* Adds authenticated ws support

* Adds the barebones endpoints to support

* Adds more endpoints

* Even more endpoints

* minicommit to switch and test

* All the interactive types

* Adds support for simultaneous connections. Updates tests. Nothing is working

* Successfully adds place order. Moves all authenticated endpoints to new switch case

* Cancel order and modify order

* Cancel all orders, cancel multi orders

* Finalises implementation. Uses testMain

* Adds WS wrapper support for some funcs

* Fixing rebasing issues

* Replaces use of currency as a variable. Updates a lot of coinut websocket auth endpoint stuff

* Fixes some fun for loops with GetEnabledPairs

* Fixes tests impacted by currency var change

* Adds coinut support for WS functions. Replaces `order` vars with `ord`. Fixes some for loops too. Removes verbose from bitfinex

* So many panics

* I'm fixing a hole, where the panics get in, and stops my mind from wandering, where it will go

* Moves func `CanUseAuthenticatedWebsocketEndpoint` to Websocket package as it fits better. Adds test coverage of `CanUseAuthenticatedWebsocketEndpoint`

* Finishes up all of coinuts ws implementations.

* GateIO implementation

* Adds some helper funcs for types, sides and status. Adds support for huobi. Removes unnecessary type

* Adds forgotten huobi endpoint

* Fixes cancel order endpoint

* go hates my formatting and so do I

* The process to get authenticated kraken websocket to work. Uses testmain. Adds new auth channel, auth subscriptions, auth data handling. Not working yet

* Finishes open orders handling

* Mini update for status only updates

* Fixes some kraken points

* Finishes WS kraken since it doesn't work

* Unfinished commit, cleaning up types

* Finishes the const replacing

* Fixes extra GetNAmes after rebase

* An end to the cleanup. testmain for gateio

* Adds ZB support

* Bitfinex cleanup. Renamed func

* Testmain-47s for everyone!!! yayaaaaaaa

* Adds kraken websocket wrapper support

* Fixes rebase issues

* Fixes tests from rebase

* Adds test for conversion. Fixes for loop. Updates test order pricing. Fixes some poor made tests. Adds proper error handling for ws responses instead of logging them. Fixed issue where commented code ruined kraken ws.

* Fixes secret linting issues. Prioritises bitfinex channelID responses over authorised

* Fixes sloppy error/var declarations

* Fixes crazy bad logic where submit order errors weren't really considered. Parralols alphapoint/alphapoint_test.go. Removes buffer for multi-websocket comms channel.

* Removal of inline string and removal of redundant nil checkerinos

* Fixes err checks. Checks whether float has decimal. Fixes append. Drops omitempties. Parallel to some tests. Moves var declarations

* Replaces my lazy sprintfs with strconv.FormatInt(time.Now().Unix(), 10)

* Adds shiny new FullyMatched bool. Fixes coinbene buy sell consts

* Fixes oopsie with coinbene const replacement

* Fixes currency issue

* Cleans up new places that use JSONDecode

* Fixes huge panic bug from string int conversion. Adds large testtable for strings to order types

* Fixes some more strconversion issues. Fixes table test var usage. Changes mapperino name

* Added some new scenarios for number splitting

* Fixes lint issues

* negative num fix

* Typo fix

* Accuracy warning comment
This commit is contained in:
Scott
2019-12-04 14:16:23 +11:00
committed by Adrian Gallagher
parent a33ddcfa0a
commit 11a68a9bb7
96 changed files with 4112 additions and 2818 deletions

View File

@@ -1,6 +1,7 @@
package order
import (
"errors"
"strings"
"testing"
"time"
@@ -398,3 +399,138 @@ func TestSortOrdersByOrderType(t *testing.T) {
orders[0].OrderType)
}
}
var stringsToOrderSide = []struct {
in string
out Side
err error
}{
{"buy", Buy, nil},
{"BUY", Buy, nil},
{"bUy", Buy, nil},
{"sell", Sell, nil},
{"SELL", Sell, nil},
{"sElL", Sell, nil},
{"bid", Bid, nil},
{"BID", Bid, nil},
{"bId", Bid, nil},
{"ask", Ask, nil},
{"ASK", Ask, nil},
{"aSk", Ask, nil},
{"any", AnySide, nil},
{"ANY", AnySide, nil},
{"aNy", AnySide, nil},
{"woahMan", Buy, errors.New("woahMan not recognised as side type")},
}
func TestStringToOrderSide(t *testing.T) {
for i := 0; i < len(stringsToOrderSide); i++ {
testData := &stringsToOrderSide[i]
t.Run(testData.in, func(t *testing.T) {
out, err := StringToOrderSide(testData.in)
if err != nil {
if err.Error() != testData.err.Error() {
t.Error("Unexpected error", err)
}
} else if out != testData.out {
t.Errorf("Unexpected output %v. Expected %v", out, testData.out)
}
})
}
}
var stringsToOrderType = []struct {
in string
out Type
err error
}{
{"limit", Limit, nil},
{"LIMIT", Limit, nil},
{"lImIt", Limit, nil},
{"market", Market, nil},
{"MARKET", Market, nil},
{"mArKeT", Market, 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},
{"any", AnyType, nil},
{"ANY", AnyType, nil},
{"aNy", AnyType, nil},
{"woahMan", Unknown, errors.New("woahMan not recognised as order type")},
}
func TestStringToOrderType(t *testing.T) {
for i := 0; i < len(stringsToOrderType); i++ {
testData := &stringsToOrderType[i]
t.Run(testData.in, func(t *testing.T) {
out, err := StringToOrderType(testData.in)
if err != nil {
if err.Error() != testData.err.Error() {
t.Error("Unexpected error", err)
}
} else if out != testData.out {
t.Errorf("Unexpected output %v. Expected %v", out, testData.out)
}
})
}
}
var stringsToOrderStatus = []struct {
in string
out Status
err error
}{
{"any", AnyStatus, nil},
{"ANY", AnyStatus, nil},
{"aNy", AnyStatus, nil},
{"new", New, nil},
{"NEW", New, nil},
{"nEw", New, nil},
{"active", Active, nil},
{"ACTIVE", Active, nil},
{"aCtIvE", Active, nil},
{"partially_filled", PartiallyFilled, nil},
{"PARTIALLY_FILLED", PartiallyFilled, nil},
{"pArTiAlLy_FiLlEd", PartiallyFilled, nil},
{"filled", Filled, nil},
{"FILLED", Filled, nil},
{"fIlLeD", Filled, nil},
{"canceled", Cancelled, nil},
{"CANCELED", Cancelled, nil},
{"cAnCelEd", Cancelled, nil},
{"pending_cancel", PendingCancel, nil},
{"PENDING_CANCEL", PendingCancel, nil},
{"pENdInG_cAnCeL", PendingCancel, nil},
{"rejected", Rejected, nil},
{"REJECTED", Rejected, nil},
{"rEjEcTeD", Rejected, nil},
{"expired", Expired, nil},
{"EXPIRED", Expired, nil},
{"eXpIrEd", Expired, nil},
{"hidden", Hidden, nil},
{"HIDDEN", Hidden, nil},
{"hIdDeN", Hidden, nil},
{"woahMan", UnknownStatus, errors.New("woahMan not recognised as order STATUS")},
}
func TestStringToOrderStatus(t *testing.T) {
for i := 0; i < len(stringsToOrderStatus); i++ {
testData := &stringsToOrderStatus[i]
t.Run(testData.in, func(t *testing.T) {
out, err := StringToOrderStatus(testData.in)
if err != nil {
if err.Error() != testData.err.Error() {
t.Error("Unexpected error", err)
}
} else if out != testData.out {
t.Errorf("Unexpected output %v. Expected %v", out, testData.out)
}
})
}
}

View File

@@ -50,6 +50,7 @@ type Submit struct {
// SubmitResponse is what is returned after submitting an order to an exchange
type SubmitResponse struct {
IsOrderPlaced bool
FullyMatched bool
OrderID string
}
@@ -127,7 +128,7 @@ type Detail struct {
// TradeHistory holds exchange history data
type TradeHistory struct {
Timestamp time.Time
TID int64
TID string
Price float64
Amount float64
Exchange string

View File

@@ -1,6 +1,7 @@
package order
import (
"fmt"
"sort"
"strings"
"time"
@@ -10,18 +11,18 @@ import (
// NewOrder creates a new order and returns a an orderID
func NewOrder(exchangeName string, amount, price float64) int {
order := &Order{}
ord := &Order{}
if len(Orders) == 0 {
order.OrderID = 0
ord.OrderID = 0
} else {
order.OrderID = len(Orders)
ord.OrderID = len(Orders)
}
order.Exchange = exchangeName
order.Amount = amount
order.Price = price
Orders = append(Orders, order)
return order.OrderID
ord.Exchange = exchangeName
ord.Amount = amount
ord.Price = price
Orders = append(Orders, ord)
return ord.OrderID
}
// DeleteOrder deletes orders by ID and returns state
@@ -300,3 +301,72 @@ func SortOrdersBySide(orders *[]Detail, reverse bool) {
sort.Sort(ByOrderSide(*orders))
}
}
// StringToOrderSide for converting case insensitive order side
// and returning a real Side
func StringToOrderSide(side string) (Side, error) {
switch {
case strings.EqualFold(side, Buy.String()):
return Buy, nil
case strings.EqualFold(side, Sell.String()):
return Sell, nil
case strings.EqualFold(side, Bid.String()):
return Bid, nil
case strings.EqualFold(side, Ask.String()):
return Ask, nil
case strings.EqualFold(side, AnySide.String()):
return AnySide, nil
default:
return Side(""), fmt.Errorf("%s not recognised as side type", side)
}
}
// StringToOrderType for converting case insensitive order type
// and returning a real Type
func StringToOrderType(oType string) (Type, error) {
switch {
case strings.EqualFold(oType, Limit.String()):
return Limit, nil
case strings.EqualFold(oType, Market.String()):
return Market, nil
case strings.EqualFold(oType, ImmediateOrCancel.String()):
return ImmediateOrCancel, nil
case strings.EqualFold(oType, Stop.String()):
return Stop, nil
case strings.EqualFold(oType, TrailingStop.String()):
return TrailingStop, nil
case strings.EqualFold(oType, AnyType.String()):
return AnyType, nil
default:
return Unknown, fmt.Errorf("%s not recognised as order type", oType)
}
}
// StringToOrderStatus for converting case insensitive order status
// and returning a real Status
func StringToOrderStatus(status string) (Status, error) {
switch {
case strings.EqualFold(status, AnyStatus.String()):
return AnyStatus, nil
case strings.EqualFold(status, New.String()):
return New, nil
case strings.EqualFold(status, Active.String()):
return Active, nil
case strings.EqualFold(status, PartiallyFilled.String()):
return PartiallyFilled, nil
case strings.EqualFold(status, Filled.String()):
return Filled, nil
case strings.EqualFold(status, Cancelled.String()):
return Cancelled, nil
case strings.EqualFold(status, PendingCancel.String()):
return PendingCancel, nil
case strings.EqualFold(status, Rejected.String()):
return Rejected, nil
case strings.EqualFold(status, Expired.String()):
return Expired, nil
case strings.EqualFold(status, Hidden.String()):
return Hidden, nil
default:
return UnknownStatus, fmt.Errorf("%s not recognised as order STATUS", status)
}
}