Unlike most user manuals and software documentation, we also try to provide useful and helpful examples that you can repurpose for your own strategies. If you don’t know how to implement your backtest, don’t hesitate to reach out to [email protected]

Contents

Implementing Strategy Logic

The backbone of BacktestXL is the BUY_IF and SELL_IF conditionals. Here, the users can the logic that will trigger buy orders (BUY_IF) and sell orders (SELL_IF). This framework is very flexible, and we included a handful of useful functions that cover most scenarios.

strat.HAS_POSITION

Description

This function returns whether we are currently holding the asset or not.

Example

If we only want to buy if we do not already hold a position, we need to add the following condition BUY_IF.

strat.HAS_POSITION == false

Make sure to use two equal signs (==)

strat.POSITION_SIZE

Description

Returns the current number of shares we are holding.

Example

If we want to incrementally buy 1 single share until we have a total of 5, we need to do the following:

In BUY_IF:

strat.POSITION_SIZE < 5

In BUY_ORDER, we will create market orders for 1 share, as follows:

strat.BUY(size=1)

strat.BARS_SINCE_TRADE

Description

Returns the number of bars that have elapsed since the most recent trade. It checks for both buy and sell trades.

Example

If we want to sell at a 3% profit or after 5 bars of buying, we could do the following in SELL_IF:

strat.HAS_POSITION == true AND (strat.BARS_SINCE_TRADE > 5 OR data[t].Close > strat.LAST_TRADE_PRICE * 1.03)

In order for BacktestXL to execute the logic as expected, it is important to use the parenthesis correctly. The previous line could be read as “I want to sell if we have a position AND (order is old OR take profit).”

strat.BARS_SINCE_BUY

Description

Similar to strat.BARS_SINCE_TRADE. Returns the number of bars that have elapsed since the most recent trade. It checks only for buy trades.

strat.BARS_SINCE_SELL

Description

Similar to strat.BARS_SINCE_TRADE. Returns the number of bars that have elapsed since the most recent trade. It checks only for sell trades.

strat.LAST_TRADED_PRICE

Description

Returns the fill price of the last trade we executed.

Example

Refer to the previous example in strat.BARS_SINCE_TRADE to see how we combined both functions in order to implement more nuanced logic.

Placing Orders

The type of order is defined within the BUY_ORDER and SELL_ORDER parameters of BacktestXL. If you do not define them, transactions will be executed via market orders for all available cash (buying) or current holdings (selling).

strat.LIMIT_BUY()

Description

Places a buy limit order. The strategy will buy as many shares as possible if no parameters are specified.

Parameters

  • limit_price: the limit price for the order
  • size: the number of shares to buy (if defined)
  • pct_cash: the percentage of current cash holdings used to buy (if defined)

Example

Create a buy limit order for as many shares as possible with a limit price equal to the most recent close price.

strat.LIMIT_BUY(limit_price=data[t].Close)

strat.LIMIT_SELL

Description

Places a sell limit order. If no parameters are specified, the strategy will sell all current holdings.

Parameters

  • limit_price: the limit price for the order
  • size: the number of shares to sell (if defined)
  • pct_holding: the percentage of current holdings used to sell (if defined)

Example

Create a limit order with a limit price equal to the average of the most recent high and low.

strat.LIMIT_SELL(limit_price = 0.5 * data[t].High + 0.5 * data[t].Low)

strat.BUY()

Description

Places a buy market order. If no parameters are specified, the strategy will buy as many shares as possible.

Parameters

  • size: the number of shares to buy (if defined)
  • pct_cash: the percentage of current cash holdings used to buy (if defined)

Example

Buy shares for a total amount of 50% of our available cash.

strat.BUY(pct_cash=0.5)

strat.SELL()

Description

Places a sell market order. If no parameters are specified, the strategy will sell all current holdings.

Parameters

  • size: the number of shares to sell (if defined)
  • pct_holding: the percentage of current holdings used to sell (if defined)

Example

Place a market order for selling 1 share of the asset.

strat.SELL(size=1)