Backtest Documentation

Intent

Backtest.jl exists in order to provide a straightforward interface for performing backtests in Julia. Moreover, I (Max Holloway) really like some of the ideas from the python framework Backtrader, and I wanted to make my own (better, simpler) version of it in Julia.

Core Ideas

In order to get up and running, one only needs to know how to make a DataReader and how to fill in StrategyOptions. After that, you can run your backtest! To add to the backtest, you will need to provide what are called "user-defined functions". These functions allow one to perform behavior, such as ordering shares or calculating metrics derived from FieldOperations.

Functions

Super Important Functions

Backtest.runFunction
run(stratoptions::StrategyOptions)::Strategy

Run a backtest and obtain the completed strategy object.

Arguments

  • stratoptions::StrategyOptions: all of the options to be used for the backtest

Returns

  • Strategy: a completed Strategy object
source
Backtest.StrategyOptionsMethod
StrategyOptions(; kwargs...)::StrategyOptions

Keyword Arguments

Required

  • datareaders::Dict{AssetId, <:AbstractDataReader}: data source for each asset
  • fieldoperations::Vector{<:AbstractFieldOperation}: field operations to be performed
  • start::{<:Dates.TimeType}: start time for the backtest (this is the DateTime of the first bar of data to be read; actions start one bar later)
  • endtime::{<:Dates.TimeType}: end time for the backtest

Optional

  • numlookbackbars::Integer: number of backtest bars to store; if -1, then all data is stored; if space is an issue, this can be changed to a positive #. However, this will limit how much data can be accessed.
  • tradinginterval::{<:Dates.TimePeriod}: how much time there is between the start of a bar
  • verbosity:Type: how much verbosity the backtest should have; INFO gives the most messages, and NOVERBOSITY gives the fewest
  • datadelay::{<:Dates.Period}: how much time transpires at the beginning of a bar before data is received; e.g. if this is 5 seconds, then data will be received by the backtest 5 seconds after the bar starts.
  • messagelatency::{<:Dates.Period}: how much time it takes to transmit a message to a brokerage/exchange
  • fieldoptimeout::{<:Dates.Period}: how much time until the field operation computatio times out; note that field operations are computed before the user receives data
  • datetimecol::String: name of datetime column
  • opencol::String: name of open column
  • highcol::String: name of high column
  • lowcol::String: name of low column
  • closecol::String: name of close column
  • volumecol::String: name of volume column
  • ondataevent::Function: user-defined function that performs logic when data is received
  • onorderevent::Function: user-defined function that performs logic when an order event is received
  • principal::{<:Dates.Period}: starting amount of buying power; in many cases this will be interpreted as a starting cash value
source
Backtest.order!Function
order!(strat::Strategy, order::OT)::OrderId where {OT<:Orders.AbstractOrder}

Place an order and obtain its orderid.

Arguments

  • strat::Strategy: the strategy object running that invokes this method
  • order::{<:Orders.AbstractOrder}:

Returns

  • OrderId: identifier for the placed order.
source

Accessor Functions

Backtest.Engine.numbarsavailableFunction
numbarsavailable(strat::Strategy)::Integer

Arguments

  • strat::Strategy: the strategy object running that invokes this method

Returns

  • Integer: the number of bars available from which we can retrieve data.
source
Backtest.Engine.dataFunction
data(strat::Strategy, ago::Integer)::NamedArray

Arguments

  • strat::Strategy: the strategy object running when this method is invoked
  • ago::Integer: the number of bars before the previous bar; if ago = 0, this accesses the previous bar's data

Returns

  • NamedArray: [assetid, fieldid]->value pairs for ago bars ago.
source
data(strat::Strategy, ago::Integer, fieldid::FieldId)::NamedArray

Arguments

  • strat::Strategy: the strategy object running when this method is invoked
  • ago::Integer: the number of bars before the previous bar; if ago = 0, this accesses the previous bar's data
  • fieldid::FieldId: Note: FieldId is an alias for String

Returns

  • NamedArray: [fieldid]->value pairs for ago bars ago.
source
data(strat::Strategy, ago::Integer, assetid::AssetId, fieldid::FieldId)

Arguments

  • strat::Strategy: the strategy object running when this method is invoked
  • ago::Integer: the number of bars before the previous bar; if ago = 0, this accesses the previous bar's data
  • assetid::AssetId: Note: AssetId is an alias for String
  • fieldid::FieldId: Note: FieldId is an alias for String

Returns

  • {<:Any}: Object associated ago bars ago with this assetid and fieldid
source
data(strat::Strategy)::NamedArray

Arguments

  • strat::Strategy: the strategy object running when this method is invoked

Returns

  • NamedArray: [assetid, fieldid]->value pairs for the previous bar
source
data(strat::Strategy, fieldid::FieldId)::NamedArray

Arguments

  • strat::Strategy: the strategy object running when this method is invoked
  • fieldid::FieldId: Note: FieldId is an alias for String

Returns:

  • NamedArray: assetid->value pairs for the previous bar.
source
data(strat::Strategy, assetid::AssetId, fieldid::FieldId)

Arguments

  • strat::Strategy: the strategy object running when this method is invoked
  • assetid::AssetId: Note: AssetId is an alias for String
  • fieldid::FieldId: Note: FieldId is an alias for String

Returns

  • {<:Any}: The object associated with assetid and fieldid for the previous bar.
source

Other Functions

Backtest.logFunction
log(strat::Strategy, message::String, verbosity::Type)

Write message to the console.

Arguments

  • strat::Strategy: the strategy object running that invokes this method
  • message::String: message to be written to console
  • verbosity::Type: the verbosity level to be used; see Verbosity Levels
source

Types

High-Level Types

The following are types that all users must understand when running a backtest.

Verbosity Types

These types allow users to specify how much verbosity they want in their backtests. They are ordered here from most to least verbose.

Backtest.AbstractVerbosityType

Top of the verbosity type hierarchy. In practice, this is not intended to be a user-facing type, however it can be used if a user wants to log every possible message in the backtest.

source
Backtest.INFOType

Log all messages at the INFO level or below. This is a lot of verbosity. Supertype: AbstractVerbosity

source
Backtest.WARNINGType

Log all messages that give warnings or important information. This is very low verbosity. Supertype: TRANSACTIONS

source

Low-Level Types

The following are types that users should never have to access directly.

Backtest.StrategyType

Container for all backtest run-time state. Strategy objects should not be modified within user-provided functions.

source