Skip to content

Commit

Permalink
coinbase/kraken: Add IOC support to SubmitOrder wrappers (thrasher-co…
Browse files Browse the repository at this point in the history
…rp#1025)

* add IOC support to coinbase and kraken wrappers

* fix lint

* use explicit TimeInForce type

* fix kraken param

* fix fmt

* fix time_in_force
  • Loading branch information
geseq authored Sep 19, 2022
1 parent f843b7d commit e78287b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
4 changes: 2 additions & 2 deletions exchanges/coinbasepro/coinbasepro.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (c *CoinbasePro) GetHolds(ctx context.Context, accountID string) ([]Account
// timeInforce - [optional] GTC, GTT, IOC, or FOK (default is GTC)
// cancelAfter - [optional] min, hour, day * Requires time_in_force to be GTT
// postOnly - [optional] Post only flag Invalid when time_in_force is IOC or FOK
func (c *CoinbasePro) PlaceLimitOrder(ctx context.Context, clientRef string, price, amount float64, side, timeInforce, cancelAfter, productID, stp string, postOnly bool) (string, error) {
func (c *CoinbasePro) PlaceLimitOrder(ctx context.Context, clientRef string, price, amount float64, side string, timeInforce RequestParamsTimeForceType, cancelAfter, productID, stp string, postOnly bool) (string, error) {
resp := GeneralizedOrderResponse{}
req := make(map[string]interface{})
req["type"] = order.Limit.Lower()
Expand All @@ -347,7 +347,7 @@ func (c *CoinbasePro) PlaceLimitOrder(ctx context.Context, clientRef string, pri
req["cancel_after"] = cancelAfter
}
if timeInforce != "" {
req["time_in_foce"] = timeInforce
req["time_in_force"] = timeInforce
}
if clientRef != "" {
req["client_oid"] = clientRef
Expand Down
11 changes: 11 additions & 0 deletions exchanges/coinbasepro/coinbasepro_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,14 @@ type wsStatus struct {
} `json:"products"`
Type string `json:"type"`
}

// RequestParamsTimeForceType Time in force
type RequestParamsTimeForceType string

var (
// CoinbaseRequestParamsTimeGTC GTC
CoinbaseRequestParamsTimeGTC = RequestParamsTimeForceType("GTC")

// CoinbaseRequestParamsTimeIOC IOC
CoinbaseRequestParamsTimeIOC = RequestParamsTimeForceType("IOC")
)
6 changes: 5 additions & 1 deletion exchanges/coinbasepro/coinbasepro_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,16 @@ func (c *CoinbasePro) SubmitOrder(ctx context.Context, s *order.Submit) (*order.
fpair.String(),
"")
case order.Limit:
timeInForce := CoinbaseRequestParamsTimeGTC
if s.ImmediateOrCancel {
timeInForce = CoinbaseRequestParamsTimeIOC
}
orderID, err = c.PlaceLimitOrder(ctx,
"",
s.Price,
s.Amount,
s.Side.Lower(),
"",
timeInForce,
"",
fpair.String(),
"",
Expand Down
4 changes: 4 additions & 0 deletions exchanges/kraken/kraken.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,10 @@ func (k *Kraken) AddOrder(ctx context.Context, symbol currency.Pair, side, order
params.Set("validate", "true")
}

if args.TimeInForce != "" {
params.Set("timeinforce", string(args.TimeInForce))
}

if err := k.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, krakenOrderPlace, params, &response); err != nil {
return response.Result, err
}
Expand Down
49 changes: 31 additions & 18 deletions exchanges/kraken/kraken_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ type AddOrderOptions struct {
ClosePrice float64
ClosePrice2 float64
Validate bool
TimeInForce RequestParamsTimeForceType
}

// CancelOrderResponse type
Expand Down Expand Up @@ -654,24 +655,25 @@ type WsOpenOrderDescription struct {

// WsAddOrderRequest request type for ws adding order
type WsAddOrderRequest struct {
Event string `json:"event"`
Token string `json:"token"`
RequestID int64 `json:"reqid,omitempty"` // Optional, client originated ID reflected in response message.
OrderType string `json:"ordertype"`
OrderSide string `json:"type"`
Pair string `json:"pair"`
Price float64 `json:"price,string,omitempty"` // optional
Price2 float64 `json:"price2,string,omitempty"` // optional
Volume float64 `json:"volume,string,omitempty"`
Leverage float64 `json:"leverage,omitempty"` // optional
OFlags string `json:"oflags,omitempty"` // optional
StartTime string `json:"starttm,omitempty"` // optional
ExpireTime string `json:"expiretm,omitempty"` // optional
UserReferenceID string `json:"userref,omitempty"` // optional
Validate string `json:"validate,omitempty"` // optional
CloseOrderType string `json:"close[ordertype],omitempty"` // optional
ClosePrice float64 `json:"close[price],omitempty"` // optional
ClosePrice2 float64 `json:"close[price2],omitempty"` // optional
Event string `json:"event"`
Token string `json:"token"`
RequestID int64 `json:"reqid,omitempty"` // Optional, client originated ID reflected in response message.
OrderType string `json:"ordertype"`
OrderSide string `json:"type"`
Pair string `json:"pair"`
Price float64 `json:"price,string,omitempty"` // optional
Price2 float64 `json:"price2,string,omitempty"` // optional
Volume float64 `json:"volume,string,omitempty"`
Leverage float64 `json:"leverage,omitempty"` // optional
OFlags string `json:"oflags,omitempty"` // optional
StartTime string `json:"starttm,omitempty"` // optional
ExpireTime string `json:"expiretm,omitempty"` // optional
UserReferenceID string `json:"userref,omitempty"` // optional
Validate string `json:"validate,omitempty"` // optional
CloseOrderType string `json:"close[ordertype],omitempty"` // optional
ClosePrice float64 `json:"close[price],omitempty"` // optional
ClosePrice2 float64 `json:"close[price2],omitempty"` // optional
TimeInForce RequestParamsTimeForceType `json:"timeinforce,omitempty"` // optional
}

// WsAddOrderResponse response data for ws order
Expand Down Expand Up @@ -708,3 +710,14 @@ type OrderVars struct {
OrderType order.Type
Fee float64
}

// RequestParamsTimeForceType Time in force
type RequestParamsTimeForceType string

var (
// KrakenRequestParamsTimeGTC GTC
KrakenRequestParamsTimeGTC = RequestParamsTimeForceType("GTC")

// KrakenRequestParamsTimeIOC IOC
KrakenRequestParamsTimeIOC = RequestParamsTimeForceType("IOC")
)
19 changes: 13 additions & 6 deletions exchanges/kraken/kraken_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,19 @@ func (k *Kraken) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Submi
status := order.New
switch s.AssetType {
case asset.Spot:
timeInForce := KrakenRequestParamsTimeGTC
if s.ImmediateOrCancel {
timeInForce = KrakenRequestParamsTimeIOC
}
if k.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
s.Pair.Delimiter = "/" // required pair format: ISO 4217-A3
orderID, err = k.wsAddOrder(&WsAddOrderRequest{
OrderType: s.Type.Lower(),
OrderSide: s.Side.Lower(),
Pair: s.Pair.String(),
Price: s.Price,
Volume: s.Amount,
OrderType: s.Type.Lower(),
OrderSide: s.Side.Lower(),
Pair: s.Pair.String(),
Price: s.Price,
Volume: s.Amount,
TimeInForce: timeInForce,
})
if err != nil {
return nil, err
Expand All @@ -748,7 +753,9 @@ func (k *Kraken) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Submi
s.Price,
0,
0,
&AddOrderOptions{})
&AddOrderOptions{
TimeInForce: timeInForce,
})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e78287b

Please sign in to comment.