Skip to content

Commit

Permalink
orders: Add methods to derive SubmitResponse and Detail types (thrash…
Browse files Browse the repository at this point in the history
…er-corp#955)

* orders: deprecate SubmitResponse return and change to *order.Detail construct detail from order.Submit struct

* orders: add coverage, fix tests

* coinut: rm test for checking

* orders: revert change for return and change field ID to a more explicit name OrderID

* orders: Add method to see if the order was placed

* order: change field name in Cancel type to be more explicit

* orders: standardize field -> OrderID

* backtester: populate change

* orders: add test

* gctscript: fix field name

* linter: fix issues

* linter: more fixes

* linter: forever

* exchanges_tests: populate order.Submit field exchange name

* Update exchanges/order/order_types.go

Co-authored-by: Scott <[email protected]>

* Update exchanges/order/orders.go

Co-authored-by: Scott <[email protected]>

* glorious: nits

* glorious: nits

* thrasher: nits

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
Co-authored-by: Scott <[email protected]>
  • Loading branch information
3 people authored Jun 6, 2022
1 parent 85da9b7 commit a12cd3d
Show file tree
Hide file tree
Showing 95 changed files with 1,372 additions and 1,165 deletions.
65 changes: 27 additions & 38 deletions backtester/eventhandlers/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (e *Exchange) ExecuteOrder(o order.Event, data data.Handler, orderManager *

ords := orderManager.GetOrdersSnapshot(gctorder.UnknownStatus)
for i := range ords {
if ords[i].ID != orderID {
if ords[i].OrderID != orderID {
continue
}
ords[i].Date = o.GetTime()
Expand Down Expand Up @@ -224,53 +224,42 @@ func (e *Exchange) placeOrder(ctx context.Context, price, amount decimal.Decimal
if f == nil {
return "", common.ErrNilEvent
}
u, err := uuid.NewV4()
orderID, err := uuid.NewV4()
if err != nil {
return "", err
}
var orderID string
p := price.InexactFloat64()
fee := f.ExchangeFee.InexactFloat64()
o := &gctorder.Submit{
Price: p,
Amount: amount.InexactFloat64(),
Fee: fee,
Exchange: f.Exchange,
ID: u.String(),
Side: f.Direction,
AssetType: f.AssetType,
Date: f.GetTime(),
LastUpdated: f.GetTime(),
Pair: f.Pair(),
Type: gctorder.Market,

submit := &gctorder.Submit{
Price: price.InexactFloat64(),
Amount: amount.InexactFloat64(),
Exchange: f.Exchange,
Side: f.Direction,
AssetType: f.AssetType,
Pair: f.Pair(),
Type: gctorder.Market,
}

var resp *engine.OrderSubmitResponse
if useRealOrders {
resp, err := orderManager.Submit(ctx, o)
if resp != nil {
orderID = resp.OrderID
}
if err != nil {
return orderID, err
}
resp, err = orderManager.Submit(ctx, submit)
} else {
submitResponse := gctorder.SubmitResponse{
IsOrderPlaced: true,
OrderID: u.String(),
Rate: f.Amount.InexactFloat64(),
Fee: fee,
Cost: p,
FullyMatched: true,
}
resp, err := orderManager.SubmitFakeOrder(o, submitResponse, useExchangeLimits)
if resp != nil {
orderID = resp.OrderID
}
var submitResponse *gctorder.SubmitResponse
submitResponse, err = submit.DeriveSubmitResponse(orderID.String())
if err != nil {
return orderID, err
return orderID.String(), err
}
submitResponse.Status = gctorder.Filled
submitResponse.OrderID = orderID.String()
submitResponse.Fee = f.ExchangeFee.InexactFloat64()
submitResponse.Cost = submit.Price
submitResponse.LastUpdated = f.GetTime()
submitResponse.Date = f.GetTime()
resp, err = orderManager.SubmitFakeOrder(submit, submitResponse, useExchangeLimits)
}
if err != nil {
return orderID.String(), err
}
return orderID, nil
return resp.OrderID, nil
}

func (e *Exchange) sizeOfflineOrder(high, low, volume decimal.Decimal, cs *Settings, f *fill.Fill) (adjustedPrice, adjustedAmount decimal.Decimal, err error) {
Expand Down
8 changes: 4 additions & 4 deletions backtester/eventhandlers/portfolio/holdings/holdings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestUpdateBuyStats(t *testing.T) {
Price: 500,
Amount: 1,
Exchange: testExchange,
ID: "decimal.NewFromInt(1337)",
OrderID: "decimal.NewFromInt(1337)",
Type: order.Limit,
Side: order.Buy,
Status: order.New,
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestUpdateBuyStats(t *testing.T) {
Price: 500,
Amount: 0.5,
Exchange: testExchange,
ID: "decimal.NewFromInt(1337)",
OrderID: "decimal.NewFromInt(1337)",
Type: order.Limit,
Side: order.Buy,
Status: order.New,
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestUpdateSellStats(t *testing.T) {
Price: 500,
Amount: 1,
Exchange: testExchange,
ID: "decimal.NewFromInt(1337)",
OrderID: "decimal.NewFromInt(1337)",
Type: order.Limit,
Side: order.Buy,
Status: order.New,
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestUpdateSellStats(t *testing.T) {
Price: 500,
Amount: 1,
Exchange: testExchange,
ID: "decimal.NewFromInt(1337)",
OrderID: "decimal.NewFromInt(1337)",
Type: order.Limit,
Side: order.Sell,
Status: order.New,
Expand Down
30 changes: 23 additions & 7 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,36 @@ func ({{.Variable}} *{{.CapitalName}}) GetHistoricTrades (ctx context.Context, p
}

// SubmitOrder submits a new order
func ({{.Variable}} *{{.CapitalName}}) SubmitOrder(ctx context.Context, s *order.Submit) (order.SubmitResponse, error) {
var submitOrderResponse order.SubmitResponse
func ({{.Variable}} *{{.CapitalName}}) SubmitOrder(ctx context.Context, s *order.Submit) (*order.SubmitResponse, error) {
if err := s.Validate(); err != nil {
return submitOrderResponse, err
}
return submitOrderResponse, common.ErrNotYetImplemented
return nil, err
}
// When an order has been submitted you can use this helpful constructor to
// return. Please add any additional order details to the
// order.SubmitResponse if you think they are applicable.
// resp, err := s.DeriveSubmitResponse( /*newOrderID*/)
// if err != nil {
// return nil, nil
// }
// resp.Date = exampleTime // e.g. If this is supplied by the exchanges API.
// return resp, nil
return nil, common.ErrNotYetImplemented
}

// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func ({{.Variable}} *{{.CapitalName}}) ModifyOrder(ctx context.Context, action *order.Modify) (*order.ModifyResponse, error) {
// if err := action.Validate(); err != nil {
// return "", err
if err := action.Validate(); err != nil {
return nil, err
}
// When an order has been modified you can use this helpful constructor to
// return. Please add any additional order details to the
// order.ModifyResponse if you think they are applicable.
// resp, err := action.DeriveModifyResponse()
// if err != nil {
// return nil, nil
// }
// resp.OrderID = maybeANewOrderID // e.g. If this is supplied by the exchanges API.
return nil, common.ErrNotYetImplemented
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
})

s := &order.Submit{
Exchange: e.GetName(),
Pair: p,
Side: testOrderSide,
Type: testOrderType,
Expand All @@ -564,7 +565,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
ClientID: config.OrderSubmission.OrderID,
AssetType: assetTypes[i],
}
var submitOrderResponse order.SubmitResponse
var submitOrderResponse *order.SubmitResponse
submitOrderResponse, err = e.SubmitOrder(context.TODO(), s)
msg = ""
if err != nil {
Expand All @@ -579,12 +580,12 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
})

modifyRequest := order.Modify{
ID: config.OrderSubmission.OrderID,
Type: testOrderType,
Side: testOrderSide,
Pair: p,
Price: config.OrderSubmission.Price,
Amount: config.OrderSubmission.Amount,
OrderID: config.OrderSubmission.OrderID,
Type: testOrderType,
Side: testOrderSide,
Pair: p,
Price: config.OrderSubmission.Price,
Amount: config.OrderSubmission.Amount,
}
modifyOrderResponse, err := e.ModifyOrder(context.TODO(), &modifyRequest)
msg = ""
Expand All @@ -602,7 +603,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
cancelRequest := order.Cancel{
Side: testOrderSide,
Pair: p,
ID: config.OrderSubmission.OrderID,
OrderID: config.OrderSubmission.OrderID,
AssetType: assetTypes[i],
}
err = e.CancelOrder(context.TODO(), &cancelRequest)
Expand All @@ -622,7 +623,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
request = append(request, order.Cancel{
Side: testOrderSide,
Pair: p,
ID: config.OrderSubmission.OrderID,
OrderID: config.OrderSubmission.OrderID,
AssetType: assetTypes[i],
})

Expand Down
5 changes: 3 additions & 2 deletions docs/EXCHANGE_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ supplied meet the requirements to make an authenticated request.


o := &order.Submit{
Exchange: b.Name, // or method GetName() if exchange.IBotInterface
Pair: currency.NewPair(currency.BTC, currency.USD),
OrderSide: order.Sell,
OrderType: order.Limit,
Side: order.Sell,
Type: order.Limit,
Price: 1000000,
Amount: 0.1,
AssetType: asset.Spot,
Expand Down
Loading

0 comments on commit a12cd3d

Please sign in to comment.