Documentation ¶
Overview ¶
Package backtest provides a simple stock backtesting framework.
Index ¶
- Constants
- func CalculateSMA(i int, data []DataEventHandler) (sma float64, err error)
- type Bar
- type BarEvent
- type Casher
- type Data
- func (d *Data) History() []DataEventHandler
- func (d *Data) Latest(symbol string) DataEventHandler
- func (d *Data) List(symbol string) []DataEventHandler
- func (d *Data) Load(s []string) error
- func (d *Data) Next() (dh DataEventHandler, ok bool)
- func (d *Data) Reset()
- func (d *Data) SetStream(stream []DataEventHandler)
- func (d *Data) SortStream()
- func (d *Data) Stream() []DataEventHandler
- type DataEvent
- type DataEventHandler
- type DataHandler
- type DataLoader
- type DataStreamer
- type Directioner
- type Event
- type EventHandler
- type EventTracker
- type Exchange
- type ExecutionHandler
- type Fill
- func (f Fill) GetCommission() float64
- func (f Fill) GetCost() float64
- func (f Fill) GetDirection() string
- func (f Fill) GetExchangeFee() float64
- func (f Fill) GetPrice() float64
- func (f Fill) GetQty() int64
- func (f Fill) IsFill() bool
- func (f Fill) NetValue() float64
- func (f *Fill) SetDirection(s string)
- func (f *Fill) SetQty(i int64)
- func (f Fill) Value() float64
- type FillEvent
- type Investor
- type OnFiller
- type OnSignaler
- type Order
- type OrderEvent
- type Portfolio
- func (p Portfolio) Cash() float64
- func (p Portfolio) InitialCash() float64
- func (p Portfolio) IsInvested(symbol string) (pos position, ok bool)
- func (p Portfolio) IsLong(symbol string) (pos position, ok bool)
- func (p Portfolio) IsShort(symbol string) (pos position, ok bool)
- func (p *Portfolio) OnFill(fill FillEvent, data DataHandler) (*Fill, error)
- func (p *Portfolio) OnSignal(signal SignalEvent, data DataHandler) (*Order, error)
- func (p *Portfolio) Reset()
- func (p *Portfolio) SetCash(cash float64)
- func (p *Portfolio) SetInitialCash(initial float64)
- func (p *Portfolio) SetRiskManager(risk RiskHandler)
- func (p *Portfolio) SetSizeManager(size SizeHandler)
- func (p *Portfolio) Update(d DataEventHandler)
- func (p Portfolio) Value() float64
- type PortfolioHandler
- type Quantifier
- type Reseter
- type Resulter
- type Risk
- type RiskHandler
- type Signal
- type SignalEvent
- type Size
- type SizeHandler
- type Statistic
- func (s Statistic) Events() []EventHandler
- func (s Statistic) MaxDrawdown() float64
- func (s Statistic) MaxDrawdownDuration() (d time.Duration)
- func (s Statistic) MaxDrawdownTime() time.Time
- func (s Statistic) PrintResult()
- func (s *Statistic) Reset()
- func (s *Statistic) SharpRatio(riskfree float64) float64
- func (s *Statistic) SortinoRatio(riskfree float64) float64
- func (s Statistic) TotalEquityReturn() (r float64, err error)
- func (s *Statistic) TrackEvent(e EventHandler)
- func (s *Statistic) TrackTransaction(f FillEvent)
- func (s Statistic) Transactions() []FillEvent
- func (s *Statistic) Update(d DataEventHandler, p PortfolioHandler)
- type StatisticHandler
- type StatisticPrinter
- type StatisticUpdater
- type StrategyHandler
- type Symboler
- type Test
- func (t *Test) Reset()
- func (t *Test) Run() error
- func (t *Test) SetData(data DataHandler)
- func (t *Test) SetExchange(exchange ExecutionHandler)
- func (t *Test) SetPortfolio(portfolio PortfolioHandler)
- func (t *Test) SetStatistic(statistic StatisticHandler)
- func (t *Test) SetStrategy(strategy StrategyHandler)
- func (t *Test) SetSymbols(symbols []string)
- func (t *Test) Stats() StatisticHandler
- type Tick
- type TickEvent
- type Timer
- type TransactionTracker
- type Updater
- type Valuer
Constants ¶
const DP = 4 // DP
DP sets the the precision of rounded floating numbers used after calculations to format
Variables ¶
This section is empty.
Functions ¶
func CalculateSMA ¶ added in v0.2.1
func CalculateSMA(i int, data []DataEventHandler) (sma float64, err error)
CalculateSMA calculates the simple moving average of a given slice of data points.
Types ¶
type Bar ¶
type Bar struct { Event DataEvent Open float64 High float64 Low float64 Close float64 AdjClose float64 Volume int64 }
Bar declares an event for an OHLCV bar (Open, High, Low, Close, Volume).
func (Bar) LatestPrice ¶
LatestPrice returns the close proce of the bar event.
type BarEvent ¶
type BarEvent interface { DataEventHandler IsBar() bool }
BarEvent declares a bar event interface.
type Casher ¶
type Casher interface { SetInitialCash(float64) InitialCash() float64 SetCash(float64) Cash() float64 }
Casher handles basic portolio info
type Data ¶
type Data struct {
// contains filtered or unexported fields
}
Data is a basic data struct
func (*Data) History ¶
func (d *Data) History() []DataEventHandler
History returns the historic data stream
func (*Data) Latest ¶
func (d *Data) Latest(symbol string) DataEventHandler
Latest returns the last known data event for a symbol.
func (*Data) List ¶
func (d *Data) List(symbol string) []DataEventHandler
List returns the data event list for a symbol.
func (*Data) Load ¶
Load loads data endpoints into a stream. This method satisfies the DataLoeder interface, but should be overwritten by the specific data loading implamentation.
func (*Data) Next ¶
func (d *Data) Next() (dh DataEventHandler, ok bool)
Next returns the first element of the data stream deletes it from the stream and appends it to history
func (*Data) Reset ¶ added in v0.2.2
func (d *Data) Reset()
Reset implements the Reseter interface and rests the data struct to a clean state with loaded data points
func (*Data) SetStream ¶
func (d *Data) SetStream(stream []DataEventHandler)
SetStream sets the data stream
type DataEvent ¶
DataEvent is the basic implementation of a data event handler.
func (DataEvent) IsDataEvent ¶
IsDataEvent declares a data event
type DataEventHandler ¶
type DataEventHandler interface { EventHandler IsDataEvent() bool LatestPrice() float64 }
DataEventHandler declares a data event interface
type DataHandler ¶
type DataHandler interface { DataLoader DataStreamer Reseter }
DataHandler is the combined data interface
type DataLoader ¶
DataLoader is the interface loading the data into the data stream
type DataStreamer ¶
type DataStreamer interface { Next() (DataEventHandler, bool) Stream() []DataEventHandler History() []DataEventHandler Latest(string) DataEventHandler List(string) []DataEventHandler }
DataStreamer is the interface returning the data streams
type Directioner ¶
Directioner defines a direction interface
type Event ¶
Event is the implementation of the basic event interface.
type EventHandler ¶
EventHandler declares the basic event interface
type EventTracker ¶
type EventTracker interface { TrackEvent(EventHandler) Events() []EventHandler }
EventTracker is responsible for all event tracking during a backtest
type Exchange ¶
Exchange is a basic execution handler implementation
func (*Exchange) ExecuteOrder ¶
func (e *Exchange) ExecuteOrder(order OrderEvent, data DataHandler) (*Fill, error)
ExecuteOrder executes an order event
type ExecutionHandler ¶
type ExecutionHandler interface {
ExecuteOrder(OrderEvent, DataHandler) (*Fill, error)
}
ExecutionHandler is the basic interface for executing orders
type Fill ¶
type Fill struct { Event Exchange string // exchange symbol Direction string // BOT for buy or SLD for sell Qty int64 Price float64 Commission float64 ExchangeFee float64 Cost float64 // the total cost of the filled order incl commission and fees }
Fill declares a basic fill event
func (Fill) GetCommission ¶
GetCommission returns the Commission field of a fill.
func (Fill) GetDirection ¶
GetDirection returns the direction of a Fill
func (Fill) GetExchangeFee ¶
GetExchangeFee returns the ExchangeFee Field of a fill
func (*Fill) SetDirection ¶
SetDirection sets the Directions field of a Fill
type FillEvent ¶
type FillEvent interface { EventHandler Directioner Quantifier IsFill() bool GetPrice() float64 GetCommission() float64 GetExchangeFee() float64 GetCost() float64 Value() float64 NetValue() float64 }
FillEvent declares the fill event interface.
type Investor ¶
type Investor interface { IsInvested(string) (position, bool) IsLong(string) (position, bool) IsShort(string) (position, bool) }
Investor is an inteface to check if a portfolio has a position of a symbol
type OnFiller ¶
type OnFiller interface {
OnFill(FillEvent, DataHandler) (*Fill, error)
}
OnFiller as an intercafe for the OnFill method
type OnSignaler ¶
type OnSignaler interface {
OnSignal(SignalEvent, DataHandler) (*Order, error)
}
OnSignaler as an intercafe for the OnSignal method
type Order ¶
type Order struct { Event Direction string // buy or sell Qty int64 // quantity of the order OrderType string // market or limit Limit float64 // limit for the order }
Order declares a basic order event
func (Order) GetDirection ¶
GetDirection returns the Direction of an Order
func (*Order) SetDirection ¶
SetDirection sets the Directions field of an Order
type OrderEvent ¶
type OrderEvent interface { EventHandler Directioner Quantifier IsOrder() bool }
OrderEvent declares the order event interface.
type Portfolio ¶
type Portfolio struct {
// contains filtered or unexported fields
}
Portfolio represent a simple portfolio struct.
func (Portfolio) InitialCash ¶
InitialCash returns the initial cash value of the portfolio
func (Portfolio) IsInvested ¶
IsInvested checks if the portfolio has an open position on the given symbol
func (Portfolio) IsLong ¶
IsLong checks if the portfolio has an open long position on the given symbol
func (Portfolio) IsShort ¶
IsShort checks if the portfolio has an open short position on the given symbol
func (*Portfolio) OnFill ¶
func (p *Portfolio) OnFill(fill FillEvent, data DataHandler) (*Fill, error)
OnFill handles an incomming fill event
func (*Portfolio) OnSignal ¶
func (p *Portfolio) OnSignal(signal SignalEvent, data DataHandler) (*Order, error)
OnSignal handles an incomming signal event
func (*Portfolio) Reset ¶ added in v0.2.2
func (p *Portfolio) Reset()
Reset the portfolio into a clean state with set initial cash.
func (*Portfolio) SetInitialCash ¶
SetInitialCash sets the initial cash value of the portfolio
func (*Portfolio) SetRiskManager ¶
func (p *Portfolio) SetRiskManager(risk RiskHandler)
SetRiskManager sets the risk manager to be used with the portfolio
func (*Portfolio) SetSizeManager ¶
func (p *Portfolio) SetSizeManager(size SizeHandler)
SetSizeManager sets the size manager to be used with the portfolio
func (*Portfolio) Update ¶
func (p *Portfolio) Update(d DataEventHandler)
Update updates the holding on a data event
type PortfolioHandler ¶
PortfolioHandler is the combined interface building block for a portfolio.
type Quantifier ¶
Quantifier defines a qty interface
type Reseter ¶ added in v0.2.2
type Reseter interface {
Reset()
}
Reseter provides a resting interface.
type Resulter ¶ added in v0.2.2
type Resulter interface { TotalEquityReturn() (float64, error) MaxDrawdown() float64 MaxDrawdownTime() time.Time MaxDrawdownDuration() time.Duration SharpRatio(float64) float64 SortinoRatio(float64) float64 }
Resulter bundles all methods which return the results of the backtest
type Risk ¶
type Risk struct { }
Risk is a basic risk handler implementation
func (*Risk) EvaluateOrder ¶
func (r *Risk) EvaluateOrder(order OrderEvent, data DataEventHandler, positions map[string]position) (*Order, error)
EvaluateOrder handles the risk of an order, refines or cancel it
type RiskHandler ¶
type RiskHandler interface {
EvaluateOrder(OrderEvent, DataEventHandler, map[string]position) (*Order, error)
}
RiskHandler is the basic interface for accessing risks of a portfolio
type Signal ¶
Signal declares a basic signal event
func (Signal) GetDirection ¶
GetDirection returns the Direction of a Signal
func (*Signal) SetDirection ¶
SetDirection sets the Directions field of a Signal
type SignalEvent ¶
type SignalEvent interface { EventHandler Directioner IsSignal() bool }
SignalEvent declares the signal event interface.
type Size ¶
Size is a basic size handler implementation
func (*Size) SizeOrder ¶
func (s *Size) SizeOrder(order OrderEvent, data DataEventHandler, pf PortfolioHandler) (*Order, error)
SizeOrder adjusts the size of an order
type SizeHandler ¶
type SizeHandler interface {
SizeOrder(OrderEvent, DataEventHandler, PortfolioHandler) (*Order, error)
}
SizeHandler is the basic interface for setting the size of an order
type Statistic ¶
type Statistic struct {
// contains filtered or unexported fields
}
Statistic is a basic test statistic, which holds simple lists of historic events
func (Statistic) Events ¶
func (s Statistic) Events() []EventHandler
Events returns the complete events history
func (Statistic) MaxDrawdown ¶ added in v0.2.3
MaxDrawdown returns the maximum draw down value in percent.
func (Statistic) MaxDrawdownDuration ¶ added in v0.2.3
MaxDrawdownDuration returns the maximum draw down value in percent
func (Statistic) MaxDrawdownTime ¶ added in v0.2.3
MaxDrawdownTime returns the time of the maximum draw down value.
func (Statistic) PrintResult ¶
func (s Statistic) PrintResult()
PrintResult prints the backtest statistics to the screen
func (*Statistic) Reset ¶ added in v0.2.2
func (s *Statistic) Reset()
Reset the statistic to a clean state
func (*Statistic) SharpRatio ¶ added in v0.2.3
SharpRatio returns the Sharp ratio compared to a risk free benchmark return.
func (*Statistic) SortinoRatio ¶ added in v0.2.3
SortinoRatio returns the Sortino ratio compared to a risk free benchmark return.
func (Statistic) TotalEquityReturn ¶ added in v0.2.2
TotalEquityReturn calculates the the total return on the first and last equity point
func (*Statistic) TrackEvent ¶
func (s *Statistic) TrackEvent(e EventHandler)
TrackEvent tracks an event
func (*Statistic) TrackTransaction ¶
TrackTransaction tracks a transaction aka a fill event
func (Statistic) Transactions ¶
Transactions returns the complete events history
func (*Statistic) Update ¶
func (s *Statistic) Update(d DataEventHandler, p PortfolioHandler)
Update the complete statistics to a given data event.
type StatisticHandler ¶
type StatisticHandler interface { EventTracker TransactionTracker StatisticPrinter Reseter StatisticUpdater Resulter }
StatisticHandler is a basic statistic interface
type StatisticPrinter ¶
type StatisticPrinter interface {
PrintResult()
}
StatisticPrinter handles printing of the statistics to screen
type StatisticUpdater ¶ added in v0.2.2
type StatisticUpdater interface {
Update(DataEventHandler, PortfolioHandler)
}
StatisticUpdater handles the updateing of the statistics
type StrategyHandler ¶
type StrategyHandler interface {
CalculateSignal(DataEventHandler, DataHandler, PortfolioHandler) (SignalEvent, error)
}
StrategyHandler is a basic strategy interface
type Symboler ¶
type Symboler interface {
GetSymbol() string
}
Symboler declares the symboler interface
type Test ¶
type Test struct {
// contains filtered or unexported fields
}
Test is a basic back test struct
func (*Test) Reset ¶ added in v0.2.2
func (t *Test) Reset()
Reset rests the backtest into a clean state with loaded data
func (*Test) SetData ¶
func (t *Test) SetData(data DataHandler)
SetData sets the data provider to to be used within the test
func (*Test) SetExchange ¶
func (t *Test) SetExchange(exchange ExecutionHandler)
SetExchange sets the execution provider to to be used within the test
func (*Test) SetPortfolio ¶
func (t *Test) SetPortfolio(portfolio PortfolioHandler)
SetPortfolio sets the portfolio provider to to be used within the test
func (*Test) SetStatistic ¶
func (t *Test) SetStatistic(statistic StatisticHandler)
SetStatistic sets the statistic provider to to be used within the test
func (*Test) SetStrategy ¶
func (t *Test) SetStrategy(strategy StrategyHandler)
SetStrategy sets the strategy provider to to be used within the test
func (*Test) SetSymbols ¶
SetSymbols sets the symbols to include into the test
func (*Test) Stats ¶ added in v0.2.2
func (t *Test) Stats() StatisticHandler
Stats returns the statistic handler of the backtest
type Tick ¶
Tick declares an tick event
func (Tick) LatestPrice ¶
LatestPrice returns the middle of Bid and Ask.
type TickEvent ¶
type TickEvent interface { DataEventHandler IsTick() bool }
TickEvent declares a tick event interface.
type TransactionTracker ¶
TransactionTracker is responsible for all transaction tracking during a backtest
type Updater ¶
type Updater interface {
Update(DataEventHandler)
}
Updater handles the updating of the portfolio on data events