Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exchanges/order: Added TimeInForce type and values #1382

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5412e91
Added TimeInForce type and updated related files
samuael Oct 29, 2023
0bbd6fb
Linter issue fix and minor coinbasepro type update
samuael Oct 29, 2023
b786e75
Bitrex consts update
samuael Oct 29, 2023
afa9695
added unit test and minor changes in bittrex
samuael Oct 30, 2023
cb2619b
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Nov 5, 2023
da4b3cb
Unit tests update
samuael Nov 15, 2023
e41ab86
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Nov 15, 2023
c9b88ab
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Nov 23, 2023
e11beef
Fix minor linter issues
samuael Nov 23, 2023
9add848
Update TestStringToTimeInForce unit test
samuael Nov 27, 2023
aecc942
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Nov 27, 2023
3a7ac71
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Nov 29, 2023
44f1cbb
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Dec 6, 2023
964ed95
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Dec 22, 2023
05b4921
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Dec 27, 2023
2179454
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Dec 31, 2023
3043702
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 2, 2024
45b3596
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 16, 2024
318152b
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 24, 2024
cede5cb
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 29, 2024
3aa605a
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Feb 28, 2024
6dd27b9
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 6, 2024
6b3e010
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 13, 2024
d82cc0c
fix conflict with gateio timeInForce
samuael Mar 18, 2024
b453062
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 18, 2024
22d0f31
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Sep 25, 2024
c651a21
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 15, 2025
f63547d
Update order tests
samuael Jan 15, 2025
28cb5f7
Complete updating the order unit tests
samuael Jan 16, 2025
f708de2
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 20, 2025
2f09ca0
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Jan 31, 2025
9fe9504
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Feb 10, 2025
6828343
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Feb 17, 2025
41de4e8
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Feb 18, 2025
84445fc
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Feb 20, 2025
745977b
update kucoin and deribit wrapper to match the time in force change
samuael Feb 20, 2025
b05fe7f
fix time-in-force related test errors
samuael Feb 20, 2025
2085156
linter issue fix
samuael Feb 20, 2025
de494ac
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Feb 24, 2025
889c7cc
time in force constants, functions and unit tests update
samuael Feb 25, 2025
83b6521
shift tif policies to TimeInForce
samuael Feb 28, 2025
9bef751
Update time-in-force, related functions, and unit tests
samuael Mar 3, 2025
18ed556
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 3, 2025
0fbaad8
fix linter issue and time-in-force processing
samuael Mar 4, 2025
892ff5e
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 4, 2025
88584b8
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 7, 2025
0cf66a2
added a good till crossing tif value
samuael Mar 8, 2025
1679be7
order type fix and fix related tim-in-force entries
samuael Mar 9, 2025
3f752f2
update time-in-force unmarshaling and unit test
samuael Mar 10, 2025
8733ed4
Merge branch 'master' of http://github.com/thrasher-corp/gocryptotrad…
samuael Mar 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ func parseOrderType(orderType string) order.Type {
return order.Limit
case order.Market.String():
return order.Market
case order.ImmediateOrCancel.String():
return order.ImmediateOrCancel
case order.Stop.String():
return order.Stop
case order.TrailingStop.String():
Expand Down
42 changes: 21 additions & 21 deletions cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,30 +444,30 @@ func generateMethodArg(ctx context.Context, t *testing.T, argGenerator *MethodAr
input = reflect.ValueOf(req)
case argGenerator.MethodInputType.AssignableTo(orderSubmitParam):
input = reflect.ValueOf(&order.Submit{
Exchange: exchName,
Type: order.Limit,
Side: order.Buy,
Pair: argGenerator.AssetParams.Pair,
AssetType: argGenerator.AssetParams.Asset,
Price: 150,
Amount: 1,
ClientID: "1337",
ClientOrderID: "13371337",
ImmediateOrCancel: true,
Leverage: 1,
Exchange: exchName,
Type: order.Limit,
Side: order.Buy,
Pair: argGenerator.AssetParams.Pair,
AssetType: argGenerator.AssetParams.Asset,
Price: 150,
Amount: 1,
ClientID: "1337",
ClientOrderID: "13371337",
TimeInForce: order.ImmediateOrCancel,
Leverage: 1,
})
case argGenerator.MethodInputType.AssignableTo(orderModifyParam):
input = reflect.ValueOf(&order.Modify{
Exchange: exchName,
Type: order.Limit,
Side: order.Buy,
Pair: argGenerator.AssetParams.Pair,
AssetType: argGenerator.AssetParams.Asset,
Price: 150,
Amount: 1,
ClientOrderID: "13371337",
OrderID: "1337",
ImmediateOrCancel: true,
Exchange: exchName,
Type: order.Limit,
Side: order.Buy,
Pair: argGenerator.AssetParams.Pair,
AssetType: argGenerator.AssetParams.Asset,
Price: 150,
Amount: 1,
ClientOrderID: "13371337",
OrderID: "1337",
TimeInForce: order.ImmediateOrCancel,
})
case argGenerator.MethodInputType.AssignableTo(orderCancelParam):
input = reflect.ValueOf(&order.Cancel{
Expand Down
7 changes: 3 additions & 4 deletions engine/order_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,9 @@ func (m *OrderManager) Modify(ctx context.Context, mod *order.Modify) (*order.Mo

// Populate additional Modify fields as some of them are required by various
// exchange implementations.
mod.Pair = det.Pair // Used by Bithumb.
mod.Side = det.Side // Used by Bithumb.
mod.PostOnly = det.PostOnly // Used by Poloniex.
mod.ImmediateOrCancel = det.ImmediateOrCancel // Used by Poloniex.
mod.Pair = det.Pair // Used by Bithumb.
mod.Side = det.Side // Used by Bithumb.
mod.TimeInForce = det.TimeInForce // PostOnly used by Poloniex.

// Following is just a precaution to not modify orders by mistake if exchange
// implementations do not check fields of the Modify struct for zero values.
Expand Down
2 changes: 1 addition & 1 deletion exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (b *Binance) newOrder(ctx context.Context, api string, o *NewOrderRequest,
params.Set("price", strconv.FormatFloat(o.Price, 'f', -1, 64))
}
if o.TimeInForce != "" {
params.Set("timeInForce", string(o.TimeInForce))
params.Set("timeInForce", o.TimeInForce)
}

if o.NewClientOrderID != "" {
Expand Down
4 changes: 2 additions & 2 deletions exchanges/binance/binance_cfutures.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,8 @@ func (b *Binance) FuturesNewOrder(ctx context.Context, x *FuturesNewOrderRequest
params.Set("positionSide", x.PositionSide)
}
params.Set("type", x.OrderType)
if string(x.TimeInForce) != "" {
params.Set("timeInForce", string(x.TimeInForce))
if x.TimeInForce != "" {
params.Set("timeInForce", x.TimeInForce)
}
if x.ReduceOnly {
params.Set("reduceOnly", "true")
Expand Down
4 changes: 2 additions & 2 deletions exchanges/binance/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ func TestFuturesNewOrder(t *testing.T) {
Symbol: currency.NewPairWithDelimiter("BTCUSD", "PERP", "_"),
Side: "BUY",
OrderType: "LIMIT",
TimeInForce: BinanceRequestParamsTimeGTC,
TimeInForce: order.GoodTillCancel.String(),
Quantity: 1,
Price: 1,
},
Expand Down Expand Up @@ -1420,7 +1420,7 @@ func TestNewOrderTest(t *testing.T) {
TradeType: BinanceRequestParamsOrderLimit,
Price: 0.0025,
Quantity: 100000,
TimeInForce: BinanceRequestParamsTimeGTC,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Binance request params can be removed?

TimeInForce: order.GoodTillCancel.String(),
}

err := b.NewOrderTest(context.Background(), req)
Expand Down
16 changes: 1 addition & 15 deletions exchanges/binance/binance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ type NewOrderRequest struct {
TradeType RequestParamsOrderType
// TimeInForce specifies how long the order remains in effect.
// Examples are (Good Till Cancel (GTC), Immediate or Cancel (IOC) and Fill Or Kill (FOK))
TimeInForce RequestParamsTimeForceType
TimeInForce string
// Quantity is the total base qty spent or received in an order.
Quantity float64
// QuoteOrderQty is the total quote qty spent or received in a MARKET order.
Expand Down Expand Up @@ -486,20 +486,6 @@ type MarginAccountAsset struct {
NetAsset float64 `json:"netAsset,string"`
}

// RequestParamsTimeForceType Time in force
type RequestParamsTimeForceType string

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

// BinanceRequestParamsTimeIOC IOC
BinanceRequestParamsTimeIOC = RequestParamsTimeForceType("IOC")

// BinanceRequestParamsTimeFOK FOK
BinanceRequestParamsTimeFOK = RequestParamsTimeForceType("FOK")
)

// RequestParamsOrderType trade order type
type RequestParamsOrderType string

Expand Down
14 changes: 5 additions & 9 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,15 @@ func (b *Binance) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Subm
} else {
sideType = order.Sell.String()
}
timeInForce := BinanceRequestParamsTimeGTC
timeInForce := order.GoodTillCancel.String()
var requestParamsOrderType RequestParamsOrderType
switch s.Type {
case order.Market:
timeInForce = ""
requestParamsOrderType = BinanceRequestParamsOrderMarket
case order.Limit:
if s.ImmediateOrCancel {
timeInForce = BinanceRequestParamsTimeIOC
if s.TimeInForce.Is(order.ImmediateOrCancel) {
timeInForce = order.ImmediateOrCancel.String()
}
requestParamsOrderType = BinanceRequestParamsOrderLimit
default:
Expand Down Expand Up @@ -936,15 +936,11 @@ func (b *Binance) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Subm
return nil, errors.New("invalid side")
}

var (
oType string
timeInForce RequestParamsTimeForceType
)

var oType, timeInForce string
switch s.Type {
case order.Limit:
oType = cfuturesLimit
timeInForce = BinanceRequestParamsTimeGTC
timeInForce = order.GoodTillTime.String()
case order.Market:
oType = cfuturesMarket
case order.Stop:
Expand Down
2 changes: 1 addition & 1 deletion exchanges/binance/cfutures_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ type FuturesNewOrderRequest struct {
Side string
PositionSide string
OrderType string
TimeInForce RequestParamsTimeForceType
TimeInForce string
NewClientOrderID string
ClosePosition string
WorkingType string
Expand Down
2 changes: 1 addition & 1 deletion exchanges/binanceus/binanceus.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ func (bi *Binanceus) newOrder(ctx context.Context, api string, o *NewOrderReques
params.Set("price", strconv.FormatFloat(o.Price, 'f', -1, 64))
}
if o.TimeInForce != "" {
params.Set("timeInForce", string(o.TimeInForce))
params.Set("timeInForce", o.TimeInForce)
}
if o.NewClientOrderID != "" {
params.Set("newClientOrderId", o.NewClientOrderID)
Expand Down
4 changes: 2 additions & 2 deletions exchanges/binanceus/binanceus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ func TestNewOrderTest(t *testing.T) {
TradeType: BinanceRequestParamsOrderLimit,
Price: 0.0025,
Quantity: 100000,
TimeInForce: BinanceRequestParamsTimeGTC,
TimeInForce: order.GoodTillCancel.String(),
}
_, err := bi.NewOrderTest(context.Background(), req)
if err != nil {
Expand Down Expand Up @@ -809,7 +809,7 @@ func TestNewOrder(t *testing.T) {
TradeType: BinanceRequestParamsOrderLimit,
Price: 0.0025,
Quantity: 100000,
TimeInForce: BinanceRequestParamsTimeGTC,
TimeInForce: order.GoodTillCancel.String(),
}
if _, err := bi.NewOrder(context.Background(), req); err != nil && !strings.Contains(err.Error(), "Account has insufficient balance for requested action") {
t.Error("Binanceus NewOrder() error", err)
Expand Down
16 changes: 1 addition & 15 deletions exchanges/binanceus/binanceus_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,26 +404,12 @@ type OrderRateLimit struct {
// RequestParamsOrderType trade order type
type RequestParamsOrderType string

// RequestParamsTimeForceType Time in force
type RequestParamsTimeForceType string

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

// BinanceRequestParamsTimeIOC IOC
BinanceRequestParamsTimeIOC = RequestParamsTimeForceType("IOC")

// BinanceRequestParamsTimeFOK FOK
BinanceRequestParamsTimeFOK = RequestParamsTimeForceType("FOK")
)

// NewOrderRequest request type
type NewOrderRequest struct {
Symbol currency.Pair
Side string
TradeType RequestParamsOrderType
TimeInForce RequestParamsTimeForceType
TimeInForce string
Quantity float64
QuoteOrderQty float64
Price float64
Expand Down
4 changes: 2 additions & 2 deletions exchanges/binanceus/binanceus_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (bi *Binanceus) GetHistoricTrades(ctx context.Context, p currency.Pair, ass
// SubmitOrder submits a new order
func (bi *Binanceus) SubmitOrder(ctx context.Context, s *order.Submit) (*order.SubmitResponse, error) {
var submitOrderResponse order.SubmitResponse
var timeInForce RequestParamsTimeForceType
var timeInForce string
var sideType string
err := s.Validate(bi.GetTradingRequirements())
if err != nil {
Expand All @@ -508,7 +508,7 @@ func (bi *Binanceus) SubmitOrder(ctx context.Context, s *order.Submit) (*order.S
case order.Market:
requestParamOrderType = BinanceRequestParamsOrderMarket
case order.Limit:
timeInForce = BinanceRequestParamsTimeGTC
timeInForce = order.GoodTillCancel.String()
requestParamOrderType = BinanceRequestParamsOrderLimit
default:
return nil, fmt.Errorf("%w %v", order.ErrUnsupportedOrderType, s.Type)
Expand Down
11 changes: 2 additions & 9 deletions exchanges/btcmarkets/btcmarkets.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ const (
askSide = "Ask"
bidSide = "Bid"

// time in force
immediateOrCancel = "IOC"
fillOrKill = "FOK"

subscribe = "subscribe"
fundChange = "fundChange"
orderChange = "orderChange"
Expand Down Expand Up @@ -377,11 +373,8 @@ func (b *BTCMarkets) formatOrderSide(o order.Side) (string, error) {

// getTimeInForce returns a string depending on the options in order.Submit
func (b *BTCMarkets) getTimeInForce(s *order.Submit) string {
if s.ImmediateOrCancel {
return immediateOrCancel
}
if s.FillOrKill {
return fillOrKill
if s.TimeInForce.Is(order.ImmediateOrCancel) || s.TimeInForce.Is(order.FillOrKill) {
return s.TimeInForce.String()
}
return "" // GTC (good till cancelled, default value)
}
Expand Down
60 changes: 30 additions & 30 deletions exchanges/btcmarkets/btcmarkets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,27 @@ func TestGetTradeByID(t *testing.T) {
func TestSubmitOrder(t *testing.T) {
t.Parallel()
_, err := b.SubmitOrder(context.Background(), &order.Submit{
Exchange: b.Name,
Price: 100,
Amount: 1,
Type: order.TrailingStop,
AssetType: asset.Spot,
Side: order.Bid,
Pair: currency.NewPair(currency.BTC, currency.AUD),
PostOnly: true,
Exchange: b.Name,
Price: 100,
Amount: 1,
Type: order.TrailingStop,
AssetType: asset.Spot,
Side: order.Bid,
Pair: currency.NewPair(currency.BTC, currency.AUD),
TimeInForce: order.PostOnly,
})
if !errors.Is(err, order.ErrTypeIsInvalid) {
t.Fatalf("received: '%v' but expected: '%v'", err, order.ErrTypeIsInvalid)
}
_, err = b.SubmitOrder(context.Background(), &order.Submit{
Exchange: b.Name,
Price: 100,
Amount: 1,
Type: order.Limit,
AssetType: asset.Spot,
Side: order.AnySide,
Pair: currency.NewPair(currency.BTC, currency.AUD),
PostOnly: true,
Exchange: b.Name,
Price: 100,
Amount: 1,
Type: order.Limit,
AssetType: asset.Spot,
Side: order.AnySide,
Pair: currency.NewPair(currency.BTC, currency.AUD),
TimeInForce: order.PostOnly,
})
if !errors.Is(err, order.ErrSideIsInvalid) {
t.Fatalf("received: '%v' but expected: '%v'", err, order.ErrSideIsInvalid)
Expand All @@ -224,14 +224,14 @@ func TestSubmitOrder(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)

_, err = b.SubmitOrder(context.Background(), &order.Submit{
Exchange: b.Name,
Price: 100,
Amount: 1,
Type: order.Limit,
AssetType: asset.Spot,
Side: order.Bid,
Pair: currency.NewPair(currency.BTC, currency.AUD),
PostOnly: true,
Exchange: b.Name,
Price: 100,
Amount: 1,
Type: order.Limit,
AssetType: asset.Spot,
Side: order.Bid,
Pair: currency.NewPair(currency.BTC, currency.AUD),
TimeInForce: order.PostOnly,
})
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -966,14 +966,14 @@ func TestGetTimeInForce(t *testing.T) {
t.Fatal("unexpected value")
}

f = b.getTimeInForce(&order.Submit{ImmediateOrCancel: true})
if f != immediateOrCancel {
t.Fatalf("received: '%v' but expected: '%v'", f, immediateOrCancel)
f = b.getTimeInForce(&order.Submit{TimeInForce: order.ImmediateOrCancel})
if f != "IOC" {
t.Fatalf("received: '%v' but expected: '%v'", f, "IOC")
}

f = b.getTimeInForce(&order.Submit{FillOrKill: true})
if f != fillOrKill {
t.Fatalf("received: '%v' but expected: '%v'", f, fillOrKill)
f = b.getTimeInForce(&order.Submit{TimeInForce: order.FillOrKill})
if f != "FOK" {
t.Fatalf("received: '%v' but expected: '%v'", f, "FOK")
}
}

Expand Down
4 changes: 2 additions & 2 deletions exchanges/btcmarkets/btcmarkets_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (b *BTCMarkets) SubmitOrder(ctx context.Context, s *order.Submit) (*order.S
b.getTimeInForce(s),
"",
s.ClientID,
s.PostOnly)
s.TimeInForce.Is(order.PostOnly))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -635,7 +635,7 @@ func (b *BTCMarkets) GetOrderInfo(ctx context.Context, orderID string, _ currenc
case stop:
resp.Type = order.Stop
case takeProfit:
resp.Type = order.ImmediateOrCancel
resp.Type = order.TakeProfit
default:
resp.Type = order.UnknownType
}
Expand Down
Loading
Loading