Today, I’m here to talk about the TSLA stock price, and to share my analysis featuring a stock screener I built in Python, and the algorithm I derived to auto-identify key buy/sell points for a stock. Let’s get started!

It’s only March of 2020 and boy, what a year it’s already been for electric carmaker Tesla and their newest car, the CyberTruck. Love it or not, the Tesla CyberTruck is one of the most talked-about cars of the year, and for a good reason. It’s the first time the world has ever seen an electric pick up truck, and although it is not the best looking car by most people’s standards, it is certainly different and futuristic to say the least. Personally, I think it’s a really cool car and can’t wait to start seeing these rolling prisms on the road next year. Welcome to the future!

Tesla has been nothing but thriving since the CyberTruck’s announcement, and if you’ve been following the stock market at all, there is no doubt that you’ve seen the outrageous surge in TSLA’s stock price since Elon Musk’s big reveal in November 2019. So what’s going on?

Disclaimer: The contents in this article are purely my opinion. I am not responsible for any financial losses you may incur from applying the awesome content of this write-up. I am merely sharing what I’ve learned over the past few years in stock trading, data science, and Python.

Let’s take a look at the TSLA stock prices from January 2019 to March 2020.

As you can see, we see a huge surge starting from November 2020 (about $300/share) skyrocketing up to almost $1000 in less than 3 months. That’s a 333% increase – absurd! To put this in perspective, if you had bought 10 share for about at $300 ($3,000 investment) in September, you’d have $10,000 today. That’s a $7,000 profit in only 3 months. Must be nice.

Not all of us were lucky enough to ride this roller coaster. During this surge, I’ll never forget the daily questions I’d ask myself, “This makes no sense right? The price is completely inflated – it can’t possibly go higher right?” But day after day TSLA would continue to go up and up. +9% one day. +10% the next. Then +13%. Then +22%. Day after day I’d kick myself for not buying TSLA the day before. The company has been reporting losses for the last 4 quarters straight with negative profits. What is happening?! Why did I sell the rest of my TSLA shares back in 2019 when the stock was in the mid 200’s (I barely broken even). It hurt, man. But that’s the stock market for you; it is a cold-blooded, stress-inducing creature and it waits for no one.

So, why did the stock price go up? For one thing, people liked the CyberTruck. In fact, the day after the unveiling there were already 146,000 preorders.

146k Cybertruck orders so far, with 42% choosing dual, 41% tri & 17% single motor — Elon Musk (@elonmusk) November 23, 2019

To put these figures into dollars and cents:

Tesla Cybertruck RWD: 24,820 reservations (worth ~$1 billion before options)

Tesla Cybertruck Dual Motor AWD: 61,320 reservations (worth ~$3 billion before options)

Tesla Cybertruck Tri Motor AWD: 59,860 reservations (worth ~$4.2 billion before options)

That’s a total of $8.2 Billion in 24 hours. That’s a lot of moolah.

Why else did the stock price go up? One word: HYPE.

When to Buy and when to Sell a Stock?

So, this comes to the next section of my analysis. During the hype when nothing made sense and no one knew why the price was rising at such an unbelievable rate, I asked myself the following questions. When a company like TSLA is trending, is it possible to catch these bullish trends before it’s too late to get in? It is possible to flag a good buy point and sell point for a trend over a period of time? Are there statistically proven methods to calculate these key points, or is it basically an arbitrary gamble deciding when to buy a stock (yes, sometimes)? What kind of technical skills do I need to derive these calculations? Essentially, I wanted to understand what the financial analysts on Wall Street do, so I put on my data analytics hat and went down a deep google rabbit hole as I often do for answers. Here is what I found.

First, let’s use the following chart to illustrate an important concept.

NVDA stock price from March 2018 to March 2020

The chart above shows the two-year historical stock prices of one of my favorite companies, Nvidia, from March 2018 to March 2020. Take a look at the graph and try to find the most ideal time to buy NVDA and the most ideal time to sell NVDA for this time frame. Since the name of the game is buy low and sell high, we see that the best time to buy would have been around December 2018 when the price was at it’s lowest point (the Absolute Min) around $130, and the best time to sell NVDA would have been February 2020 at the Absolute Max when the price was around $295. These two points are indicated by the purple arrows above.

However, when a stock is going down, it is nearly impossible to tell whether you’ve hit rock bottom or it will continue to drop. Think about today with the Coronavirus – have we hit rock bottom or will the stock market continue to drop? Same goes for the highs. When the price is going up, it is nearly impossible to know whether you’ve hit the max or if it will continue to go higher. Think back to the Tesla surge – how many of you hesitated to buy during this surge because you didn’t know if it would go up even more the next day? It was a risk, and unless you have some insider information (which is illegal obviously), you can’t predict these absolute min and max movements.

So the million-dollar question becomes: When the stock price is dropping how to you know when to buy, and when the stock price is rising, how do you know when to sell?

Answer: Since you can’t expect to predict the Absolute Max or the Absolute Min when you’re in it, you buy when the stock is rising after the Absolute Min, and you sell when the stock is dropping after the Absolute Max. We call these two points the start and end points of a trend.

For this particular trend (or “wave”) in NVDA stock price, we buy around March 2019 at $150 and sell around March 2020 at $260, completing this trend. These are the two green circles we see in the chart following the Absolute Max and the Absolute Min.

Take another look at the chart above to let this concept sink in.

The reason we buy at the rising green circle is because we’ve identified the Absolute Min and want to catch the stock as it is going up in momentum. The reason we sell at the sinking green circle is because we’ve already seen the Absolute Max, so we want to cash out on the stock before it drops even further and potentially snowballs into further loss. Remember: you cannot predict the Absolute Min and Absolute Max while you’re in it, so the best thing you can do is the buy and sell as close to the Absolute Min and Absolute Max as possible.

Now that we’ve covered this concept of buying/selling as close to the Absolute Min/Max as possible, you may be wondering how do we actually calculate these green circles? How do we know the dates and prices at which to buy and sell for a trend? The answer is in Moving Averages.

A little background on NVDIA before we continue: For those of you who don’t know, Nvidia is a technology company that makes the powerful GPUs we have in our computers and gaming systems today. Because of this company’s powerful GPU processors and chips, data processing is both incredibly fast and affordable. This has revolutionized the world of data science and has made machine learning much more efficient and accessible to big data companies and even people like you and me. For that reason, it is one of my favorite companies (and the reason I own NVDA and chose my current laptop!)

Moving Averages

Moving Averages are technical (lagging) indicators which investors use to analyze price trends. It is essentially an average of a stock price over a defined number of time periods. Lagging simply means that we are using the historical data to calculate the current moving average price. If we take the 10-Day Moving Average (or MA10) of a stock for example, we are simply taking the last 10 stock prices and taking the average. If a stock price is rising, the moving average will rise with it. If the stock price is dropping, then the moving average will drop as well. If the stock price does not change, or it fluctuates uniformly over the last 10 days, the stock price will be close to the simple average (no trend). Below is the mathematical formula for the Simple Moving Average:

Taking the moving average smooths out the trend and removes the “noise” that comes with the inherent volatility of a stock price. Let’s take the 10-Day Moving Average for NVDA and see how it behaves in respect to the stock price. Below is the same chart for NVDA’s two-year stock price in red with the 10-Day Moving Average overlay on top in blue.

Notice how the blue 10-Day Moving Average moves as the stock price goes up and down. It follows the trend with a slight lag and smooths out the ups and downs (noise) of the stock price’s movement.

Now, let’s see what happens when we overlay the 20-day Moving Average on top of the 10-day Moving. How do you think they would compare?

Notice how when we use the 20-Day Moving Average, the (orange) line is a little flatter and doesn’t move as much as the 10-Day Moving Average. This is because the MA20 takes in more data points, so each data point only has 1/20th of total weight, instead of 1/10th of the weight. Since there are more data points baked into the calculation, it requires bigger swings in data to move/influence the 20-Day Moving Average compared to the 10-Day Moving Average.

Moving Average Crossovers

So why are we looking at the MA10 and MA20 overlay? The reason is because the crossovers between these two moving average lines tell us when a new trend has started (trend start) and when that trend ends (trend end). If you look at the points at which the blue line crosses over the orange line, they are roughly near the points min/max, and the points at which we would buy and sell. This is the statistically method to identify the start and end dates of a trend (see green circle below) I was looking for. Bingo!

However… after using this Simple Moving Average method for a number of weeks, I realized one of the short comings of this method. If we look at the graph above, we see that the crossovers don’t catch the start and end points quickly enough. In the second green circle around October 2019, the stock price had fallen so sharply that the moving averages weren’t able to cross over in time. Another example in the stock price all the way at the end (to the right) you see that there is still a big gap in the MA10 and MA20 lines – The lines have not started to converge despite the sharp drop in stock price in March 2020.

This is because as mentioned earlier, with the Simple Moving Average, all the data points within the period are weighted equally. The price ten days ago has the same weight as the price today. In real life, the most recent stock prices should hold more weight than the stock prices in the past as they have more effect and influence on the current stock price and trend. The remedy? Instead of using the simple moving average, we should be using the Exponential Moving Average.

The Exponential Moving Average (EMA) Crossover

The Exponential Moving Average is a more complicated version of the simple moving average that holds more weight on the recent stock prices compared to the old. In an EMA10, the stock price from yesterday will have a much bigger weight than the stock price from 10 days ago. This allows us to factor in the most recent swings into our buy/sell algorithm. If a stock price dropped dramatically yesterday, the moving average will be proportionately lower today compared to a simple moving average. Here’s what the Exponential Moving Average formula looks like.

Up until this point of my exploratory analysis, I have been using Yahoo Finance as my stock screener, but I noticed that it only has the simple moving average indicator in its chart interface and no Exponential Moving Average. This was a bummer as I knew EMA would be more useful than the SMA. For this reason, I decided to put my python skills to use and create my very own stock screener that would allow me to calculate EMA10 and EMA20 crossovers for any given stock. In the next section, I will share my stock screener and how I made this stock screener project in Python.

Scion’s Stock Screener Python Project

The stock screener I built had a bunch of cool features to help me analyze my stocks. Here is a list of some of it’s main features:

Company Info

Individual Stock Lookups

Historical Stock Prices with ability to set Date Range

Interactive Graphing with key buy/sell dates and prices embedded

10-Day Exponential Moving Average Lines

20-Day Exponential Moving Average Lines

EMA20-EMA10 vs. crossover = 0 Line Plot

Output with key buy/sell dates and price

Financial Analyst Recommendations

Most Actively Traded Stocks for the Day

Biggest Gainer Stocks of the Day

Biggest Loser Stocks of the Day

Index tracking for S&P500, Dow Jones, NASDAQ, and BitCoin

Potential Buys (coming soon!)

Potential Sells (coming soon!)

To create this Python application, I used the Yahoo Finance API to pull in the data, the tkinter library to create the GUI (Graphical User Interface, aka the buttons and interface/outputs), and the famous MatplotLib library to output the graphs. All the coding and functions were written using my favorite IDE (Integrated Development Environment), Spyder.

Below is a recording of my stock screener demoing all of the mentioned functionalities:

My Stock Screener Demo (Python)

The Buy/Sell Algorithm

The most difficult of this project by far was figuring out how to code the algorithm. The algorithm did a number of things; it read in the data, calculated the EMA10 and EMA20 for a stock, searched for crossovers, determined whether the crossover was a buy point or sell point, and identified the trend start dates and trend end dates from these crossovers. Additionally, I had to constantly tweak the algorithm to filter out as many false positives I could. This was a challenging process that took many iterations but I am quite happy with the current algorithm. Although this project is not 100% complete and that I still have a few features to finish such as the Potential Buys and Potential Sells buttons. Overall, this was a very fun project and I learned a lot from it. It took me about 5 nights to complete while watching Rick and Morty in the background. Well-worth the time.

Bringing it all together: EMA Crossovers for NVDA and TSLA using my Stock Screener

Earlier, we saw that using simple moving average for NVDA’s stock price did not always catch the start and end point quickly enough. Below is the chart using Exponential Moving Averages with my stock screener. Yellow X represents the EMA10 and EMA20 crossover, and as we can see from the output, my algorithm was able to pick up a sell point at March 9, 2020 at $245.44.

In the output above, we see that the most recent trend start date was on August 21, 2019 at $170.83 and the trend end date was on March 9, 2020 at $245.44. If we exercised this position, we would have made $74.61 per share, or an increase of 43.7%!

Now that we understand EMAs, let’s also revisit the TSLA stock price from the beginning. Here are the outputs from my stock screener:

As we see from TSLA’s output, we see that the most recent trend start date was on September 10, 2019 at $253.54 and the trend end date was on March 6, 2020 at $703.48. If we exercised this position, we would have made $449,94 per share, or an increase of 298.7%! Wow – wish I had created this stock trader before September 10! 🙂

Below is a bonus output I found interesting. This is EMA20-EMA10 Crossover chart. If you take the EMA20 and EMA10 on a day and subtract the two values, you get the difference. When the difference is zero, that means that the two EMAs are the same and thus crossing over. This is indicated by the red horizontal line on the graph.

If we look at the right side of the graph, this is where the giant surge occurred. Notice how steep the EMA20-EMA10 graph plunges into the negative area, letting us know that we are far from a crossover point. This is because the price was increasing so dramatically that there was no change/end in trend in sight. Due to the epidemic corona virus in the news recently, almost all companies were affected and thus we see the dramatic skyrocket, crossing over EMA20-EMA10 = 0 line. Cool stuff, right?

Conclusion

If you are still reading, thank you for taking the time to read my analysis! I know it was rather long but I wanted to share everything I learned and everything I wish I knew when I first started with stocks. I hope you found it informative and interesting, and perhaps learned what an EMA crossover is and how you can use this concept for your own personal financial investing strategy.

I’d like to add that although my algorithm for EMA crossovers works quite well on real life data, it is not always foolproof as sometimes the algorithm will still pick up false crossovers that do not turn out to be trend starts and trend ends. This is unavoidable due to the unpredictable fluctuations in stock prices. One way to account for this is to tweak the time periods of the EMA. I used 10 and 20 day moving averages but you could use 5 and 30 to widen the gap between the two time periods. The important thing to note is that the EMA Crossover method should really only be used as a single reference point when deciding whether to buy and sell a stock, and not the sole criteria. There are other data points you should consider such as Revenue, PE Ratio, financial earnings reports, balance statements, industry trends, and recent news.

Also, if you missed out on the TSLA surge, don’t feel bad about it. TSLA’s stock is the definition of a volatile stock with high risk but high return. As cool as their cars are and as cool their CEO is, the company was bleeding and not making money last year. When it comes to stocks, hindsight is 20/20 (if though it’s currently the year 2020), and you can’t predict every major trend before it happens. Stock trading is essentially a form of gambling. Looking back it’s so easy to say you should have bought it. However, TSLA was risky last year because of its shaky financials, and during the surge the price felt so overvalued that we were almost expecting the price to fall at the drop of a hat. We were acting upon normal logical impulse to sit back… at least that’s what I like to tell myself…

And in regards to the CyberTruck… Personally I love it! It looks a little strange I admit, but I truly admire what Elon Musk has done for our world. Imagine seeing one of these on the road coming home from work one evening. The CyberTruck looks like something my 10-year-old self would have imagined the future to look like. And thanks to Elon Musk and what this car symbolizes, we are about to advance one step closer in the future thanks to this car. Someone had to brave enough to do it and that person is Elon Musk. This is why Elon Musk is one of my personal heroes.

And finally, I know a lot of you are hoping to see my algorithm. I have decided not to share the algorithm for the three reasons below:

My project is not yet complete and I still want to tweak the algorithm a little bit more I worked hard on the algorithm and it feels kind of cheap to just give it away for free (what’s that Joker quote? If you’re good at something, never do it for free!) If you wanted to recreate the basic version of the algorithm, I’ve already shared with you how to create it (EMAs Crossovers)

Thanks again for checking this out! If you have any questions feel free to comment below or drop me a message at scionli89@gmail.com.

Cheers,

Scion