Dummies Guide to Trading with Machine Learning


blog photo
Eric Tang12. August 2022
Trading


Ever wonder how a trader with decades of experience on thousands of stocks and lightning fast reaction times might perform in the market? With some machine learning knowledge, you might be able to automate such a trader yourself! 💻 📈

As we’ve witnessed the increasing involvement of machine learning (ML) in finance, we here at lemon.markets 🍋 (the Berlin-based fintech start-up dedicated to building a trading API for developers) decided to compile our guide for those curious about how ML works and driven enough to implement their own ML models for trading in the stock market!

For those of you with no machine learning experience, don’t be afraid! This will be a “dummies” guide targeting people at all levels of experience. Readers who need to know more about the financial side and those who need to learn more about the technological side are all equally welcome here! Even those with some experience may benefit from some of the learnings we’ve condensed into this article. For those who prefer videos to reading, here is a companion YouTube video for the vocabulary part of this article: right here! Now, let’s get you from this: 😵‍💫, to this: 🤑 when it comes to ML in algorithmic trading!

What is Machine Learning in Finance? 🤑

Biblical Noah questions elephant (algorithms) and penguin (maths) about their hybrid pengui-phant child (machine learning).

Machine learning is the use of a computer to learn through finding rules/patterns in data. It relies heavily on statistics and mathematical algorithms to implement. As such, ML is great for processing incredibly large amounts of data and finding rules/patterns or making predictions based on it. Now that we know what ML is, let’s consider why we use it for finance in particular.

Why use Machine Learning in Finance? 🤷

ML can extend the capabilities of the average human trader: it doesn’t need to sleep, it isn’t interfered with by emotion, it can process vast amounts of data quickly, it has much faster reaction times, and it can arguably learn faster than humans. These characteristics of ML make it useful for possibly predicting the market, optimizing trading strategies, and providing insight into patterns beyond traditional algorithmic trading strategies. ML’s advantages make it capable of spotting patterns that might be invisible to humans and making optimizations to trading strategies that would be too tedious for humans.

There are various ways to use ML: either as an entirely automated trading bot, or as a tool to help you trade. The former is difficult to implement and requires a fair bit of experience, but it is the most appealing end product. It’s also the project idea many beginners mistakenly aim for when they first start. The latter is a more feasible and practical goal for the those just beginning with ML trading.

Using ML can help you process more data and gain insights into patterns. It may also help optimise your entry and exit points. There are also ensemble ML trading strategies combining several algorithms and models into one large bot to make use of the combined strength of them all! If you ever do discover an algorithm that appears to be profitable remember to (1) factor in fees and expenses before you deploy (sometimes this can turn a profitable algorithm into one that incurs a loss) and (2) keep it secret from everyone else (check out the term “crowding” below to find out why).

So to begin, let’s start with an extensive glossary that will level up your knowledge of ML for trading!

A Few Key Terms and Concepts 🗝

Many of the terms and ideas below actually originate from financial institutions such as hedge funds, while others are particular to machine learning. We have gone ahead and organized these terms into three categories: terms you’ll need for understanding different ML models, terms you’ll need to understand financial metrics, and terms you’ll need to be aware of potential mistakes during development. We hope that this guide can serve as an explanation for all the vocabulary you encounter, both technical and financial!

Models 🤖

Some widely applicable concepts are included here so that you’ll have no trouble understanding online discussions, posts, and papers on trading with ML. These include terms key to the process of development as well as all the different types of models you can build!

preprocessing: Before you can begin feeding data into your ML model, you’ll need to preprocess it first! This can involve formatting it correctly (filling matrices, converting data types, storing everything in a csv/xml file, etc.) and performing operations to account for noise and special cases. This is a crucial part of the ML development process. For more specifics on what you can do in Python in particular, take a look at this GeeksforGeeks article.

  • Examples: Normalizing data to account for more volatile/less volatile stocks. Scaling to adjust distribution/range of data to a standard size. Centering the data — making the mean 0.

bootstrapping: A method of repeated random sampling with replacement from historical periods. Essentially pulling data at random from a larger dataset to create several new independent, random samples. This is also an example of what you might do with data during preprocessing. If you want more in-depth explanations on how and why it works, you can also check out this other article explaining bootstrapping!

aggregating: A method where independent samples of data are run through the ML model and the corresponding outputs for these data sets are then averaged (for regression) or used for a vote (for classification) to determine the final result.

bagging: The combination of bootstrapping and aggregating💰, where datasets from bootstrapping are run through the ML algorithm in parallel and the results are aggregated. This also reduces variance which helps to lower the chances of overfitting.

classification: This is a type of ML task where the output is some number of discrete categories. In classification tasks the model is often asked to predict what to label some input, such as deciding whether a picture depicts a cat or dog.

  • Examples: Will tomorrow’s return be positive/negative? Is a stock trending down or up? Is a social media post bullish or bearish in sentiment?

regression: This is another type of ML task where the model must give an estimate of a quantity that is continuous in nature. As the term regression suggests, this might involve fitting a best-fit line to some input data.

  • Examples: Predict the gains of positions by the end of the day. Forecast what price a stock will be in a week.

supervised learning: This is one of the main types of ML. In these types of models, the data points are labeled with the desired result and the model is used to find a rule/pattern so that it returns the corresponding outputs for any input you give it. Classification/regression and bagging are all terms associated with supervised learning in particular.

  • Examples: Linear Regression, Logistic Regression, Support Vector Machines, Multi-class Classification, Decision trees, Bayesian Logic.
  • Dummy Example: Labelling lots of pictures “lemon” or “lemonade” and having the ML model learn to identify lemon and lemonade pics.

unsupervised learning: This is another of the main types of ML. Unlabelled data is fed into the model in this case, and the machine decides for itself how to find clusters or some sort of rule.

  • Examples: Hierarchical clustering, K-means clustering, Principal Component Analysis, Singular Value Decomposition, Independent Component Analysis.
  • Dummy Example: Giving the ML model lots of unlabelled lemon pictures and having it identify features that are characteristic of lemons (yellow, generally round, may have a leaf attached, etc.).

reinforcement learning: This is a type of machine learning where an intelligent agent takes actions in an environment and learns through a trial-and-error method.

  • Examples: Q-Learning, Deep Deterministic Policy Gradient, Deep Q Network
  • Dummy Example: You may have heard this term being thrown around a lot, especially in connection to AlphaGo and other creations of DeepMind. In this case the model played countless games of Go against itself to learn the optimal strategy.

ensemble learning: This is a method of machine learning where multiple algorithms are combined to generate better performance than you would obtain with any of them individually.

Example: Combining dozens or even hundreds of decision trees to create a random forest with better predictive accuracy (see meme below).

deep learning: A type learning where the model is a neural network, a model inspired by the structure of the brain consisting of many “neurons” organized into multiple hidden layers.

  • Example: The ML models behind language translation, image generation and recognition, and autonomous driving all involve deep learning.

learning rate: A scalar multiplier used when training a model with gradient descent. During this, the learning rate is multiplied by the gradient. Tweaking it can help your model avoid local maxima in search of the global maximum or speeding up the rate at which your model reaches a maximum.

batching: A technique of splitting the data into randomly selected batches of equal size. This helps reduce the chances of the model hitting a bottleneck in terms of memory requirements (easier to calculate and store error values for 100 predictions in memory than 10.000 predictions 🙃) and can give you some rough idea of runtime.

hyperparameters: The variables you tweak throughout development to fine-tune your ML model. Some common hyperparameters: the learning rate and batch size.

backtesting: Once you’ve finished coding your model you have to test it! Backtesting is when you test the strategy on historical data: acquire some OHLC data, calculate returns, look at risk, judge your ML model’s performance, etc. (we’ve included several financial terms and metrics you can use to judge your ML model below!)

There are several software solutions out there for backtesting offered through TradingView, NinjaTrader, and Portfolio Visualizer among others. If you want to do some more coding: Python has backtesting.py and bt, while R has PerformanceAnalytics and PortfolioAnalytics. This is by no means all the backtesting tools out there, simply do an online search to find countless others if none of these fit your preference!

Metrics 📊

How do you know when you’ve made it? Keep these financial metrics in mind for judging whether or not your project is a success. These include ways of assessing the performance and risk of your strategy!

alpha: Defined as portfolio returns in excess of the benchmark used for evaluation, essentially how much you might be beating the S&P 500. It is the abnormal rate of return on investment you are gaining from actively managing a portfolio. Check out Investopedia for more details!

Risk-Free Rate: The risk-free rate can be calculated by subtracting the current inflation rate from the yield of the Treasury bond matching your investment duration. This is theoretically zero-risk (since bonds get paid off unless the government defaults/collapses, in which case you have more problems than just losing your investment ☹️) and is used when computing the Sharpe Ratio.

Sharpe Ratio: This is the average return earned in excess of the risk-free rate per unit of volatility. It models the portfolio’s performance given the amount of risk taken. Its weakness is that it assumes investment returns are normally distributed. This can be used to evaluate the risk of an automated strategy.

Information Ratio (IR): The measurement of portfolio returns above the returns of a benchmark, usually an index such as the S&P 500, to the volatility of those returns. It is essentially a metric for balancing risk and returns just like the Sharpe Ratio. It differs from the Sharpe Ratio in that it uses a specific benchmark rather than the risk-free rate.

maximum drawdown: The maximum observed loss from a peak to trough in a portfolio. It is expressed in percentage terms of the peak and serves as a way of indicating downside risk over a specified time period. This may be a metric you want to consider during backtesting to assess the risk profile of your strategy.

Information Coefficient (IC): This metric measures the quality of a forecast (shows how closely forecasts match up to actual results). It is often used to evaluate the skill of an analyst. The IC can be useful in determining whether or not your trading algorithm is performing well or not and is calculated by: IC = (2 x Proportion Correct) - 1.

Mistakes 🚫

Let’s cover some common mistakes that occur in ML. These can happen with poorly chosen or badly organised data, or issues with the ML model’s architecture/design.

look-ahead bias: A potential mistake one could make while backtesting, where data that was not readily available at the time is used when simulating historical conditions.

  • Example: You have price information from a year into the future leaking into your backtest which may heavily interfere with the results.

sample selection bias: Choosing non-random data which impacts or negates statistical significance of a test or disrupts the chances your ML model will learn the real patterns underlying some stock price movement.

  • Examples: Survivorship bias where you fail to account for stocks or funds that have completely failed and are no longer in the market. Self-selection bias, such as an online post asking for machine learning resources only receiving responses from people trying to sell you ML courses. 😕

time period bias: Sampling error caused by selecting observations that only cover a specific time period which limits the data’s scope or captures some anomaly rather than data that represents the norm.

  • Example: Sampling from a few years where company had reduced operating costs which helped their stock price immensely.

overfitting: This is what happens when your model fits the training data too well. In this case, the algorithm is no longer generalizable and fails to be useful. Take a look at the graph below to see how overfitting is detrimental for an ML model.

Picture of horrendously overfit regression line in graph of lemonade stand profits.

In this case the black regression line is overfit as it closely follows an anomaly where profits fell in May (maybe there was a sudden reduction in global lemon supply?). This will likely cause it to be inaccurate when generalized to try and predict profit for other years.

under-fitting: The opposite of overfitting; this is when your model does not fit at all to training data. In this case, the model has not managed to learn any sort of pattern or rule. Take a look at the graph below to see how under-fitting is detrimental for an ML model.

Picture of horrendously underfit regression line in graph of lemonade stand profits.

In this case, the black regression line is underfit as it fails to be an approximation of monthly profits.

crowding: The tendency of investors to focus on similar ideas/strategies. This is not so much a mistake, but an important phenomenon to keep in mind, since this eventually leads to a signal disappearing (the inefficiency in the market disappears after too many investors crowd a signal and eventually there is no more alpha to be made). This is part of why machine learning models need to consistently be retrained and why many algorithmic strategies are short-lived if not kept secret.

Basic Roadmap for Your Machine Learning Project 🗺

Now that we’ve clarified some key terms, let’s take a look at how these terms stack up in a basic roadmap for building your ML algorithm!

1.Define goals for the model and search for online resources.

Make sure you have a good idea of what your model is meant to accomplish (e.g. input/output, the task you want done, some idea of what accuracy constitutes success). Take time to research what methods are most appropriate for automating the task you want done. Sometimes this may mean you find a non-ML way of achieving your goals!

2. Choose a model architecture/algorithm.

Do you use supervised, unsupervised, or reinforcement learning? Is your task a matter of classification, regression, or something more complex? During this step it may help to look into what others are doing online either through browsing research papers or online forums.

3. Gather data.

Gather some high quality data for training and testing! Of course, this involves making sure your data has none of the sample selection bias or time period bias mentioned above!

In this case you might consider using the lemon.markets market data API endpoint for some historical OHLC stock market data. 😉 If you have any difficulties with this, please let us know through our Slack community, as we are currently working on supporting more complex and diverse use cases through our API!

4. Preprocess the data for the model and split it into testing and training sets.

Now that you have your data, preprocess it so that it fits your model. Afterwards, split it up so that you can train your model on part of the dataset and test it on data it has not seen before. A good percentage split is usually 80:20, 80% for training and 20% for testing. Make sure your data isn’t biased in any way when you split it!

4. Build, test, optimize, repeat.

Try to reduce the loss and increase the accuracy of your model. Do extensive backtesting and take a look at the metrics we mentioned: Sharpe Ratio, IR, IC, and alpha to determine whether or not you’re ready to deploy. Change hyper-parameters, check your data for flaws, and make sure the architecture you are using is appropriate!

5. Deploy!

Finally your model is finished! Go ahead and begin using it for its intended purpose, but make sure to do maintenance and keep track of its performance! Keep in mind, of course, this is only a general overview of a process that could take weeks or months.

The most difficult parts where one can easily mess up or become stuck are of course finding and preprocessing high quality data as well as fine-tuning the model. Check out our “10 mistakes when it comes to (automated) trading and how to avoid them” blog post so that you might avoid some of these obstacles when getting started yourself! Some of these mistakes are also referred to in the Key Terms section above (e.g. overfitting, survivorship bias, etc.)!

Limitations of Machine Learning 🥲

Naturally, machine learning is not the solution to everything, nor is it a free-money hack. ML strategies often struggle when given too many variables and when they are made to predict too many different agents. Furthermore, they require high-quality data, lots of it. As complex algorithms built on many statistical and computational concepts, they also demand a great deal of knowledge and experience from those attempting to implement them. On top of all that, many models, especially those using unsupervised or reinforcement learning end up being black boxes; even the creators will be unable to fully explain how they work the way they do.

Used correctly and implemented well, however, an ML trading algorithm can be an advanced trading tool with great potential (and profit 🤑). As with any nascent technology that’s trendy, ML might prove to be absolutely useless for some trading tasks, but there is no doubt that it will become indispensable to the financial traders of the future. Therefore, even if you never develop a profitable algorithm for personal use, the concepts you learn and pick up along the way may help you immensely in your professional life (especially if you work in tech or finance-related fields). Furthermore, there is room for technological development, and future ML algorithms may overcome the limitations they face now!

Upcoming lemon.markets Machine Learning Projects? 🍋🤖

The goal of this article was to provide a brief intro and overview on ML and set up for more in depth discussions in subsequent content pieces. The list of terms above is by no means exhaustive. If you believe some should be added or removed so that future viewers may find this article more helpful, message us down in the comments below or on Slack! If you’re looking for a glossary that is even more extensive with expert-level terms and definitions, you can also check out the Google Machine Learning Glossary!

If you wish to learn more about algorithmic trading and levelling up your stock market knowledge, take a look around and follow the lemon.markets Medium page as we have much more content planned. This is just the first part in a series of blog posts and videos exploring how you can develop your own ML project with lemon.markets!

I hope you enjoyed this article. The goal was to introduce you to some basics of using machine learning to forecast and trade in the stock market!

Don’t forget to sign up to lemon.markets to start building your own algorithmic trading project. If you have any questions, make sure to contact us via support@lemon.markets or join our Slack community.

We are looking forward to your (ML) projects with lemon.markets! 🚀

🍋 Eric

Das könnte Dich auch interessieren

blog photo

Using Time Series Forecasting to predict stock prices 🔮

In this article you will learn what time series forecasting is and how its application in finance looks like. Then you'll also dive into Facebook's Prophet Model for Time Series Forecasting and use it together with the lemon.markets Market Data API to forecast the development of the Tesla Stock.

blog photo

Dummies Guide to Trading with Machine Learning

Ever wonder how a trader with decades of experience on thousands of stocks and lightning fast reaction times might perform in the market? With some machine learning knowledge, you might be able to automate such a trader yourself! 💻 📈

blog photo

4 reasons why YOU should automate your trading strategy

In the current volatile market conditions, everyone is trying to find ways to minimise portfolio loss. In that context, have you ever thought about automating your trading strategy? In this article, we will dive into 4 reasons for doing so. Expect to learn how it can save you time, make your trading more efficient and lead to data-based decisions.

Tiefer eintauchen

Finde weitere Ressourcen für einen einfachen Einstieg

In unserer Dokumentation erfahrt Ihr mehr über unsere API-Struktur, die verschiedenen Endpunkte und spezifische Anwendungsfälle.

Austauschen

Tritt der lemon.markets Community bei

Tritt unserem Slack-Channel bei, um Dich aktiv an unserer Community zu beteiligen, Fragen an andere Nutzer:innen zu stellen und immer auf dem Laufenden zu bleiben.

Team unterstützen

Lust lemon.markets mit uns zu bauen?

Wir sind immer auf der Suche nach großartigen Ergänzungen für unser Team, die uns beim Aufbau einer Brokerage Infrastruktur für das 21. Jahrhundert helfen.

Brauchst Du Hilfe?
Stell Deine Fragen in unserer CommunityStell Deine Fragen in unserer CommunityLeg mit unserer API Dokumentation losLeg mit unserer API Dokumentation losLass Dich von unserem Blog inspirierenLass Dich von unserem Blog inspirieren
© lemon.markets 2022DatenschutzImpressum
Systems are down

Als vertraglich gebundener Vermittler gemäß § 3 Abs. 2 WpIG für Rechnung und unter der Haftung der DonauCapital Wertpapier GmbH, Passauer Str. 5, 94161 Ruderting (kurz: DonauCapital), bietet Dir die lemon.markets GmbH, die Anlagevermittlung (§ 2 Abs. 2 Nr. 3 WpIG) von Finanzinstrumenten im Sinne des § 3 Abs. 5 WpIG sowie die Vermittlung von Konten an.