=======
Code snippet show casing backtesting a strategy in python on historical stock market data.
Intro
The goal of this article is to describe how to back-test a technical indicator-based strategy on python. I will specifically use a Bollinger band-based strategy to create signals and positions.
Description of strategy
Create 20-day (+/- 2 standard deviations) Bollinger bands on the adjusted close price. Buy, when the price crosses the lower band from the top and hold until the price crosses the upper band from below the next time. Sell when the price crosses the upper band from below and hold until the price crosses the lower band from the top the next time.
Here are the steps to create your own back-testing code.
- Import necessary libraries
- Download OHLCV Data
- Calculate daily returns
- Create strategy-based data columns
- Create strategy indicators
- Create signals and positions
- Analyze results