Building a Trading Backtesting System — Part 3: Risk Management and Stop Loss Techniques

Xavier Escudero
4 min readMar 3, 2024
Image generated with Leonardo.AI

Risk management can be implemented using various built-in functionalities:

  1. Stop Loss Orders. These orders can be based on fixed price levels, percentage drops from entry prices, or dynamically adjusted based on indicators or market conditions.
  2. Position Sizing. To determine the quantity of shares or contracts to buy or sell for each trade. Position sizing strategies can be based on metrics such as account size, risk tolerance, volatility, or other factors.
  3. Portfolio Rebalancing. We can use techniques like periodic rebalancing, where the allocation of capital among different assets or trading strategies is adjusted over time to maintain desired risk levels or performance targets.

Stop Loss orders

Fixed stop loss percentage

Many people uses stop losses based on a percentage of the execution price, due to its simplicity and ease, specially if you trade manually.

We’re going to implement the order stop loss, after the buy order has been completed (from notify_order), instead using bracket orders.

We configure a stop loss parameter by percentage of execution price.

--

--

Xavier Escudero

Innovation enthusiast, passionate about automation in several fields, like software testing and trading bots