AI-Powered Trading Bots

Fintech MK
11 min readNov 4, 2021
Fintech Thursdays — Stojancho Tudjarski

Legal Notice

The materials presented here are not trading advice!

Act upon their results on your own behalf, and keep in mind that you can lose all of your money!

Introduction: AI + Finance

Money and finance have a long-term love story with AI. They were meant to be together. Financial services firms have been dreaming of harnessing AI for decades, but their ambitions often don’t align with their abilities. The AI research community, on the other hand, was intrigued by money but didn’t want to touch it with a ten-foot pole due to concerns over scalability and compliance. Fortunately, financial firms finally embrace AI technology for trading purposes, with deep learning at its core.

Automated Trading with Trading Bots

With the rapid development of AI-powered technology, Artificial Intelligence (AI) has permeated almost every industry, including finance and trading. In this article, we will cover in detail the role of AI in the business (and passion) of trading, starting with the basics and ending with some unique approaches that have been developed recently in this field. We will also show how to deploy your own AI-powered trading bot, having some previous knowledge of coding or programming!

A trading bot is a piece of software designed to execute orders in online financial markets with minimal or no human intervention. It can read market data and decide when to buy and sell to maximize profit with minimal risk. This article aims to shed some light on the various technologies that go into building an artificial intelligence-powered trading bot, focusing on the role of machine learning in this process.

Traditional Trading Bot Technology

At its core, a trading bot (also known as an automated trading program or robot) consists of a trading strategy and software that monitors the real-time stock movements and automatically executes actions based on that trading strategy.

In other words, a trading bot is simply software that buys and sells financial instruments without human intervention, guided by trading strategy.

This leads us to the trading bots’ main building blocks: trading strategy and software that monitors the stock movements and acts accordingly.

Trading Strategies

A trading strategy is a defined way to react to the real-time movements of specific financial instruments’ prices and trade volumes.

Traditional, manually designed trading strategies, bots are confined to rigid, predefined rules for buying and selling. Therefore, the executor of the bot is simply focused on the current values of some technical indicators. The most popular technical indicators are long, medium, and short-term weighted moving averages (and their linear combinations) and Bollinger bands, but many of them are. I’m frequently reading about the new ones, proving their efficiency in some specific instruments. When those indicators cross some threshold value, some action occurs. When you create a trading bot program based on changes in price or volume, for example, once a new price level has been established as a new support or resistance level, your bot will buy when it drops below that level and sell when it goes above that level. This kind of approach is quite straightforward and doesn’t require too much sophistication in terms of Artificial Intelligence capabilities. That’s why such techniques were used during most of financial history and still are used nowadays by many people for their own investments.

The main advantage of using this kind of trading bot was to apply the predictable and disciplined behavior of the algorithm by eliminating the humans’ risk of unreasonable behavior elaborated with the well-known set of cognitive biases and human emotions.

Typically, building a trading strategy consists of:

  • choosing a financial instrument,
  • choosing one technical indicator
  • arbitrary choosing low and high values for the selected indicators when some buy or sell action should be performed
  • carefully performing backtesting of the defined strategy

Trading Executors

In a more abstract point of view, the trading strategy is a black box that receives stock movements at the input. It generates a sequence of buy and sell signals at the output when it decides to. The technology of the trading strategy executors, among adequately designed systems, remains the same and is independent of the chosen trading strategy. Therefore, the executor simply connects the outside world, i.e., real-time stock movements, with the trading strategy and adequately buys and sells some volume of designated instruments, being directly connected with the market via brokers. With the credentials provided by the owner of the account at the broker’s house, and backed by real money, of course :-)

Finally, AI in the Story

And, finally, let’s talk about AI. Technically speaking, there are several ways the stock movements can be abstracted so that AI and Machine Learning terminology and technologies can step in with their full potential.

We’ll cover two of them:

  1. Time series,
  2. RRNs and LSTMs
  3. Reinforcement Learning

Time Series

This is pretty much straightforward. To be honest, can’t see a more native and intuitional way how to present stock movements. First, they heavily depend on the previous stock movements, i.e., history, and second, you don’t know the future. Therefore, they are auto-regressive in their very nature. In other words, in short: time-series.

Consequently, all the heavy artillery ML is equipped for time-series analysis, and prognosis can be applied here. The latest state-of-the-art algorithm here is Facebook’s Prophet. It’s one year old and already famous for its outstanding predictive performance.

RRNs and LSTMs

Deep Learning brought one advantage of using neural networks for stock predictions in a self-regression manner. Recurrent Neural Networks (RNNs) inherently offer the possibility to apply them over time series, with their new predicted value with each new input, taking into account the several past inputs simultaneously. The built-it lousy side effect is that they “forget” very quickly what they “experienced” in the past, just several inputs, i.e., time-slots ago. Long-Short-Term Memories (LSTMs) improved this behavior by enhancing the architecture of the neural networks in a way that they became capable of “learning” what to “remember” longer and what to “forget” shortly after. Still, the major drawback remained. The lousy “easy forgetting” side effect mentioned for RNNs was delayed but not overcome. The next try involved attention mechanisms in RNNs and LSTMs, an additional “controlling forgetting” layer above all of that. Still, the forgetting effect remained in place.

The serious drawback here is that the time series and Deep Learning algorithms look for past values patterns and seasonalities and their dependence on past values only. Unfortunately, financial markets are too complicated and heavily unpredictable to be taken into account with this technology. Simply, one single event, seen in the past very few times, can significantly impact the real-time stock movements in terms of seconds. Trump’s ban of some Chinese stocks from NUSE, for example. Or, a tragic accident with a nurse’s death, vaccinated two days ago with experimental Moderna Covid-19 vaccine. Examples are countless.

It appeared that more advanced AI approaches backed with Deep Learning are more robust to such kinds of sudden incidents/accidents.

Reinforcement Learning

Reinforcement Learning was publicized by the Deep Mind company, currently owned by Ellon Musk. It gained popularity by providing software that beat humans in a few games, Go, for example. Just to mention that the Go game has several orders of magnitude (120, to be more precise) more possible situations on the board than chess. Even famous for beating humans in games, it appeared that we could reinforce the same principles over the “trading stocks game”.

When dealing with Reinforcement Learning, we are talking about Environment, State, Action, Reward, and Policy goals.

How about seeing the stock market from the following perspective:

  1. Environment: Stock market
  2. State: Our portfolio stock prices
  3. Action: Take or leave a BUY or SELL position
  4. Reward: This is quite simple, the money, isn’t it?
  5. Policy goal: Make more money

As a consequence, it is evident: Now we are talking about Reinforcement Learning.

Furthermore, we raised the game to an entirely new level: we are talking about managing portfolios instead of predicting single stock movement, which is almost mission impossible. Everybody with some financial insights is aware of the benefits of spreading risks to a broader set of instruments.

Implementation Guidelines

Introduction to FreqTrade

FreqTrade is a framework for building a crypto-trading bot.

-Based on Python 3.7+ under Windows, macOS, and Linux.

-Persistence is achieved through SQLite embedded DB.

-Dry-run: Run the bot without paying money.

-Backtesting: Run a simulation of your buy/sell strategy.

-Edge position sizing: Calculate your win rate, risk-reward ratio, the best stop-loss, and adjust your position size before taking a position for each specific market.

-Blacklist/whitelist cryptocurrencies to work with.

-Manageable via Telegram: Manage the bot with Telegram. Control it from your phone!

-Display profit/loss in fiat.

-Provide a daily summary of your profit/loss.

-Provide a performance status of your current trades.

One cool feature here is that we can use this library/tool as a trading bot executor and independently define and implement the trading strategy.

FinRL

Well, nothing to say something special here, but this: Reinforcement Learning is a complicated topic, and you don’t want to deal with it. Trust me on this. Still, you can leverage it by using FinRL library. It makes it easy and deployable as a black box trading strategy within FreqTrade tool.

The Latest and Maybe the Greatest: Their Majesty the Transformers

Transformers neural networks architecture proved its tremendous value for Natural Language Processing tasks: it beat in all NLP benchmark tests. Still, as already shown above: the numbers are still numbers, no matter in which domain. Therefore, no reason not to try to implement strategies proven as the best in some specific use-cases to other ones. Above, we discussed using Reinforcement Learning with proven validity in games to apply portfolio optimizations and generate buy and sell signals.

This work is still in its early development days; there are already some implementations for time series forecasting using encoder-decoder transformers schema. We’ll see how it will look like after a few months.

Conclusion

In conclusion, it is clear that trading bots with AI are a very viable option. The technology stack is already there and performing extremely well. There are already many startups out there utilizing artificial intelligence in trading bots, and I’m sure that number will increase over time. When that happens, you should be prepared to use AI powered trading bot to improve your portfolio or even take advantage of automated trading opportunities where you can profit from other people’s trades. However, you should also be aware of how much work goes into creating one. You have to evaluate which broker supports which exchanges, review historical data for trends, set up an environment for testing algorithms, integrate technologies like Google Cloud ML Engine into your codebase, etc… Overall it’s not just about writing code — the process behind developing an AI-powered trading bot is much more involved. That said, once you have all these things together — you could build an algorithmic machine that makes money for you while you sleep! Isn’t automation cool?

Nowadays, trading bots have evolved to become a whole lot more than just a software program. It’s true that trading bots can get you into trouble if you don’t know what you’re doing, but all of that is changing thanks to AI. Now it is possible for trading bots to integrate Artificial Intelligence and rely on complex algorithms instead of standard ones. What does that mean for traders? It means they can be more effective while spending less time learning complicated trades and creating a strategy. Instead, they can focus on analyzing market patterns and getting in tune with their chosen markets. When it comes to bot trading strategies, anything that makes trading easier is always welcomed by professionals! If you would like to learn more about AI-powered bot trading strategies, then please contact striki@striki.ai. We will be happy to answer any questions or concerns you may have regarding our technology or your business in general.

References

[2102.05624] NAST: Non-Autoregressive Spatial-Temporal Transformer for Time Series Forecasting (arxiv.org)

Option Pricing Using Reinforcement Learning | by Letian Wang | The Startup | Medium

Combining Technical Indicators with Deep Learning for Stock Trading | by Victor Sim | Artificial Intelligence in Plain English | Jan, 2021 | Medium

Automating Stock Investing Technical Analysis With Python | by Farhad Malik | FinTechExplained | Feb, 2021 | Medium

A review of Reinforcement learning for financial time series prediction and portfolio optimization | by Nick Smith | Journal of Quantitative finance | Jan, 2021 | Medium

Algorithmic Trading with Economics Driven Deep Learning | by Yao Lei Xu | Jan, 2021 | Towards Data Science

Encoder-Decoder Model for Multistep Time Series Forecasting Using PyTorch | by Gautham Kumaran | Towards Data Science

#thisisalmostautogeneratedtext

Author:

Stojancho Tudjarski

Graduated software engineer with 29 years of experience in the IT industry.

The last 11 years he spent as an AI practitioner, learning, doing, and consulting. The last 18 months, intensively focused on the latest breakthroughs in the NLP area of AI, which included BERT and GPT-2 language models, and experimenting with them a lot. Trained his own BERT and GPT-2 models for all of the Balkan languages, making them appearing for the first time, publicly available for the first time, at https://huggingface.co/macedonizer, with the support from the Maceonizers team from FINKI.

Experimented extensively with fine-tuning GPT-2 with Shakespeare’s writings, among other things. Result: a neural network capable of writing poetry in Shakespeare’s style, at a scale. In addition to this: passed through the process of mixing styles, which resulted in an automatic text generator about Covid-19, in Shakespeare’s style. All the experiments are documented at https://dmind.ai. The same is done with Blaze Koneski’s writing style, autogenerated texts available on the same site.

Continually monitoring the latest happenings in the AI area on a daily basis and evaluating ideas where the new academic advances can be monetized in the business.

--

--

Fintech MK

First Fintech Community in North Macedonia which aims at developing and enabling the Fintech Ecosystem regionally.