Skip to content

Commit

Permalink
update time-in-force values and order type conversion with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuael committed Mar 10, 2025
1 parent 517732f commit c91d208
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 169 deletions.
11 changes: 0 additions & 11 deletions exchanges/poloniex/poloniex.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,17 +750,6 @@ func (p *Poloniex) CreateSmartOrder(ctx context.Context, arg *SmartOrderRequestP
return resp, p.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, authNonResourceIntensiveEPL, http.MethodPost, "/smartorders", nil, arg, &resp)
}

func orderTypeString(oType order.Type) string {
switch oType {
case order.StopLimit:
return "STOP_LIMIT"
case order.AnyType, order.UnknownType:
return ""
default:
return oType.String()
}
}

// CancelReplaceSmartOrder cancel an existing untriggered smart order and place a new smart order on the same symbol with details from existing smart order unless amended by new parameters.
// The replacement smart order can amend price, stopPrice, quantity, amount, type, and timeInForce fields. Specify the existing smart order id in the path;
// if id is a clientOrderId, prefix with cid: e.g. cid:myId-1.
Expand Down
75 changes: 38 additions & 37 deletions exchanges/poloniex/poloniex_futures_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package poloniex

import (
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/types"
)

Expand Down Expand Up @@ -113,43 +114,43 @@ type FuturesTradeFill struct {

// FuturesV3Order represents a futures v3 order detail
type FuturesV3Order struct {
OrderID string `json:"ordId"`
AveragePrice types.Number `json:"avgPx"`
CreationTime types.Time `json:"cTime"`
ClientOrderID string `json:"clOrdId"`
DeductAmount types.Number `json:"deductAmt"`
ExecutedAmount types.Number `json:"execAmt"`
DeductCurrency string `json:"deductCcy"`
ExecQuantity types.Number `json:"execQty"`
FeeAmount types.Number `json:"feeAmt"`
FeeCurrency string `json:"feeCcy"`
PositionSide string `json:"posSide"`
Leverage types.Number `json:"lever"`
MarginMode string `json:"mgnMode"`
Price types.Number `json:"px"`
ReduceOnly bool `json:"reduceOnly"`
StopLossPrice types.Number `json:"slPx"`
Side string `json:"side"`
StopLossTriggerPrice string `json:"slTrgPx"`
StopLossTriggerPriceType string `json:"slTrgPxType"`
Source string `json:"source"`
State string `json:"state"`
SelfTradePreventionMode string `json:"stpMode"`
Symbol string `json:"symbol"`
Size types.Number `json:"sz"`
TimeInForce string `json:"timeInForce"`
TakeProfitPrice types.Number `json:"tpPx"`
TakeProfitTriggerPrice types.Number `json:"tpTrgPx"`
TakeProfitTriggerPriceType string `json:"tpTrgPxType"`
Type string `json:"type"`
UpdateTime types.Time `json:"uTime"`
FeeRate types.Number `json:"feeRate"`
ID string `json:"id"`
OrderType string `json:"ordType"`
Quantity types.Number `json:"qty"`
Role string `json:"role"`
TradeID string `json:"trdId"`
CancelReason string `json:"cancelReason"`
OrderID string `json:"ordId"`
AveragePrice types.Number `json:"avgPx"`
CreationTime types.Time `json:"cTime"`
ClientOrderID string `json:"clOrdId"`
DeductAmount types.Number `json:"deductAmt"`
ExecutedAmount types.Number `json:"execAmt"`
DeductCurrency string `json:"deductCcy"`
ExecQuantity types.Number `json:"execQty"`
FeeAmount types.Number `json:"feeAmt"`
FeeCurrency string `json:"feeCcy"`
PositionSide string `json:"posSide"`
Leverage types.Number `json:"lever"`
MarginMode string `json:"mgnMode"`
Price types.Number `json:"px"`
ReduceOnly bool `json:"reduceOnly"`
StopLossPrice types.Number `json:"slPx"`
Side string `json:"side"`
StopLossTriggerPrice string `json:"slTrgPx"`
StopLossTriggerPriceType string `json:"slTrgPxType"`
Source string `json:"source"`
State string `json:"state"`
SelfTradePreventionMode string `json:"stpMode"`
Symbol string `json:"symbol"`
Size types.Number `json:"sz"`
TimeInForce order.TimeInForce `json:"timeInForce"`
TakeProfitPrice types.Number `json:"tpPx"`
TakeProfitTriggerPrice types.Number `json:"tpTrgPx"`
TakeProfitTriggerPriceType string `json:"tpTrgPxType"`
Type string `json:"type"`
UpdateTime types.Time `json:"uTime"`
FeeRate types.Number `json:"feeRate"`
ID string `json:"id"`
OrderType string `json:"ordType"`
Quantity types.Number `json:"qty"`
Role string `json:"role"`
TradeID string `json:"trdId"`
CancelReason string `json:"cancelReason"`
}

// V3FuturesPosition represents a v3 futures position detail
Expand Down
56 changes: 46 additions & 10 deletions exchanges/poloniex/poloniex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,25 +1106,22 @@ func TestCreateSmartOrder(t *testing.T) {
t.Parallel()
_, err := p.CreateSmartOrder(context.Background(), &SmartOrderRequestParam{})
require.ErrorIs(t, err, errNilArgument)
_, err = p.CreateSmartOrder(context.Background(), &SmartOrderRequestParam{
Side: "BUY",
})

_, err = p.CreateSmartOrder(context.Background(), &SmartOrderRequestParam{Side: "BUY"})
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)

_, err = p.CreateSmartOrder(context.Background(), &SmartOrderRequestParam{
Symbol: spotTradablePair,
})
_, err = p.CreateSmartOrder(context.Background(), &SmartOrderRequestParam{Symbol: spotTradablePair})
require.ErrorIs(t, err, order.ErrSideIsInvalid)

sharedtestvalues.SkipTestIfCredentialsUnset(t, p, canManipulateRealOrders)
result, err := p.CreateSmartOrder(context.Background(), &SmartOrderRequestParam{
Symbol: spotTradablePair,
Side: "BUY",
Type: orderTypeString(order.StopLimit),
Quantity: 100,
Type: "STOP_LIMIT",
Price: 40000.50000,
TimeInForce: "GTC",
ClientOrderID: "1234Abc",
Side: "BUY",
TimeInForce: "GTC",
Quantity: 100,
})
require.NoError(t, err)
assert.NotNil(t, result)
Expand Down Expand Up @@ -2012,3 +2009,42 @@ func TestIntervalString(t *testing.T) {
require.ErrorIs(t, err, val.Error, err)
}
}

func TestTimeInForceString(t *testing.T) {
t.Parallel()
timeInForceStringMap := map[order.TimeInForce]struct {
String string
Error error
}{
order.GoodTillCancel: {String: "GTC"},
order.FillOrKill: {String: "FOK"},
order.ImmediateOrCancel: {String: "IOC"},
order.GoodTillCrossing: {Error: order.ErrInvalidTimeInForce},
}
for k, v := range timeInForceStringMap {
result, err := TimeInForceString(k)
assert.ErrorIs(t, err, v.Error)
assert.Equal(t, v.String, result)
}
}

func TestOrderTypeString(t *testing.T) {
t.Parallel()
orderStringMap := map[order.Type]struct {
String string
Error error
}{
order.Market: {String: order.Market.String()},
order.Limit: {String: order.Limit.String()},
order.LimitMaker: {String: order.LimitMaker.String()},
order.StopLimit: {String: "STOP_LIMIT"},
order.AnyType: {},
order.UnknownType: {},
order.TrailingStop: {Error: order.ErrUnsupportedOrderType},
}
for k, v := range orderStringMap {
result, err := OrderTypeString(k)
require.ErrorIs(t, err, v.Error)
assert.Equal(t, v.String, result)
}
}
159 changes: 80 additions & 79 deletions exchanges/poloniex/poloniex_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package poloniex
import (
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/types"
)

Expand Down Expand Up @@ -557,43 +558,43 @@ type CancelReplaceOrderResponse struct {

// TradeOrder represents a trade order instance.
type TradeOrder struct {
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
State string `json:"state"`
AccountType string `json:"accountType"`
Side string `json:"side"`
Type string `json:"type"`
TimeInForce string `json:"timeInForce"`
Quantity types.Number `json:"quantity"`
Price types.Number `json:"price"`
AvgPrice types.Number `json:"avgPrice"`
Amount types.Number `json:"amount"`
FilledQuantity types.Number `json:"filledQuantity"`
FilledAmount types.Number `json:"filledAmount"`
CreateTime types.Time `json:"createTime"`
UpdateTime types.Time `json:"updateTime"`
OrderSource string `json:"orderSource"`
Loan bool `json:"loan"`
CancelReason int64 `json:"cancelReason"`
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
State string `json:"state"`
AccountType string `json:"accountType"`
Side string `json:"side"`
Type string `json:"type"`
TimeInForce order.TimeInForce `json:"timeInForce"`
Quantity types.Number `json:"quantity"`
Price types.Number `json:"price"`
AvgPrice types.Number `json:"avgPrice"`
Amount types.Number `json:"amount"`
FilledQuantity types.Number `json:"filledQuantity"`
FilledAmount types.Number `json:"filledAmount"`
CreateTime types.Time `json:"createTime"`
UpdateTime types.Time `json:"updateTime"`
OrderSource string `json:"orderSource"`
Loan bool `json:"loan"`
CancelReason int64 `json:"cancelReason"`
}

// SmartOrderItem represents a smart order detail.
type SmartOrderItem struct {
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
State string `json:"state"`
AccountType string `json:"accountType"`
Side string `json:"side"`
Type string `json:"type"`
TimeInForce string `json:"timeInForce"`
Quantity types.Number `json:"quantity"`
Price types.Number `json:"price"`
Amount types.Number `json:"amount"`
StopPrice types.Number `json:"stopPrice"`
CreateTime types.Time `json:"createTime"`
UpdateTime types.Time `json:"updateTime"`
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
State string `json:"state"`
AccountType string `json:"accountType"`
Side string `json:"side"`
Type string `json:"type"`
TimeInForce order.TimeInForce `json:"timeInForce"`
Quantity types.Number `json:"quantity"`
Price types.Number `json:"price"`
Amount types.Number `json:"amount"`
StopPrice types.Number `json:"stopPrice"`
CreateTime types.Time `json:"createTime"`
UpdateTime types.Time `json:"updateTime"`
}

// CancelOrderResponse represents a cancel order response instance.
Expand Down Expand Up @@ -662,20 +663,20 @@ type CancelReplaceSmartOrderResponse struct {

// SmartOrderDetail represents a smart order information and trigger detailed information.
type SmartOrderDetail struct {
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
State string `json:"state"`
AccountType string `json:"accountType"`
Side string `json:"side"`
Type string `json:"type"`
TimeInForce string `json:"timeInForce"`
Quantity types.Number `json:"quantity"`
Price types.Number `json:"price"`
Amount types.Number `json:"amount"`
StopPrice types.Number `json:"stopPrice"`
CreateTime types.Time `json:"createTime"`
UpdateTime types.Time `json:"updateTime"`
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
State string `json:"state"`
AccountType string `json:"accountType"`
Side string `json:"side"`
Type string `json:"type"`
TimeInForce order.TimeInForce `json:"timeInForce"`
Quantity types.Number `json:"quantity"`
Price types.Number `json:"price"`
Amount types.Number `json:"amount"`
StopPrice types.Number `json:"stopPrice"`
CreateTime types.Time `json:"createTime"`
UpdateTime types.Time `json:"updateTime"`
TriggeredOrder struct {
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Expand Down Expand Up @@ -1006,38 +1007,38 @@ type FuturesOrders struct {

// FuturesOrder represents a futures order detail.
type FuturesOrder struct {
OrderID string `json:"id"`
Symbol string `json:"symbol"`
OrderType string `json:"type"`
Side string `json:"side"`
Price types.Number `json:"price"`
Size float64 `json:"size"`
Value types.Number `json:"value"`
FilledValue types.Number `json:"filledValue"`
FilledSize float64 `json:"filledSize"`
SelfTradePrevention string `json:"stp"`
Stop string `json:"stop"`
StopPriceType string `json:"stopPriceType"`
StopTriggered bool `json:"stopTriggered"`
StopPrice float64 `json:"stopPrice"`
TimeInForce string `json:"timeInForce"`
PostOnly bool `json:"postOnly"`
Hidden bool `json:"hidden"`
Iceberg bool `json:"iceberg"`
VisibleSize float64 `json:"visibleSize"`
Leverage types.Number `json:"leverage"`
ForceHold bool `json:"forceHold"`
CloseOrder bool `json:"closeOrder"`
ReduceOnly bool `json:"reduceOnly"`
ClientOrderID string `json:"clientOid"`
Remark string `json:"remark"`
IsActive bool `json:"isActive"`
CancelExist bool `json:"cancelExist"`
CreatedAt types.Time `json:"createdAt"`
SettleCurrency string `json:"settleCurrency"`
Status string `json:"status"`
UpdatedAt types.Time `json:"updatedAt"`
OrderTime types.Time `json:"orderTime"`
OrderID string `json:"id"`
Symbol string `json:"symbol"`
OrderType string `json:"type"`
Side string `json:"side"`
Price types.Number `json:"price"`
Size float64 `json:"size"`
Value types.Number `json:"value"`
FilledValue types.Number `json:"filledValue"`
FilledSize float64 `json:"filledSize"`
SelfTradePrevention string `json:"stp"`
Stop string `json:"stop"`
StopPriceType string `json:"stopPriceType"`
StopTriggered bool `json:"stopTriggered"`
StopPrice float64 `json:"stopPrice"`
TimeInForce order.TimeInForce `json:"timeInForce"`
PostOnly bool `json:"postOnly"`
Hidden bool `json:"hidden"`
Iceberg bool `json:"iceberg"`
VisibleSize float64 `json:"visibleSize"`
Leverage types.Number `json:"leverage"`
ForceHold bool `json:"forceHold"`
CloseOrder bool `json:"closeOrder"`
ReduceOnly bool `json:"reduceOnly"`
ClientOrderID string `json:"clientOid"`
Remark string `json:"remark"`
IsActive bool `json:"isActive"`
CancelExist bool `json:"cancelExist"`
CreatedAt types.Time `json:"createdAt"`
SettleCurrency string `json:"settleCurrency"`
Status string `json:"status"`
UpdatedAt types.Time `json:"updatedAt"`
OrderTime types.Time `json:"orderTime"`

MarginType int64 `json:"marginType"` // Margin Mode, 0 (Isolated) or 1 (Cross)
Trades []struct {
Expand Down
Loading

0 comments on commit c91d208

Please sign in to comment.