fix: taker price, matching engine kline emit order and process order, nan in sortino and sharpe

This commit is contained in:
zenix 2022-08-29 14:11:02 +09:00
parent f17249ba89
commit 1eb03c3dba
7 changed files with 36 additions and 31 deletions

View File

@ -180,10 +180,20 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *ty
order := m.newOrder(o, orderID)
if isTaker {
var price fixedpoint.Value
if order.Type == types.OrderTypeMarket {
order.Price = m.Market.TruncatePrice(m.LastPrice)
price = order.Price
} else if order.Type == types.OrderTypeLimit {
order.AveragePrice = m.Market.TruncatePrice(m.LastPrice)
if m.LastKLine.High.Compare(order.Price) > 0 && order.Side == types.SideTypeBuy {
order.AveragePrice = order.Price
} else if m.LastKLine.Low.Compare(order.Price) < 0 && order.Side == types.SideTypeSell {
order.AveragePrice = order.Price
} else {
order.AveragePrice = m.Market.TruncatePrice(m.LastPrice)
}
price = order.AveragePrice
}
// emit the order update for Status:New
@ -193,7 +203,7 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *ty
var order2 = order
// emit trade before we publish order
trade := m.newTradeFromOrder(&order2, false, m.Market.TruncatePrice(m.LastPrice))
trade := m.newTradeFromOrder(&order2, false, price)
m.executeTrade(trade)
// unlock the rest balances for limit taker
@ -627,6 +637,7 @@ func (m *SimplePriceMatching) processKLine(kline types.KLine) {
m.buyToPrice(kline.Open)
}
}
m.LastKLine = kline
switch kline.Direction() {
case types.DirectionDown:
@ -658,8 +669,6 @@ func (m *SimplePriceMatching) processKLine(kline types.KLine) {
m.buyToPrice(kline.Close)
}
}
m.LastKLine = kline
}
func (m *SimplePriceMatching) newOrder(o types.SubmitOrder, orderID uint64) types.Order {

View File

@ -79,8 +79,8 @@ type SessionSymbolReport struct {
InitialBalances types.BalanceMap `json:"initialBalances,omitempty"`
FinalBalances types.BalanceMap `json:"finalBalances,omitempty"`
Manifests Manifests `json:"manifests,omitempty"`
Sharpe float64 `json:"sharpeRatio"`
Sortino float64 `json:"sortinoRatio"`
Sharpe fixedpoint.Value `json:"sharpeRatio"`
Sortino fixedpoint.Value `json:"sortinoRatio"`
}
func (r *SessionSymbolReport) InitialEquityValue() fixedpoint.Value {
@ -119,16 +119,16 @@ func (r *SessionSymbolReport) Print(wantBaseAssetBaseline bool) {
color.Red("ASSET DECREASED: %v %s (%s)", finalQuoteAsset.Sub(initQuoteAsset), r.Market.QuoteCurrency, finalQuoteAsset.Sub(initQuoteAsset).Div(initQuoteAsset).FormatPercentage(2))
}
if r.Sharpe > 0.0 {
color.Green("REALIZED SHARPE RATIO: %.4f", r.Sharpe)
if r.Sharpe.Sign() > 0 {
color.Green("REALIZED SHARPE RATIO: %s", r.Sharpe.FormatString(4))
} else {
color.Red("REALIZED SHARPE RATIO: %.4f", r.Sharpe)
color.Red("REALIZED SHARPE RATIO: %s", r.Sharpe.FormatString(4))
}
if r.Sortino > 0.0 {
color.Green("REALIZED SORTINO RATIO: %.4f", r.Sortino)
if r.Sortino.Sign() > 0 {
color.Green("REALIZED SORTINO RATIO: %s", r.Sortino.FormatString(4))
} else {
color.Red("REALIZED SORTINO RATIO: %.4f", r.Sortino)
color.Red("REALIZED SORTINO RATIO: %s", r.Sortino.FormatString(4))
}
if wantBaseAssetBaseline {

View File

@ -4,7 +4,6 @@ import (
"bufio"
"context"
"fmt"
"math"
"os"
"path/filepath"
"sort"
@ -25,6 +24,7 @@ import (
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/data/tsv"
"github.com/c9s/bbgo/pkg/exchange"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
@ -135,15 +135,15 @@ var BacktestCmd = &cobra.Command{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var now = time.Now()
var now = time.Now().Local()
var startTime, endTime time.Time
startTime = userConfig.Backtest.StartTime.Time()
startTime = userConfig.Backtest.StartTime.Time().Local()
// set default start time to the past 6 months
// userConfig.Backtest.StartTime = now.AddDate(0, -6, 0).Format("2006-01-02")
if userConfig.Backtest.EndTime != nil {
endTime = userConfig.Backtest.EndTime.Time()
endTime = userConfig.Backtest.EndTime.Time().Local()
} else {
endTime = now
}
@ -622,16 +622,8 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
Market: market,
}
finiteRatio := func(ratio float64) float64 {
if math.IsInf(ratio, 1) {
return 99999.99
} else if math.IsInf(ratio, -1) {
return -99999.99
}
return ratio
}
sharpeRatio := finiteRatio(intervalProfit.GetSharpe())
sortinoRatio := finiteRatio(intervalProfit.GetSortino())
sharpeRatio := fixedpoint.NewFromFloat(intervalProfit.GetSharpe())
sortinoRatio := fixedpoint.NewFromFloat(intervalProfit.GetSortino())
report := calculator.Calculate(symbol, trades, lastPrice)
accountConfig := userConfig.Backtest.GetAccount(session.Exchange.Name().String())

View File

@ -210,10 +210,12 @@ func (s *BacktestService) QueryKLinesCh(since, until time.Time, exchange types.E
tableName := targetKlineTable(exchange.Name())
var query string
// need to sort by start_time desc in order to let matching engine process 1m first
// otherwise any other close event could peek on the final close price
if len(symbols) == 1 {
query = "SELECT * FROM `binance_klines` WHERE `end_time` BETWEEN :since AND :until AND `symbol` = :symbols AND `interval` IN (:intervals) ORDER BY end_time ASC"
query = "SELECT * FROM `binance_klines` WHERE `end_time` BETWEEN :since AND :until AND `symbol` = :symbols AND `interval` IN (:intervals) ORDER BY end_time ASC, start_time DESC"
} else {
query = "SELECT * FROM `binance_klines` WHERE `end_time` BETWEEN :since AND :until AND `symbol` IN (:symbols) AND `interval` IN (:intervals) ORDER BY end_time ASC"
query = "SELECT * FROM `binance_klines` WHERE `end_time` BETWEEN :since AND :until AND `symbol` IN (:symbols) AND `interval` IN (:intervals) ORDER BY end_time ASC, start_time DESC"
}
query = strings.ReplaceAll(query, "binance_klines", tableName)

View File

@ -1,2 +0,0 @@
package strategy

View File

@ -754,6 +754,9 @@ func Stdev(a Series, params ...int) float64 {
diff := a.Index(i) - avg
s += diff * diff
}
if length-ddof == 0 {
return 0
}
return math.Sqrt(s / float64(length-ddof))
}

View File

@ -2,10 +2,11 @@ package types
import (
"encoding/json"
"log"
"math"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/datatype/floats"