Skip to content

Commit

Permalink
orders/gateio: Add GetTradeAmount method to order.Submit type (thrash…
Browse files Browse the repository at this point in the history
…er-corp#1584)

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
  • Loading branch information
shazbert and Ryan O'Hara-Reid authored Jul 23, 2024
1 parent 45ef2b1 commit 425ec0c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
19 changes: 7 additions & 12 deletions exchanges/gateio/gateio_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,19 +1013,14 @@ func (g *Gateio) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Submi
return nil, err
}

// When doing spot market orders when purchasing base currency, the
// quote currency amount is used. When selling the base currency the
// base currency amount is used.
tradingAmount := s.Amount
if tradingAmount == 0 && s.Type == order.Market {
tradingAmount = s.QuoteAmount
}

sOrder, err := g.PlaceSpotOrder(ctx, &CreateOrderRequestData{
Side: s.Side.Lower(),
Type: s.Type.Lower(),
Account: g.assetTypeToString(s.AssetType),
Amount: types.Number(tradingAmount),
Side: s.Side.Lower(),
Type: s.Type.Lower(),
Account: g.assetTypeToString(s.AssetType),
// When doing spot market orders when purchasing base currency, the
// quote currency amount is used. When selling the base currency the
// base currency amount is used.
Amount: types.Number(s.GetTradeAmount(g.GetTradingRequirements())),
Price: types.Number(s.Price),
CurrencyPair: s.Pair,
Text: s.ClientOrderID,
Expand Down
20 changes: 20 additions & 0 deletions exchanges/order/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
Expand Down Expand Up @@ -2125,3 +2126,22 @@ func TestSideUnmarshal(t *testing.T) {
var jErr *json.UnmarshalTypeError
assert.ErrorAs(t, s.UnmarshalJSON([]byte(`14`)), &jErr, "non-string valid json is rejected")
}

func TestGetTradeAmount(t *testing.T) {
t.Parallel()
var s *Submit
require.Zero(t, s.GetTradeAmount(protocol.TradingRequirements{}))
baseAmount := 420.0
quoteAmount := 69.0
s = &Submit{Amount: baseAmount, QuoteAmount: quoteAmount}
// below will default to base amount with nothing set
require.Equal(t, baseAmount, s.GetTradeAmount(protocol.TradingRequirements{}))
require.Equal(t, baseAmount, s.GetTradeAmount(protocol.TradingRequirements{SpotMarketOrderAmountPurchaseQuotationOnly: true}))
s.AssetType = asset.Spot
s.Type = Market
s.Side = Buy
require.Equal(t, quoteAmount, s.GetTradeAmount(protocol.TradingRequirements{SpotMarketOrderAmountPurchaseQuotationOnly: true}))
require.Equal(t, baseAmount, s.GetTradeAmount(protocol.TradingRequirements{SpotMarketOrderAmountSellBaseOnly: true}))
s.Side = Sell
require.Equal(t, baseAmount, s.GetTradeAmount(protocol.TradingRequirements{SpotMarketOrderAmountSellBaseOnly: true}))
}
17 changes: 17 additions & 0 deletions exchanges/order/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ func (s *Submit) Validate(requirements protocol.TradingRequirements, opt ...vali
return nil
}

// GetTradeAmount returns the trade amount based on the exchange's trading
// requirements. Some exchanges depending on direction and order type use
// quotation (funds in balance) or base amounts. If the exchange does not have
// any specific requirements it will return the base amount.
func (s *Submit) GetTradeAmount(tr protocol.TradingRequirements) float64 {
if s == nil {
return 0
}
switch {
case tr.SpotMarketOrderAmountPurchaseQuotationOnly && s.AssetType == asset.Spot && s.Type == Market && s.Side.IsLong():
return s.QuoteAmount
case tr.SpotMarketOrderAmountSellBaseOnly && s.AssetType == asset.Spot && s.Type == Market && s.Side.IsShort():
return s.Amount
}
return s.Amount
}

// UpdateOrderFromDetail Will update an order detail (used in order management)
// by comparing passed in and existing values
func (d *Detail) UpdateOrderFromDetail(m *Detail) error {
Expand Down

0 comments on commit 425ec0c

Please sign in to comment.