Skip to content

Commit bd08c32

Browse files
committed
Update
1 parent 55355bb commit bd08c32

File tree

6 files changed

+96
-12
lines changed

6 files changed

+96
-12
lines changed

backtest/backtest.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Backtest struct {
4242
symbol string
4343
strategy Strategy
4444
exchanges []ExchangeSim
45+
baseOutputDir string
4546
outputDir string
4647
exchangeLogFiles []string // 撮合日志记录文件
4748

@@ -83,12 +84,12 @@ func init() {
8384
// outputDir: 日志输出目录
8485
func NewBacktest(data *dataloader.Data, symbol string, start time.Time, end time.Time, strategy Strategy, exchanges []ExchangeSim, outputDir string) *Backtest {
8586
b := &Backtest{
86-
data: data,
87-
symbol: symbol,
88-
start: start,
89-
end: end,
90-
strategy: strategy,
91-
outputDir: outputDir,
87+
data: data,
88+
symbol: symbol,
89+
start: start,
90+
end: end,
91+
strategy: strategy,
92+
baseOutputDir: outputDir,
9293
}
9394
b.exchanges = exchanges
9495
var exs []Exchange
@@ -122,12 +123,12 @@ func (b *Backtest) Run() {
122123

123124
SetIdGenerate(utils.NewIdGenerate(b.start))
124125

125-
err := os.MkdirAll(b.outputDir, os.ModePerm)
126+
err := os.MkdirAll(b.baseOutputDir, os.ModePerm)
126127
if err != nil {
127128
panic(err)
128129
}
129130

130-
b.outputDir = filepath.Join(b.outputDir, time.Now().Format("20060102150405"))
131+
b.outputDir = filepath.Join(b.baseOutputDir, time.Now().Format("20060102150405"))
131132

132133
logger := NewBtLogger(b,
133134
filepath.Join(b.outputDir, "result.log"),

backtest/logger.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ func NewBtLogger(gct GetCurrentTime, path string, level string, jsonFormat bool,
5050
}
5151

5252
func (l *BtLogger) build() {
53-
writeSyncer := []zapcore.WriteSyncer{
54-
zapcore.AddSync(l.createLumberjackHook()),
53+
var writeSyncer []zapcore.WriteSyncer
54+
55+
if l.Path != "" {
56+
writeSyncer = append(writeSyncer, zapcore.AddSync(l.createLumberjackHook()))
5557
}
5658

5759
if l.Stdout {

exchangelogger.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package crex
2+
3+
// EmptyExchangeLogger 交易所撮合日志
4+
type EmptyExchangeLogger struct {
5+
}
6+
7+
func (l *EmptyExchangeLogger) Debug(args ...interface{}) {
8+
9+
}
10+
11+
func (l *EmptyExchangeLogger) Debugf(template string, args ...interface{}) {
12+
13+
}
14+
15+
func (l *EmptyExchangeLogger) Debugw(msg string, keysAndValues ...interface{}) {
16+
17+
}
18+
19+
func (l *EmptyExchangeLogger) Info(args ...interface{}) {
20+
21+
}
22+
23+
func (l *EmptyExchangeLogger) Infof(template string, args ...interface{}) {
24+
25+
}
26+
27+
func (l *EmptyExchangeLogger) Infow(msg string, keysAndValues ...interface{}) {
28+
29+
}
30+
31+
func (l *EmptyExchangeLogger) Warn(args ...interface{}) {
32+
33+
}
34+
35+
func (l *EmptyExchangeLogger) Warnf(template string, args ...interface{}) {
36+
37+
}
38+
39+
func (l *EmptyExchangeLogger) Warnw(msg string, keysAndValues ...interface{}) {
40+
41+
}
42+
43+
func (l *EmptyExchangeLogger) Error(args ...interface{}) {
44+
45+
}
46+
47+
func (l *EmptyExchangeLogger) Errorf(template string, args ...interface{}) {
48+
49+
}
50+
51+
func (l *EmptyExchangeLogger) Errorw(msg string, keysAndValues ...interface{}) {
52+
53+
}
54+
55+
func (l *EmptyExchangeLogger) Sync() {
56+
57+
}

exchanges/bitmexsim/bitmexsim_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import (
55
)
66

77
func testExchange() Exchange {
8-
return NewBitMEXSim(nil, 10000, 0, 0)
8+
return NewBitMEXSim(nil, 10000, -0.00025, 0.00075)
99
}

exchanges/deribitsim/deribitsim.go

+4
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ func (b *DeribitSim) RunEventLoopOnce() (err error) {
619619
}
620620

621621
func (b *DeribitSim) logOrderInfo(msg string, event string, order *Order) {
622+
if b.eLog == nil {
623+
return
624+
}
625+
622626
ob := b.data.GetOrderBook()
623627
position := b.getPosition(order.Symbol)
624628
b.eLog.Infow(msg,

exchanges/deribitsim/deribitsim_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@ package deribitsim
22

33
import (
44
. "github.com/coinrust/crex"
5+
"github.com/coinrust/crex/dataloader"
56
"github.com/coinrust/crex/math"
7+
"github.com/coinrust/crex/utils"
68
"testing"
9+
"time"
710
)
811

912
func testExchange() Exchange {
10-
return NewDeribitSim(nil, 10000, 0, 0)
13+
start, _ := time.Parse("2006-01-02 15:04:05", "2019-10-01 00:00:00")
14+
end, _ := time.Parse("2006-01-02 15:04:05", "2019-10-02 00:00:00")
15+
SetIdGenerate(utils.NewIdGenerate(start))
16+
data := dataloader.NewCsvData("../../data-samples/deribit/deribit_BTC-PERPETUAL_and_futures_tick_by_tick_book_snapshots_10_levels_2019-10-01_2019-11-01.csv")
17+
data.Reset(start, end)
18+
ex := NewDeribitSim(data, 10000, -0.00025, 0.00075)
19+
ex.SetExchangeLogger(&EmptyExchangeLogger{})
20+
return ex
21+
}
22+
23+
func TestReduceOrder(t *testing.T) {
24+
ex := testExchange()
25+
order, err := ex.PlaceOrder("BTC-PERPETUAL", Buy, OrderTypeMarket, 3000, 10)
26+
if err != nil {
27+
t.Error(err)
28+
return
29+
}
30+
t.Logf("%#v", order)
1131
}
1232

1333
// 计算公式:

0 commit comments

Comments
 (0)