diff --git a/engine/websocketroutine_manager.go b/engine/websocketroutine_manager.go index d92913d2870..ff56ad5877f 100644 --- a/engine/websocketroutine_manager.go +++ b/engine/websocketroutine_manager.go @@ -307,7 +307,7 @@ func (m *WebsocketRoutineManager) websocketDataHandler(exchName string, data int if err != nil { return err } - m.printOrderSummary(d, true) + m.printOrderSummary(od, true) } case []order.Detail: for x := range d { @@ -330,7 +330,7 @@ func (m *WebsocketRoutineManager) websocketDataHandler(exchName string, data int if err != nil { return err } - m.printOrderSummary(&d[x], true) + m.printOrderSummary(od, true) } } case order.ClassificationError: diff --git a/exchanges/kraken/kraken_test.go b/exchanges/kraken/kraken_test.go index e682d8ee121..7f417ac3267 100644 --- a/exchanges/kraken/kraken_test.go +++ b/exchanges/kraken/kraken_test.go @@ -1,6 +1,7 @@ package kraken import ( + "bufio" "context" "errors" "fmt" @@ -13,6 +14,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/stretchr/testify/assert" "github.com/thrasher-corp/gocryptotrader/common" "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/config" @@ -1296,9 +1298,9 @@ func TestWsCancelAllOrders(t *testing.T) { func TestWsPong(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "event": "pong", - "reqid": 42 -}`) + "event": "pong", + "reqid": 42 + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1308,11 +1310,11 @@ func TestWsPong(t *testing.T) { func TestWsSystemStatus(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "connectionID": 8628615390848610000, - "event": "systemStatus", - "status": "online", - "version": "1.0.0" -}`) + "connectionID": 8628615390848610000, + "event": "systemStatus", + "status": "online", + "version": "1.0.0" + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1322,59 +1324,59 @@ func TestWsSystemStatus(t *testing.T) { func TestWsSubscriptionStatus(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "channelID": 10001, - "channelName": "ticker", - "event": "subscriptionStatus", - "pair": "XBT/EUR", - "status": "subscribed", - "subscription": { - "name": "ticker" - } -}`) + "channelID": 10001, + "channelName": "ticker", + "event": "subscriptionStatus", + "pair": "XBT/EUR", + "status": "subscribed", + "subscription": { + "name": "ticker" + } + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`{ - "channelID": 10001, - "channelName": "ohlc-5", - "event": "subscriptionStatus", - "pair": "XBT/EUR", - "reqid": 42, - "status": "unsubscribed", - "subscription": { - "interval": 5, - "name": "ohlc" - } -}`) + "channelID": 10001, + "channelName": "ohlc-5", + "event": "subscriptionStatus", + "pair": "XBT/EUR", + "reqid": 42, + "status": "unsubscribed", + "subscription": { + "interval": 5, + "name": "ohlc" + } + }`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`{ - "channelName": "ownTrades", - "event": "subscriptionStatus", - "status": "subscribed", - "subscription": { - "name": "ownTrades" - } -}`) + "channelName": "ownTrades", + "event": "subscriptionStatus", + "status": "subscribed", + "subscription": { + "name": "ownTrades" + } + }`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`{ - "errorMessage": "Subscription depth not supported", - "event": "subscriptionStatus", - "pair": "XBT/USD", - "status": "error", - "subscription": { - "depth": 42, - "name": "book" - } -}`) + "errorMessage": "Subscription depth not supported", + "event": "subscriptionStatus", + "pair": "XBT/USD", + "status": "error", + "subscription": { + "depth": 42, + "name": "book" + } + }`) err = k.wsHandleData(pressXToJSON) if err == nil { t.Error("Expected error") @@ -1384,64 +1386,64 @@ func TestWsSubscriptionStatus(t *testing.T) { func TestWsTicker(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "channelID": 1337, - "channelName": "ticker", - "event": "subscriptionStatus", - "pair": "XBT/EUR", - "status": "subscribed", - "subscription": { - "name": "ticker" - } -}`) + "channelID": 1337, + "channelName": "ticker", + "event": "subscriptionStatus", + "pair": "XBT/EUR", + "status": "subscribed", + "subscription": { + "name": "ticker" + } + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 1337, - { - "a": [ - "5525.40000", - 1, - "1.000" - ], - "b": [ - "5525.10000", - 1, - "1.000" - ], - "c": [ - "5525.10000", - "0.00398963" - ], - "h": [ - "5783.00000", - "5783.00000" - ], - "l": [ - "5505.00000", - "5505.00000" - ], - "o": [ - "5760.70000", - "5763.40000" - ], - "p": [ - "5631.44067", - "5653.78939" - ], - "t": [ - 11493, - 16267 - ], - "v": [ - "2634.11501494", - "3591.17907851" - ] - }, - "ticker", - "XBT/USD" -]`) + 1337, + { + "a": [ + "5525.40000", + 1, + "1.000" + ], + "b": [ + "5525.10000", + 1, + "1.000" + ], + "c": [ + "5525.10000", + "0.00398963" + ], + "h": [ + "5783.00000", + "5783.00000" + ], + "l": [ + "5505.00000", + "5505.00000" + ], + "o": [ + "5760.70000", + "5763.40000" + ], + "p": [ + "5631.44067", + "5653.78939" + ], + "t": [ + 11493, + 16267 + ], + "v": [ + "2634.11501494", + "3591.17907851" + ] + }, + "ticker", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1451,35 +1453,35 @@ func TestWsTicker(t *testing.T) { func TestWsOHLC(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "channelID": 13337, - "channelName": "ohlc", - "event": "subscriptionStatus", - "pair": "XBT/EUR", - "status": "subscribed", - "subscription": { - "name": "ohlc" - } -}`) + "channelID": 13337, + "channelName": "ohlc", + "event": "subscriptionStatus", + "pair": "XBT/EUR", + "status": "subscribed", + "subscription": { + "name": "ohlc" + } + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 13337, - [ - "1542057314.748456", - "1542057360.435743", - "3586.70000", - "3586.70000", - "3586.60000", - "3586.60000", - "3586.68894", - "0.03373000", - 2 - ], - "ohlc-5", - "XBT/USD" -]`) + 13337, + [ + "1542057314.748456", + "1542057360.435743", + "3586.70000", + "3586.70000", + "3586.60000", + "3586.60000", + "3586.68894", + "0.03373000", + 2 + ], + "ohlc-5", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1489,42 +1491,42 @@ func TestWsOHLC(t *testing.T) { func TestWsTrade(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "channelID": 133337, - "channelName": "trade", - "event": "subscriptionStatus", - "pair": "XBT/EUR", - "status": "subscribed", - "subscription": { - "name": "trade" - } -}`) + "channelID": 133337, + "channelName": "trade", + "event": "subscriptionStatus", + "pair": "XBT/EUR", + "status": "subscribed", + "subscription": { + "name": "trade" + } + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 133337, - [ - [ - "5541.20000", - "0.15850568", - "1534614057.321597", - "s", - "l", - "" - ], - [ - "6060.00000", - "0.02455000", - "1534614057.324998", - "b", - "l", - "" - ] - ], - "trade", - "XBT/USD" -]`) + 133337, + [ + [ + "5541.20000", + "0.15850568", + "1534614057.321597", + "s", + "l", + "" + ], + [ + "6060.00000", + "0.02455000", + "1534614057.324998", + "b", + "l", + "" + ] + ], + "trade", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1534,31 +1536,31 @@ func TestWsTrade(t *testing.T) { func TestWsSpread(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "channelID": 1333337, - "channelName": "spread", - "event": "subscriptionStatus", - "pair": "XBT/EUR", - "status": "subscribed", - "subscription": { - "name": "spread" - } -}`) + "channelID": 1333337, + "channelName": "spread", + "event": "subscriptionStatus", + "pair": "XBT/EUR", + "status": "subscribed", + "subscription": { + "name": "spread" + } + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 1333337, - [ - "5698.40000", - "5700.00000", - "1542057299.545897", - "1.01234567", - "0.98765432" - ], - "spread", - "XBT/USD" -]`) + 1333337, + [ + "5698.40000", + "5700.00000", + "1542057299.545897", + "1.01234567", + "0.98765432" + ], + "spread", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1568,173 +1570,173 @@ func TestWsSpread(t *testing.T) { func TestWsOrdrbook(t *testing.T) { t.Parallel() pressXToJSON := []byte(`{ - "channelID": 13333337, - "channelName": "book", - "event": "subscriptionStatus", - "pair": "XBT/USD", - "status": "subscribed", - "subscription": { - "name": "book" - } -}`) + "channelID": 13333337, + "channelName": "book", + "event": "subscriptionStatus", + "pair": "XBT/USD", + "status": "subscribed", + "subscription": { + "name": "book" + } + }`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 13333337, - { - "as": [ - [ - "5541.30000", - "2.50700000", - "1534614248.123678" - ], - [ - "5541.80000", - "0.33000000", - "1534614098.345543" - ], - [ - "5542.70000", - "0.64700000", - "1534614244.654432" - ], - [ - "5544.30000", - "2.50700000", - "1534614248.123678" - ], - [ - "5545.80000", - "0.33000000", - "1534614098.345543" - ], - [ - "5546.70000", - "0.64700000", - "1534614244.654432" - ], - [ - "5547.70000", - "0.64700000", - "1534614244.654432" - ], - [ - "5548.30000", - "2.50700000", - "1534614248.123678" - ], - [ - "5549.80000", - "0.33000000", - "1534614098.345543" - ], - [ - "5550.70000", - "0.64700000", - "1534614244.654432" - ] - ], - "bs": [ - [ - "5541.20000", - "1.52900000", - "1534614248.765567" - ], - [ - "5539.90000", - "0.30000000", - "1534614241.769870" - ], - [ - "5539.50000", - "5.00000000", - "1534613831.243486" - ], - [ - "5538.20000", - "1.52900000", - "1534614248.765567" - ], - [ - "5537.90000", - "0.30000000", - "1534614241.769870" - ], - [ - "5536.50000", - "5.00000000", - "1534613831.243486" - ], - [ - "5535.20000", - "1.52900000", - "1534614248.765567" - ], - [ - "5534.90000", - "0.30000000", - "1534614241.769870" - ], - [ - "5533.50000", - "5.00000000", - "1534613831.243486" - ], - [ - "5532.50000", - "5.00000000", - "1534613831.243486" - ] - ] - }, - "book-100", - "XBT/USD" -]`) + 13333337, + { + "as": [ + [ + "5541.30000", + "2.50700000", + "1534614248.123678" + ], + [ + "5541.80000", + "0.33000000", + "1534614098.345543" + ], + [ + "5542.70000", + "0.64700000", + "1534614244.654432" + ], + [ + "5544.30000", + "2.50700000", + "1534614248.123678" + ], + [ + "5545.80000", + "0.33000000", + "1534614098.345543" + ], + [ + "5546.70000", + "0.64700000", + "1534614244.654432" + ], + [ + "5547.70000", + "0.64700000", + "1534614244.654432" + ], + [ + "5548.30000", + "2.50700000", + "1534614248.123678" + ], + [ + "5549.80000", + "0.33000000", + "1534614098.345543" + ], + [ + "5550.70000", + "0.64700000", + "1534614244.654432" + ] + ], + "bs": [ + [ + "5541.20000", + "1.52900000", + "1534614248.765567" + ], + [ + "5539.90000", + "0.30000000", + "1534614241.769870" + ], + [ + "5539.50000", + "5.00000000", + "1534613831.243486" + ], + [ + "5538.20000", + "1.52900000", + "1534614248.765567" + ], + [ + "5537.90000", + "0.30000000", + "1534614241.769870" + ], + [ + "5536.50000", + "5.00000000", + "1534613831.243486" + ], + [ + "5535.20000", + "1.52900000", + "1534614248.765567" + ], + [ + "5534.90000", + "0.30000000", + "1534614241.769870" + ], + [ + "5533.50000", + "5.00000000", + "1534613831.243486" + ], + [ + "5532.50000", + "5.00000000", + "1534613831.243486" + ] + ] + }, + "book-100", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 13333337, - { - "a": [ - [ - "5541.30000", - "2.50700000", - "1534614248.456738" - ], - [ - "5542.50000", - "0.40100000", - "1534614248.456738" - ] - ], - "c": "4187525586" - }, - "book-10", - "XBT/USD" -]`) + 13333337, + { + "a": [ + [ + "5541.30000", + "2.50700000", + "1534614248.456738" + ], + [ + "5542.50000", + "0.40100000", + "1534614248.456738" + ] + ], + "c": "4187525586" + }, + "book-10", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) } pressXToJSON = []byte(`[ - 13333337, - { - "b": [ - [ - "5541.30000", - "0.00000000", - "1534614335.345903" - ] - ], - "c": "4187525586" - }, - "book-10", - "XBT/USD" -]`) + 13333337, + { + "b": [ + [ + "5541.30000", + "0.00000000", + "1534614335.345903" + ] + ], + "c": "4187525586" + }, + "book-10", + "XBT/USD" + ]`) err = k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1744,70 +1746,70 @@ func TestWsOrdrbook(t *testing.T) { func TestWsOwnTrades(t *testing.T) { t.Parallel() pressXToJSON := []byte(`[ - [ - { - "TDLH43-DVQXD-2KHVYY": { - "cost": "1000000.00000", - "fee": "1600.00000", - "margin": "0.00000", - "ordertxid": "TDLH43-DVQXD-2KHVYY", - "ordertype": "limit", - "pair": "XBT/USD", - "postxid": "OGTT3Y-C6I3P-XRI6HX", - "price": "100000.00000", - "time": "1560516023.070651", - "type": "sell", - "vol": "1000000000.00000000" - } - }, - { - "TDLH43-DVQXD-2KHVYY": { - "cost": "1000000.00000", - "fee": "600.00000", - "margin": "0.00000", - "ordertxid": "TDLH43-DVQXD-2KHVYY", - "ordertype": "limit", - "pair": "XBT/USD", - "postxid": "OGTT3Y-C6I3P-XRI6HX", - "price": "100000.00000", - "time": "1560516023.070658", - "type": "buy", - "vol": "1000000000.00000000" - } - }, - { - "TDLH43-DVQXD-2KHVYY": { - "cost": "1000000.00000", - "fee": "1600.00000", - "margin": "0.00000", - "ordertxid": "TDLH43-DVQXD-2KHVYY", - "ordertype": "limit", - "pair": "XBT/USD", - "postxid": "OGTT3Y-C6I3P-XRI6HX", - "price": "100000.00000", - "time": "1560520332.914657", - "type": "sell", - "vol": "1000000000.00000000" - } - }, - { - "TDLH43-DVQXD-2KHVYY": { - "cost": "1000000.00000", - "fee": "600.00000", - "margin": "0.00000", - "ordertxid": "TDLH43-DVQXD-2KHVYY", - "ordertype": "limit", - "pair": "XBT/USD", - "postxid": "OGTT3Y-C6I3P-XRI6HX", - "price": "100000.00000", - "time": "1560520332.914664", - "type": "buy", - "vol": "1000000000.00000000" - } - } - ], - "ownTrades" -]`) + [ + { + "TDLH43-DVQXD-2KHVYY": { + "cost": "1000000.00000", + "fee": "1600.00000", + "margin": "0.00000", + "ordertxid": "TDLH43-DVQXD-2KHVYY", + "ordertype": "limit", + "pair": "XBT/USD", + "postxid": "OGTT3Y-C6I3P-XRI6HX", + "price": "100000.00000", + "time": "1560516023.070651", + "type": "sell", + "vol": "1000000000.00000000" + } + }, + { + "TDLH43-DVQXD-2KHVYY": { + "cost": "1000000.00000", + "fee": "600.00000", + "margin": "0.00000", + "ordertxid": "TDLH43-DVQXD-2KHVYY", + "ordertype": "limit", + "pair": "XBT/USD", + "postxid": "OGTT3Y-C6I3P-XRI6HX", + "price": "100000.00000", + "time": "1560516023.070658", + "type": "buy", + "vol": "1000000000.00000000" + } + }, + { + "TDLH43-DVQXD-2KHVYY": { + "cost": "1000000.00000", + "fee": "1600.00000", + "margin": "0.00000", + "ordertxid": "TDLH43-DVQXD-2KHVYY", + "ordertype": "limit", + "pair": "XBT/USD", + "postxid": "OGTT3Y-C6I3P-XRI6HX", + "price": "100000.00000", + "time": "1560520332.914657", + "type": "sell", + "vol": "1000000000.00000000" + } + }, + { + "TDLH43-DVQXD-2KHVYY": { + "cost": "1000000.00000", + "fee": "600.00000", + "margin": "0.00000", + "ordertxid": "TDLH43-DVQXD-2KHVYY", + "ordertype": "limit", + "pair": "XBT/USD", + "postxid": "OGTT3Y-C6I3P-XRI6HX", + "price": "100000.00000", + "time": "1560520332.914664", + "type": "buy", + "vol": "1000000000.00000000" + } + } + ], + "ownTrades" + ]`) err := k.wsHandleData(pressXToJSON) if err != nil { t.Error(err) @@ -1816,150 +1818,116 @@ func TestWsOwnTrades(t *testing.T) { func TestWsOpenOrders(t *testing.T) { t.Parallel() - pressXToJSON := []byte(`[ - [ - { - "OGTT3Y-C6I3P-XRI6HX": { - "cost": "0.00000", - "descr": { - "close": "", - "leverage": "0.1", - "order": "sell 10.00345345 XBT/USD @ limit 34.50000 with 0:1 leverage", - "ordertype": "limit", - "pair": "XBT/USD", - "price": "34.50000", - "price2": "0.00000", - "type": "sell" - }, - "expiretm": "0.000000", - "fee": "0.00000", - "limitprice": "34.50000", - "misc": "", - "oflags": "fcib", - "opentm": "0.000000", - "price": "34.50000", - "refid": "OKIVMP-5GVZN-Z2D2UA", - "starttm": "0.000000", - "status": "open", - "stopprice": "0.000000", - "userref": 0, - "vol": "10.00345345", - "vol_exec": "0.00000000" - } - }, - { - "OGTT3Y-C6I3P-XRI6HX": { - "cost": "0.00000", - "descr": { - "close": "", - "leverage": "0.1", - "order": "sell 0.00000010 XBT/USD @ limit 5334.60000 with 0:1 leverage", - "ordertype": "limit", - "pair": "XBT/USD", - "price": "5334.60000", - "price2": "0.00000", - "type": "sell" - }, - "expiretm": "0.000000", - "fee": "0.00000", - "limitprice": "5334.60000", - "misc": "", - "oflags": "fcib", - "opentm": "0.000000", - "price": "5334.60000", - "refid": "OKIVMP-5GVZN-Z2D2UA", - "starttm": "0.000000", - "status": "open", - "stopprice": "0.000000", - "userref": 0, - "vol": "0.00000010", - "vol_exec": "0.00000000" - } - }, - { - "OGTT3Y-C6I3P-XRI6HX": { - "cost": "0.00000", - "descr": { - "close": "", - "leverage": "0.1", - "order": "sell 0.00001000 XBT/USD @ limit 90.40000 with 0:1 leverage", - "ordertype": "limit", - "pair": "XBT/USD", - "price": "90.40000", - "price2": "0.00000", - "type": "sell" - }, - "expiretm": "0.000000", - "fee": "0.00000", - "limitprice": "90.40000", - "misc": "", - "oflags": "fcib", - "opentm": "0.000000", - "price": "90.40000", - "refid": "OKIVMP-5GVZN-Z2D2UA", - "starttm": "0.000000", - "status": "open", - "stopprice": "0.000000", - "userref": 0, - "vol": "0.00001000", - "vol_exec": "0.00000000" - } - }, - { - "OGTT3Y-C6I3P-XRI6HX": { - "cost": "0.00000", - "descr": { - "close": "", - "leverage": "0.1", - "order": "sell 0.00001000 XBT/USD @ limit 9.00000 with 0:1 leverage", - "ordertype": "limit", - "pair": "XBT/USD", - "price": "9.00000", - "price2": "0.00000", - "type": "sell" - }, - "expiretm": "0.000000", - "fee": "0.00000", - "limitprice": "9.00000", - "misc": "", - "oflags": "fcib", - "opentm": "0.000000", - "price": "9.00000", - "refid": "OKIVMP-5GVZN-Z2D2UA", - "starttm": "0.000000", - "status": "open", - "stopprice": "0.000000", - "userref": 0, - "vol": "0.00001000", - "vol_exec": "0.00000000" - } - } - ], - "openOrders" -]`) - err := k.wsHandleData(pressXToJSON) - if err != nil { - t.Error(err) + pairs := currency.Pairs{ + currency.Pair{Base: currency.XBT, Quote: currency.USD}, + currency.Pair{Base: currency.XBT, Quote: currency.USDT}, + } + k := Kraken{ + Base: exchange.Base{ + Name: "dummy", + CurrencyPairs: currency.PairsManager{ + Pairs: map[asset.Item]*currency.PairStore{ + asset.Spot: { + Available: pairs, + Enabled: pairs, + ConfigFormat: ¤cy.PairFormat{ + Uppercase: true, + Delimiter: currency.DashDelimiter, + }, + }, + }, + }, + Websocket: &stream.Websocket{ + Wg: new(sync.WaitGroup), + DataHandler: make(chan interface{}, 128), + }, + }, } - pressXToJSON = []byte(`[ - [ - { - "OGTT3Y-C6I3P-XRI6HX": { - "status": "closed" - } - }, - { - "OGTT3Y-C6I3P-XRI6HX": { - "status": "closed" - } - } - ], - "openOrders" -]`) - err = k.wsHandleData(pressXToJSON) + + k.API.Endpoints = k.NewEndpoints() + + fixture, err := os.Open("testdata/wsOpenTrades.json") + defer func() { assert.Nil(t, fixture.Close()) }() if err != nil { - t.Error(err) + t.Errorf("Error opening test fixture 'testdata/wsOpenTrades.json': %v", err) + return } + + s := bufio.NewScanner(fixture) + for s.Scan() { + if err = k.wsHandleData(s.Bytes()); err != nil { + t.Errorf("Error in wsHandleData; err: '%v', msg: '%v'", err, s.Bytes()) + } + } + if err := s.Err(); err != nil { + t.Error(err) + } + + seen := 0 + + for reading := true; reading; { + select { + default: + reading = false + case resp := <-k.Websocket.DataHandler: + seen++ + switch v := resp.(type) { + case *order.Detail: + switch seen { + case 1: + assert.Equal(t, "OGTT3Y-C6I3P-XRI6HR", v.OrderID, "OrderID") + assert.Equal(t, order.Limit, v.Type, "order type") + assert.Equal(t, order.Sell, v.Side, "order side") + assert.Equal(t, order.Open, v.Status, "order status") + assert.Equal(t, 34.5, v.Price, "price") + assert.Equal(t, 10.00345345, v.Amount, "amount") + case 2: + assert.Equal(t, "OKB55A-UEMMN-YUXM2A", v.OrderID, "OrderID") + assert.Equal(t, order.Market, v.Type, "order type") + assert.Equal(t, order.Buy, v.Side, "order side") + assert.Equal(t, order.Pending, v.Status, "order status") + assert.Equal(t, 0.0, v.Price, "price") + assert.Equal(t, 0.0001, v.Amount, "amount") + assert.Equal(t, time.UnixMicro(1692851641361371), v.Date, "Date") + case 3: + assert.Equal(t, "OKB55A-UEMMN-YUXM2A", v.OrderID, "OrderID") + assert.Equal(t, order.Open, v.Status, "order status") + case 4: + assert.Equal(t, "OKB55A-UEMMN-YUXM2A", v.OrderID, "OrderID") + assert.Equal(t, order.UnknownStatus, v.Status, "order status") + assert.Equal(t, 26425.2, v.AverageExecutedPrice, "AverageExecutedPrice") + assert.Equal(t, 0.0001, v.ExecutedAmount, "ExecutedAmount") + assert.Equal(t, 0.0, v.RemainingAmount, "RemainingAmount") // Not in the message; Testing regression to bad derivation + assert.Equal(t, 0.00687, v.Fee, "Fee") + case 5: + assert.Equal(t, "OKB55A-UEMMN-YUXM2A", v.OrderID, "OrderID") + assert.Equal(t, order.Closed, v.Status, "order status") + assert.Equal(t, 0.0001, v.ExecutedAmount, "ExecutedAmount") + assert.Equal(t, 26425.2, v.AverageExecutedPrice, "AverageExecutedPrice") + assert.Equal(t, 0.00687, v.Fee, "Fee") + assert.Equal(t, time.UnixMicro(1692851641361447), v.LastUpdated, "LastUpdated") + case 6: + assert.Equal(t, "OGTT3Y-C6I3P-XRI6HR", v.OrderID, "OrderID") + assert.Equal(t, order.UnknownStatus, v.Status, "order status") + assert.Equal(t, 10.00345345, v.ExecutedAmount, "ExecutedAmount") + assert.Equal(t, 0.001, v.Fee, "Fee") + assert.Equal(t, 34.5, v.AverageExecutedPrice, "AverageExecutedPrice") + case 7: + assert.Equal(t, "OGTT3Y-C6I3P-XRI6HR", v.OrderID, "OrderID") + assert.Equal(t, order.Closed, v.Status, "order status") + assert.Equal(t, time.UnixMicro(1692675961789052), v.LastUpdated, "LastUpdated") + assert.Equal(t, 10.00345345, v.ExecutedAmount, "ExecutedAmount") + assert.Equal(t, 0.001, v.Fee, "Fee") + assert.Equal(t, 34.5, v.AverageExecutedPrice, "AverageExecutedPrice") + reading = false + } + default: + t.Errorf("Unexpected type in DataHandler: %T (%s)", v, v) + } + } + } + + assert.Equal(t, 7, seen, "number of DataHandler emissions") } func TestWsAddOrderJSON(t *testing.T) { diff --git a/exchanges/kraken/kraken_types.go b/exchanges/kraken/kraken_types.go index 2e2f7ecd9b1..2f833479ff2 100644 --- a/exchanges/kraken/kraken_types.go +++ b/exchanges/kraken/kraken_types.go @@ -600,6 +600,7 @@ type wsSubscription struct { type WsOpenOrder struct { UserReferenceID int64 `json:"userref"` ExpireTime float64 `json:"expiretm,string"` + LastUpdated float64 `json:"lastupdated,string"` OpenTime float64 `json:"opentm,string"` StartTime float64 `json:"starttm,string"` Fee float64 `json:"fee,string"` @@ -608,7 +609,7 @@ type WsOpenOrder struct { Volume float64 `json:"vol,string"` ExecutedVolume float64 `json:"vol_exec,string"` Cost float64 `json:"cost,string"` - Price float64 `json:"price,string"` + AveragePrice float64 `json:"avg_price,string"` Misc string `json:"misc"` OFlags string `json:"oflags"` RefID string `json:"refid"` diff --git a/exchanges/kraken/kraken_websocket.go b/exchanges/kraken/kraken_websocket.go index 8d2041f3381..a35d162b387 100644 --- a/exchanges/kraken/kraken_websocket.go +++ b/exchanges/kraken/kraken_websocket.go @@ -550,74 +550,83 @@ func (k *Kraken) wsProcessOpenOrders(ownOrders interface{}) error { return err } for key, val := range result { - var oStatus order.Status - oStatus, err = order.StringToOrderStatus(val.Status) - if err != nil { - k.Websocket.DataHandler <- order.ClassificationError{ - Exchange: k.Name, - OrderID: key, - Err: err, - } + d := &order.Detail{ + Exchange: k.Name, + OrderID: key, + AverageExecutedPrice: val.AveragePrice, + Amount: val.Volume, + LimitPriceUpper: val.LimitPrice, + ExecutedAmount: val.ExecutedVolume, + Fee: val.Fee, + Date: convert.TimeFromUnixTimestampDecimal(val.OpenTime).Truncate(time.Microsecond), + LastUpdated: convert.TimeFromUnixTimestampDecimal(val.LastUpdated).Truncate(time.Microsecond), } - if val.Description.Price > 0 { - oSide, err := order.StringToOrderSide(val.Description.Type) - if err != nil { + + if val.Status != "" { + if s, err := order.StringToOrderStatus(val.Status); err != nil { k.Websocket.DataHandler <- order.ClassificationError{ Exchange: k.Name, OrderID: key, Err: err, } + } else { + d.Status = s } + } + + if val.Description.Pair != "" { if strings.Contains(val.Description.Order, "sell") { - oSide = order.Sell + d.Side = order.Sell + } else { + if oSide, err := order.StringToOrderSide(val.Description.Type); err != nil { + k.Websocket.DataHandler <- order.ClassificationError{ + Exchange: k.Name, + OrderID: key, + Err: err, + } + } else { + d.Side = oSide + } } - oType, err := order.StringToOrderType(val.Description.OrderType) - if err != nil { + + if oType, err := order.StringToOrderType(val.Description.OrderType); err != nil { k.Websocket.DataHandler <- order.ClassificationError{ Exchange: k.Name, OrderID: key, Err: err, } + } else { + d.Type = oType } - p, err := currency.NewPairFromString(val.Description.Pair) - if err != nil { + if p, err := currency.NewPairFromString(val.Description.Pair); err != nil { k.Websocket.DataHandler <- order.ClassificationError{ Exchange: k.Name, OrderID: key, Err: err, } + } else { + d.Pair = p + if d.AssetType, err = k.GetPairAssetType(p); err != nil { + k.Websocket.DataHandler <- order.ClassificationError{ + Exchange: k.Name, + OrderID: key, + Err: err, + } + } } + } - var a asset.Item - a, err = k.GetPairAssetType(p) - if err != nil { - return err - } - k.Websocket.DataHandler <- &order.Detail{ - Leverage: val.Description.Leverage, - Price: val.Price, - Amount: val.Volume, - LimitPriceUpper: val.LimitPrice, - ExecutedAmount: val.ExecutedVolume, - RemainingAmount: val.Volume - val.ExecutedVolume, - Fee: val.Fee, - Exchange: k.Name, - OrderID: key, - Type: oType, - Side: oSide, - Status: oStatus, - AssetType: a, - Date: convert.TimeFromUnixTimestampDecimal(val.OpenTime), - Pair: p, - } - } else { - k.Websocket.DataHandler <- &order.Detail{ - Exchange: k.Name, - OrderID: key, - Status: oStatus, - } + if val.Description.Price > 0 { + d.Leverage = val.Description.Leverage + d.Price = val.Description.Price + } + + if val.Volume > 0 { + // Note: We don't seem to ever get both there values + d.RemainingAmount = val.Volume - val.ExecutedVolume } + k.Websocket.DataHandler <- d } } return nil diff --git a/exchanges/kraken/testdata/wsOpenTrades.json b/exchanges/kraken/testdata/wsOpenTrades.json new file mode 100644 index 00000000000..40a516eb7c2 --- /dev/null +++ b/exchanges/kraken/testdata/wsOpenTrades.json @@ -0,0 +1,6 @@ +[[{"OGTT3Y-C6I3P-XRI6HR":{"cost":"0.00000","descr":{"close":"","leverage":"0.1","order":"sell 10.00345345 XBT/USD @ limit 34.50000 with 0:1 leverage","ordertype":"limit","pair":"XBT/USD","price":"34.50000","price2":"0.00000","type":"sell"},"expiretm":"0.000000","fee":"0.00000","limitprice":"34.50000","misc":"","oflags":"fcib","opentm":"0.000000","avg_price":"0.00000","refid":"LIMIT-OPEN","starttm":"0.000000","status":"open","stopprice":"0.000000","userref":0,"vol":"10.00345345","vol_exec":"0.00000000"}},{"OKB55A-UEMMN-YUXM2A":{"avg_price":"0.00000","cost":"0.00000","descr":{"close":null,"leverage":null,"order":"buy 0.00010000 XBT/USDT @ market 0.00000","ordertype":"market","pair":"XBT/USDT","price":"0.00000","price2":"0.00000","type":"buy"},"expiretm":null,"fee":"0.00000","limitprice":"0.00000","misc":"","oflags":"fciq","opentm":"1692851641.361371","refid":null,"starttm":null,"status":"pending","stopprice":"0.00000","timeinforce":"GTC","userref":0,"vol":"0.00010000","vol_exec":"0.00000000"}}],"openOrders",{"sequence":1}] +[[{"OKB55A-UEMMN-YUXM2A":{"status":"open","userref":0}}],"openOrders",{"sequence":2}] +[[{"OKB55A-UEMMN-YUXM2A":{"vol_exec":"0.00010000","cost":"2.64252","fee":"0.00687","avg_price":"26425.20000","userref":0}}],"openOrders",{"sequence":3}] +[[{"OKB55A-UEMMN-YUXM2A":{"lastupdated":"1692851641.361447","status":"closed","vol_exec":"0.00010000","cost":"2.64252","fee":"0.00687","avg_price":"26425.20000","userref":0}}],"openOrders",{"sequence":4}] +[[{"OGTT3Y-C6I3P-XRI6HR":{"vol_exec":"10.00345345","cost":"345.119144","fee":"0.001","avg_price":"34.50000"}}],"openOrders",{"sequence":5}] +[[{"OGTT3Y-C6I3P-XRI6HR":{"status":"closed","vol_exec":"10.00345345","lastupdated":"1692675961.789052","cost":"345.119144","fee":"0.001","avg_price":"34.50000","userref":0}}],"openOrders",{"sequence":6}] diff --git a/go.mod b/go.mod index 60f3af129f9..6dbb71eee9a 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/pquerna/otp v1.4.0 github.com/shopspring/decimal v1.3.1 github.com/spf13/viper v1.16.0 + github.com/stretchr/testify v1.8.3 github.com/thrasher-corp/gct-ta v0.0.0-20200623072738-f2b55b7f9f41 github.com/thrasher-corp/goose v2.7.0-rc4.0.20191002032028-0f2c2a27abdb+incompatible github.com/thrasher-corp/sqlboiler v1.0.1-0.20191001234224-71e17f37a85e @@ -33,6 +34,7 @@ require ( require ( github.com/boombuler/barcode v1.0.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/friendsofgo/errors v0.9.2 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/golang/protobuf v1.5.3 // indirect @@ -40,6 +42,7 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect