How To Create A Fully Automated AI Based Trading System With Python
The art of stock trading is not as easy as other people make it to be. For many who go into trading, it becomes a full-time job essentially. The time spent looking at various reports, updates, and news articles eventually takes up a good portion of one person’s day.
All these are needed to be kept abreast of the current status of the stock market and to make predictions more accurate and informed. The allure of stock trading is also complemented by the public’s idea of stock traders. They are the young and the rich, the risk-takers who like to work hard and party even harder. Literary works like “The Wolf of Wall Street” only further supports this idea.
And just like any other profession, burnout is a very real threat to those who partake in the activity. The stress of having invested your own or someone else’s money on something as volatile as the stock market is enough to keep one’s anxiety and stress level high. In looking for a reprieve, traders turn to distractions like gambling, partying, or even going on holidays on yachts with a spy camera with audio.
But could they all afford to shut off their trading completely?
Not entirely. In the past, employees and assistants were more than willing to step up to the plate and take control of the trading until such time that the boss returns. Today, we have the might of powerful and interconnected computers to do most of the work without any human intervention. Yes, the stock market is one of those industries that have benefited greatly from artificial intelligence and automation. The question now is can you create an automated system yourself?
It is possible to automate a system that you can use for trading using Python. To save time, I’ve enumerated the steps we need to do before elaborating on them one by one later in the article.
- Gather as much real-time and granular stock price data.
- Instead of using a personal set of parameters, we’re going to be using AI and incorporating it into the system.
- The next step would be to decide on whether to sell, hold, or buy. It is here that the broker comes in to act. You can use services like Alpaca or RobinHood.
- The last thing we need to do is to implement the system somewhere and monitor its activities. You can program the system to send a notification every time the system acts to keep you updated.
Gathering the data
Getting the needed data is not going to be easy. In the past years, there were data sources like Yahoo! Finance API and Google Finance but sadly both services have been discontinued. But there are still a few alternatives available in the market today. In choosing such services, here are a couple of features to keep in mind:
- Free of charge/cheap
- Real-time data
- Easy to use
- High limit rate
Using an unofficial alternative to yahoo Finance API, yfinance is a simple tool for this experiment. You first have to install it after which you can access everything with the use of the Ticker object. More information is available on other methods like ‘dividends’, ‘splits’, ‘earnings,’ and many more. These will return data in ‘pandas’ DataFrame objects.
At this point, we only want to get the price of the stock through time. You can use the ‘history’ method for this step. Select the interval dates and data frequency down to a minute. And now let’s proceed to add AI.
As a caveat, this process is a very simple way to use the ARIMA model in order to forecast what the value of the next stock price will be. Imagine it as a form of a dummy model. You can certainly use other better models if you want to use this for real trading.
You first have to divide the dataframe into two: train and test. And then use the test set to validate the results of the model we just created. Then let’s use the training data model to get the forecast. Note here that the parameters on this model are fixed while in the real world you need to use cross-validation to choose the best ones.
Connect the broker
This next part will depend on which broker you’re going to be using. Two examples we’ve cited previously were Alpaca and RobinHood. Depending on the type of account you have, you might have to work around a few limitations. For example, RobinHood only allows 3 trades per 5 days if your balance is below $25,000. Alpaca, on the other hand, allows more requests but has a limit of 200 requests every minute per API key.
Deployment and monitoring
We will be deploying the system on AWS Lambda. Although not the best option since Lambda doesn’t offer storage and ideally you would want to store the model that you’ve trained. However, for this example storing it at AWS Lambda will do for the meantime. Schedule Lambda to run every day to train the model even more. As for monitoring, we’ll set the model to send notifications on Telegram every time an action takes place.
Like this post? Don’t forget to share it!