From 60e4442f859f00e5dcbb758669eb832fcc378322 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 5 Oct 2021 22:06:36 +0800 Subject: [PATCH] add document for the backtest engine --- pkg/backtest/exchange.go | 27 +++++++++++++++++++++++++++ pkg/backtest/stream.go | 1 + 2 files changed, 28 insertions(+) diff --git a/pkg/backtest/exchange.go b/pkg/backtest/exchange.go index 12b214156..af29df310 100644 --- a/pkg/backtest/exchange.go +++ b/pkg/backtest/exchange.go @@ -1,3 +1,30 @@ +/* +The backtest process + +The backtest engine loads the klines from the database into a kline-channel, +there are multiple matching engine that matches the order sent from the strategy. + +for each kline, the backtest engine: + +1) load the kline, run matching logics to send out order update and trades to the user data stream. +2) once the matching process for the kline is done, the kline will be pushed to the market data stream. +3) go to 1 and load the next kline. + +There are 2 ways that a strategy could work with backtest engine: + +1. the strategy receives kline from the market data stream, and then it submits the order by the given market data to the backtest engine. + backtest engine receives the order and then pushes the trade and order updates to the user data stream. + + the strategy receives the trade and update its position. + +2. the strategy places the orders when it starts. (like grid) the strategy then receives the order updates and then submit a new order + by its order update message. + +We need to ensure that: + +1. if the strategy submits the order from the market data stream, since it's a separate goroutine, the strategy should block the backtest engine + to process the trades before the next kline is published. +*/ package backtest import ( diff --git a/pkg/backtest/stream.go b/pkg/backtest/stream.go index 01169a4b5..aac2e98d0 100644 --- a/pkg/backtest/stream.go +++ b/pkg/backtest/stream.go @@ -93,6 +93,7 @@ func (s *Stream) Connect(ctx context.Context) error { continue } + // here we generate trades and order updates matching.processKLine(k) numKlines++ }