Files
gocryptotrader/exchanges/order/timeinforce.go
cranktakular fd9aaf00a2 Coinbase: Update exchange implementation (#1480)
* Slight enhance of Coinbase tests

Continual enhance of Coinbase tests

The revamp continues

Oh jeez the Orderbook part's unfinished don't look

Coinbase revamp, Orderbook still unfinished

* Coinbase revamp; CreateReport is still WIP

* More coinbase improvements; onto sandbox testing

* Coinbase revamp continues

* Coinbase revamp continues

* Coinbasepro revamp is ceaseless

* Coinbase revamp, starting on advanced trade API

* Coinbase Advanced Trade Starts in Ernest

V3 done, onto V2

Coinbase revamp nears completion

Coinbase revamp nears completion

Test commit should fail

Coinbase revamp nears completion

* Coinbase revamp stage wrapper

* Coinbase wrapper coherence continues

* Coinbase wrapper continues writhing

* Coinbase wrapper & codebase cleanup

* Coinbase updates & wrap progress

* More Coinbase wrapper progress

* Wrapper is wrapped, kinda

* Test & type checking

* Coinbase REST revamp finished

* Post-merge fix

* WS revamp begins

* WS Main Revamp Done?

* CB websocket tidying up

* Coinbase WS wrapperupperer

* Coinbase revamp done??

* Linter progress

* Continued lint cleanup

* Further lint cleanup

* Increased lint coverage

* Does this fix all sloppy reassigns & shadowing?

* Undoing retry policy change

* Documentation regeneration

* Coinbase code improvements

* Providing warning about known issue

* Updating an error to new format

* Making gocritic happy

* Review adherence

* Endpoints moved to V3 & nil pointer fixes

* Removing seemingly superfluous constant

* Glorious improvements

* Removing unused error

* Partial public endpoint addition

* Slight improvements

* Wrapper improvements; still a few errors left in other packages

* A lil Coinbase progress

* Json cleaning

* Lint appeasement

* Config repair

* Config fix (real)

* Little fix

* New public endpoint incorporation

* Additional fixes

* Improvements & Appeasements

* LineSaver

* Additional fixes

* Another fix

* Fixing picked nits

* Quick fixies

* Lil fixes

* Subscriptions: Add List.Enabled

* CoinbasePro: Add subscription templating

* fixup! CoinbasePro: Add subscription templating

* fixup! CoinbasePro: Add subscription templating

* Comment fix

* Subsequent fixes

* Issues hopefully fixed

* Lint fix

* Glorious fixes

* Json formatting

* ShazNits

* (L/N)i(n/)t

* Adding a test

* Tiny test improvement

* Template patch testing

* Fixes

* Further shaznits

* Lint nit

* JWT move and other fixes

* Small nits

* Shaznit, singular

* Post-merge fix

* Post-merge fixes

* Typo fix

* Some glorious nits

* Required changes

* Stop going

* Alias attempt

* Alias fix & test cleanup

* Test fix

* GetDepositAddress logic improvement

* Status update: Fixed

* Lint fix

* Happy birthday to PR 1480

* Cleanups

* Necessary nit corrections

* Fixing sillybug

* As per request

* Programming progress

* Order fixes

* Further fixies

* Test fix

* Pre-merge fixes

* More shaznits

* Context

* Sonic error handling

* Import fix

* Better Sonic error handling

* Perfect Sonic error handling?

* F purge

* Coinbase improvements

* API Update Conformity

* Coinbase continuation

* Coinbase order improvements

* Coinbase order improvements

* CreateOrderConfig improvements

* Managing API updates

* Coinbase API update progression

* jwt rename

* Comment link fix

* Coinbase v2 cleanup

* Post-merge fixes

* Review fixes

* GK's suggestions

* Linter fix

* Minor gbjk fixes

* Nit fixes

* Merge fix

* Lint fixes

* Coinbase rename stage 1

* Coinbase rename stage 2

* Coinbase rename stage 3

* Coinbase rename stage 4

* Coinbase rename final fix

* Coinbase: PoC on converting to request structs

* Applying requested changes

* Many review fixes, handled

* Thrashed by nits

* More minor modifications

* The last nit!?

---------

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
2025-09-16 13:37:00 +10:00

148 lines
4.0 KiB
Go

package order
import (
"errors"
"fmt"
"strings"
)
// var error definitions
var (
ErrInvalidTimeInForce = errors.New("invalid time in force value provided")
ErrUnsupportedTimeInForce = errors.New("unsupported time in force value")
)
// TimeInForce enforces a standard for time-in-force values across the code base.
type TimeInForce uint16
// TimeInForce types
const (
UnknownTIF TimeInForce = 0
GoodTillCancel TimeInForce = 1 << iota
GoodTillDay
GoodTillTime
GoodTillCrossing
FillOrKill
ImmediateOrCancel
PostOnly
StopOrReduce
supportedTimeInForceFlag = GoodTillCancel | GoodTillDay | GoodTillTime | GoodTillCrossing | FillOrKill | ImmediateOrCancel | PostOnly | StopOrReduce
)
// time-in-force string representations
const (
gtcStr = "GTC"
gtdStr = "GTD"
gttStr = "GTT"
gtxStr = "GTX"
fokStr = "FOK"
iocStr = "IOC"
postonlyStr = "POSTONLY"
sorStr = "SOR"
)
// Is checks to see if the enum contains the flag
func (t TimeInForce) Is(in TimeInForce) bool {
return in != 0 && t&in == in
}
// StringToTimeInForce converts time in force string value to TimeInForce instance.
func StringToTimeInForce(timeInForce string) (TimeInForce, error) {
var result TimeInForce
timeInForce = strings.ToUpper(timeInForce)
switch timeInForce {
case "IMMEDIATEORCANCEL", "IMMEDIATE_OR_CANCEL", iocStr:
result = ImmediateOrCancel
case "GOODTILLCANCEL", "GOODTILCANCEL", "GOOD_TIL_CANCELLED", "GOOD_TILL_CANCELLED", "GOOD_TILL_CANCELED", gtcStr:
result = GoodTillCancel
case "GOODTILLDAY", "GOOD_TIL_DAY", "GOOD_TILL_DAY", gtdStr:
result = GoodTillDay
case "GOODTILLTIME", "GOOD_TIL_TIME", gttStr:
result = GoodTillTime
case "GOODTILLCROSSING", "GOOD_TIL_CROSSING", "GOOD TIL CROSSING", "GOOD_TILL_CROSSING", gtxStr:
result = GoodTillCrossing
case "FILLORKILL", "FILL_OR_KILL", fokStr:
result = FillOrKill
case "POC", "POST_ONLY", "PENDINGORCANCEL", postonlyStr:
result = PostOnly
case "STOPORREDUCE", "STOP_OR_REDUCE", sorStr:
result = StopOrReduce
}
if result == UnknownTIF && timeInForce != "" {
return UnknownTIF, fmt.Errorf("%w: tif=%s", ErrInvalidTimeInForce, timeInForce)
}
return result, nil
}
// IsValid returns whether or not the supplied time in force value is valid or
// not
func (t TimeInForce) IsValid() bool {
// Neither ImmediateOrCancel nor FillOrKill can coexist with anything else
// If either bit is set then it must be the only bit set
isIOCorFOK := t&(ImmediateOrCancel|FillOrKill) != 0
hasTwoBitsSet := t&(t-1) != 0
if isIOCorFOK && hasTwoBitsSet {
return false
}
return t == UnknownTIF || supportedTimeInForceFlag&t == t
}
// String implements the stringer interface.
func (t TimeInForce) String() string {
if t == UnknownTIF {
return ""
}
var tifStrings []string
if t.Is(ImmediateOrCancel) {
tifStrings = append(tifStrings, iocStr)
}
if t.Is(GoodTillCancel) {
tifStrings = append(tifStrings, gtcStr)
}
if t.Is(GoodTillDay) {
tifStrings = append(tifStrings, gtdStr)
}
if t.Is(GoodTillTime) {
tifStrings = append(tifStrings, gttStr)
}
if t.Is(GoodTillCrossing) {
tifStrings = append(tifStrings, gtxStr)
}
if t.Is(FillOrKill) {
tifStrings = append(tifStrings, fokStr)
}
if t.Is(PostOnly) {
tifStrings = append(tifStrings, postonlyStr)
}
if t.Is(StopOrReduce) {
tifStrings = append(tifStrings, sorStr)
}
if len(tifStrings) == 0 {
return "UNKNOWN"
}
return strings.Join(tifStrings, ",")
}
// Lower returns a lower case string representation of time-in-force
func (t TimeInForce) Lower() string {
return strings.ToLower(t.String())
}
// UnmarshalJSON deserializes a string data into TimeInForce instance.
func (t *TimeInForce) UnmarshalJSON(data []byte) error {
for val := range strings.SplitSeq(strings.Trim(string(data), `"`), ",") {
tif, err := StringToTimeInForce(val)
if err != nil {
return err
}
*t |= tif
}
return nil
}
// MarshalJSON returns the JSON-encoded order time-in-force value
func (t TimeInForce) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.String() + `"`), nil
}