Stock market investing with Python and Raspberry Pi
Introduction

Wait... Stock market and programming languages? How come we are mixing apples and oranges now? Well, in the past few months you have probably been hearing countless news about the stock market collapsing, then rallying, then collapsing again, and so on. If you are like me, you have also had a bit more spare time than usual due to the COVID 19 pandemic (There is always a silver lining, right?) and these headlines might have lit a spark of curiosity.
Well, they certainly did on me. After the initial hit of the pandemic, stock prices fell dramatically, and I rushed to create a Robinhood account and put some money in it. After a first few winning trades during the huge rally that followed, things went back to “normal” and I realized that I had little to no idea about trading... But I am pretty good with numbers, so there was some work to do.
Stock market 101
To get the basics out of the way: The goal of trading stocks, as when trading any other sort of goods or products, is to buy low and sell high, in order to make a profit. Notice how this is different from stock investing. The goal with investing is to give money to a company so that they can use it to grow alongside your investment. This is intended for long-term investments and are usually a moderately safe bet, although not very profitable. Stock trading, however, focuses on taking advantage of price fluctuations to make profit.
This is usually labelled as gambling, which in some cases it might be. However, there are many kinds of trading strategies, and some of them make more sense than the others. Swing trading is by far the strategy that makes the most sense to me. Swing trading consists in buying and holding stocks for relatively short periods of time, in the range of several days. to even weeks or months.
Swing trading
The philosophy behind swing trading relies on the nature of the stock market itself. The stock market is not an accurate representation of a company, nor the economy. Is the public’s perception of each and every company in the market. Why is this important, you may ask? Well, companies have intrinsic value. They have assets (Both physical and intellectual) with actual worth. This underlying value has an influence over the price of a company’s stock and, over short periods of time, this intrinsic value does not change. However, the stock price does fluctuate according to countless external factors, but it is safe to assume that the public as a whole is aware of the intrinsic value of a company, so if the price of a stock falls below the perceived value of the company, it will likely rise up again.
Now a strategy can be built around the assumption that the market understands, to some degree, that a company has intrinsic value. If we detect unusual dips in a stock price, we can identify potential bargains to invest, in the hopes that the price will eventually rise again.
Now, doing this by hand over say, 500 companies, is tedious and probably a full time job. Besides, how are we supposed to test whether or not our strategy of buying "bargains" is any good? Well, here is where a bit of programming may proof extremely useful.

It is not difficult to detect peaks in a variable. A few fundamental concepts from data science such as standard deviation and moving averages are enough for this purpose. Getting data from the stock market is also fairly easy using Python and the package called yfinance, which allows to pull data from yahoo finance database.
Although this is still a work in progress, I have successfully implemented a few pieces of code to test several strategies to take advantage of dips in stock prices which analyze huge collections of data from hundreds of companies.
With the help of a Raspberry Pi Zero W, which is now permanently plugged in my room, we can automatically pull data from the stock market daily, analyze it, and push sell and buy recommendations to a google sheet that I can comfortably read every morning from my iPad while having some coffee.
