SATS Technical Analysis in Python

SATS technical analysis

Just a week ago, I wrote about SATS using the 52-week low formula and python. Now, I am doing SATS again, but from a technical analysis standpoint. Since I already have all the data, might as well. This is a completely new one for me as I have not done before a python on technical analysis.

The data source from which I will be pulling all the data is AlphaVantage. You would need to sign up with your email to get the API key. The good thing about AlphaVantage is it includes a library of technical analysis tools.

I will be evaluating two technical analysis strategy. The first is backtesting the 50 and 200 SMA cross over, otherwise called the golden cross. So let’s say if we bought at all the cross over points, how much would we have made?

The second is the Bollinger bands and RSI strategy, which I have written an extensive post about it. Let’s say if we bought when prices are below the lower band and when RSI is <30, how much would we have made?

Pulling Data from Alpha Vantage

Alright, let’s dive in. These are the libraries and modules that I would be using. I have covered up my API key, but you need the API key to get the technical indicators and the time series data.

We use time series to pull out the ENTIRE price data of SATS since the inception of listing. This is denoted by the value “full” in the output size parameter. Then I reindex it to reverse the order of the data frame. So SATS.head() would give the earliest date and SATS.tail() would give the latest date. You can see how this looks like.

Data Cleaning

Before we start our analysis, let’s do some data cleaning first. The first thing to change is our column names. That can be done simply by replacing it with new values.

There done!

The second thing is to replace null or blank values. If you look at the price chart, there are rows which are showing a closing price of 0.

SATS Price Chart

I have no idea why. All I know is we need to remove these values. A simple trick is to use the boolean masking as shown below. so we just want values of SATS['Close'] that is not equal to 0.

Done! Price chart looks clean now.

The Golden Cross Strategy

Alright, now we are ready for some technical analysis action. The golden cross strategy is when the 50 SMA crosses above the 200 SMA. Traders and investors take that as a buy signal. Vice versa, when the 50 SMA crosses below the 200 SMA, that is a bearish signal.

So we just need two things. The 50 SMA and the 200 SMA. Because of AlphaVantage, we can obtain these two values in a one-liner code. Let’s see how this looks and plot out the two moving averages.

SATS golden cross strategy

So far so good. The blue line is the faster line while the red line is the slower line. Another way of looking at it is one is short-term while the other is long-term. Now that we have both the moving averages, and we know the golden cross strategy, how do we implement this in code?

We want to tell python to long SATS if the 50 SMA is higher than 200 SMA. But when the 50 SMA falls below the 200 SMA, we are going to SELL and realise these profits. That’s the simple logic behind.

To do this, I created a new column called Buy. This column would store only either 1 or 0. 1 means we are long and 0 means we are out of the market. The condition for 1 would be exactly the same as the logic described above. The 50 SMA has to be above the 200 SMA. Else, it would be 0. I also added in another condition that says both the 50 and 200 SMA can’t be null values. Let’s see how the code looks.

It is a one-liner pythonic code. I am putting in the for loop and the if, else statement all in one line. This is called the list comprehension in python.

Now you can imagine the Buy column to be filled with either 1 or 0. If the condition of 50 SMA > 200 SMA passes, it would be 1. Else, it would be 0. That is our simple strategy.

Backtesting the Golden Cross Strategy

The next step is to calculate the profits that we made when we are in the market. So that means when the buy column is = 1, how much profits did we made.

In this case, we first need to calculate the daily profits. Then, from the daily profits, we can calculate the cumulative sum of profits. The daily profit is just the price of tomorrow minus the price of today. To get the price of tomorrow, we can use the .shift() function. Let’s see how the codes look.

From the chart above, you can see that when the 50 SMA is above the 200 SMA, there are days when we make money and there are days that we lost money. This is normal because the moving average lines are lagging indicators. Prices would definitely fall for a couple of days or even weeks before the 50 SMA falls below 200.

SATS technical analysis

Let’s say I use this simple strategy from the year 2000. My decision is real simple. I only buy SATS when 50>200 SMA and I sell when 50<200 SMA. How much would I have made?

To calculate the total cumulative profits by implementing the golden cross strategy, we can use the cumsum() function on the daily profits.

This is the cumulative total profits we made for every 1 share we bought and sold along the way.

The last time we closed off the position is on 28th January, the date when the 50 SMA falls below the 200 SMA. That means SATS is on bearish mode now, technically speaking. At that point in time, we have made a cumulative profit of $2.20 for every 1 share we bought.

Then you might ask, what about the commission fees every time you buy and sell. I have also thought of the same question. So I came up with a solution to count the number of times when the buy column switches from 0 to 1 and from 1 to 0. That is because these instances represent a buying or selling decision.

I used a zip function to get the date today and the date yesterday. Then we just compare the date today and the date yesterday. If one is 0 and the other is 1 or vice versa, then that means it is either a buying or selling decision. The total count turns out to be 28.

Assuming the average brokerage fee of $25, the total commission paid would be $700 (28 counts * $25). This means that over a period of 20 years, the total commission fees to implement this strategy costs $700. Hence, the minimum number of shares to break-even can be calculated to be $700 divided by the total cumulative profits of $2.20. That is about 320 shares.

The downside of the golden cross strategy is there are a lot of false signals, especially during a ranging market. If you look closely, there are quite a couple of times when the 50 SMA crosses above the 200 SMA, only to see itself falling back below the 200 after a short period of time.

All these add to the commission fees and losses. So we can tweak the formula to set the conditions even stricter. For example, the 50 SMA must be a certain margin above the 200 SMA for buy to be equals to 1. But that is up to you on how you want to play around with it.

The Bollinger Band & RSI Strategy

Next up, let’s try the Bollinger band & RSI strategy. Once again, we are using AlphaVantage’s TechIndicators to extract all the indicator values. After which, we are going to concatenate the new values into a new data frame. And finally, remove all the blank and null values.

The steps are exactly the same as above.

The strategy that we are going to execute here is to buy when prices are below the lower band and RSI index is less than 25. These spots are usually the areas when the price is the most depressed.

What if we buy at these spots and sell when prices hit the upper band and when the RSI index is above 75? Wouldn’t it be great to backtest this strategy? Unfortunately, I have not been able to figure out a way to switch the buy column to 1 and 0 like our previous golden cross strategy.

How do I make it such that the buy column would be equal to 1 when RSI < 25 and price < lower band AND ensure that it stays at 1 UNTIL it hits RSI > 75 and price > upper band, then it switches back to 0.

Next, when it turns 0, how do I make sure that it stays at 0 all the way UNTIL it hits RSI < 25 and price < lower band? Then switch back to 1. That is still a work in progress and I guess an open question to the community. If you have some ideas, please comment below. If I figured it out, I will update accordingly here.

In the meantime, I came up with a workaround solution to just plot the buy and sell positions. So when RSI < 25 and price < lower band, then that would be a green marker spot to buy. Vice versa, when RSI > 75 and price > upper band, that would be a red marker spot to sell. Let’s see how everything looks.

SATS technical analysis

The Bollinger bands can’t really be seen as the picture is quite compressed. But you can see where the green and red spots are. These are the points where you buy and sell respectively.

If you follow this strategy of simply buying when it’s green and selling when it’s red, it seems that there are quite a couple of times we sold off too early. Again, we can tweak this to set stricter conditions for selling, let’s say RSI must be above > 80 or prices must be a certain % higher than the upper band.

On the other hand, the entry points seem to be quite decent. Most of the time it is when prices are low and there are good upside gains after the initial position. Furthermore, it is quite rare for this green marker occurrence, which means RSI < 25 and prices < Lower band. It happened only 4 or 5 times over 20 years.

Guess what? The recent price drop has put SATS into the green marker spot. The blood moon has come. The last time this happened was in 2011. If we use the simple Bollinger bands + RSI strategy, that is a buy signal. But as seen during the case in 2008, low can go lower. How low? Don’t try to time the market.

But of course, this is just one of the thousands of technical analysis tools. Different combinations output different results. The golden cross strategy would evaluate it to be a sell signal. Ichimoku Cloud would also evaluate it to be a sell signal. Other indicators like Bollinger might signal to be a buy. It all really depends on the individual, isn’t it?

Personally, for me, the best time to buy is when there is a confluence between the technical analysis and fundamental analysis. Unfortunately, my fundamental analysis using the 52-week low has evaluated it to be a no-go, at least for now at current prices.

But again, there are people who place a heavier emphasis on technicals over fundamentals. I am just showing the charts and data from an objective standpoint. Ultimately, it all really depends on individual judgement. Don’t blindly follow others including myself.

Summary

On closing, I am starting to appreciate the beauty and power of the python language. It is starting to do things that Excel can’t do. Initially, my very first python post was just plotting simple returns and simple charts. All of these can be done easily in excel. But now, I realised that there is so much more that could be done, though I am definitely not utilising the full potential of it.

Interestingly, there are trains of thoughts that suggest programming and coding to be the new literacy. Initially, English was recognised as the most important language in the world. Then, people start learning Chinese as China is poised to overtake the global economy. Now, the tone starts shifting towards coding as the “new” language to be fluent at. Elementary Schools around the globe implementing mandatory coding lessons is just one evident trend of the future.

To a certain extent, those who know how to interact and talk to machines have an advantage in their hands. I believe the same applies to finance and investments. That’s why instead of going down the traditional finance path of CFA or signing up for value investing courses, I have decided to divert. This means going broad rather than going deep. For the better or worse, only time will tell.

To find out more about how you can learn python in the area of investing, do check up my resources page that was recently created. It’s not difficult. 🙂

  •  
    86
    Shares
  • 86
  •  
  •  
  •  
  •  
  •  
  •  
  •  

2 Comments

Leave a Reply

Your email address will not be published.