btcmarkets: update order submission (#908)

* btcm/order: update order submission

* btcmarkets: addr nits

* btcmarkets: reinstate new order test
This commit is contained in:
Ryan O'Hara-Reid
2022-03-25 15:05:24 +11:00
committed by GitHub
parent 489e2ebade
commit 022001fecf
12 changed files with 321 additions and 97 deletions

View File

@@ -40,6 +40,17 @@ func TestValidate(t *testing.T) {
ExpectedErr: ErrSideIsInvalid,
Submit: &Submit{Pair: testPair, AssetType: asset.Spot},
}, // valid pair but invalid order side
{
ExpectedErr: errTimeInForceConflict,
Submit: &Submit{
Pair: testPair,
AssetType: asset.Spot,
Side: Ask,
Type: Market,
ImmediateOrCancel: true,
FillOrKill: true,
},
},
{
ExpectedErr: ErrTypeIsInvalid,
Submit: &Submit{Pair: testPair,
@@ -703,7 +714,7 @@ func TestUpdateOrderFromModify(t *testing.T) {
LimitPriceUpper: 0,
LimitPriceLower: 0,
TriggerPrice: 0,
TargetAmount: 0,
QuoteAmount: 0,
ExecutedAmount: 0,
RemainingAmount: 0,
Fee: 0,
@@ -739,7 +750,7 @@ func TestUpdateOrderFromModify(t *testing.T) {
LimitPriceUpper: 1,
LimitPriceLower: 1,
TriggerPrice: 1,
TargetAmount: 1,
QuoteAmount: 1,
ExecutedAmount: 1,
RemainingAmount: 1,
Fee: 1,
@@ -792,7 +803,7 @@ func TestUpdateOrderFromModify(t *testing.T) {
if od.TriggerPrice != 1 {
t.Error("Failed to update")
}
if od.TargetAmount != 1 {
if od.QuoteAmount != 1 {
t.Error("Failed to update")
}
if od.ExecutedAmount != 1 {
@@ -895,7 +906,7 @@ func TestUpdateOrderFromDetail(t *testing.T) {
LimitPriceUpper: 0,
LimitPriceLower: 0,
TriggerPrice: 0,
TargetAmount: 0,
QuoteAmount: 0,
ExecutedAmount: 0,
RemainingAmount: 0,
Fee: 0,
@@ -931,7 +942,7 @@ func TestUpdateOrderFromDetail(t *testing.T) {
LimitPriceUpper: 1,
LimitPriceLower: 1,
TriggerPrice: 1,
TargetAmount: 1,
QuoteAmount: 1,
ExecutedAmount: 1,
RemainingAmount: 1,
Fee: 1,
@@ -984,7 +995,7 @@ func TestUpdateOrderFromDetail(t *testing.T) {
if od.TriggerPrice != 1 {
t.Error("Failed to update")
}
if od.TargetAmount != 1 {
if od.QuoteAmount != 1 {
t.Error("Failed to update")
}
if od.ExecutedAmount != 1 {

View File

@@ -35,31 +35,36 @@ type Submit struct {
ReduceOnly bool
Leverage float64
Price float64
Amount float64
StopPrice float64
LimitPriceUpper float64
LimitPriceLower float64
TriggerPrice float64
TargetAmount float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
Exchange string
InternalOrderID string
ID string
AccountID string
ClientID string
ClientOrderID string
WalletAddress string
Offset string
Type Type
Side Side
Status Status
AssetType asset.Item
Date time.Time
LastUpdated time.Time
Pair currency.Pair
Trades []TradeHistory
// Amount in base terms
Amount float64
// QuoteAmount is the max amount in quote currency when purchasing base.
// This is only used in Market orders.
QuoteAmount float64
StopPrice float64
LimitPriceUpper float64
LimitPriceLower float64
TriggerPrice float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
Exchange string
InternalOrderID string
ID string
AccountID string
ClientID string
ClientOrderID string
WalletAddress string
Offset string
Type Type
Side Side
Status Status
AssetType asset.Item
Date time.Time
LastUpdated time.Time
Pair currency.Pair
Trades []TradeHistory
}
// SubmitResponse is what is returned after submitting an order to an exchange
@@ -88,7 +93,7 @@ type Modify struct {
LimitPriceUpper float64
LimitPriceLower float64
TriggerPrice float64
TargetAmount float64
QuoteAmount float64
ExecutedAmount float64
RemainingAmount float64
Fee float64
@@ -129,7 +134,7 @@ type Detail struct {
LimitPriceLower float64
TriggerPrice float64
AverageExecutedPrice float64
TargetAmount float64
QuoteAmount float64
ExecutedAmount float64
RemainingAmount float64
Cost float64

View File

@@ -13,6 +13,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/validate"
)
var errTimeInForceConflict = errors.New("multiple time in force options applied")
// Validate checks the supplied data and returns whether or not it's valid
func (s *Submit) Validate(opt ...validate.Checker) error {
if s == nil {
@@ -38,8 +40,20 @@ func (s *Submit) Validate(opt ...validate.Checker) error {
return ErrTypeIsInvalid
}
if s.Amount <= 0 {
return fmt.Errorf("submit validation error %w, suppled: %.8f", ErrAmountIsInvalid, s.Amount)
if s.ImmediateOrCancel && s.FillOrKill {
return errTimeInForceConflict
}
if s.Amount == 0 && s.QuoteAmount == 0 {
return fmt.Errorf("submit validation error %w, amount and quote amount cannot be zero", ErrAmountIsInvalid)
}
if s.Amount < 0 {
return fmt.Errorf("submit validation error base %w, suppled: %v", ErrAmountIsInvalid, s.Amount)
}
if s.QuoteAmount < 0 {
return fmt.Errorf("submit validation error quote %w, suppled: %v", ErrAmountIsInvalid, s.QuoteAmount)
}
if s.Type == Limit && s.Price <= 0 {
@@ -92,8 +106,8 @@ func (d *Detail) UpdateOrderFromDetail(m *Detail) {
d.TriggerPrice = m.TriggerPrice
updated = true
}
if m.TargetAmount > 0 && m.TargetAmount != d.TargetAmount {
d.TargetAmount = m.TargetAmount
if m.QuoteAmount > 0 && m.QuoteAmount != d.QuoteAmount {
d.QuoteAmount = m.QuoteAmount
updated = true
}
if m.ExecutedAmount > 0 && m.ExecutedAmount != d.ExecutedAmount {
@@ -256,8 +270,8 @@ func (d *Detail) UpdateOrderFromModify(m *Modify) {
d.TriggerPrice = m.TriggerPrice
updated = true
}
if m.TargetAmount > 0 && m.TargetAmount != d.TargetAmount {
d.TargetAmount = m.TargetAmount
if m.QuoteAmount > 0 && m.QuoteAmount != d.QuoteAmount {
d.QuoteAmount = m.QuoteAmount
updated = true
}
if m.ExecutedAmount > 0 && m.ExecutedAmount != d.ExecutedAmount {