Documentation ¶
Overview ¶
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:
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.
- 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:
- 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.
Index ¶
- Variables
- type Exchange
- func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error
- func (e *Exchange) CloseMarketData() error
- func (e *Exchange) ConsumeKLine(k types.KLine)
- func (e *Exchange) GetMarketData() (chan types.KLine, error)
- func (e *Exchange) InitMarketData()
- func (e *Exchange) Name() types.ExchangeName
- func (e *Exchange) NewStream() types.Stream
- func (e *Exchange) PlatformFeeCurrency() string
- func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error)
- func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error)
- func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error)
- func (e Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error)
- func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, ...) ([]types.KLine, error)
- func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error)
- func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error)
- func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error)
- func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error)
- func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error)
- func (e Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error)
- func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)
- type PriceOrder
- type PriceOrderSlice
- func (slice PriceOrderSlice) Find(price fixedpoint.Value, descending bool) (pv PriceOrder, idx int)
- func (slice PriceOrderSlice) First() (PriceOrder, bool)
- func (slice PriceOrderSlice) InsertAt(idx int, po PriceOrder) PriceOrderSlice
- func (slice PriceOrderSlice) Len() int
- func (slice PriceOrderSlice) Less(i, j int) bool
- func (slice PriceOrderSlice) Remove(price fixedpoint.Value, descending bool) PriceOrderSlice
- func (slice PriceOrderSlice) Swap(i, j int)
- func (slice PriceOrderSlice) Upsert(po PriceOrder, descending bool) PriceOrderSlice
- type SimplePriceMatching
- func (m *SimplePriceMatching) BuyToPrice(price fixedpoint.Value) (closedOrders []types.Order, trades []types.Trade)
- func (m *SimplePriceMatching) CancelOrder(o types.Order) (types.Order, error)
- func (m *SimplePriceMatching) EmitBalanceUpdate(balances types.BalanceMap)
- func (m *SimplePriceMatching) EmitOrderUpdate(order types.Order)
- func (m *SimplePriceMatching) EmitTradeUpdate(trade types.Trade)
- func (m *SimplePriceMatching) OnBalanceUpdate(cb func(balances types.BalanceMap))
- func (m *SimplePriceMatching) OnOrderUpdate(cb func(order types.Order))
- func (m *SimplePriceMatching) OnTradeUpdate(cb func(trade types.Trade))
- func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *types.Order, trades *types.Trade, err error)
- func (m *SimplePriceMatching) SellToPrice(price fixedpoint.Value) (closedOrders []types.Order, trades []types.Trade)
- type Stream
Constants ¶
This section is empty.
Variables ¶
var ErrUnimplemented = errors.New("unimplemented method")
Functions ¶
This section is empty.
Types ¶
type Exchange ¶
type Exchange struct {
// contains filtered or unexported fields
}
func NewExchange ¶
func NewExchange(sourceName types.ExchangeName, sourceExchange types.Exchange, srv *service.BacktestService, config *bbgo.Backtest) (*Exchange, error)
func (*Exchange) CancelOrders ¶
func (*Exchange) CloseMarketData ¶ added in v1.28.0
func (*Exchange) ConsumeKLine ¶ added in v1.28.0
func (*Exchange) GetMarketData ¶ added in v1.28.0
func (*Exchange) InitMarketData ¶ added in v1.28.0
func (e *Exchange) InitMarketData()
func (*Exchange) Name ¶
func (e *Exchange) Name() types.ExchangeName
func (*Exchange) PlatformFeeCurrency ¶
func (*Exchange) QueryAccount ¶
func (*Exchange) QueryAccountBalances ¶
func (*Exchange) QueryClosedOrders ¶
func (Exchange) QueryDepositHistory ¶
func (*Exchange) QueryKLines ¶
func (*Exchange) QueryMarkets ¶
func (*Exchange) QueryOpenOrders ¶
func (*Exchange) QueryTicker ¶ added in v1.11.1
func (*Exchange) QueryTickers ¶ added in v1.11.0
func (*Exchange) QueryTrades ¶
func (Exchange) QueryWithdrawHistory ¶
func (*Exchange) SubmitOrders ¶
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)
type PriceOrder ¶
type PriceOrder struct { Price fixedpoint.Value Order types.Order }
type PriceOrderSlice ¶
type PriceOrderSlice []PriceOrder
func (PriceOrderSlice) Find ¶
func (slice PriceOrderSlice) Find(price fixedpoint.Value, descending bool) (pv PriceOrder, idx int)
FindPriceVolumePair finds the pair by the given price, this function is a read-only operation, so we use the value receiver to avoid copy value from the pointer If the price is not found, it will return the index where the price can be inserted at. true for descending (bid orders), false for ascending (ask orders)
func (PriceOrderSlice) First ¶
func (slice PriceOrderSlice) First() (PriceOrder, bool)
func (PriceOrderSlice) InsertAt ¶
func (slice PriceOrderSlice) InsertAt(idx int, po PriceOrder) PriceOrderSlice
func (PriceOrderSlice) Len ¶
func (slice PriceOrderSlice) Len() int
func (PriceOrderSlice) Less ¶
func (slice PriceOrderSlice) Less(i, j int) bool
func (PriceOrderSlice) Remove ¶
func (slice PriceOrderSlice) Remove(price fixedpoint.Value, descending bool) PriceOrderSlice
func (PriceOrderSlice) Swap ¶
func (slice PriceOrderSlice) Swap(i, j int)
func (PriceOrderSlice) Upsert ¶
func (slice PriceOrderSlice) Upsert(po PriceOrder, descending bool) PriceOrderSlice
type SimplePriceMatching ¶
type SimplePriceMatching struct { Symbol string Market types.Market LastPrice fixedpoint.Value LastKLine types.KLine CurrentTime time.Time Account *types.Account // contains filtered or unexported fields }
SimplePriceMatching implements a simple kline data driven matching engine for backtest
func (*SimplePriceMatching) BuyToPrice ¶
func (m *SimplePriceMatching) BuyToPrice(price fixedpoint.Value) (closedOrders []types.Order, trades []types.Trade)
func (*SimplePriceMatching) CancelOrder ¶
func (*SimplePriceMatching) EmitBalanceUpdate ¶
func (m *SimplePriceMatching) EmitBalanceUpdate(balances types.BalanceMap)
func (*SimplePriceMatching) EmitOrderUpdate ¶
func (m *SimplePriceMatching) EmitOrderUpdate(order types.Order)
func (*SimplePriceMatching) EmitTradeUpdate ¶
func (m *SimplePriceMatching) EmitTradeUpdate(trade types.Trade)
func (*SimplePriceMatching) OnBalanceUpdate ¶
func (m *SimplePriceMatching) OnBalanceUpdate(cb func(balances types.BalanceMap))
func (*SimplePriceMatching) OnOrderUpdate ¶
func (m *SimplePriceMatching) OnOrderUpdate(cb func(order types.Order))
func (*SimplePriceMatching) OnTradeUpdate ¶
func (m *SimplePriceMatching) OnTradeUpdate(cb func(trade types.Trade))
func (*SimplePriceMatching) PlaceOrder ¶
func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (closedOrders *types.Order, trades *types.Trade, err error)
func (*SimplePriceMatching) SellToPrice ¶
func (m *SimplePriceMatching) SellToPrice(price fixedpoint.Value) (closedOrders []types.Order, trades []types.Trade)