backtest

package
v1.18.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2021 License: MIT Imports: 14 Imported by: 3

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:

  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.

  1. 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.

Index

Constants

View Source
const DefaultFeeRate = 0.15 * 0.001

DefaultFeeRate set the fee rate for most cases BINANCE uses 0.1% for both maker and taker

for BNB holders, it's 0.075% for both maker and taker

MAX uses 0.050% for maker and 0.15% for taker

Variables

View Source
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, srv *service.BacktestService, config *bbgo.Backtest) *Exchange

func (Exchange) CancelOrders

func (e Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error

func (*Exchange) Done

func (e *Exchange) Done() chan struct{}

func (Exchange) Name

func (e Exchange) Name() types.ExchangeName

func (*Exchange) NewStream

func (e *Exchange) NewStream() types.Stream

func (Exchange) PlatformFeeCurrency

func (e Exchange) PlatformFeeCurrency() string

func (Exchange) QueryAccount

func (e Exchange) QueryAccount(ctx context.Context) (*types.Account, error)

func (*Exchange) QueryAccountBalances

func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error)

func (Exchange) QueryClosedOrders

func (e Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error)

func (Exchange) QueryDepositHistory

func (e Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error)

func (Exchange) QueryKLines

func (e Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error)

func (Exchange) QueryMarkets

func (e Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error)

func (Exchange) QueryOpenOrders

func (e Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error)

func (Exchange) QueryTicker added in v1.11.1

func (e Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error)

func (Exchange) QueryTickers added in v1.11.0

func (e Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error)

func (Exchange) QueryTrades

func (e Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error)

func (Exchange) QueryWithdrawHistory

func (e Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error)

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

	MakerCommission fixedpoint.Value `json:"makerCommission"`
	TakerCommission fixedpoint.Value `json:"takerCommission"`
	// 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 (m *SimplePriceMatching) CancelOrder(o types.Order) (types.Order, error)

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)

type Stream

type Stream struct {
	types.StandardStream
	// contains filtered or unexported fields
}

func (*Stream) Close

func (s *Stream) Close() error

func (*Stream) Connect

func (s *Stream) Connect(ctx context.Context) error

func (*Stream) SetPublicOnly added in v1.5.0

func (s *Stream) SetPublicOnly()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL