Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orders: Add methods to derive SubmitResponse and Detail types. #955

Merged
merged 21 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
}

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) {
thrasher- marked this conversation as resolved.
Show resolved Hide resolved
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