gctrpc/order manager: Add ModifyOrder endpoint (#724)

* gctcli: modifyorder stubs

* gctcli: add ModifyOrderRequest and ModifyOrderResponse in rpc.proto

* gctcli: regenerate rpc.pb.go after the addition of ModifyOrder structs

* gctrpc: add ModifyOrder() and regenerate dependent files

* gctcli: modifyorder command now uses newly generated ModifyOrder() RPC

* exchanges/order/orders.go: use time.Time.Equal() instead of ==

* gctrpc: update ModifyOrderRequest and ModifyResponse and regenerate gRPC

* gctcli/commands: rework modifyorder

* engine: implement RPCServer.ModifyOrder

* engine: commit an initial version OrderManager.Modify(), still does not update state of managed orders

* engine: OrderManager.Modify now updates the inner state of managed orders, but introduces race conditions, needs fixes

* engine/order_manager.go: comply with golangci-lint

* gctcli: fix getOrderCommand.ArgsUsage

* gctcli: fix getModifyOrderCommand args and ArgsUsage

* engine: OrderManager.Modify() now correctly updates price of modified order

* engine: RPCServer.ModifyOrder now uses checkParams() as advised

* exchanges: (1) IBotExchange.ModifyOrder now returns a Modify struct, (2) all exchanges are updated to comply with that change

* exchanges/order: Detail.UpdateOrderFromModify also updates the ID

* engine/order_manager: add store.modifyExisting() and use it in OrderManager.Modify to update (on success) the state of the modified order

* exchanges: Bitfinex.ModifyOrder() now returns the ID in case of an error

* engine: OrdetManager.Modify() now emits an order event

* exchanges/bithumb: proper order.payment_currency key

* engine/order_manager: populate more Modify fields as they are needed by (some) exchanges, add comments

* engine: test OrderManager.Modify()

* engine: test store.modifyExisting()

* engine: write a docstring for store.modifyExisting

* engine: OrderManager.Modify() now also sets Modify.Price and Modify.Amount in case of zero values

* engine: TestOrderManager_Modify() now verify the effects of price and/or amount set to 0

* engine: OrderManger.Modify() now uses the commsManager to let observers know of errors

* engine: TestOrderManager_Modify() uses t.Fatal()

* engine: TestOrderManager_Modify() and TestStore_modifyOrder() supply t.Error() with proper messages

* exchanges/order_manager_test: fix a golangci-lint complaint

* engine/order_manager: fix an error comparison bug and simplify

* gctcli/commands: check if either price or amount is set, otherwise we would waste an API call
This commit is contained in:
Yordan Miladinov
2021-08-06 03:09:14 +03:00
committed by GitHub
parent 547d123984
commit 2da239735f
41 changed files with 1805 additions and 950 deletions

View File

@@ -263,8 +263,8 @@ func (a *Alphapoint) SubmitOrder(s *order.Submit) (order.SubmitResponse, error)
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (a *Alphapoint) ModifyOrder(_ *order.Modify) (string, error) {
return "", common.ErrNotYetImplemented
func (a *Alphapoint) ModifyOrder(_ *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrNotYetImplemented
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -919,8 +919,8 @@ func (b *Binance) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Binance) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (b *Binance) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -647,27 +647,36 @@ func (b *Bitfinex) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bitfinex) ModifyOrder(action *order.Modify) (string, error) {
func (b *Bitfinex) ModifyOrder(action *order.Modify) (order.Modify, error) {
if err := action.Validate(); err != nil {
return "", err
return order.Modify{}, err
}
orderIDInt, err := strconv.ParseInt(action.ID, 10, 64)
if err != nil {
return action.ID, err
return order.Modify{ID: action.ID}, err
}
if b.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
if action.Side == order.Sell && action.Amount > 0 {
action.Amount = -1 * action.Amount
}
err = b.WsModifyOrder(&WsUpdateOrderRequest{
request := WsUpdateOrderRequest{
OrderID: orderIDInt,
Price: action.Price,
Amount: action.Amount,
})
return action.ID, err
}
if action.Side == order.Sell && action.Amount > 0 {
request.Amount *= -1
}
err = b.WsModifyOrder(&request)
return order.Modify{
Exchange: action.Exchange,
AssetType: action.AssetType,
Pair: action.Pair,
ID: action.ID,
Price: action.Price,
Amount: action.Amount,
}, err
}
return "", common.ErrNotYetImplemented
return order.Modify{}, common.ErrNotYetImplemented
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -373,8 +373,8 @@ func (b *Bitflyer) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bitflyer) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (b *Bitflyer) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -314,7 +314,7 @@ func (b *Bithumb) PlaceTrade(orderCurrency, transactionType string, units float6
params := url.Values{}
params.Set("order_currency", strings.ToUpper(orderCurrency))
params.Set("Payment_currency", "KRW")
params.Set("payment_currency", "KRW")
params.Set("type", strings.ToUpper(transactionType))
params.Set("units", strconv.FormatFloat(units, 'f', -1, 64))
params.Set("price", strconv.FormatInt(price, 10))
@@ -329,7 +329,7 @@ func (b *Bithumb) ModifyTrade(orderID, orderCurrency, transactionType string, un
params := url.Values{}
params.Set("order_currency", strings.ToUpper(orderCurrency))
params.Set("Payment_currency", "KRW")
params.Set("payment_currency", "KRW")
params.Set("type", strings.ToUpper(transactionType))
params.Set("units", strconv.FormatFloat(units, 'f', -1, 64))
params.Set("price", strconv.FormatInt(price, 10))

View File

@@ -438,22 +438,31 @@ func (b *Bithumb) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bithumb) ModifyOrder(action *order.Modify) (string, error) {
func (b *Bithumb) ModifyOrder(action *order.Modify) (order.Modify, error) {
if err := action.Validate(); err != nil {
return "", err
return order.Modify{}, err
}
order, err := b.ModifyTrade(action.ID,
o, err := b.ModifyTrade(action.ID,
action.Pair.Base.String(),
action.Side.Lower(),
action.Amount,
int64(action.Price))
if err != nil {
return "", err
return order.Modify{}, err
}
return order.Data[0].ContID, nil
return order.Modify{
Exchange: action.Exchange,
AssetType: action.AssetType,
Pair: action.Pair,
ID: o.Data[0].ContID,
Price: float64(int64(action.Price)),
Amount: action.Amount,
Side: action.Side,
}, nil
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -575,27 +575,35 @@ func (b *Bitmex) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bitmex) ModifyOrder(action *order.Modify) (string, error) {
func (b *Bitmex) ModifyOrder(action *order.Modify) (order.Modify, error) {
if err := action.Validate(); err != nil {
return "", err
return order.Modify{}, err
}
var params OrderAmendParams
if math.Mod(action.Amount, 1) != 0 {
return "", errors.New("contract amount can not have decimals")
return order.Modify{}, errors.New("contract amount can not have decimals")
}
params.OrderID = action.ID
params.OrderQty = int32(action.Amount)
params.Price = action.Price
order, err := b.AmendOrder(&params)
o, err := b.AmendOrder(&params)
if err != nil {
return "", err
return order.Modify{}, err
}
return order.OrderID, nil
return order.Modify{
Exchange: action.Exchange,
AssetType: action.AssetType,
Pair: action.Pair,
ID: o.OrderID,
Price: action.Price,
Amount: float64(params.OrderQty),
}, nil
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -489,8 +489,8 @@ func (b *Bitstamp) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bitstamp) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (b *Bitstamp) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -566,8 +566,8 @@ func (b *Bittrex) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bittrex) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (b *Bittrex) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -530,8 +530,8 @@ func (b *BTCMarkets) SubmitOrder(s *order.Submit) (order.SubmitResponse, error)
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *BTCMarkets) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (b *BTCMarkets) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -530,8 +530,8 @@ func (b *BTSE) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *BTSE) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (b *BTSE) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -552,8 +552,8 @@ func (c *CoinbasePro) SubmitOrder(s *order.Submit) (order.SubmitResponse, error)
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (c *CoinbasePro) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (c *CoinbasePro) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -597,8 +597,8 @@ func (c *Coinbene) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (c *Coinbene) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (c *Coinbene) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -642,8 +642,8 @@ func (c *COINUT) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (c *COINUT) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (c *COINUT) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -467,8 +467,8 @@ func (e *EXMO) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (e *EXMO) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (e *EXMO) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -594,9 +594,9 @@ func (f *FTX) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (f *FTX) ModifyOrder(action *order.Modify) (string, error) {
func (f *FTX) ModifyOrder(action *order.Modify) (order.Modify, error) {
if err := action.Validate(); err != nil {
return "", err
return order.Modify{}, err
}
if action.TriggerPrice != 0 {
@@ -607,25 +607,42 @@ func (f *FTX) ModifyOrder(action *order.Modify) (string, error) {
action.Price,
0)
if err != nil {
return "", err
return order.Modify{}, err
}
return strconv.FormatInt(a.ID, 10), err
return order.Modify{
Exchange: action.Exchange,
AssetType: action.AssetType,
Pair: action.Pair,
ID: strconv.FormatInt(a.ID, 10),
Price: action.Price,
Amount: action.Amount,
TriggerPrice: action.TriggerPrice,
Type: action.Type,
}, err
}
var o OrderData
var err error
switch action.ID {
case "":
if action.ID == "" {
o, err = f.ModifyOrderByClientID(action.ClientOrderID, action.ClientOrderID, action.Price, action.Amount)
if err != nil {
return "", err
return order.Modify{}, err
}
default:
} else {
o, err = f.ModifyPlacedOrder(action.ID, action.ClientOrderID, action.Price, action.Amount)
if err != nil {
return "", err
return order.Modify{}, err
}
}
return strconv.FormatInt(o.ID, 10), err
return order.Modify{
Exchange: action.Exchange,
AssetType: action.AssetType,
Pair: action.Pair,
ID: strconv.FormatInt(o.ID, 10),
Price: action.Price,
Amount: action.Amount,
}, err
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -518,8 +518,8 @@ func (g *Gateio) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (g *Gateio) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (g *Gateio) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -551,8 +551,8 @@ func (g *Gemini) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (g *Gemini) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (g *Gemini) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -575,8 +575,8 @@ func (h *HitBTC) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (h *HitBTC) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (h *HitBTC) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -898,8 +898,8 @@ func (h *HUOBI) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (h *HUOBI) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (h *HUOBI) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -51,7 +51,7 @@ type IBotExchange interface {
SupportsWithdrawPermissions(permissions uint32) bool
GetFundingHistory() ([]FundHistory, error)
SubmitOrder(s *order.Submit) (order.SubmitResponse, error)
ModifyOrder(action *order.Modify) (string, error)
ModifyOrder(action *order.Modify) (order.Modify, error)
CancelOrder(o *order.Cancel) error
CancelBatchOrders(o []order.Cancel) (order.CancelBatchResponse, error)
CancelAllOrders(orders *order.Cancel) (order.CancelAllResponse, error)

View File

@@ -416,8 +416,8 @@ func (i *ItBit) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (i *ItBit) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (i *ItBit) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -755,8 +755,8 @@ func (k *Kraken) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (k *Kraken) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (k *Kraken) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -456,8 +456,8 @@ func (l *Lbank) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (l *Lbank) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (l *Lbank) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -414,8 +414,8 @@ func (l *LocalBitcoins) SubmitOrder(s *order.Submit) (order.SubmitResponse, erro
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (l *LocalBitcoins) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (l *LocalBitcoins) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -312,8 +312,8 @@ func (o *OKGroup) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (o *OKGroup) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (o *OKGroup) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -196,7 +196,7 @@ func (d *Detail) UpdateOrderFromDetail(m *Detail) {
updated = true
}
if updated {
if d.LastUpdated == m.LastUpdated {
if d.LastUpdated.Equal(m.LastUpdated) {
d.LastUpdated = time.Now()
} else {
d.LastUpdated = m.LastUpdated
@@ -214,6 +214,10 @@ func (d *Detail) UpdateOrderFromDetail(m *Detail) {
// by comparing passed in and existing values
func (d *Detail) UpdateOrderFromModify(m *Modify) {
var updated bool
if m.ID != "" && d.ID != m.ID {
d.ID = m.ID
updated = true
}
if d.ImmediateOrCancel != m.ImmediateOrCancel {
d.ImmediateOrCancel = m.ImmediateOrCancel
updated = true
@@ -351,7 +355,7 @@ func (d *Detail) UpdateOrderFromModify(m *Modify) {
updated = true
}
if updated {
if d.LastUpdated == m.LastUpdated {
if d.LastUpdated.Equal(m.LastUpdated) {
d.LastUpdated = time.Now()
} else {
d.LastUpdated = m.LastUpdated

View File

@@ -541,14 +541,14 @@ func (p *Poloniex) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (p *Poloniex) ModifyOrder(action *order.Modify) (string, error) {
func (p *Poloniex) ModifyOrder(action *order.Modify) (order.Modify, error) {
if err := action.Validate(); err != nil {
return "", err
return order.Modify{}, err
}
oID, err := strconv.ParseInt(action.ID, 10, 64)
if err != nil {
return "", err
return order.Modify{}, err
}
resp, err := p.MoveOrder(oID,
@@ -557,10 +557,20 @@ func (p *Poloniex) ModifyOrder(action *order.Modify) (string, error) {
action.PostOnly,
action.ImmediateOrCancel)
if err != nil {
return "", err
return order.Modify{}, err
}
return strconv.FormatInt(resp.OrderNumber, 10), nil
return order.Modify{
Exchange: action.Exchange,
AssetType: action.AssetType,
Pair: action.Pair,
ID: strconv.FormatInt(resp.OrderNumber, 10),
Price: action.Price,
Amount: action.Amount,
PostOnly: action.PostOnly,
ImmediateOrCancel: action.ImmediateOrCancel,
}, nil
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -413,8 +413,8 @@ func (y *Yobit) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (y *Yobit) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (y *Yobit) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number

View File

@@ -511,8 +511,8 @@ func (z *ZB) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (z *ZB) ModifyOrder(action *order.Modify) (string, error) {
return "", common.ErrFunctionNotSupported
func (z *ZB) ModifyOrder(action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number