# 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! đ» đ

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? đ€**

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).

Meme credit:Â https://blog.evjang.com/2018/11/memes.html

**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.

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.

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*,

*, or*

**unsupervised***? Is your task a matter of*

**reinforcement learning***,*

**classification***, 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.*

**regression****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Â

*Â mentioned above!*

**time period bias**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

*in any way when you split it!*

**biased****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***, and*

**IC***to determine whether or not youâre ready to deploy. Change*

**alpha***, check your data for flaws, and make sure the architecture you are using is appropriate!*

**hyper-parameters****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

## You might also be interested in

#### 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.

#### 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! đ» đ

#### 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.

#### Find more resources to get started easily

Check out our documentation to find out more about our API structure, different endpoints and specific use cases.

#### Join lemon.markets community

Join our Slack channel to actively participate in our community, ask questions to other users and stay up to date at all times.

#### Interested in building lemon.markets with us?

We are always looking for great additions to our team that help us build a brokerage infrastructure for the 21st century.