backtest

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 6 Imported by: 0

README

Backtest

Warning: This is "alpha" software. Do not use unless you have personally reviewed the code.

An "opinionless" backtesting package written in Go.

This package won't fetch or load candlestick data for you. It doesn't offer indicators.

You'll need to bring your own data and indicators.

Example

See the example.

Contributing

Please open an issue for discussion before sending a PR.

Bugs

Probably.

Enhancements

  • equity curve
  • graphs, probably.

License

The MIT License (MIT)

Copyright (c) 2022 Scott Barr

See LICENSE.md

Documentation

Index

Constants

View Source
const (
	// Long declares an Order as a long.
	Long = Direction(1)

	// Short declares an Order as a short.
	Short = Direction(-1)

	// OrderTypeLimit delcares an Order is for a market price entry.
	OrderTypeMarket = 0

	// OrderTypeLimit delcares an Order is for a Limit entry.
	OrderTypeLimit = 1

	// OrderTypeClose closes an order with a matching ID
	OrderTypeClose = 2
)
View Source
const (
	// DefaultRiskFreeReturn is based on the US 30 Year Treasury Bonds at June 2022.
	DefaultRiskFreeReturn = 0.0326
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Backtest

type Backtest struct {
	Candles        []Candle
	Strategy       Strategy
	Orders         []Order
	Positions      []Position
	Balance        float64
	RiskFreeReturn float64

	Location *time.Location
	// contains filtered or unexported fields
}

func NewBacktest

func NewBacktest(candles []Candle, strategy Strategy) *Backtest

func (*Backtest) Run

func (b *Backtest) Run() Result

type CSVRenderer added in v0.0.7

type CSVRenderer struct {
	Header bool
}

func NewCSVRenderer added in v0.0.7

func NewCSVRenderer(header bool) *CSVRenderer

func (*CSVRenderer) Render added in v0.0.7

func (f *CSVRenderer) Render(r Result) string

type Candle

type Candle struct {
	Open   float64
	High   float64
	Low    float64
	Close  float64
	Volume float64
	Time   int64
}

type Direction

type Direction int

type Order

type Order struct {
	ID          int64
	Type        int
	Direction   Direction
	RiskPercent float64
	Entry       float64
	StopLoss    float64
	Target      float64
	CreatedAt   int64
}

Order is set with the intention of opening a Position.

type Position

type Position struct {
	Order      Order
	Risk       float64
	Close      float64
	Profit     float64
	RiskReward float64
	FilledAt   int64
	ClosedAt   int64
}

type Renderer added in v0.0.7

type Renderer interface {
	Render(Result) string
}

type Result

type Result struct {
	Balance     float64
	NetProfit   float64
	ROIPercent  float64
	TradeCount  int
	Wins        int
	Losses      int
	WinRate     float64
	MaxDrawdown float64
	MaxValue    float64
	SharpeRatio float64
	Trades      []Position
}

func (*Result) String

func (r *Result) String() string

func (*Result) Summary added in v0.0.7

func (r *Result) Summary() []string

type Strategy

type Strategy interface {
	Tick(Candle) []Order
}

Strategy is the interface needed to run a backtest.

type TableRenderer added in v0.0.7

type TableRenderer struct {
	Header bool
}

func NewTableRenderer added in v0.0.7

func NewTableRenderer(header bool) *TableRenderer

func (*TableRenderer) Render added in v0.0.7

func (f *TableRenderer) Render(r Result) string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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