Define your trading logic within the handleData() function.
Define your trading logic within the handleData() function. This function is called by the backtest engine repeatedly, once at each time step / tick.
Variables that are declared in this function are reset with each time step loop. Therefore, if you need a variable that maintains state across time steps, declare such variable outside this function.
Define any initialization logic within the initialize() function.
Define any initialization logic within the initialize() function. This function is called by the backtest engine once, before handleData() is invoked.
View / update the backtest settings, such as initial cash balance, margining type, slippage parameters, etc.
View / update the backtest settings, such as initial cash balance, margining type, slippage parameters, etc..
Get security / instrument by its symbol
Get security / instrument by its symbol
Get call / put option associated with underlying symbol
Get call / put option associated with underlying symbol
Get portfolio statistics, such as cost basis, market value, etc.
Get portfolio statistics, such as cost basis, market value, etc..
Print the argument.
Print the argument. Logs will appear in the Logs Tab Panel. Useful for debugging your strategy
Pause execution of backtest for X milliseconds.
Pause execution of backtest for X milliseconds. (Limit 5000 milliseconds)
Plot various time series data, such as technical studies (Moving Averages, Bollinger Bands, Portfolio Profit / Loss, etc.
Plot various time series data, such as technical studies (Moving Averages, Bollinger Bands, Portfolio Profit / Loss, etc...) Specify different types of plots (Line, Area, Candlestick, etc..) and whether to plot on Upper Panel or Lower Panels
For example:
//lookup Nasdaq 100 Index (NDX) val ndx = this.getInstrument ("^NDX"); // NDX 50 period Moving Average val movingAvg50 = ndx.studies.MovingAverage(numPeriods=50) // plot 50 day moving average using Line chart on lower panel this.plot(plotItem=movingAvg50,plotName="50 day",plotType=PlotTypeT.LINE, PlotPanelT.LOWER_PANEL)
Plot various time series data, such as technical studies (Moving Averages, Bollinger Bands, Portfolio Profit / Loss, etc.
Plot various time series data, such as technical studies (Moving Averages, Bollinger Bands, Portfolio Profit / Loss, etc...)
For example:
//lookup Nasdaq 100 Index (NDX) val ndx = this.getInstrument ("^NDX"); // NDX 50 period Moving Average val movingAvg50 = ndx.studies.MovingAverage(numPeriods=50) // plot 50 day moving average this.plot(plotItem=movingAvg50,plotName="50 period moving average")
Plot attributes of a sequence of positions, such as gainLoss, delta, etc.
Plot attributes of a sequence of positions, such as gainLoss, delta, etc. Useful when you have selected a list of positions, using "selectPosition" API
For example:
var positions= this.selectPosition(orderID="CoveredCall") this.plotPositions(positions, "MyCoveredCalls", "gainLoss")
Select the portfolio
Select the portfolio
Select a position by its Order ID.
Select a position by its Order ID. Returns a sequence of zero or more matching positions.
To select by Order ID, be sure to specify the order ID when you buy / sell an order.
val cc = ndx.strategies.withStock.CoveredCall cc.buy(quantity=10,orderID="vertical1001",comments="Enter CoveredCall When 50 Day MVG crosses above 200 Day MVG") ..... ..... // select the covered call order using the orderID val coveredCall = this.selectPosition(orderID="vertical1001")
Close the specified position
Close the specified position
Close the position by its Position ID
Close the position by its Position ID
All backtest strategies should extend / inherit the StrategyRunnerT class. This class sets up the test environment in which a backtest will be run. This class provides several helper functions that will allow you to define your desired trading strategies.
You will need to implement the handleData() function. The handleData() function is the function where you define your trading logic. This function will be called repeatedly, once with each timestep / tick of the data.
The initialize() function is optional and can be used to initialize any class variables. This function is invoked only once, before the handleData() function is invoked.
For example: