{"id":683,"date":"2022-10-03T13:25:10","date_gmt":"2022-10-03T13:25:10","guid":{"rendered":"https:\/\/eodhd.com\/financial-academy\/?p=683"},"modified":"2025-02-05T12:28:12","modified_gmt":"2025-02-05T12:28:12","slug":"visualising-trading-signals-in-python","status":"publish","type":"post","link":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python","title":{"rendered":"Visualising Trading Signals in Python"},"content":{"rendered":"\n<p>Plot buy and sell trading signals in Python&#8217;s graph <\/p>\n\n\n\n<p id=\"29f2\">I\u2019m going to assume you know the basics of technical analysis in trading. If this is all new to you, I recommend a great resource called&nbsp;Investopedia.<\/p>\n\n\n\n<p class=\"bordered_paragraph\">If you want to learn how to install the <a href=\"https:\/\/github.com\/EodHistoricalData\/EODHD-APIs-Python-Financial-Library\">EODHD APIs Python Financial Official Library<\/a> and activate your API key, I recommend to start with exploring of our <a href=\"https:\/\/eodhistoricaldata.com\/financial-apis\/python-financial-libraries-and-code-samples\/#EODHD_APIs_Python_Financial_Library_by_Michael_Whittle\">Documentation for it<\/a>.<\/p>\n\n\n\n<p id=\"178f\"><strong>Let\u2019s take a basic example\u2026<\/strong><\/p>\n\n\n\n<p id=\"1792\">We want to determine when would be a good time to buy or sell our stock or cryptocurrency, but how can we do this? One way is to use moving averages to determine the direction of a market.<\/p>\n\n\n\n<p id=\"05ca\">A&nbsp;<a href=\"https:\/\/www.investopedia.com\/terms\/s\/sma.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Simple Moving Avergage (SMA)<\/a>&nbsp;is when we take an average of the last&nbsp;<em>n<\/em>&nbsp;closing prices of historical trading data. If we are looking at the daily chart, each candlestick or data point will be 1 day. The SMA50 will be an average of the last 50 days of closing prices. This alone does not do much for us, aside from smoothing out the \u201cnoise\u201d in a market. Where this gets interesting is when you compare it with another moving average, for example SMA200. Every time the SMA50 crosses above the SMA200 we know that the market is in an upward trend, and when the SMA50 crosses below the SMA200 it is a downward trend. You can use any moving averages you like but this combination is considered special. When the SMA50 crosses above the SMA200 it\u2019s known as the \u201c<a href=\"https:\/\/www.investopedia.com\/ask\/answers\/121114\/what-difference-between-golden-cross-and-death-cross-pattern.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Golden Cross<\/a>\u201d, and when the SMA50 crosses below the SMA200 it is known as the \u201c<a href=\"https:\/\/www.investopedia.com\/ask\/answers\/121114\/what-difference-between-golden-cross-and-death-cross-pattern.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Death Cross<\/a>\u201d. When this event occurs it usually is followed by a strong price movement.<\/p>\n\n\n\n<p><\/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=\"775b\">What we will need to start visualising trading signals in Python?<\/h2>\n\n\n\n<p id=\"cacf\">I\u2019m going to use a Jupyter notebook in&nbsp;<a href=\"https:\/\/colab.research.google.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google Colab<\/a>. It\u2019s free and easy to use if you want to follow along. I\u2019m also going to use the&nbsp;EODHD APIs&nbsp;end-of-day demo key to demonstrate this.<\/p>\n\n\n\n<p id=\"0844\">The first step is we need to import our libraries.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">import json\nimport requests\nimport pandas as pd\nimport matplotlib.pyplot as plt<\/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>We then want to retrieve the daily data for Apple\u2019s stock (AAPL) and store it in a Pandas dataframe.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">resp = requests.get(\"https:\/\/eodhistoricaldata.com\/api\/eod\/AAPL?api_token=demo&amp;fmt=json\")\njson_data = json.loads(resp.content)\ndf = pd.DataFrame(json_data)\ndf<\/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>If all has gone to plan so far it should look like this.<\/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\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png\" alt=\"Apple\u2019s stock (AAPL) in a Pandas dataframe\" class=\"wp-image-697\"\/><\/figure>\n\n\n\n<p id=\"3e68\">10534 days of Apple data!<\/p>\n\n\n\n<p id=\"c05c\">\u201cclose\u201d and \u201cadjusted_close\u201d are very similar, but I\u2019m not going to get into the difference now, we\u2019ll use \u201cclose\u201d for this tutorial.<\/p>\n\n\n\n<p id=\"c1d3\">We want to add our SMA50 and SMA200 now.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df[\"sma50\"] = df.close.rolling(50, min_periods=50).mean()\ndf[\"sma200\"] = df.close.rolling(200, min_periods=200).mean()\ndf<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-Pandas-SMA50-and-SMA200.png\" alt=\"Apple stock (AAPL) Pandas SMA50 and SMA200\" class=\"wp-image-698\"\/><\/figure>\n\n\n\n<p id=\"1c90\">You will see our two new features\/columns called \u201csma50\u201d and \u201csma200\u201d. You may wonder what \u201cNaN\u201d means. This means \u201cNot a Number\u201d, and you get this because in order to calculate the SMA50 for example, you need at least 50 data points to calculate the first point.<\/p>\n\n\n\n<p id=\"8961\">We have two ways we could deal with this. We could either replace the \u201cNaN\u201d with something else, like the closing price, or we could just drop those rows. As we have a ridiculous amount of data already, missing the first 200 rows will have no impact at all.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df.dropna(inplace=True)\ndf<\/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>And they are gone\u2026<\/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\/10\/Apple-stock-AAPL-closing-price.png\" alt=\"Apple stock (AAPL) closing price\" class=\"wp-image-699\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a241\">What does this look like graphed?<\/h2>\n\n\n\n<p id=\"4f3d\">For the next step, well use a library called Matplotlib. It\u2019s used heavily in Data Science for plotting data (you will notice we imported it earlier).<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">plt.figure(figsize=(30,10))\nplt.plot(df[\"close\"], color=\"black\", label=\"Price\")\nplt.plot(df[\"sma50\"], color=\"blue\", label=\"SMA50\")\nplt.plot(df[\"sma200\"], color=\"green\", label=\"SMA200\")\nplt.ylabel(\"Price\")\nplt.xticks(rotation=90)\nplt.title(\"APPL Daily SMA50\/SMA200\")\nplt.legend()\nplt.show()<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-plot-daily-SMA50-SMA200.png\" alt=\"APPL plot daily SMA50 SMA200\" class=\"wp-image-700\"\/><\/figure>\n\n\n\n<p>It worked, but it\u2019s not easy to see. We are viewing over 28 years of data after all. This is way too much for what we need, so I will reduce this to the last year of data.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df = df.tail(365)<\/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>And this is what it looks like now\u2026<\/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\/10\/APPL-plot-daily-SMA50-SMA200-last-year.png\" alt=\"APPL plot daily SMA50 SMA200 last year\" class=\"wp-image-701\"\/><\/figure>\n\n\n\n<p>You will probably notice something that isn\u2019t ideal. The x-axis ticks are index numbers instead of the actual date. It\u2019s not a problem per se, but just isn\u2019t all that nice. I\u2019m going to replace the x-axis ticks with the actual date.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df.set_index(['date'], inplace=True)\ndf<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-index-numbers.png\" alt=\"APPL index numbers\" class=\"wp-image-702\"\/><\/figure>\n\n\n\n<p>And it looks like this now\u2026<\/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\/10\/APPL-index-numbers-plot.png\" alt=\"\" class=\"wp-image-703\"\/><\/figure>\n\n\n\n<p id=\"a6e1\">The next problem now is we have all 365 days on the x-axis, so a complete mess. But don\u2019t fret, we can easily fix this. Well \u201ceasily\u201d, it took me a while to figure this out.<\/p>\n\n\n\n<p id=\"97a3\">The code we will need is as follows\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">ax = plt.gca()\nfor index, label in enumerate(ax.xaxis.get_ticklabels()):\n    if index % 7 != 0:\n        label.set_visible(False)<\/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>What this is doing is saying is if the index number divided by 7 doesn\u2019t equal 0, then don\u2019t display it. Basically, show every 7th day.<\/p>\n\n\n\n<p>The complete code looks like this\u2026.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">plt.figure(figsize=(30,10))\nplt.plot(df[\"close\"], color=\"black\", label=\"Price\")\nplt.plot(df[\"sma50\"], color=\"blue\", label=\"SMA50\")\nplt.plot(df[\"sma200\"], color=\"green\", label=\"SMA200\")\nplt.ylabel(\"Price\")\nplt.xticks(rotation=90)\nplt.title(\"APPL Daily SMA50\/SMA200\")\nax = plt.gca()\nfor index, label in enumerate(ax.xaxis.get_ticklabels()):\n  if index % 7 != 0:\n    label.set_visible(False)\nplt.legend()\nplt.show()<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-plot-daily-SMA50-SMA200-every-7th-day.png\" alt=\"APPL plot daily SMA50 SMA200 every 7th day\" class=\"wp-image-704\"\/><\/figure>\n\n\n\n<p id=\"f8fd\">Nice!<\/p>\n\n\n\n<p id=\"1bd1\">In the graph you will see a black line, which is the closing price of each day. You will see a blue line which is the SMA50, and the green line which is the SMA200.<\/p>\n\n\n\n<p id=\"d27b\">If you look at the first time (in the last year) the blue line crosses above the green line, you will see the market move into an upward trend. This would have been a great and profitable time to buy. Later, you see the blue line drop below the green line and the price dropped significantly. Ideally we would not want to wait so long before selling, but we can look into ways we can do this later.<\/p>\n\n\n\n<p class=\"has-text-align-center bordered_paragraph has-background\" style=\"background-color:#f1f6f9;font-size:16px;font-style:normal;font-weight:500\">While understanding the trading signals is crucial, the reliability of the data you use is equally important. Learn more:<br><a href=\"https:\/\/eodhd.com\/financial-academy\/fundamental-analysis-examples\/reliability-of-free-stock-data\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Free vs Paid Stock Data: Which One Can You Trust?<\/strong><\/a> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7a6c\">How can we programmatically tell when the SMA\u2019s cross over?<\/h2>\n\n\n\n<p id=\"8ee8\">In Pandas, we can create a new feature\/column with a boolean (true or false) entry based on a certain condition.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">pd.options.mode.chained_assignment = None\ndf.loc[df[\"sma50\"] &gt; df[\"sma200\"], \"sma50gtsma200\"] = True\ndf[\"sma50gtsma200\"].fillna(False, inplace=True)\ndf.loc[df[\"sma50\"] &lt; df[\"sma200\"], \"sma50ltsma200\"] = True\ndf[\"sma50ltsma200\"].fillna(False, inplace=True)\ndf<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-SMAs-cross-over-true-or-false.png\" alt=\"APPL SMA\u2019s cross over true or false\" class=\"wp-image-705\"\/><\/figure>\n\n\n\n<p id=\"6538\">You will see our two new features\/columns called \u201csma50<strong>gt<\/strong>sma200\u201d and \u201csma50<strong>lt<\/strong>sma200\u201d. The former returns True if the SMA50 is greater than the SMA200, and the latter returns True of the SMA50 is less than the SMA200. If it\u2019s not True, then set to False.<\/p>\n\n\n\n<p id=\"ce8f\">What we want to do now is determine the point at when the cross-over occurs, and you can do it as follows.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df[\"sma50gtsma200co\"] = df.sma50gtsma200.ne(df.sma50gtsma200.shift())\ndf.loc[df[\"sma50gtsma200\"] == False, \"sma50gtsma200co\"] = False\n\ndf[\"sma50ltsma200co\"] = df.sma50ltsma200.ne(df.sma50ltsma200.shift())\ndf.loc[df[\"sma50ltsma200\"] == False, \"sma50ltsma200co\"] = False\n\ndf<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-plot-daily-SMA50-SMA200-cross-over.png\" alt=\"APPL plot daily SMA50 SMA200 cross-over\" class=\"wp-image-706\"\/><\/figure>\n\n\n\n<p id=\"3fd8\">The two new features columns, \u201csma50<strong>gt<\/strong>sma200<strong>co<\/strong>\u201d and \u201csma50<strong>lt<\/strong>sma200<strong>co<\/strong>\u201d, will show when they cross over. We can confirm this as follows.<\/p>\n\n\n\n<p id=\"fc97\">The buy signal(s)\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">buysignals = df[df[\"sma50gtsma200co\"] == True]\nbuysignals<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-SMA50-SMA200-buysignals.png\" alt=\"\" class=\"wp-image-707\"\/><\/figure>\n\n\n\n<p>And the sell signal(s)\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">sellsignals = df[df[\"sma50ltsma200co\"] == True]\nsellsignals<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-SMA50-SMA200-sellsignals.png\" alt=\"APPL SMA50 SMA200 sellsignals\" class=\"wp-image-708\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f814\">Plotting our buy and sell trading signals in Python<\/h2>\n\n\n\n<p id=\"c5f8\">The next step is we want to plot our buy and sell trading signals on the graph in Python. We can do it as follows\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">for idx in buysignals.index.tolist():\n  plt.plot(\n      idx,\n      df.loc[idx][\"close\"],\n      \"g*\",\n      markersize=25\n  )\n\nfor idx in sellsignals.index.tolist():\n  plt.plot(\n      idx,\n      df.loc[idx][\"close\"],\n      \"r*\",\n      markersize=25\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<p id=\"c054\">What this will do is plot a green star for the entries in the \u201cbuysignals\u201d dataframe, and a red star for the entries in the \u201csellsignals\u201d dataframe. It\u2019s worth pointing out it will plot all the buy and sell trading signals in the Python&#8217;s dataframes so you can combine signals to get more advanced results.<\/p>\n\n\n\n<p id=\"8c49\">The full code looks like this\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">plt.figure(figsize=(30,10))\nplt.plot(df[\"close\"], color=\"black\", label=\"Price\")\nplt.plot(df[\"sma50\"], color=\"blue\", label=\"SMA50\")\nplt.plot(df[\"sma200\"], color=\"green\", label=\"SMA200\")\nplt.ylabel(\"Price\")\nplt.xticks(rotation=90)\nplt.title(\"APPL Daily SMA50\/SMA200\")\nfor idx in buysignals.index.tolist():\n  plt.plot(\n      idx,\n      df.loc[idx][\"close\"],\n      \"g*\",\n      markersize=25\n  )\n\nfor idx in sellsignals.index.tolist():\n  plt.plot(\n      idx,\n      df.loc[idx][\"close\"],\n      \"r*\",\n      markersize=25\n  )\n\nax = plt.gca()\nfor index, label in enumerate(ax.xaxis.get_ticklabels()):\n  if index % 7 != 0:\n    label.set_visible(False)\nplt.legend()\nplt.show()<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-plot-with-buysignals-and-sellsignals-dataframe.png\" alt=\"APPL plot with buysignals and sellsignals dataframe\" class=\"wp-image-710\"\/><\/figure>\n\n\n\n<p id=\"20c3\">What is nice about plotting the buy and sell trading signals in Python is it allows is to see visually what is happening, but it also allows us to fine tune our strategies.<\/p>\n\n\n\n<p id=\"5549\">I mean I would be happy with the buy signal above but the following sell signal is late due to the moving averages lag.<\/p>\n\n\n\n<p id=\"11d5\">Let\u2019s try and add&nbsp;<a href=\"https:\/\/www.investopedia.com\/terms\/m\/macd.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Moving Average Convergence Divergence (MACD)<\/a>&nbsp;to it and see if it helps.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df[\"ema12\"] = df[\"close\"].ewm(span=12, adjust=False).mean()\ndf[\"ema26\"] = df[\"close\"].ewm(span=26, adjust=False).mean()\n\ndf[\"macd\"] = df[\"ema12\"] - df[\"ema26\"]        \ndf[\"signal\"] = df[\"macd\"].ewm(span=9, adjust=False).mean()\n\ndf.loc[df[\"macd\"] &gt; df[\"signal\"], \"macdgtsignal\"] = True\ndf[\"macdgtsignal\"].fillna(False, inplace=True)\n\ndf.loc[df[\"macd\"] &lt; df[\"signal\"], \"macdltsignal\"] = True\ndf[\"macdltsignal\"].fillna(False, inplace=True)\n\ndf<\/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<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/APPL-Moving-Average-Convergence-Divergence-MACD-dataframe.png\" alt=\"APPL Moving Average Convergence Divergence (MACD) dataframe\" class=\"wp-image-711\"\/><\/figure>\n\n\n\n<p id=\"ce1d\">You can read up about the&nbsp;MACD&nbsp;technical indicator on&nbsp;Investopedia, but in summary, the MACD is calculated by subtracting the EMA26 from the EMA12. EMA is another type of moving average called an&nbsp;<a href=\"https:\/\/www.investopedia.com\/terms\/e\/ema.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Exponential Moving Average<\/a>. The Signal is the Exponential Moving Average using the last 9 MACD entries.<\/p>\n\n\n\n<p id=\"5710\">A buy signal is determined when the MACD crosses above the Signal and a sell signal is the reverse. We then follow the same process as before by adding in our additional feature\/columns to identify when this happens exactly.<\/p>\n\n\n\n<p id=\"4c86\">What we can do now is combine our SMA50\/200 and MACD\/Signal into composite buy and sell trading signals<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">buysignals = df[(df[\"sma50gtsma200co\"] == 1) &amp; (df[\"macdgtsignal\"] == 1)]\n\nsellsignals = df[(df[\"sma50ltsma200co\"] == 1) &amp; (df[\"macdltsignal\"] == 1)]<\/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>This is saying a buy signal is when the SMA50 crosses above the SMA200 and the MACD is greater than the Signal. A sell signal is when the SMA50 crosses below the the SMA200 and the MACD is less than the Signal.<\/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\/10\/APPL-SMA50-SMA200-and-MACD.png\" alt=\"APPL SMA50 SMA200 and MACD\" class=\"wp-image-712\"\/><\/figure>\n\n\n\n<p>You will notice it still thinks our buy signal is correct but our two sell signals have been removed. It may make sense not to sell and hold on, but I\u2019m not entirely sure this is correct in this case.<\/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\/10\/APPL-SMA50-SMA200-and-MACD-plot.png\" alt=\"APPL SMA50 SMA200 and MACD plot\" class=\"wp-image-713\"\/><\/figure>\n\n\n\n<p id=\"c311\">What if we adjust our sell signal to say if the SMA50 is above the SMA200 and the MACD crosses below the Signal, what happens then?<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df[\"macdgtsignalco\"] = df.macdgtsignal.ne(df.macdgtsignal.shift())\ndf.loc[df[\"macdgtsignal\"] == False, \"macdgtsignalco\"] = False\n\ndf[\"macdltsignalco\"] = df.macdltsignal.ne(df.macdltsignal.shift())\ndf.loc[df[\"macdltsignal\"] == False, \"macdltsignalco\"] = False\n\nbuysignals = df[(df[\"sma50gtsma200co\"] == 1) &amp; (df[\"macdgtsignal\"] == 1)]\n\nsellsignals = df[(df[\"sma50gtsma200\"] == 1) &amp; (df[\"macdltsignalco\"] == 1)]<\/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>We now have 5 sell trading signals in our Python&#8217;s dataframe&#8230;<\/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\/10\/APPL-5-sell-signals.png\" alt=\"APPL 5 sell signals\" class=\"wp-image-714\"\/><\/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\/10\/APPL-5-sell-signals-plot.png\" alt=\"APPL 5 sell signals plot\" class=\"wp-image-715\"\/><\/figure>\n\n\n\n<p id=\"7d0c\">This looks a lot better now. Our buy signal looks good, and we have a number of sell signals to choose from. In fact even selling at that first sell signal after the buy with some leverage would be pretty decent.<\/p>\n\n\n\n<p id=\"d7cc\">This should give you an idea of what can be done and the tools to make it happen. You can experiment using all sorts of technical indicators and candlestick patterns to come up with your \u201cmagic strategy\u201d.<\/p>\n\n\n\n<p id=\"97ee\">Why don\u2019t you try adding the&nbsp;<a href=\"https:\/\/www.investopedia.com\/terms\/r\/rsi.asp\" rel=\"noreferrer noopener\" target=\"_blank\">Relative Strength Index<\/a>&nbsp;(RSI) yourself. A buy signal is generally considered if the RSI14 is below 30 and a sell signal is considered if the RSI14 is above 70.<\/p>\n\n\n\n<p id=\"aec8\">I will help get you started. The calculation for RSI is not as simple as the moving averages and MACD. It\u2019s probably easier for you to use the \u201cpandas_ta\u201d library. I\u2019ll get you started.<\/p>\n\n\n\n<p id=\"0edc\">In Google Colab you will need to install the \u201cpandas_ta\u201d library using a dedicated code cell.<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">pip install pandas_ta<\/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>The import the library\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">import pandas_ta as ta<\/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>And then calculate the RSI14\u2026<\/p>\n\n\n\n            <div class=\"code__wrapper\">\n                <div class=\"code__content\">\n                    \n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code lang=\"python\" class=\"language-python\">df[\"rsi14\"] = ta.rsi(df[\"close\"], length=14, fillna=50)<\/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=\"2006\">The first 14 entries of RSI14 will be NaN. What I\u2019m doing here with the \u201cfillna\u201d is to set the default value to 50.<\/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<p id=\"d651\">I hope you found this article interesting and useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Plot buy and sell trading signals in Python&#8217;s graph I\u2019m going to assume you know the basics of technical analysis in trading. If this is all new to you, I recommend a great resource called&nbsp;Investopedia. If you want to learn how to install the EODHD APIs Python Financial Official Library and activate your API key, [&hellip;]<\/p>\n","protected":false},"author":8,"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":[42],"tags":[],"coding-language":[30],"ready-to-go-solution":[56],"qualification":[31,32],"financial-apis-category":[36],"financial-apis-manuals":[39],"class_list":["post-683","post","type-post","status-publish","format-standard","hentry","category-stocks-data-analysis-examples","coding-language-python","ready-to-go-solution-eodhd-python-financial-library","qualification-experienced","qualification-guru","financial-apis-category-stock-market-prices","financial-apis-manuals-end-of-day"],"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>Visualising Trading Signals in Python | EODHD APIs Academy<\/title>\n<meta name=\"description\" content=\"Trading Signals in Python: Plotting and visualising the buysignals &amp; sellsignals with SMA50, SMA200 and the MACD\" \/>\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\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visualising Trading Signals in Python\" \/>\n<meta property=\"og:description\" content=\"Trading Signals in Python: Plotting and visualising the buysignals &amp; sellsignals with SMA50, SMA200 and the MACD\" \/>\n<meta property=\"og:url\" content=\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\" \/>\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=\"2022-10-03T13:25:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-05T12:28:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Visualising-Trading-Signals-in-Python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"799\" \/>\n\t<meta property=\"og:image:height\" content=\"450\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Michael Whittle\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:site\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Whittle\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#article\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\"},\"author\":{\"name\":\"Michael Whittle\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/50784c270b6267df5969514d80d510ad\"},\"headline\":\"Visualising Trading Signals in Python\",\"datePublished\":\"2022-10-03T13:25:10+00:00\",\"dateModified\":\"2025-02-05T12:28:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\"},\"wordCount\":1629,\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png\",\"articleSection\":[\"Stocks Data Analysis Examples\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\",\"name\":\"Visualising Trading Signals in Python | EODHD APIs Academy\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png\",\"datePublished\":\"2022-10-03T13:25:10+00:00\",\"dateModified\":\"2025-02-05T12:28:12+00:00\",\"description\":\"Trading Signals in Python: Plotting and visualising the buysignals & sellsignals with SMA50, SMA200 and the MACD\",\"breadcrumb\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage\",\"url\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png\",\"contentUrl\":\"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/eodhd.com\/financial-academy\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visualising Trading Signals in Python\"}]},{\"@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\/50784c270b6267df5969514d80d510ad\",\"name\":\"Michael Whittle\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5076af85c7ee0445454257247cad4970ae8cf5d7d4940d2b32c521f51c0a0f5a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5076af85c7ee0445454257247cad4970ae8cf5d7d4940d2b32c521f51c0a0f5a?s=96&d=mm&r=g\",\"caption\":\"Michael Whittle\"},\"description\":\"Solution architect, developer, and analyst with over 20+ years experience (TOP author on Medium).\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/author\/michaelwhittle\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Visualising Trading Signals in Python | EODHD APIs Academy","description":"Trading Signals in Python: Plotting and visualising the buysignals & sellsignals with SMA50, SMA200 and the MACD","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\/stocks-data-analysis-examples\/visualising-trading-signals-in-python","og_locale":"en_US","og_type":"article","og_title":"Visualising Trading Signals in Python","og_description":"Trading Signals in Python: Plotting and visualising the buysignals & sellsignals with SMA50, SMA200 and the MACD","og_url":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python","og_site_name":"Financial Academy","article_publisher":"https:\/\/www.facebook.com\/eodhistoricaldata","article_published_time":"2022-10-03T13:25:10+00:00","article_modified_time":"2025-02-05T12:28:12+00:00","og_image":[{"width":799,"height":450,"url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Visualising-Trading-Signals-in-Python.png","type":"image\/png"}],"author":"Michael Whittle","twitter_card":"summary_large_image","twitter_creator":"@EOD_data","twitter_site":"@EOD_data","twitter_misc":{"Written by":"Michael Whittle","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#article","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python"},"author":{"name":"Michael Whittle","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/50784c270b6267df5969514d80d510ad"},"headline":"Visualising Trading Signals in Python","datePublished":"2022-10-03T13:25:10+00:00","dateModified":"2025-02-05T12:28:12+00:00","mainEntityOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python"},"wordCount":1629,"publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage"},"thumbnailUrl":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png","articleSection":["Stocks Data Analysis Examples"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python","url":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python","name":"Visualising Trading Signals in Python | EODHD APIs Academy","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/#website"},"primaryImageOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage"},"thumbnailUrl":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png","datePublished":"2022-10-03T13:25:10+00:00","dateModified":"2025-02-05T12:28:12+00:00","description":"Trading Signals in Python: Plotting and visualising the buysignals & sellsignals with SMA50, SMA200 and the MACD","breadcrumb":{"@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#primaryimage","url":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png","contentUrl":"https:\/\/eodhistoricaldata.com\/financial-academy\/wp-content\/uploads\/2022\/10\/Apple-stock-AAPL-in-Pandas-dataframe.png"},{"@type":"BreadcrumbList","@id":"https:\/\/eodhd.com\/financial-academy\/stocks-data-analysis-examples\/visualising-trading-signals-in-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/eodhd.com\/financial-academy\/"},{"@type":"ListItem","position":2,"name":"Visualising Trading Signals in Python"}]},{"@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\/50784c270b6267df5969514d80d510ad","name":"Michael Whittle","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5076af85c7ee0445454257247cad4970ae8cf5d7d4940d2b32c521f51c0a0f5a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5076af85c7ee0445454257247cad4970ae8cf5d7d4940d2b32c521f51c0a0f5a?s=96&d=mm&r=g","caption":"Michael Whittle"},"description":"Solution architect, developer, and analyst with over 20+ years experience (TOP author on Medium).","url":"https:\/\/eodhd.com\/financial-academy\/author\/michaelwhittle"}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pdOdVT-b1","jetpack_sharing_enabled":true,"acf":[],"_links":{"self":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/683","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\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/comments?post=683"}],"version-history":[{"count":8,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/683\/revisions"}],"predecessor-version":[{"id":6256,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/683\/revisions\/6256"}],"wp:attachment":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/media?parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/categories?post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/tags?post=683"},{"taxonomy":"coding-language","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/coding-language?post=683"},{"taxonomy":"ready-to-go-solution","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/ready-to-go-solution?post=683"},{"taxonomy":"qualification","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/qualification?post=683"},{"taxonomy":"financial-apis-category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-category?post=683"},{"taxonomy":"financial-apis-manuals","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-manuals?post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}