Use a backtesting engine in MATLAB® to run a backtest on an investment strategy over a time series of market data. You can define a backtesting engine by using `backtestEngine`

object. A `backtestEngine`

object sets properties of the backtesting environment, such as the risk-free rate, and holds the results of the backtest. In this example, you can create a backtesting engine to run a simple backtest and examine the results.

**Create Strategy**

Define an investment strategy by using the `backtestStrategy`

function. This example builds a simple equal-weighted investment strategy that invests equally across all assets. For more information on creating backtest strategies, see `backtestStrategy`

.

strategy =
backtestStrategy with properties:
Name: "EqualWeighted"
RebalanceFcn: [function_handle]
RebalanceFrequency: 20
TransactionCosts: [0.0025 0.0050]
LookbackWindow: 0
InitialWeights: [1x0 double]

**Set Backtesting Engine Properties**

The backtesting engine has several properties that you set by using parameters to the `backtestEngine`

function.

**Risk-Free Rate**

The `RiskFreeRate`

property holds the interest rate earned for uninvested capital (that is, cash). When the sum of portfolio weights is below 1, the remaining capital is invested in cash and earns the risk-free rate. The risk-free rate and the cash-borrow rate can be defined in annualized terms or as explicit "per-time-step" interest rates. The `RatesConvention`

property is used to specify how the `backtestEngine`

interprets the two rates (the default interpretation is "Annualized"). For this example, set the risk-free rate to 2% annualized.

**Cash Borrow Rate**

The `CashBorrowRate`

property sets the interest accrual rate applied to negative cash balances. If at any time the portfolio weights sum to a value greater than 1, then the cash position is negative by the amount in excess of 1. This behavior of portfolio weights is analogous to borrowing capital on margin to invest with leverage. Like the `RiskFreeRate`

property, the `CashBorrowRate`

** **property** **can either be annualized or per-time-step depending on the value of the `RatesConvention`

property.

**Initial Portfolio Value**

The `InitialPortfolioValue`

property sets the value of the portfolio at the start of the backtest for all strategies. The default is $10,000.

**Create Backtest Engine**

Using the prepared properties, create the backtesting engine using the `backtestEngine`

function.

backtester =
backtestEngine with properties:
Strategies: [1x1 backtestStrategy]
RiskFreeRate: 0.0200
CashBorrowRate: 0.0600
RatesConvention: "Annualized"
Basis: 0
InitialPortfolioValue: 1000000
NumAssets: []
Returns: []
Positions: []
Turnover: []
BuyCost: []
SellCost: []

Several additional properties of the backtesting engine are initialized to empty. The backtesting engine populates these properties, which contain the results of the backtest, upon completion of the backtest.

**Load Data and Run Backtest**

Run the backtest over daily price data from the 30 component stocks of the DJIA.

Run the backtest using the `runBacktest`

function.

backtester =
backtestEngine with properties:
Strategies: [1x1 backtestStrategy]
RiskFreeRate: 0.0200
CashBorrowRate: 0.0600
RatesConvention: "Annualized"
Basis: 0
InitialPortfolioValue: 1000000
NumAssets: 30
Returns: [250x1 timetable]
Positions: [1x1 struct]
Turnover: [250x1 timetable]
BuyCost: [250x1 timetable]
SellCost: [250x1 timetable]

**Examine Results**

The backtesting engine populates the read-only properties of the `backtestEngine`

object with the backtest results. Daily values for portfolio returns, asset positions, turnover, and transaction costs are available to examine.

Use `equityCurve`

to plot the equity curve for the simple equal-weighted investment strategy.