If you are too impatient and want to know now how to implement this strategy, you can directly look at the recording at the end of this article. I recorded myself coding and gave extra information on the way to code. You can apply it at home.
Otherwise, let’s start the challenge.
Again, I will not go too much into details regarding the code and API; you can find more information on how to get live market reading the article below:
I will cover the process more superficially inside this article.
The first step will consist of importing the necessary packages. These packages are detailed in the article cited above.
So, you will start by importing your packages previously installed by using the following lines of code:
These lines of code above are querying Python to import the necessary libraries to develop our strategy.
Once we are set up, let’s pursue the next step.
Now that libraries are imported, we can import markets data.
Now that the different packages needed have been uploaded. We are going to use the SP500 to test our hypothesis.
So, we will call live market data using Yahoo Finance API.
For your information, Yahoo Finance API will need 3 mandatory arguments in this order:
- Tickers (1)
- Start date + End date or Period (2)
- Interval (3)
For our case, the ticker(argument 1) will be SPY. Furthermore, we are going to choose for our test the last day period(argument 2) instead of defining a Start and End date. And we will set up an interval(argument 3) of 1 minute.
As a quick reminder, SP500’s ticker is SPY.
To call your data, you will have to use the following structure:
Above described the structure to get market data which interest us.
Now that we have the structure let’s run the code.
Line of code to download market data
Below a sample of the output you should get:
Now, that we have downloaded and stored our data, we can continue and define our moving average, buying & selling signals.
If you want more in-depth information, you can go across this article:
So, we will now create the following calculated field:
- Moving Average
- Upper Band
- Lower Band
For the middle band, we will use the rolling function to get the mean value of the 21 latest periods. We are going to apply our strategy for the 21 last minutes, which means that we are going to calculate the mean for the 21 minutes before.
MiddleBand(t) = Average(Close(t-1 -> t-22))
For the Upper Band and Lower Band, we are going to use the rolling function as well, but instead of calculating the mean, we are going to calculate the standard deviation.
So, let’s code it on Python:
The code above, once executed will create 3 new columns to your dataframe
Now that you have defined your bands, 3 extra columns must have been created.
We can finally deploy our strategy and test it.
Now, the last step of our plan is to plot our data and check if we can predict market movement. I have recorded this part live in the video below:
This video is a step by step tutorial for coding the Bollinger Bands.
Boom! We made it.
If you cannot run the video, watch the result and hear my comments. Here a sample of what you should have once you have plotted your graph.
I have coloured in green in the graph below, the good prediction and black the wrong prediction.
Final output after running the Python code
Furthermore, I have uploaded the full Python script described in the video above and you can now develop it at home:
Full Python code