Logic Behind Backtestr

Last updated: July 27, 2025

We hope this page will be helpful to our users. - Vehizoma

This page will be explaining how Backtestr converts user action/parameters into backtesting results.

What is action?

"action" is a numpy array that contains position parallel to dates. If not altered it will only contain zeros by default.

Lets go with an example;

Action image

From index 6 (action[6]) to index 11 (action[11]) Backtestr will calculate as if a position with %100 margin was present. So one can infer that between action[5] and action[6] Backtestr opened a long/buy position.

With the same logic, between action[11] and action[12] Backtestr closed buy/long position.

Because of action's nature we need to iterate over it with a loop. On every iteration we need to decide about what to do. The most suitable option to use is for loops .

What if action is not 1s and 0s?

What if action[x] is not 1 or 0? Well it depends:

If action[x] to action[z] = Y and (y>1): It will be calculated as a long position with a leverage of Yx.

If action[x] to action[z] = Y and (Y<-1): It will be calculated as a short position with a leverage of Yx.

If action[x] to action[z] = Y and (0<Y<1): It will be calculated as a long/buy position with size of balance*Y.

If action[x] to action[z] = Y and (-1<Y<0): It will be calculated as a short/sell position with size of balance*abs(Y).

What if action is reduced/increased?

Position gets closed and reopened.

Let action[x] = 2 and action[x+1] = 3. Backtestr closes and reopens positions.

Commission and slippage are calculated accordingly.

Limitations&Assumptions

Unfortunately Backtestr does not support mutli-asset backtesting. (yet)

Slippage is calculated at a fixed semi-realistic rate (0.02%).

No partial fills. Backtestr assumes every order as filled 100%.

There are no overnight funding fees exclusively coded.

No market impact. Price stays unaffected after order executions.

Fixed commission rate. Pretty self explanatory.

No order rejection. Backtestr assumes every order as a valid trade.

Variables

Initial Balance: The amount of money at start. Not that important.

Commission: The amount that is cut when entering/exiting positions by the platform. Usually a percentage. It depends on the platform and should be set accordingly by user.

Slippage: Trades may not happen at the expected price due to various reasons(low liquidity, high latency etc.). Backtestr calculates slippage at a fixed rate of 0.02% (0.0002).

Trailing Stop Percentage: If not None it will trail prices according to position (long/short).

Following variable names should not be altered.

  • array_close: array of close prices.
  • array_open: array of open prices.
  • array_low: array of low prices.
  • array_high: array of high prices.
  • array_volume: array of volume data.
  • action: array of positions.