<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bill Voisine on Medium]]></title>
        <description><![CDATA[Stories by Bill Voisine on Medium]]></description>
        <link>https://medium.com/@integralBill?source=rss-23d7fee494e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*RUXn_H1yAeTTO6fuqYg58w.jpeg</url>
            <title>Stories by Bill Voisine on Medium</title>
            <link>https://medium.com/@integralBill?source=rss-23d7fee494e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:49:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@integralBill/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Time-Series and Correlations with Stock Market Data using Python]]></title>
            <link>https://medium.com/data-science/time-series-and-correlations-with-stock-market-data-using-python-e66774e3a16f?source=rss-23d7fee494e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e66774e3a16f</guid>
            <category><![CDATA[time-series-analysis]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[stock-market]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[finance]]></category>
            <dc:creator><![CDATA[Bill Voisine]]></dc:creator>
            <pubDate>Mon, 25 Nov 2019 16:00:14 GMT</pubDate>
            <atom:updated>2021-05-14T22:07:56.322Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve recently created an account with IEX Cloud, a financial data service. As I’ve been learning the features of this new data source (new to me) and experimenting within my Jupyter Notebook, I thought the below may be helpful for others as well. Therefore, I’m creating my first Medium article and will focus it on financial time series data.</p><p>There are quite a few articles and sources on defining correlation, and the differences between correlation and causation; so what you will find below will primarily show some ways to test correlation and what the results mean. You may find this article beneficial if you’re looking to use <strong>IEX Cloud</strong>, if you’re looking to do Correlation tests in Python, and if you’re interested in Time-Series data!</p><p>If you’re following this and coding it yourself, go to <a href="https://iexcloud.io/">https://iexcloud.io/</a> and get yourself an API key! You’ll need it next! Also, don’t forgot to install an IEX Python library: <a href="https://addisonlynch.github.io/iexfinance/">https://addisonlynch.github.io/iexfinance/</a> (Lynch, 2019). Install this library using:</p><p><strong>$ pip3 install iexfinance</strong></p><pre><strong>import</strong> <strong>config</strong><br><strong>import</strong> <strong>os</strong></pre><pre><em>#os.environ[&#39;IEX_API_VERSION&#39;] = &quot;iexcloud-sandbox&quot;</em><br>os.environ[&#39;IEX_TOKEN&#39;] = config.iex_api_key  <em># Replace &quot;config.iex_api_key&quot; with your API Key from IEX Cloud!</em></pre><p>We’ll load up some of the libraries we’ll need next. Also, we’ll be using data between January 1, 2017 and November 22, 2019.</p><pre><strong>from</strong> <strong>datetime</strong> <strong>import</strong> datetime<br><strong>from</strong> <strong>iexfinance.stocks</strong> <strong>import</strong> get_historical_data<br><strong>import</strong> <strong>matplotlib.pyplot</strong> <strong>as</strong> <strong>plt</strong><br>%matplotlib inline</pre><pre>start = datetime(2017, 1, 1)<br>end = datetime(2019, 11, 22)</pre><p>We’ll <strong>arbitrarily</strong> choose ‘close’ for the sake of simplicity. From this API data response, you could also choose Open, High, Low, and Volume. We’ll experiment with ‘close.’</p><p>Now, let’s make an API call and download more data, ‘SPDR S&amp;P 500 Trust ETF,’ which tracks the S&amp;P 500 (ticker: SPY). We’ll be using this later in some correlation tests.</p><pre>SPY = get_historical_data(&quot;SPY&quot;, start, end, output_format=&#39;pandas&#39;)</pre><pre>plt.figure(figsize=(10, 4))<br>plt.plot(SPY.index, SPY[&#39;close&#39;])<br>plt.title(&#39;Daily Times Series for the SPY&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*IQ4QNmQ7051BYvR_DlDr9A.png" /></figure><p>Now, let’s continue to explore the API by downloading data for the FAANG stocks (Facebook, Amazon, Apple, Netflix, and Google) (Kenton, 2019), as well as add an interest of mine, Tesla. Also, chart these up. Pay careful attention to the charts and do a comparison of those with the SPY chart above. Some of these will look very similar to the SPY chart, some won’t.</p><pre>FAANGT = get_historical_data([&quot;FB&quot;,&quot;AMZN&quot;,&quot;AAPL&quot;,&quot;NFLX&quot;,&quot;GOOG&quot;,&quot;TSLA&quot;], start, end, output_format=&#39;pandas&#39;)<br>print(FAANGT.head())</pre><pre>FB                                      AMZN                  \<br>              open    high     low   close    volume    open    high     low   <br>date                                                                           <br>2017-01-03  116.03  117.84  115.51  116.86  20663912  757.92  758.76  747.70   <br>2017-01-04  117.55  119.66  117.29  118.69  19630932  758.39  759.68  754.20   <br>2017-01-05  118.86  120.95  118.32  120.67  19492150  761.55  782.40  760.26   <br>2017-01-06  120.98  123.88  120.03  123.41  28545263  782.36  799.44  778.48   <br>2017-01-09  123.55  125.43  123.04  124.90  22880360  798.00  801.77  791.77   </pre><pre>                             ...    GOOG                                   \<br>             close   volume  ...    open    high     low   close   volume   <br>date                         ...                                            <br>2017-01-03  753.67  3521066  ...  778.81  789.63  775.80  786.14  1657268   <br>2017-01-04  757.18  2510526  ...  788.36  791.34  783.16  786.90  1072958   <br>2017-01-05  780.45  5830068  ...  786.08  794.48  785.02  794.02  1335167   <br>2017-01-06  795.99  5986234  ...  795.26  807.90  792.20  806.15  1640170   <br>2017-01-09  796.92  3446109  ...  806.40  809.97  802.83  806.65  1274645   </pre><pre>              TSLA                                    <br>              open    high     low   close    volume  <br>date                                                  <br>2017-01-03  214.86  220.33  210.96  216.99   5923254  <br>2017-01-04  214.75  228.00  214.31  226.99  11213471  <br>2017-01-05  226.42  227.48  221.95  226.75   5911695  <br>2017-01-06  226.93  230.31  225.45  229.01   5527893  <br>2017-01-09  228.97  231.92  228.00  231.28   3979484  </pre><pre>[5 rows x 30 columns]</pre><pre>plt.figure(figsize=(15, 4))<br>#FB<br>plt.subplot(1, 2, 1)<br>plt.plot(FAANGT.index, FAANGT[&#39;FB&#39;][&#39;close&#39;])<br>plt.title(&#39;Daily Times Series for the FB&#39;)</pre><pre>#AMZN<br>plt.subplot(1, 2, 2)<br>plt.plot(FAANGT.index, FAANGT[&#39;AMZN&#39;][&#39;close&#39;])<br>plt.title(&#39;Daily Times Series for the AMZN&#39;);<br></pre><pre>plt.figure(figsize=(15, 4))<br>#AAPL<br>plt.subplot(1, 2, 1)<br>plt.plot(FAANGT.index, FAANGT[&#39;AAPL&#39;][&#39;close&#39;])<br>plt.title(&#39;Daily Times Series for the AAPL&#39;);</pre><pre>#GOOG<br>plt.subplot(1, 2, 2)<br>plt.plot(FAANGT.index, FAANGT[&#39;GOOG&#39;][&#39;close&#39;])<br>plt.title(&#39;Daily Times Series for the GOOG&#39;);</pre><pre>plt.figure(figsize=(15, 4))<br>#TSLA<br>plt.subplot(1, 2, 1)<br>plt.plot(FAANGT.index, FAANGT[&#39;TSLA&#39;][&#39;close&#39;])<br>plt.title(&#39;Daily Times Series for the TSLA&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*aq-vn9aen11wveiVmuZheQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*cH_D_apI9Als4p0zFAJMtA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*6r6NYbc9uHqW1irtlm5lyg.png" /></figure><p>Now that we have data for both the FAANG stocks (and TSLA) and the S&amp;P 500, and we’ve plotted these so that we know what they look like; let’s try an experiment! We’re going to try a Pearson Correlation test, to test correlation on all of these equities and the S&amp;P 500. What do you think? Based-on viewing the charts and going by intuition, will they correlate?</p><p>Correlation will show when the Pearson Correlation Coefficient is between -1 and +1. If closer to +1, we’re seeing a positive correlation. If Pearson’s correlation is closer to -1, a negative correlation (Cheong, 2019).</p><pre><strong>import</strong> <strong>pandas</strong> <strong>as</strong> <strong>pd<br>import scipy.stats as stats</strong></pre><pre><em># Slice this up to make it easier to work with.</em><br>indx = pd.IndexSlice<br>df1 = FAANGT.loc[:, (indx[:],&#39;close&#39;)]<br><br>c, p = stats.pearsonr(df1[&#39;FB&#39;].dropna()[&#39;close&#39;], SPY.dropna()[&#39;close&#39;])<br>print(f&quot;FB vs SPY Pearson Correlation: <strong>{c}\n</strong>&quot;)<br><br>c, p = stats.pearsonr(df1[&#39;AMZN&#39;].dropna()[&#39;close&#39;], SPY.dropna()[&#39;close&#39;])<br>print(f&quot;AMZN vs SPY Pearson Correlation: <strong>{c}\n</strong>&quot;)<br><br>c, p = stats.pearsonr(df1[&#39;AAPL&#39;].dropna()[&#39;close&#39;], SPY.dropna()[&#39;close&#39;])<br>print(f&quot;AAPL vs SPY Pearson Correlation: <strong>{c}\n</strong>&quot;)<br><br>c, p = stats.pearsonr(df1[&#39;GOOG&#39;].dropna()[&#39;close&#39;], SPY.dropna()[&#39;close&#39;])<br>print(f&quot;GOOG vs SPY Pearson Correlation: <strong>{c}\n</strong>&quot;)<br><br>c, p = stats.pearsonr(df1[&#39;TSLA&#39;].dropna()[&#39;close&#39;], SPY.dropna()[&#39;close&#39;])<br>print(f&quot;TSLA vs SPY Pearson Correlation: <strong>{c}</strong>&quot;)</pre><pre>FB vs SPY Pearson Correlation: 0.7325442525842248<br><br>AMZN vs SPY Pearson Correlation: 0.910899729798812<br><br>AAPL vs SPY Pearson Correlation: 0.9176098570966427<br><br>GOOG vs SPY Pearson Correlation: 0.9485878709468345<br><br>TSLA vs SPY Pearson Correlation: -0.26968006350226387</pre><p>As of 11/22/2019, Google (GOOG) has the highest Pearson Correlation Coefficient out of all of these options. Also, Tesla (TSLA) has negative correlation to the S&amp;P 500. Many of these you could find by looking in the charts above and comparing the charts with the S&amp;P 500 chart; but now you have a quantitative approach for correlation!</p><p>Now, to explore the API a bit more, let’s see how the <strong>social sentiment</strong> feature looks. We’ll take a look at yesterday (11/22/2019), for Tesla.</p><pre><strong>from</strong> <strong>iexfinance.altdata</strong> <strong>import</strong>  get_social_sentiment, get_ceo_compensation<br><br>period=&#39;minute&#39;<br>specDay=&quot;20191122&quot;<br><br>TSLA_Sent = get_social_sentiment(&quot;TSLA&quot;, period, specDay, output_format=&#39;pandas&#39;)<br>print(TSLA_Sent.head())</pre><pre>minute  negative  positive  sentiment  totalScores<br>0   0000      0.12      0.88   0.084958           26<br>1   0001      0.12      0.88   0.160624           17<br>2   0002      0.11      0.89   0.061056           18<br>3   0003      0.29      0.71  -0.180071           17<br>4   0004      0.07      0.93   0.066293           15</pre><p>For yesterday, what was the highest score for the most positive, and most negative, social sentiment of Tesla?</p><pre>TSLA_Sent_Pos = TSLA_Sent[&#39;sentiment&#39;].max()<br>TSLA_Sent_Neg = TSLA_Sent[&#39;sentiment&#39;].min()</pre><pre>print(&quot;Highest Social Sentiment on 11/22/2019:&quot;, TSLA_Sent_Pos)<br>print(&quot;Lowest Social Sentiment on 11/22/2019:&quot;, TSLA_Sent_Neg)</pre><pre>Highest Social Sentiment on 11/22/2019: 0.9785<br>Lowest Social Sentiment on 11/22/2019: -0.9487</pre><p>This API also has CEO information! Let’s take a look at CEO information for the FAANG and TSLA stocks we researched earlier. We’ll use df1 which was created and used to simplify when performing the correlation tests.</p><pre><strong>import</strong> <strong>pprint</strong><br><br><strong>for</strong> n, q <strong>in</strong> df1:<br>    pprint.pprint(get_ceo_compensation(n))</pre><pre>{&#39;bonus&#39;: 0,<br> &#39;companyName&#39;: &#39;Facebook Inc. Class A&#39;,<br> &#39;location&#39;: &#39;Menlo Park, CA&#39;,<br> &#39;name&#39;: &#39;Mark Zuckerberg&#39;,<br> &#39;nonEquityIncentives&#39;: 0,<br> &#39;optionAwards&#39;: 0,<br> &#39;otherComp&#39;: 22554542,<br> &#39;pensionAndDeferred&#39;: 0,<br> &#39;salary&#39;: 1,<br> &#39;stockAwards&#39;: 0,<br> &#39;symbol&#39;: &#39;FB&#39;,<br> &#39;total&#39;: 22554543,<br> &#39;year&#39;: &#39;2018&#39;}<br>{&#39;bonus&#39;: 0,<br> &#39;companyName&#39;: &#39;Amazon.com Inc.&#39;,<br> &#39;location&#39;: &#39;Seattle, WA&#39;,<br> &#39;name&#39;: &#39;Jeffrey Bezos&#39;,<br> &#39;nonEquityIncentives&#39;: 0,<br> &#39;optionAwards&#39;: 0,<br> &#39;otherComp&#39;: 1600000,<br> &#39;pensionAndDeferred&#39;: 0,<br> &#39;salary&#39;: 81840,<br> &#39;stockAwards&#39;: 0,<br> &#39;symbol&#39;: &#39;AMZN&#39;,<br> &#39;total&#39;: 1681840,<br> &#39;year&#39;: &#39;2018&#39;}<br>{&#39;bonus&#39;: 0,<br> &#39;companyName&#39;: &#39;Apple Inc.&#39;,<br> &#39;location&#39;: &#39;Cupertino, CA&#39;,<br> &#39;name&#39;: &#39;Timothy Cook&#39;,<br> &#39;nonEquityIncentives&#39;: 12000000,<br> &#39;optionAwards&#39;: 0,<br> &#39;otherComp&#39;: 682219,<br> &#39;pensionAndDeferred&#39;: 0,<br> &#39;salary&#39;: 3000000,<br> &#39;stockAwards&#39;: 0,<br> &#39;symbol&#39;: &#39;AAPL&#39;,<br> &#39;total&#39;: 15682219,<br> &#39;year&#39;: &#39;2018&#39;}<br>{&#39;bonus&#39;: 0,<br> &#39;companyName&#39;: &#39;Netflix Inc.&#39;,<br> &#39;location&#39;: &#39;Los Gatos, CA&#39;,<br> &#39;name&#39;: &#39;Reed Hastings&#39;,<br> &#39;nonEquityIncentives&#39;: 0,<br> &#39;optionAwards&#39;: 35380417,<br> &#39;otherComp&#39;: 0,<br> &#39;pensionAndDeferred&#39;: 0,<br> &#39;salary&#39;: 700000,<br> &#39;stockAwards&#39;: 0,<br> &#39;symbol&#39;: &#39;NFLX&#39;,<br> &#39;total&#39;: 36080417,<br> &#39;year&#39;: &#39;2018&#39;}<br>{&#39;bonus&#39;: 0,<br> &#39;companyName&#39;: &#39;Alphabet Inc. Class A&#39;,<br> &#39;location&#39;: &#39;Mountain View, CA&#39;,<br> &#39;name&#39;: &#39;Larry Page&#39;,<br> &#39;nonEquityIncentives&#39;: 0,<br> &#39;optionAwards&#39;: 0,<br> &#39;otherComp&#39;: 0,<br> &#39;pensionAndDeferred&#39;: 0,<br> &#39;salary&#39;: 1,<br> &#39;stockAwards&#39;: 0,<br> &#39;symbol&#39;: &#39;GOOG&#39;,<br> &#39;total&#39;: 1,<br> &#39;year&#39;: &#39;2018&#39;}<br>{&#39;bonus&#39;: 0,<br> &#39;companyName&#39;: &#39;Tesla Inc&#39;,<br> &#39;location&#39;: &#39;Palo Alto, CA&#39;,<br> &#39;name&#39;: &#39;Elon Musk&#39;,<br> &#39;nonEquityIncentives&#39;: 0,<br> &#39;optionAwards&#39;: 2283988504,<br> &#39;otherComp&#39;: 0,<br> &#39;pensionAndDeferred&#39;: 0,<br> &#39;salary&#39;: 56380,<br> &#39;stockAwards&#39;: 0,<br> &#39;symbol&#39;: &#39;TSLA&#39;,<br> &#39;total&#39;: 2284044884,<br> &#39;year&#39;: &#39;2018&#39;}</pre><p>There’s a lot more to explore and analyze. In the next article, I plan to explore some “real-time” data of IEX Cloud, such the books. Also, I may continue on with time-series analysis and move on to some basic forecasting. I hope you found this exploration useful!</p><h3>References</h3><ul><li>Kenton, W. (2019, November 18). What Are FAANG Stocks? Retrieved November 28, 2019, from <a href="https://www.investopedia.com/terms/f/faang-stocks.asp">https://www.investopedia.com/terms/f/faang-stocks.asp</a>.</li><li>Lynch, A. (2019, October 24). addisonlynch/iexfinance. Retrieved November 28, 2019, from <a href="https://github.com/addisonlynch/iexfinance">https://github.com/addisonlynch/iexfinance</a>.</li><li>Cheong, J. H. (2019, May 13). Four ways to quantify synchrony between time series data. Retrieved November 28, 2019, from <a href="https://towardsdatascience.com/four-ways-to-quantify-synchrony-between-time-series-data-b99136c4a9c9">https://towardsdatascience.com/four-ways-to-quantify-synchrony-between-time-series-data-b99136c4a9c9</a>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e66774e3a16f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/time-series-and-correlations-with-stock-market-data-using-python-e66774e3a16f">Time-Series and Correlations with Stock Market Data using Python</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>