Files
gocryptotrader/exchanges/okx/helpers.go
Samuael A. a8a3bc4ee2 exchanges: Order types update (#1850)
* Added TimeInForce type and updated related files

* Linter issue fix and minor coinbasepro type update

* Bitrex consts update

* added unit test and minor changes in bittrex

* Unit tests update

* Fix minor linter issues

* Update TestStringToTimeInForce unit test

* fix conflict with gateio timeInForce

* Update order tests

* Complete updating the order unit tests

* update kucoin and deribit wrapper to match the time in force change

* fix time-in-force related test errors

* linter issue fix

* time in force constants, functions and unit tests update

* shift tif policies to TimeInForce

* Update time-in-force, related functions, and unit tests

* fix linter issue and time-in-force processing

* added a good till crossing tif value

* order type fix and fix related tim-in-force entries

* update time-in-force unmarshaling and unit test

* update order type to support time-in-force and hybrid order type representation

* added tests for type and time-in-force check from order type

* fix time-in-force error in gateio

* fix minor unit test issues

* linter issue fix

* update based on review comments

* add unit test and fix missing issues

* minor fix and added benchmark unit test

* change GTT to GTC for limit

* fix linter issue

* linter issues fix

* update order types declaration and handling by endpoints

* added time-in-force value to place order param

* fix minor issues based on review comment and move tif code to separate files

* update on exchanges linked to time-in-force

* resolve missing review comments

* minor linter issues fix

* added time-in-force handler and update timeInForce parametered endpoint

* minor fixes based on review

* nits fix

* update based on review

* linter fix

* rm getTimeInForce func and minor change to time-in-force

* minor change

* update based on review comments

* wrappers and time-in-force calling approach

* minor change

* update gateio string to timeInForce conversion and unit test

* fix types to string conversion

* fix build errors

* update on order types handling and unit tests

* fix linter issue

* order type unit tests update

* order types string as constant replacement and unit tests update

* update order type-string functions unit test
2025-06-12 15:39:17 +10:00

183 lines
5.0 KiB
Go

package okx
import (
"fmt"
"strings"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
// orderTypeFromString returns the order Type and TimeInForce for okx order type strings
func orderTypeFromString(orderType string) (order.Type, order.TimeInForce, error) {
orderType = strings.ToLower(orderType)
switch orderType {
case orderMarket:
return order.Market, order.UnknownTIF, nil
case orderLimit:
return order.Limit, order.UnknownTIF, nil
case orderPostOnly:
return order.Limit, order.PostOnly, nil
case orderFOK:
return order.Limit, order.FillOrKill, nil
case orderIOC:
return order.Limit, order.ImmediateOrCancel, nil
case orderOptimalLimitIOC:
return order.OptimalLimit, order.ImmediateOrCancel, nil
case orderMarketMakerProtection:
return order.MarketMakerProtection, order.UnknownTIF, nil
case orderMarketMakerProtectionAndPostOnly:
return order.MarketMakerProtection, order.PostOnly, nil
case orderTWAP:
return order.TWAP, order.UnknownTIF, nil
case orderMoveOrderStop:
return order.TrailingStop, order.UnknownTIF, nil
case orderChase:
return order.Chase, order.UnknownTIF, nil
default:
return order.UnknownType, order.UnknownTIF, fmt.Errorf("%w %q", order.ErrTypeIsInvalid, orderType)
}
}
// orderTypeString returns a string representation of order.Type instance
func orderTypeString(orderType order.Type, tif order.TimeInForce) (string, error) {
switch orderType {
case order.MarketMakerProtection:
if tif == order.PostOnly {
return orderMarketMakerProtectionAndPostOnly, nil
}
return orderMarketMakerProtection, nil
case order.OptimalLimit:
return orderOptimalLimitIOC, nil
case order.Limit:
if tif == order.PostOnly {
return orderPostOnly, nil
}
return orderLimit, nil
case order.Market:
switch tif {
case order.FillOrKill:
return orderFOK, nil
case order.ImmediateOrCancel:
return orderIOC, nil
}
return orderMarket, nil
case order.Trigger,
order.Chase,
order.TWAP,
order.OCO:
return orderType.Lower(), nil
case order.ConditionalStop:
return orderConditional, nil
case order.TrailingStop:
return orderMoveOrderStop, nil
default:
switch tif {
case order.PostOnly:
return orderPostOnly, nil
case order.FillOrKill:
return orderFOK, nil
case order.ImmediateOrCancel:
return orderIOC, nil
}
return "", fmt.Errorf("%w: %q", order.ErrUnsupportedOrderType, orderType)
}
}
// getAssetsFromInstrumentID parses an instrument ID and returns a list of assets types
// that the instrument is associated with
func (ok *Okx) getAssetsFromInstrumentID(instrumentID string) ([]asset.Item, error) {
if instrumentID == "" {
return nil, errMissingInstrumentID
}
pf, err := ok.CurrencyPairs.GetFormat(asset.Spot, true)
if err != nil {
return nil, err
}
splitSymbol := strings.Split(instrumentID, pf.Delimiter)
if len(splitSymbol) <= 1 {
return nil, fmt.Errorf("%w %v", currency.ErrCurrencyNotSupported, instrumentID)
}
pair, err := currency.NewPairDelimiter(instrumentID, pf.Delimiter)
if err != nil {
return nil, fmt.Errorf("%w: %q", err, instrumentID)
}
switch {
case len(splitSymbol) == 2:
resp := make([]asset.Item, 0, 2)
enabled, err := ok.IsPairEnabled(pair, asset.Spot)
if err != nil {
return nil, err
}
if enabled {
resp = append(resp, asset.Spot)
}
enabled, err = ok.IsPairEnabled(pair, asset.Margin)
if err != nil {
return nil, err
}
if enabled {
resp = append(resp, asset.Margin)
}
if len(resp) > 0 {
return resp, nil
}
case len(splitSymbol) > 2:
var aType asset.Item
switch strings.ToLower(splitSymbol[len(splitSymbol)-1]) {
case "swap":
aType = asset.PerpetualSwap
case "c", "p":
aType = asset.Options
default:
aType = asset.Futures
}
enabled, err := ok.IsPairEnabled(pair, aType)
if err != nil {
return nil, err
} else if enabled {
return []asset.Item{aType}, nil
}
}
return nil, fmt.Errorf("%w: no asset enabled with instrument ID `%v`", asset.ErrNotEnabled, instrumentID)
}
// assetTypeFromInstrumentType returns an asset Item instance given and Instrument Type string
func assetTypeFromInstrumentType(instrumentType string) (asset.Item, error) {
switch strings.ToUpper(instrumentType) {
case instTypeSwap, instTypeContract:
return asset.PerpetualSwap, nil
case instTypeSpot:
return asset.Spot, nil
case instTypeMargin:
return asset.Margin, nil
case instTypeFutures:
return asset.Futures, nil
case instTypeOption:
return asset.Options, nil
case "":
return asset.Empty, nil
default:
return asset.Empty, asset.ErrNotSupported
}
}
// assetTypeString returns a string representation of asset type
func assetTypeString(assetType asset.Item) (string, error) {
switch assetType {
case asset.Spot:
return instTypeSpot, nil
case asset.Margin:
return instTypeMargin, nil
case asset.Futures:
return instTypeFutures, nil
case asset.Options:
return instTypeOption, nil
case asset.PerpetualSwap:
return instTypeSwap, nil
default:
return "", asset.ErrNotSupported
}
}