My First Python Trading Bot

Michael Rozenvasser
4 min readDec 22, 2020
Stonks are c00l!

I have always been deeply fascinated by the mystique of hedge funds. Who works there? Why are they so secretive? Why do the wealthiest in the world trust them with large portions of their assets? And lastly, why are they so darn good?

Website for 3rd largest hedge fund in the world.

In today’s world, almost all hedge funds are algorithm based. Very rarely do you have hedge funds that make investment decisions based off traditional security analysis. Machine learning algorithms, Ph.D.’s, even quantum computers are at the crux of most hedge funds today, attempting to beat the market daily and achieve maximum return for their investors and ultimately themselves. One day I hope to tell you the story from the inside, but for now we will start by building our own Python trading algorithm.

The investment strategy that the algorithm will be based on is called the Three White Soldiers pattern. We will define a closing price as a candlestick. If we have three candlesticks in a row that consecutively increase in price, then we have our Three White Soldiers pattern in front of us. See the image below for a visualization of the pattern:

Three candlesticks in an upward trend signify there may be a change in market sentiment.

We will be using an Alpaca brokerage account with paper money(not real money). First we set up our API so we can get real time data from the stock market:

API_KEY is a unique key given to you when you sign up for Alpaca, and Tickers is Peloton:PTON.

After we set up our connection successfully we want to actually see how the algorithm will function:

In the beginning of the algorithm we created an empty list of all minute candlesticks. In the first section of code we are making sure that we are actually getting our ticks. If the current tick’s price is higher than the current candlestick price we assign the current tick as the new high and vice versa.

Then in the next big section of code we are checking if there are three higher closes minute by minute and assigning names to them for easier understanding.

Lastly, we are going to compare and see if each candlestick is actually a higher price than the previous one, and if so then we execute our trade. We then will take our profit at 2x the distance between the last candle close and the first candle open. Lets see this in action:

Process when I execute the script.

As we can see every minute we are appending a candlestick to our list. The last three closes were all higher than the previous close so the algorithm executed and actually bought 100 shares of Peloton!

My Alpaca account.

Our total profit so far with our bot is 0.01%, I think I’m ready for the hedge fund world.

Future improvements

This is a very basic algorithm but one that works! Some great ways to expand on this would be to automate the buying process, so I do not have to execute the Python command every time I want it to run. In addition, it would really cool to automatically buy the stock when there are three upticks and sell when there are two downticks. Lastly, it would be interesting to tinker with the candlestick data. Instead of adding a candlestick every minute, maybe every thirty seconds would be more beneficial to capturing the true trend of the stock.

Conclusion

Having a project in mind and building out that said project is the best way to learn programming in my opinion. Google and Youtube are your best friends.

--

--