backtest

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: GPL-3.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTickSubscriptionAlreadyExists is the error for a tick subscription already existing.
	ErrTickSubscriptionAlreadyExists = errors.New("tick subscription already exists")
	// ErrInvalidExchange is the error for an invalid exchange.
	ErrInvalidExchange = errors.New("invalid exchange")
	// ErrNoDataForOrderValidation is the error for no data for order validation.
	ErrNoDataForOrderValidation = errors.New("no data for order validation")
	// ErrStartAfterEnd is the error for starting after ending.
	ErrStartAfterEnd = errors.New("start after end")
	// ErrInvalidPricePeriod is the error for an invalid price period.
	ErrInvalidPricePeriod = errors.New("invalid price period")
)
View Source
var (
	// ErrInvalidMode is returned when the mode is invalid.
	ErrInvalidMode = errors.New("invalid mode")
)

Functions

This section is empty.

Types

type Backtest

type Backtest struct {
	ID                  uuid.UUID                  `json:"id"`
	StartTime           time.Time                  `json:"start_time"`
	EndTime             time.Time                  `json:"end_time"`
	Mode                Mode                       `json:"mode"`
	PricePeriod         period.Symbol              `json:"price_period"`
	CurrentCandlestick  CurrentCandlestick         `json:"current_candlestick"`
	Accounts            map[string]account.Account `json:"accounts"`
	PricesSubscriptions []tick.Subscription        `json:"tick_subscriptions"`
	Orders              []order.Order              `json:"orders"`
	Callbacks           runtime.Callbacks          `json:"callbacks"`
}

Backtest is the struct for a backtest.

func New

func New(params Parameters, callbacks runtime.Callbacks) (Backtest, error)

New creates a new backtest.

func (*Backtest) AddOrder

func (bt *Backtest) AddOrder(ord order.Order, cs candlestick.Candlestick) error

AddOrder adds an order to the backtest.

func (*Backtest) Advance

func (bt *Backtest) Advance() (done bool, err error)

Advance advances the backtest to the next candlestick.

func (*Backtest) CreateTickSubscription

func (bt *Backtest) CreateTickSubscription(exchange string, pair string) (tick.Subscription, error)

CreateTickSubscription creates a new tick subscription for the backtest.

func (Backtest) CurrentTime

func (bt Backtest) CurrentTime() string

CurrentTime returns the current time of the backtest.

func (Backtest) Done

func (bt Backtest) Done() bool

Done returns true if the backtest is done.

func (Backtest) MarshalBinary

func (bt Backtest) MarshalBinary() ([]byte, error)

MarshalBinary marshals a backtest to binary data.

func (*Backtest) SetCurrentTime

func (bt *Backtest) SetCurrentTime(ts time.Time)

SetCurrentTime sets the current time of the backtest.

func (*Backtest) UnmarshalBinary

func (bt *Backtest) UnmarshalBinary(data []byte) error

UnmarshalBinary unmarshals a backtest from binary data.

type CurrentCandlestick

type CurrentCandlestick struct {
	Time  time.Time
	Price candlestick.PriceType
}

CurrentCandlestick represent the current price based on candlestick step.

type Mode

type Mode string

Mode is the mode of the backtest.

const (
	// ModeIsFullOHLC is the mode where the backtest uses full OHLC data.
	ModeIsFullOHLC Mode = "full_ohlc"
	// ModeIsCloseOHLC is the mode where the backtest uses close OHLC data.
	ModeIsCloseOHLC Mode = "close_ohlc"
)

func (Mode) Opt

func (m Mode) Opt() *Mode

Opt will return a pointer to the mode.

func (Mode) String

func (m Mode) String() string

String will return the string representation of the mode.

func (Mode) Validate

func (m Mode) Validate() error

Validate will validate the mode.

type Parameters

type Parameters struct {
	Accounts    map[string]account.Account
	StartTime   time.Time
	EndTime     *time.Time
	Mode        *Mode
	PricePeriod *period.Symbol
}

Parameters is the struct for the backtest parameters.

func (*Parameters) EmptyFieldsToDefault

func (params *Parameters) EmptyFieldsToDefault() *Parameters

EmptyFieldsToDefault sets empty fields to default values.

func (Parameters) Validate

func (params Parameters) Validate() error

Validate validates the backtest parameters.

Jump to

Keyboard shortcuts

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