Build an AI Stock Trading Bot for Free

Build an AI Stock Trading Bot for Free

Create an Artificial Intelligence Model to Manage Investments

Image for postPhoto from Phys.org

Quantitative Trading

Algorithmic trading is increasing in popularity as new technology emerges making it accessible to more quantitative investors. I have written in the past about the development of algorithmic trading systems in Java. However, Python has incredibly powerful analytical libraries with easy to understand documentation and implementation. This article will introduce you to the core components of developing an algorithmic trading system in Python, as well as deploying a trained AI model to execute live trades. The code for this project and laid out herein this article can be found on GitHub.

This article will be broken up into the following steps:

  • I: Connecting to a Brokerage House
  • II: Trading System Development
  • III: AI Trading Model Development
  • IV: AI Trading Model Deployment
  • (Bonus): Cloud Deployment

Connecting to a Brokerage House

The first step is to connect to a brokerage house which will allow us to receive live data about the securities we are interested in trading. For this article I will be using Alpaca which is one of the easiest free ways to get started algorithmic trading, and for our purposes, AI trading. Create an account and go to the dashboard to generate an API key.

Image for post

Once you generate your API key you can throw it straight in python. I created a helper class to manage the API connection.

That?s it for the brokerage connection, we can use an instance of the AlpacaPaperSocket class as a reference to act on the API. We will be requesting stock data to give to our AI model down below, but if you are interested in how to request stock information and place orders now the documentation can be found here. It is important for me to note that this is a piece of the puzzle, you can use whatever brokerage house you would like to.(Obviously one that offers an API to fulfill data and order requests.)

Trading System Development

Now that we have established connection to the brokerage house, we can build our trading system. I have created a new design pattern capable of housing systems for any security with varying time frames and different AI models. Sounds complicated? Don?t worry, its actually a very simple design.

Image for post

The main idea is to construct an abstract TradingSystem class so that we can implement custom rule sets for each type of system we wish to trade with. The code is rather straight forward, allowing us to initialize the system and thread an infinite loop.

The TradingSystem is an abstract class with a few abstract functions. Allowing the functions to be abstract lets them vary from implementation to implementation while holding the similar class structure. For example, consider you have a portfolio management system and a day trading system. The portfolio management system?s system_loop will house a different AI model than the day trading system?s system_loop. The implementation of the abstract class TradingSystem is very straight forward. For the purpose of this article I will be building a portfolio management system, and later you will see me train an AI model to execute trades. For now, consider the following implementation?

This PortfolioManagementSystem will house the AI which will execute trades. We will return to this implementation after we develop our AI trading model.

AI Trading Model Development

For this system, I will be building and training an AI model to act as the portfolio manager for my system. The idea is to train the neural network to buy at a certain threshold of negative change and sell at a certain threshold of positive change in the stocks price. We are essentially teaching our AI to buy the dip and sell the rip. [Please note I DO NOT recommend you implement this in a live system, we will discuss this subject further down] To train this neural network, I will build and annotate a data set based on weekly historical market data for IBM and create a feature called signal which will yield a value in the set {0, 1, -1} based on a threshold of change.

Image for post

Let?s now consider the architecture of this neural network. We will be saving the weights after back propagation so after successfully testing the model we can deploy it.

We will be using a Sequential model, however, it is important to understand the mathematics behind the scenes or else we can?t expect our AI to make the decisions we want it to. In this case, choosing the activation function is critical, if we close our eyes and choose ReLU with a binary crossentropy loss function we will get a confusion matrix looking similar to the following?

precision recall f1-score support -1 0.00 0.00 0.00 21 0 0.33 1.00 0.50 22 1 0.00 0.00 0.00 23 accuracy 0.33 66 macro avg 0.11 0.33 0.17 66weighted avg 0.11 0.33 0.17 66

Why is this the case? Let?s visualize the ReLU function?

Image for postReLU Activation

How can we expect our model to recognize a short signal if we are setting the negative inputs to zero, or using a loss function expecting a binary output? Hyperbolic tangent and hinge loss are here to help.

Image for postHyperbolic Tangent Activation

Updating our neural network, recognizing where we went wrong we have the following model.

After training the model we find a significant improvement in our classification report.

precision recall f1-score support -1 0.83 1.00 0.91 25 0 1.00 0.06 0.12 16 1 0.71 1.00 0.83 25 accuracy 0.77 66 macro avg 0.85 0.69 0.62 66weighted avg 0.83 0.77 0.69 66

Now that we have successfully developed our model its time to save the model and load it into a class dedicated to hosting it.

We verify the structure of our neural network and weights loaded correctly by looking at the classification report of the entire data set.

precision recall f1-score support -1 0.73 1.00 0.84 95 0 1.00 0.07 0.14 69 1 0.77 1.00 0.87 97 accuracy 0.75 261 macro avg 0.83 0.69 0.62 261weighted avg 0.82 0.75 0.67 261

Great! Now we have built an AI portfolio manager to make the decision to buy, sell, and hold based on the change in stock price. This is often the most sought after piece of any trading system. Back in college, when I would run my algorithmic trading system for the futures markets with annual returns over 20%, the first question was always ?But, how does it know when to trade??. This is one of the most difficult questions to answer, but when you can answer it you have a profitable trading system. The development of a profitable AI trading model is beyond the scope of this project. However, if this article gets enough support I would be happy to write about which quantitative techniques I use to develop profitable AI trading models.

Deploying the AI Model to a Trading System

Revisiting the implementation of the abstract TradingSystem class we have our PortfolioManagementSystem. I have updated the abstract functions to fulfill their respective purpose.

Let?s talk about the system_loop. The system_loop initializes variables for this weeks close, last weeks close, the current delta, and the day count. The infinite loop (threaded for concurrent systems) is responsible for gathering data once a day, and determining whether or not we have reached a weekly split yet. Upon reaching a weekly split the variables are updated and we consult our AI on whether or not to buy or sell.

Cloud Deployment for 24/7 Up-time

Naturally a question that arises is ?Do you expect me to run this Python script all week on my computer? What if the power shuts off, I lose internet, etc??. The answer, no I don?t expect you to run this Python script all week, I expect you to host it by taking advantage of cloud deployment. This will allow for 24/7 up time of your software while mitigating the complications of running it on your own machine. If you are interested in deploying your model to the cloud you can accomplish this from a fantastic tutorial on algorithmic trading system deployment (steps which will be synonymous to AI trading bots) on Google Cloud here.

Conclusion

This article was created to get you started developing artificial intelligent stock trading bots. We went over how to connect to a brokerage house, specifically Alpaca for this example. Then we created the TradingSystem class itself and its inherit fields along with an implementation of this class in a system dedicated to portfolio management. Afterwards, we built an artificial intelligence model to make trading decisions and discussed issues with a lack of understanding of the mathematics behind the scenes. Lastly, we deployed the model to the implemented system giving our AI the capabilities of buying, selling, and holding. There are a variety of upgrades you can make to this bot, optimizing for speed, AI architecture, P/L reporting, etc? Nevertheless, this is how you can build a free artificial intelligent stock trading bot in Python.

16