From f06ec76618ba465dc5a65927807a3e81dd300fb7 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 22 May 2022 01:19:43 +0800 Subject: [PATCH] backtest: check quoteQuantity only when price is given --- apps/backtest-report/components/TradingViewChart.js | 4 +++- config/grid.yaml | 8 ++++---- pkg/backtest/matching.go | 8 +++++--- pkg/types/order.go | 8 ++++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/apps/backtest-report/components/TradingViewChart.js b/apps/backtest-report/components/TradingViewChart.js index 7eb08930d..5e7b90a0b 100644 --- a/apps/backtest-report/components/TradingViewChart.js +++ b/apps/backtest-report/components/TradingViewChart.js @@ -43,6 +43,8 @@ const parseOrder = () => { case "quantity": d[key] = +d[key]; break; + case "update_time": + case "creation_time": case "time": d[key] = new Date(d[key]); break; @@ -150,7 +152,7 @@ const ordersToMarkets = (interval, orders) => { // var markers = [{ time: data[data.length - 48].time, position: 'aboveBar', color: '#f68410', shape: 'circle', text: 'D' }]; for (let i = 0; i < orders.length; i++) { let order = orders[i]; - let t = order.time.getTime() / 1000.0; + let t = order.update_time.getTime() / 1000.0; let lastMarker = markers.length > 0 ? markers[markers.length - 1] : null; if (lastMarker) { let remainder = lastMarker.time % intervalSecs; diff --git a/config/grid.yaml b/config/grid.yaml index c862135b8..726e30ca4 100644 --- a/config/grid.yaml +++ b/config/grid.yaml @@ -31,8 +31,8 @@ backtest: # for testing max draw down (MDD) at 03-12 # see here for more details # https://www.investopedia.com/terms/m/maximum-drawdown-mdd.asp - startTime: "2022-01-10" - endTime: "2022-01-11" + startTime: "2022-05-09" + endTime: "2022-05-20" symbols: - BTCUSDT sessions: [binance] @@ -53,9 +53,9 @@ exchangeStrategies: # exp: # domain: [20_000, 30_000] # range: [0.2, 0.001] - gridNumber: 100 + gridNumber: 30 profitSpread: 1000.0 # The profit price spread that you want to add to your sell order when your buy order is executed upperPrice: 50_000.0 lowerPrice: 20_000.0 - long: true # The sell order is submitted in the same order amount as the filled corresponding buy order, rather than the same quantity. + # long: true # The sell order is submitted in the same order amount as the filled corresponding buy order, rather than the same quantity. diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index dc6581725..7fe2343db 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -118,9 +118,11 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *typ return nil, nil, fmt.Errorf("order quantity %s is less than minQuantity %s, order: %+v", o.Quantity.String(), m.Market.MinQuantity.String(), o) } - quoteQuantity := o.Quantity.Mul(price) - if quoteQuantity.Compare(m.Market.MinNotional) < 0 { - return nil, nil, fmt.Errorf("order amount %s is less than minNotional %s, order: %+v", quoteQuantity.String(), m.Market.MinNotional.String(), o) + if !price.IsZero() { + quoteQuantity := o.Quantity.Mul(price) + if quoteQuantity.Compare(m.Market.MinNotional) < 0 { + return nil, nil, fmt.Errorf("order amount %s is less than minNotional %s, order: %+v", quoteQuantity.String(), m.Market.MinNotional.String(), o) + } } switch o.Side { diff --git a/pkg/types/order.go b/pkg/types/order.go index 8439fac2d..60d744214 100644 --- a/pkg/types/order.go +++ b/pkg/types/order.go @@ -220,26 +220,30 @@ type Order struct { func (o Order) CsvHeader() []string { return []string{ - "time", "order_id", "symbol", "side", "order_type", + "status", "price", "quantity", + "creation_time", + "update_time", } } func (o Order) CsvRecords() [][]string { return [][]string{ { - o.UpdateTime.Time().UTC().Format(time.RFC1123), strconv.FormatUint(o.OrderID, 10), o.Symbol, string(o.Side), string(o.Type), + string(o.Status), o.Price.String(), o.Quantity.String(), + o.CreationTime.Time().UTC().Format(time.RFC1123), + o.UpdateTime.Time().UTC().Format(time.RFC1123), }, } }