{"id":10,"date":"2020-09-18T10:54:00","date_gmt":"2020-09-18T10:54:00","guid":{"rendered":"https:\/\/eodhd.com\/financial-academy\/?p=10"},"modified":"2025-02-05T13:25:53","modified_gmt":"2025-02-05T13:25:53","slug":"i-tested-a-machine-learning-models-performance-in-the-stock-market","status":"publish","type":"post","link":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market","title":{"rendered":"I Tested a Machine Learning Model\u2019s Performance in the Stock Market"},"content":{"rendered":"\n<p id=\"cc6a\">When it comes to using machine learning in the stock market, there are multiple approaches a trader can do to utilize ML models. From determining future risk to predicting stock prices, machine learning can be used for virtually any kind of financial modeling.<\/p>\n\n\n\n<p id=\"69f7\">In our previous articles, we delved into the usage of two&nbsp;<a href=\"https:\/\/www.aptech.com\/blog\/introduction-to-the-fundamentals-of-time-series-data-and-analysis\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Time Series<\/em><\/a>&nbsp;models:&nbsp;<strong>SARIMAX<\/strong>&nbsp;and&nbsp;<strong>Facebook Prophet<\/strong>. We utilized both of these models to forecast the potential, future prices of Bitcoin. Check out the following article if you are interested: <a href=\"https:\/\/towardsdatascience.com\/predicting-prices-of-bitcoin-with-machine-learning-3e83bb4dd35f\">https:\/\/towardsdatascience.com\/predicting-prices-of-bitcoin-with-machine-learning-3e83bb4dd35f<\/a><\/p>\n\n\n\n<p>In another article, we used&nbsp;<a href=\"https:\/\/www.edureka.co\/blog\/classification-algorithms\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>classification models<\/em><\/a>&nbsp;to classify stocks based on their performance in quarterly reports. You can read about the entire process of how we engineered different features from these reports and trained our classification models to determine investment decisions in the article below: <a href=\"https:\/\/medium.com\/swlh\/teaching-a-machine-to-trade-stocks-like-warren-buffett-part-i-445849b208c6\">https:\/\/medium.com\/swlh\/teaching-a-machine-to-trade-stocks-like-warren-buffett-part-i-445849b208c6<\/a><\/p>\n\n\n\n<p id=\"b9f8\">These are just a couple examples of how we can utilize machine learning models for financial markets.<\/p>\n\n\n\n<p id=\"4a25\">In these articles, we just used models to predict future prices, however, that is just half the battle. The next step is how do we evaluate these models if they were to actually be used while trading? The answer to that question is called \u2014&nbsp;<strong><em>Backtesting.<\/em><\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"22fd\">Backtesting<\/h2>\n\n\n\n<p id=\"f0aa\"><em>What is backtesting?<\/em>&nbsp;Backtesting is the process of applying a trading strategy, predictive model, or analytical method to historical data to evaluate its accuracy and performance.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>It is very important to note that backtesting&nbsp;<strong><em>does not<\/em><\/strong>&nbsp;100% accurately, represent live-trading in the past. However, you should only use it to inform your decision to live-trade your strategy or not. Even then, it may be more practical to&nbsp;<a href=\"https:\/\/www.investopedia.com\/articles\/trading\/10\/backtesting-walkforward-important-correlation.asp#:~:text=Forward%20Performance%20Testing%20Basics,logic%20in%20a%20live%20market.\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>forward-test<\/strong><\/a>&nbsp;your strategy before trading with real money.<\/p>\n<\/blockquote>\n\n\n\n<p>When we create a machine learning model, we need to backtest the model in order to determine how well it would have potentially performed in the past by feeding it historical data. There are two approaches when it comes to backtesting \u2014<strong>&nbsp;<\/strong><a href=\"https:\/\/www.quantstart.com\/articles\/Event-Driven-Backtesting-with-Python-Part-I\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Event-Driven<\/strong><\/a>&nbsp;and&nbsp;<a href=\"https:\/\/towardsdatascience.com\/backtest-trading-strategies-with-pandas-vectorized-backtesting-26001b0ba3a5\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Vectorized Backtesting<\/strong><\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5ce9\">Vectorized Backtesting<\/h3>\n\n\n\n<p id=\"9bd8\">Today, we will be using vectorized backtesting in order to evaluate the performance of our machine learning model. This approach allows us to quickly observe how our ML model might have performed in the past.&nbsp;<em>If you would like to learn more about vectorized backtesting, then we suggest reading the following article by a machine learning researcher which contributed to the outcome of this current project:<\/em> <a href=\"https:\/\/towardsdatascience.com\/backtest-trading-strategies-with-pandas-vectorized-backtesting-26001b0ba3a5\">https:\/\/towardsdatascience.com\/backtest-trading-strategies-with-pandas-vectorized-backtesting-26001b0ba3a5<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b61f\">Coding Our Machine Learning Model<\/h2>\n\n\n\n<p id=\"050b\">In order to evaluate the performance of a machine learning model, we\u2019ll first have to construct it in Python. The model we will be using is the&nbsp;<em>AutoARIMA<\/em>&nbsp;time series model from the&nbsp;<a href=\"http:\/\/alkaline-ml.com\/pmdarima\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>pmdarima<\/strong><\/a>&nbsp;Python library. This model will quickly find the optimum parameters for us so we won\u2019t need to worry about adjusting any modeling parameters.<\/p>\n\n\n\n<p id=\"5776\">However, if you do want to learn more about the parameters and what they mean, check out&nbsp;<a href=\"https:\/\/towardsdatascience.com\/predicting-prices-of-bitcoin-with-machine-learning-3e83bb4dd35f\" rel=\"noreferrer noopener\" target=\"_blank\">our previous article<\/a>&nbsp;mentioned above that uses time series modeling to predict Bitcoin prices.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"115f\">Step 1. Importing Necessary Libraries<\/h3>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import pandas as pd\nimport numpy as np\nfrom pmdarima.arima import AutoARIMA\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom tqdm.notebook import tqdm\nfrom sklearn.metrics import mean_squared_error\nfrom datetime import date, timedelta\nimport yfinance as yf<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<p>Each library here serves an important purpose for building and backtesting our ML model.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"31f7\">Step 2. Getting the Data<\/h3>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Getting the date five years ago to download the current timeframe\nyears = (date.today() - timedelta(weeks=260)).strftime(\"%Y-%m-%d\")\n\n# Stocks to analyze\nstocks = ['GE', 'GPRO', 'FIT', 'F']\n\n# Getting the data for multiple stocks\ndf = yf.download(stocks, start=years).dropna()\n\n# Storing the dataframes in a dictionary\nstock_df = {}\n\nfor col in set(df.columns.get_level_values(0)):\n    \n    # Assigning the data for each stock in the dictionary\n    stock_df[col] = df[col]<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<p id=\"d1a1\">The data we will be using is a small collection of stocks with about 260 weeks of historical data. To be even less biased towards the performance of our model, we used stocks that have decreased in value in the past few years.<\/p>\n\n\n\n<p id=\"2349\">Another option for acquiring such data is through&nbsp;<a href=\"https:\/\/eodhistoricaldata.com\/?ref=31CX3ILN&amp;utm_source=medium&amp;utm_medium=post&amp;utm_campaign=i_tested_a_machine_learning_model_s_performance_in_the_stock_market\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>a financial data API such as EOD Historical Data<\/strong><\/a>, which has more than just historical price data. It is free to sign up and you\u2019ll have access to vast amounts of financial data.&nbsp;<em>Disclosure: I earn a small commission from any purchases made through the link above.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"d3f5\">Step 3. Preprocessing the Data<\/h3>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Finding the log returns\nstock_df['LogReturns'] = stock_df['Adj Close'].apply(np.log).diff().dropna()\n\n# Using Moving averages\nstock_df['MovAvg'] = stock_df['Adj Close'].rolling(10).mean().dropna()\n\n# Logarithmic scaling of the data and rounding the result\nstock_df['Log'] = stock_df['MovAvg'].apply(np.log).apply(lambda x: round(x, 2))<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<p id=\"332b\">First, we\u2019ll get the log returns for ours stocks, which we used later on to determine overall returns. Next, a 10-day moving average was applied to the dataset in order to smooth out and reduce the noise in closing prices. Finally, we scaled the moving averages by using a logarithmic scale rounded to 2 decimal places.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png\" alt=\"\" class=\"wp-image-20\"\/><figcaption class=\"wp-element-caption\">The 10-Day Moving Averages for our selected stocks<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_Bt0dVf1G36BJvNiRmpEOrQ.png\" alt=\"\" class=\"wp-image-23\"\/><figcaption class=\"wp-element-caption\">Moving Averages on a Logarithmic Scale<\/figcaption><\/figure>\n\n\n\n<p id=\"28c8\">The reason we used moving averages and logarithmic scaling is because we hope that these values will be better suited for our model to predict prices much more accurately. There is no right answer in preprocessing or transforming the data to feed into our model so feel free to experiment with other scales, moving average windows, opening prices, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dde2\">Step 4. Training and Predicting with AutoARIMA<\/h3>\n\n\n\n<p id=\"a174\">In order to run our model to get predictions with AutoARIMA, we will first layout all the requirements we want from our model and how to achieve them. Those things will be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Number of days to train with&nbsp;<\/strong>\u2014 Let\u2019s use about a half year\u2019s worth of data from the past closing prices.<\/li>\n\n\n\n<li><strong>Number of days to predict<\/strong>&nbsp;\u2014 Let\u2019s predict the next 5 days into the future for our forecast amount and then use the last day as the price target for our trading strategy.<\/li>\n\n\n\n<li><strong>When and how often will the model run&nbsp;<\/strong>\u2014 Let\u2019s run the model every other day or whenever the current price reaches or passes the price target.<\/li>\n\n\n\n<li><strong>What date range we want to evaluate<\/strong>&nbsp;\u2014 We can set whatever range we like to backtest our ML model. Feel free to use whatever range you want but be aware that the bigger the range, the longer the training will take.<\/li>\n<\/ul>\n\n\n\n<p id=\"ea58\">All of these values can be tinkered with. Feel free to try out different values to potentially find an amount that works best with this trading strategy.<\/p>\n\n\n\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist105416485\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-autoarima_loop-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"autoarima_loop.py content, created by marcosan93 on 03:10AM on September 17, 2020.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"autoarima_loop.py\">\n        <tr>\n          <td id=\"file-autoarima_loop-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC1\" class=\"blob-code blob-code-inner js-file-line\"># Days in the past to train on<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">days_to_train = 180 <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC4\" class=\"blob-code blob-code-inner js-file-line\"># Days in the future to predict<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">days_to_predict = 5<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC7\" class=\"blob-code blob-code-inner js-file-line\"># Establishing a new DF for predictions<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">stock_df[&#39;Predictions&#39;] = pd.DataFrame(index=stock_df[&#39;Log&#39;].index,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">                                       columns=stock_df[&#39;Log&#39;].columns)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC11\" class=\"blob-code blob-code-inner js-file-line\"># Iterate through each stock<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">for stock in tqdm(stocks):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">    # Current predicted value<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">    pred_val = 0<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">    # Training the model in a predetermined date range<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">    for day in tqdm(range(1000, <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">                          stock_df[&#39;Log&#39;].shape[0]-days_to_predict)):        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC21\" class=\"blob-code blob-code-inner js-file-line\">        # Data to use, containing a specific amount of days<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC22\" class=\"blob-code blob-code-inner js-file-line\">        training = stock_df[&#39;Log&#39;][stock].iloc[day-days_to_train:day+1].dropna()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC23\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC24\" class=\"blob-code blob-code-inner js-file-line\">        # Determining if the actual value crossed the predicted value<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC25\" class=\"blob-code blob-code-inner js-file-line\">        cross = ((training[-1] &gt;= pred_val &gt;= training[-2]) or <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L26\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"26\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC26\" class=\"blob-code blob-code-inner js-file-line\">                 (training[-1] &lt;= pred_val &lt;= training[-2]))<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L27\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"27\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC27\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L28\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"28\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC28\" class=\"blob-code blob-code-inner js-file-line\">        # Running the model when the latest training value crosses the predicted value or every other day <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L29\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"29\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC29\" class=\"blob-code blob-code-inner js-file-line\">        if cross or day % 2 == 0:<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L30\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"30\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC30\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L31\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"31\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC31\" class=\"blob-code blob-code-inner js-file-line\">            # Finding the best parameters<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L32\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"32\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC32\" class=\"blob-code blob-code-inner js-file-line\">            model    = AutoARIMA(start_p=0, start_q=0,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L33\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"33\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC33\" class=\"blob-code blob-code-inner js-file-line\">                                 start_P=0, start_Q=0,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L34\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"34\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC34\" class=\"blob-code blob-code-inner js-file-line\">                                 max_p=8, max_q=8,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L35\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"35\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC35\" class=\"blob-code blob-code-inner js-file-line\">                                 max_P=5, max_Q=5,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L36\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"36\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC36\" class=\"blob-code blob-code-inner js-file-line\">                                 error_action=&#39;ignore&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L37\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"37\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC37\" class=\"blob-code blob-code-inner js-file-line\">                                 information_criterion=&#39;bic&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L38\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"38\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC38\" class=\"blob-code blob-code-inner js-file-line\">                                 suppress_warnings=True)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L39\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"39\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC39\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L40\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"40\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC40\" class=\"blob-code blob-code-inner js-file-line\">            # Getting predictions for the optimum parameters by fitting to the training set            <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L41\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"41\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC41\" class=\"blob-code blob-code-inner js-file-line\">            forecast = model.fit_predict(training,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L42\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"42\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC42\" class=\"blob-code blob-code-inner js-file-line\">                                         n_periods=days_to_predict)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L43\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"43\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC43\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L44\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"44\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC44\" class=\"blob-code blob-code-inner js-file-line\">            # Getting the last predicted value from the next N days<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L45\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"45\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC45\" class=\"blob-code blob-code-inner js-file-line\">            stock_df[&#39;Predictions&#39;][stock].iloc[day:day+days_to_predict] = np.exp(forecast[-1])<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L46\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"46\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC46\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L47\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"47\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC47\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L48\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"48\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC48\" class=\"blob-code blob-code-inner js-file-line\">            # Updating the current predicted value<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_loop-py-L49\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"49\"><\/td>\n          <td id=\"file-autoarima_loop-py-LC49\" class=\"blob-code blob-code-inner js-file-line\">            pred_val = forecast[-1]<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/9284269aba45c47ca03ed149a4a53d77\/raw\/9fc85c8f9fc12ec53ea8bbd028c1cc7b49221751\/autoarima_loop.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/9284269aba45c47ca03ed149a4a53d77#file-autoarima_loop-py\" class=\"Link--inTextBlock\">\n          autoarima_loop.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n\n\n\n<p>After we run the code above, we will be given a DF of predictions for every stock in our portfolio. We will use these predictions for our backtest but let\u2019s visualize them first.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8850\">Visualizing the Model\u2019s Predictions<\/h2>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Shift ahead by 1 to compare the actual values to the predictions\npred_df = stock_df['Predictions'].shift(1).astype(float).dropna()<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<p>Let\u2019s create a new DataFrame for our predictions but with some alterations. We\u2019ve shifted the predictions forward by one day so that our predictions will hopefully not suffer from&nbsp;<a href=\"https:\/\/corporatefinanceinstitute.com\/resources\/knowledge\/finance\/look-ahead-bias\/#:~:text=What%20is%20Look%2Dahead%20Bias,from%20a%20study%20or%20simulation.\" target=\"_blank\" rel=\"noreferrer noopener\"><em>look-ahead bias<\/em><\/a>.<\/p>\n\n\n\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist105416618\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-arima_preds_actual-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"arima_preds_actual.py content, created by marcosan93 on 03:27AM on September 17, 2020.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"arima_preds_actual.py\">\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">for stock in stocks:<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">    fig = go.Figure()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">    # Plotting the actual values<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">    fig.add_trace(go.Scatter(x=pred_df.index,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">                             y=stock_df[&#39;MovAvg&#39;][stock].loc[pred_df.index],<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">                             name=&#39;Actual Moving Average&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">                             mode=&#39;lines&#39;))<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">    # Plotting the predicted values<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">    fig.add_trace(go.Scatter(x=pred_df.index,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">                             y=pred_df[stock],<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">                             name=&#39;Predicted Moving Average&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">                             mode=&#39;lines&#39;))<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">    # Setting the labels<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">    fig.update_layout(title=f&#39;Predicting the Moving Average for the Next {days_to_predict} days for {stock}&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">                      xaxis_title=&#39;Date&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">                      yaxis_title=&#39;Prices&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC21\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_preds_actual-py-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-arima_preds_actual-py-LC22\" class=\"blob-code blob-code-inner js-file-line\">    fig.show()<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/f6fff8151f0737b81155398700cc4209\/raw\/54e48c9511dc824c822a51ebf03d0ecd759b0a26\/arima_preds_actual.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/f6fff8151f0737b81155398700cc4209#file-arima_preds_actual-py\" class=\"Link--inTextBlock\">\n          arima_preds_actual.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n\n\n\n<p class=\"has-text-align-center has-small-font-size\">Here we iterate through each stock and visualize the predicted and actual values<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_wuc6kCKKro0ZemSJb0pkFA.png\" alt=\"\" class=\"wp-image-32\"\/><figcaption class=\"wp-element-caption\">One Example of the Predicted vs Actual Values<\/figcaption><\/figure>\n\n\n\n<p id=\"d35f\">We can see that our model seems to do well enough but remember that the predictions are based on the last predicted day and serve as price targets in our trading strategy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d593\">Evaluating the Predictions<\/h2>\n\n\n\n<p id=\"8118\">Now that we\u2019ve seen the differences between the actual values and the predicted values, we can quickly evaluate it\u2019s quality by using the&nbsp;<a href=\"https:\/\/www.statisticshowto.com\/probability-and-statistics\/regression-analysis\/rmse-root-mean-square-error\/#:~:text=Root%20Mean%20Square%20Error%20(RMSE)%20is%20the%20standard%20deviation%20of,the%20line%20of%20best%20fit.\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Root Mean Square Error<\/em><\/a>&nbsp;to see how far off our predictions are.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">for stock in stocks:\n\n    # Finding the root mean squared error\n    rmse = mean_squared_error(stock_df['MovAvg'][stock].loc[pred_df.index], pred_df\n[stock], squared=False)\n\nprint(f\"On average, the model is off by {rmse} for {stock}\\n\")<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<h2 class=\"wp-block-heading\">The Trading Strategy for the Model<\/h2>\n\n\n\n<p id=\"3cd0\">Our strategy for our model is simple:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Buy<\/strong>&nbsp;\u2014 When the predicted price target shows a significant increase from the current price.<\/li>\n\n\n\n<li><strong>Sell<\/strong>&nbsp;\u2014 When the predicted price target shows a significant decrease from the current price.<\/li>\n\n\n\n<li><strong>Hold<\/strong>&nbsp;(or Do Nothing)\u2014 If the price target shows neither a significant increase or decrease from the current price.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>For example:<\/strong>&nbsp;If we set our model to predict 10 days in advance, then the last day\u2019s predicted amount is the price target. If the price target is $103 and the current closing price is $100, then we will buy that stock because its price is predicted to increase by 3% in the next 10 days.<\/p>\n\n\n\n<p>However, if the current price exceeds the predicted price target sooner than expected, then we can run the model again for a newer price target.<\/p>\n<\/blockquote>\n\n\n\n<p id=\"8f15\">Let\u2019s create a function that will establish positions in our backtest based on the strategy above:<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">def get_positions(difference, thres=3, short=True):\n    \"\"\"\n    Compares the percentage difference between actual \n    values and the respective predictions.\n    \n    Returns the decision or positions to long or short \n    based on the difference.\n    \n    Optional: shorting in addition to buying\n    \"\"\"\n    \n    if difference &gt; thres\/100:\n        \n        return 1\n    \n    \n    elif short and difference &lt; -thres\/100:\n        \n        return -1\n    \n    \n    else:\n        \n        return 0<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<h2 class=\"wp-block-heading\">Positions Based on Model Predictions<\/h2>\n\n\n\n<p>Now using our function to establish positions, we can begin the backtesting portion of our model and trading strategy. We will need to create another DataFrame containing the Log Returns to use and the percentage difference between our predicted and actual values.<\/p>\n\n\n\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist105416951\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-autoarima_trade_df-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"autoarima_trade_df.py content, created by marcosan93 on 04:10AM on September 17, 2020.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"autoarima_trade_df.py\">\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC1\" class=\"blob-code blob-code-inner js-file-line\"># Creating a DF dictionary for trading the model<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">trade_df = {}<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC4\" class=\"blob-code blob-code-inner js-file-line\"># Getting the percentage difference between the predictions and the actual values<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">trade_df[&#39;PercentDiff&#39;] = (stock_df[&#39;Predictions&#39;].dropna() \/ <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">                           stock_df[&#39;MovAvg&#39;].loc[stock_df[&#39;Predictions&#39;].dropna().index]) - 1<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC8\" class=\"blob-code blob-code-inner js-file-line\"># Getting positions<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">trade_df[&#39;Positions&#39;] = trade_df[&#39;PercentDiff&#39;].applymap(lambda x: get_positions(x, <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">                                                                                 thres=1, <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">                                                                                 short=True) \/ len(stocks))<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC13\" class=\"blob-code blob-code-inner js-file-line\"># Preventing lookahead bias by shifting the positions<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">trade_df[&#39;Positions&#39;] = trade_df[&#39;Positions&#39;].shift(2).dropna()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC16\" class=\"blob-code blob-code-inner js-file-line\"># Getting Log Returns<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-autoarima_trade_df-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-autoarima_trade_df-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">trade_df[&#39;LogReturns&#39;] = stock_df[&#39;LogReturns&#39;].loc[trade_df[&#39;Positions&#39;].index]                                    <\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/9ce7ca535e3490ee5f6aef681aaed8a1\/raw\/3145bae91aca8de8f77f59adc56ef263e51aa46e\/autoarima_trade_df.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/9ce7ca535e3490ee5f6aef681aaed8a1#file-autoarima_trade_df-py\" class=\"Link--inTextBlock\">\n          autoarima_trade_df.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n\n\n\n<blockquote class=\"wp-block-quote is-style-default is-layout-flow wp-block-quote-is-layout-flow\">\n<p>If you noticed in the \u201cPositions\u201d DF, we have shifted the series of positions by 2 days. This is done to account for look-ahead bias as well as the situation in which we may find ourselves deciding to initiate a trade closer to the end of the trading day based on the prediction from the day before. If we decided to initiate a trade at the very beginning of the trading day, then we may be fine with just shifting positions by 1 day instead.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9c21\">Plotting the Positions<\/h3>\n\n\n\n<p id=\"0785\">Once we have established the positions in our backtest, we can then count the number of positions, the type of position, and which stock they belonged to. This is done to further analyze how often our strategy determines each position.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_7sCRoPIVQus37nYcGsC5uQ.png\" alt=\"\" class=\"wp-image-37\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fa64\">Vectorized Backtesting the Model<\/h2>\n\n\n\n<p id=\"1818\">With our trading DataFrame ready to go, we can use vectorized backtesting and quickly visualize the returns for each individual stock and the returns from the overall portfolio.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"d814\">Returns for Each Stock<\/h3>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Calculating Returns by multiplying the \n# positions by the log returns\nreturns = trade_df['Positions'] * trade_df['LogReturns']\n\n# Calculating the performance as we take the cumulative \n# sum of the returns and transform the values back to normal\nperformance = returns.cumsum().apply(np.exp)\n\n# Plotting the performance per stock\npx.line(performance,\n        x=performance.index,\n        y=performance.columns,\n        title='Returns Per Stock Using ARIMA Forecast',\n        labels={'variable':'Stocks',\n                'value':'Returns'})<\/code><\/pre>\n\n                <\/div>\n                <div class=\"code__btns\">\n                    <button class=\"code__copy\" class=\"copy\" title=\"Copy url\">\n                        <svg class=\"code__copy__icon\" width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                            <use xlink:href=\"\/img\/icons\/copy.svg#copy\"><\/use>\n                        <\/svg>\n                        <img decoding=\"async\" class=\"code__copy__approve\" alt=\"\" src=\"\/img\/approve_ico.svg\" loading=\"eager\">\n                    <\/button>\n                <\/div>\n            <\/div>\n        \n\n\n<p id=\"a4fd\">This code yields the following output for our vectorized backtest:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_MPdfgS-hLixY8T5iWHYbew.png\" alt=\"\" class=\"wp-image-39\"\/><\/figure>\n\n\n\n<p id=\"f91d\">From this visualization, we can see that our ARIMA model strategy performs better with some stocks compared to others. With most of the stocks, you can see a significant jump right around March due to COVID\u2019s effect on the market.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1d05\">Returns on the Portfolio<\/h3>\n\n\n\n<p id=\"6d80\">In order to really evaluate our portfolio returns, we will need to compare our results with SPY. If we are capable of beating SPY returns, then our model shows promise and may be considered for forward-testing or live trading.<\/p>\n\n\n\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist105417138\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-arima_port_rets-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"arima_port_rets.py content, created by marcosan93 on 04:32AM on September 17, 2020.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"arima_port_rets.py\">\n        <tr>\n          <td id=\"file-arima_port_rets-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC1\" class=\"blob-code blob-code-inner js-file-line\"># Returns for the portfolio<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">returns = (trade_df[&#39;Positions&#39;] * trade_df[&#39;LogReturns&#39;]).sum(axis=1)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC4\" class=\"blob-code blob-code-inner js-file-line\"># Returns for SPY<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">spy = yf.download(&#39;SPY&#39;, start=returns.index[0]).loc[returns.index]<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">spy = spy[&#39;Adj Close&#39;].apply(np.log).diff().dropna().cumsum().apply(np.exp)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC9\" class=\"blob-code blob-code-inner js-file-line\"># Calculating the performance as we take the cumulative sum of the returns and transform the values back to normal<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">performance = returns.cumsum().apply(np.exp)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC12\" class=\"blob-code blob-code-inner js-file-line\"># Plotting the comparison between SPY returns and ARIMA returns<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">fig = go.Figure()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">fig.add_trace(go.Scatter(x=spy.index,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">                         y=spy,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">                         name=&#39;SPY Returns&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">                         mode=&#39;lines&#39;))<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">fig.add_trace(go.Scatter(x=performance.index,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC21\" class=\"blob-code blob-code-inner js-file-line\">                         y=performance.values,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC22\" class=\"blob-code blob-code-inner js-file-line\">                         name=&#39;ARIMA Returns on Portfolio&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC23\" class=\"blob-code blob-code-inner js-file-line\">                         mode=&#39;lines&#39;))<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC24\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC25\" class=\"blob-code blob-code-inner js-file-line\">fig.update_layout(title=&#39;SPY vs ARIMA Overall Portfolio Returns&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L26\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"26\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC26\" class=\"blob-code blob-code-inner js-file-line\">                  xaxis_title=&#39;Date&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L27\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"27\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC27\" class=\"blob-code blob-code-inner js-file-line\">                  yaxis_title=&#39;Returns&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L28\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"28\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC28\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-arima_port_rets-py-L29\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"29\"><\/td>\n          <td id=\"file-arima_port_rets-py-LC29\" class=\"blob-code blob-code-inner js-file-line\">fig.show()<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/62d6a8ea6e6b36015c6bf3038fb8aeae\/raw\/be627a8f84f9c66e3d31027e5c639818ec21f7b0\/arima_port_rets.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/marcosan93\/62d6a8ea6e6b36015c6bf3038fb8aeae#file-arima_port_rets-py\" class=\"Link--inTextBlock\">\n          arima_port_rets.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n\n\n\n<p id=\"6b9e\">And here\u2019s the result:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_mFdQDqCfXtHRVDbggnWmnw.png\" alt=\"\" class=\"wp-image-41\"\/><\/figure>\n\n\n\n<p>As you can see, our model initialy under-performs when compared to the SPY returns. However, it begins to beat SPY at the start of the stock market crash due to COVID. Feel free to change several values here and there within the model and within the strategy if you wish to achieve a different outcome.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5fb8\">Closing Thoughts<\/h2>\n\n\n\n<p id=\"04e6\">When we used this AutoARIMA model in combination with our simple stock trading strategy, we were able to achieve a better return performance than if we had just invested in SPY. However, we were able to perform pretty well at the end possibly due to the sudden stock market crash.<\/p>\n\n\n\n<p id=\"a82e\">If we backtested on a different time frame or with different stocks, then it\u2019s very probable we would not have achieved similar results. At this point, it\u2019s a smart move to begin forward-testing this strategy in order to gain a better understanding of our model\u2019s true performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to using machine learning in the stock market, there are multiple approaches a trader can do to utilize ML models. From determining future risk to predicting stock prices, machine learning can be used for virtually any kind of financial modeling. In our previous articles, we delved into the usage of two&nbsp;Time Series&nbsp;models:&nbsp;SARIMAX&nbsp;and&nbsp;Facebook [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[65],"tags":[],"coding-language":[30],"ready-to-go-solution":[],"qualification":[31,32],"financial-apis-category":[36],"financial-apis-manuals":[38,40],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-backtesting-strategies-examples","coding-language-python","qualification-experienced","qualification-guru","financial-apis-category-stock-market-prices","financial-apis-manuals-intraday","financial-apis-manuals-technical-indicators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.9 (Yoast SEO v26.7) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Machine Learning Model Performance in Predicting Stock Market Trends | EODHD APIs Academy<\/title>\n<meta name=\"description\" content=\"Improve your stock market predictions with machine learning models. Learn how to evaluate and optimize model performance for better results\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"I Tested a Machine Learning Model\u2019s Performance in the Stock Market\" \/>\n<meta property=\"og:description\" content=\"Improve your stock market predictions with machine learning models. Learn how to evaluate and optimize model performance for better results\" \/>\n<meta property=\"og:url\" content=\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\" \/>\n<meta property=\"og:site_name\" content=\"Financial Academy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/eodhistoricaldata\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-18T10:54:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-05T13:25:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2022\/03\/i-tested-a-machine-learning-models-performance-in-the-stock-market.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"452\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Marco Santos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/_marco_santos_\" \/>\n<meta name=\"twitter:site\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marco Santos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#article\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\"},\"author\":{\"name\":\"Marco Santos\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/21a7f35c812f05fb7c4e60a624a3237d\"},\"headline\":\"I Tested a Machine Learning Model\u2019s Performance in the Stock Market\",\"datePublished\":\"2020-09-18T10:54:00+00:00\",\"dateModified\":\"2025-02-05T13:25:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\"},\"wordCount\":1810,\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png\",\"articleSection\":[\"Backtesting Strategies Examples\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\",\"name\":\"Machine Learning Model Performance in Predicting Stock Market Trends | EODHD APIs Academy\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png\",\"datePublished\":\"2020-09-18T10:54:00+00:00\",\"dateModified\":\"2025-02-05T13:25:53+00:00\",\"description\":\"Improve your stock market predictions with machine learning models. Learn how to evaluate and optimize model performance for better results\",\"breadcrumb\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage\",\"url\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png\",\"contentUrl\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/eodhd.com\/financial-academy\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"I Tested a Machine Learning Model\u2019s Performance in the Stock Market\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/\",\"name\":\"Financial APIs Academy | EODHD\",\"description\":\"Financial Stock Market Academy\",\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/eodhd.com\/financial-academy\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\",\"name\":\"EODHD (EOD Historical Data)\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png\",\"contentUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png\",\"width\":159,\"height\":82,\"caption\":\"EODHD (EOD Historical Data)\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/eodhistoricaldata\",\"https:\/\/x.com\/EOD_data\",\"https:\/\/www.reddit.com\/r\/EODHistoricalData\/\",\"https:\/\/eod-historical-data.medium.com\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/21a7f35c812f05fb7c4e60a624a3237d\",\"name\":\"Marco Santos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c176ca2898e90e41837f2fe073b4001074052c63f3559b29e7e4ae9d7a048a1b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c176ca2898e90e41837f2fe073b4001074052c63f3559b29e7e4ae9d7a048a1b?s=96&d=mm&r=g\",\"caption\":\"Marco Santos\"},\"description\":\"NYC Data Scientist specializing in AI\/ML with a passion for code\",\"sameAs\":[\"http:\/\/linkedin.com\/in\/marco-s-santos\",\"https:\/\/x.com\/https:\/\/twitter.com\/_marco_santos_\"],\"url\":\"https:\/\/eodhd.com\/financial-academy\/author\/marcosantos\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Machine Learning Model Performance in Predicting Stock Market Trends | EODHD APIs Academy","description":"Improve your stock market predictions with machine learning models. Learn how to evaluate and optimize model performance for better results","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market","og_locale":"en_US","og_type":"article","og_title":"I Tested a Machine Learning Model\u2019s Performance in the Stock Market","og_description":"Improve your stock market predictions with machine learning models. Learn how to evaluate and optimize model performance for better results","og_url":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market","og_site_name":"Financial Academy","article_publisher":"https:\/\/www.facebook.com\/eodhistoricaldata","article_published_time":"2020-09-18T10:54:00+00:00","article_modified_time":"2025-02-05T13:25:53+00:00","og_image":[{"width":800,"height":452,"url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2022\/03\/i-tested-a-machine-learning-models-performance-in-the-stock-market.png","type":"image\/png"}],"author":"Marco Santos","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/_marco_santos_","twitter_site":"@EOD_data","twitter_misc":{"Written by":"Marco Santos","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#article","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market"},"author":{"name":"Marco Santos","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/21a7f35c812f05fb7c4e60a624a3237d"},"headline":"I Tested a Machine Learning Model\u2019s Performance in the Stock Market","datePublished":"2020-09-18T10:54:00+00:00","dateModified":"2025-02-05T13:25:53+00:00","mainEntityOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market"},"wordCount":1810,"publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage"},"thumbnailUrl":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png","articleSection":["Backtesting Strategies Examples"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market","url":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market","name":"Machine Learning Model Performance in Predicting Stock Market Trends | EODHD APIs Academy","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/#website"},"primaryImageOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage"},"thumbnailUrl":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png","datePublished":"2020-09-18T10:54:00+00:00","dateModified":"2025-02-05T13:25:53+00:00","description":"Improve your stock market predictions with machine learning models. Learn how to evaluate and optimize model performance for better results","breadcrumb":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#primaryimage","url":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png","contentUrl":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/03\/1_8wK5TaHB8RXCYf9dMLHdiQ.png"},{"@type":"BreadcrumbList","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/i-tested-a-machine-learning-models-performance-in-the-stock-market#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/eodhd.com\/financial-academy\/"},{"@type":"ListItem","position":2,"name":"I Tested a Machine Learning Model\u2019s Performance in the Stock Market"}]},{"@type":"WebSite","@id":"https:\/\/eodhd.com\/financial-academy\/#website","url":"https:\/\/eodhd.com\/financial-academy\/","name":"Financial APIs Academy | EODHD","description":"Financial Stock Market Academy","publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/eodhd.com\/financial-academy\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/eodhd.com\/financial-academy\/#organization","name":"EODHD (EOD Historical Data)","url":"https:\/\/eodhd.com\/financial-academy\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/","url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png","contentUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png","width":159,"height":82,"caption":"EODHD (EOD Historical Data)"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/eodhistoricaldata","https:\/\/x.com\/EOD_data","https:\/\/www.reddit.com\/r\/EODHistoricalData\/","https:\/\/eod-historical-data.medium.com\/"]},{"@type":"Person","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/21a7f35c812f05fb7c4e60a624a3237d","name":"Marco Santos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c176ca2898e90e41837f2fe073b4001074052c63f3559b29e7e4ae9d7a048a1b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c176ca2898e90e41837f2fe073b4001074052c63f3559b29e7e4ae9d7a048a1b?s=96&d=mm&r=g","caption":"Marco Santos"},"description":"NYC Data Scientist specializing in AI\/ML with a passion for code","sameAs":["http:\/\/linkedin.com\/in\/marco-s-santos","https:\/\/x.com\/https:\/\/twitter.com\/_marco_santos_"],"url":"https:\/\/eodhd.com\/financial-academy\/author\/marcosantos"}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pdOdVT-a","jetpack_sharing_enabled":true,"acf":[],"_links":{"self":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":2,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":6287,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/10\/revisions\/6287"}],"wp:attachment":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/tags?post=10"},{"taxonomy":"coding-language","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/coding-language?post=10"},{"taxonomy":"ready-to-go-solution","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/ready-to-go-solution?post=10"},{"taxonomy":"qualification","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/qualification?post=10"},{"taxonomy":"financial-apis-category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-category?post=10"},{"taxonomy":"financial-apis-manuals","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-manuals?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}