Combining Stop Loss and Trailing Stop Loss for Risk Management (Python Tutorial)
In this tutorial I would like to go over how the exit strategies ‘stop loss’ and ‘trailing stop loss’ can work together to manage the risk of a trade, make a significant difference in the results of your trading strategy. This approach is described in detail on Investopedia.com.
First, we will review the concepts of stop loss and and trailing stop loss, next we will go over the Python implementation using the backtesting.py framework and finally we will review some results of this approach.
So let’s get started!
This story is solely for general information purposes, and should not be relied upon for trading recommendations or financial advice. Source code and information is provided for educational purposes only, and should not be relied upon to make an investment decision. Please review my full cautionary guidance before continuing.
What is a Stop Loss?
A stop loss is a type of order that brokers provide to traders a means to limit losses. A stop loss is an important exit strategy to manage risk when entering a trade.
The way the stop loss works is that if the trader enters a long position for example, the stop price is set below the long entry price. The stop price can be either calculated by using a certain dollar amount or percentage of the entry price.
Let’s assume a trader enters a long position when the price of a stock is $10 and the trader wants to use a 1% stop loss. In this case a stop order would be placed at $9. In case the stock price increases, the sky is the limit. However, if the price drops, the stop order will get automatically executed at $9 and the trader limits his losses.
Trade Ideas provides AI stock suggestions, AI alerts, scanning, automated trading, real-time stock market data, charting, educational resources, and more. Get a 15% discount with promo code ‘BOTRADING15’.
What is a Trailing Stop Loss?
The trailing stop loss is a variation of the stop loss exit strategy intended to protect gains that were made after entering a trade. After entering a long position, for example, we keep track of the price at it moves up and the stop loss price moves up with the price. When the price moves down again, the trailing stop loss is executed.
To use the same example as above, let’s say we want to use a trailing stop loss of 1%. The long position was entered at $10 but now the price climbs to $12 and then drops down again to $9.
We calculate the trailing stop loss from the maximum price of $12 so the trailing stop loss gets executed at $10.8. This means, we protected our gains and the trade resulted in a win.
How can Stop Loss and Trailing Stop Loss work together?
The two types of stop orders can work together to form a comprehensive risk management exit strategy.
The stop loss protects from losses in case the price immediately moves in the wrong direction after the trade is entered.
The trailing stop loss protect our gains in case the price moves in the right direction after the trade is entered but then reverses the direction.