{"id":3246,"date":"2017-10-11T15:20:18","date_gmt":"2017-10-11T15:20:18","guid":{"rendered":"http:\/\/joapen.com\/blog\/?p=3246"},"modified":"2017-11-10T18:09:23","modified_gmt":"2017-11-10T18:09:23","slug":"quantitative-trading-strategy-pandas-matplotlib","status":"publish","type":"post","link":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/","title":{"rendered":"Quantitative trading strategy: Pandas and matplotlib"},"content":{"rendered":"<p>Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code.<\/p>\n<p>I found <a href=\"http:\/\/www.learndatasci.com\/python-finance-part-yahoo-finance-api-pandas-matplotlib\/\">this tutorial<\/a> very useful for these purposes. The use of Pandas for reading data from yahoo, google&#8230; and matplotlib to build an easy chart is key to take the first steps. The best of the tutorial are the comments about each step.<\/p>\n<h1>Problems I have found<\/h1>\n<p>Yahoo has closed the API that enabled Pandas to retrieve data. My colleague has found a workaround to continue using the API. The solution consists on:<\/p>\n<ol>\n<li>Add with pip the library: <a href=\"https:\/\/github.com\/ranaroussi\/fix-yahoo-finance\">fix_yahoo_finance<\/a>.<\/li>\n<li>Add these 2 lines to the code:<\/li>\n<\/ol>\n<p style=\"padding-left: 60px;\"><em>import fix_yahoo_finance as yf<\/em><\/p>\n<p style=\"padding-left: 60px;\"><em>yf.pdr_override() # &lt;== that&#8217;s all it takes \ud83d\ude42<\/em><\/p>\n<p>This <a href=\"https:\/\/www.youtube.com\/watch?v=aaWLM60KxGg\">video<\/a> is a must see to me as <a class=\"yt-simple-endpoint style-scope yt-formatted-string\" href=\"https:\/\/www.youtube.com\/channel\/UC7rAMt91olTWlc-hIjFGNWg\">Jev Kuznetsov<\/a> explains it from scratch.<\/p>\n<ul>\n<li>He shows the example using <a href=\"https:\/\/ipython.org\/\">IPython NoteBook<\/a>.<\/li>\n<li>He takes an strategy from: <a href=\"http:\/\/www.quantifiedstrategies.com\/\">Quantified Strategies.<\/a><\/li>\n<li>Jev documents different topics <a href=\"http:\/\/tradingwithpython.blogspot.com.es\/\">here<\/a>.<\/li>\n<\/ul>\n<h1>The first code I performed was this one:<\/h1>\n<p style=\"padding-left: 30px;\">from pandas_datareader import data<br \/>\nimport pandas as pd<br \/>\nimport matplotlib.pyplot as plt<\/p>\n<p style=\"padding-left: 30px;\"># Define the instruments to download. We would like to see Apple, Microsoft and the S&amp;P500 index.<br \/>\ntickers = [&#8216;T&#8217;, &#8216;VZ&#8217;, &#8216;SPY&#8217;]<\/p>\n<p style=\"padding-left: 30px;\"># Define which online source one should use<br \/>\ndata_source = &#8216;google&#8217;<\/p>\n<p style=\"padding-left: 30px;\"># We would like all available data from 01\/01\/2000 until 12\/31\/2016.<br \/>\nstart_date = &#8216;2015-01-01&#8217;<br \/>\nend_date = &#8216;2017-10-10&#8217;<\/p>\n<p style=\"padding-left: 30px;\"># User pandas_reader.data.DataReader to load the desired data. As simple as that.<br \/>\npanel_data = data.DataReader(tickers, data_source, start_date, end_date)<\/p>\n<p style=\"padding-left: 30px;\"># Getting just the adjusted closing prices. This will return a Pandas DataFrame<br \/>\n# The index in this DataFrame is the major index of the panel_data.<br \/>\nclose = panel_data.ix[&#8216;Close&#8217;]<\/p>\n<p style=\"padding-left: 30px;\"># Getting all weekdays between 01\/01\/2000 and 12\/31\/2016<br \/>\nall_weekdays = pd.date_range(start=start_date, end=end_date, freq=&#8217;B&#8217;)<\/p>\n<p style=\"padding-left: 30px;\"># How do we align the existing prices in adj_close with our new set of dates?<br \/>\n# All we need to do is reindex close using all_weekdays as the new index<br \/>\nclose = close.reindex(all_weekdays)<br \/>\n#print(close.head(-100))<br \/>\nprint(close.describe())<br \/>\nclose = close.fillna(method=&#8217;ffill&#8217;)<br \/>\n# Get the MSFT time series. This now returns a Pandas Series object indexed by date.<br \/>\nvz = close.ix[:, &#8216;VZ&#8217;]<br \/>\nspy = close.ix[:, &#8216;SPY&#8217;]<br \/>\n# Calculate the 20 and 100 days moving averages of the closing prices<br \/>\nshort_rolling_vz = vz.rolling(window=20).mean()<br \/>\nlong_rolling_vz = vz.rolling(window=100).mean()<\/p>\n<p style=\"padding-left: 30px;\"><em># Plot everything by leveraging the very powerful matplotlib package<\/em><br \/>\nfig = plt.figure()<br \/>\nax = fig.add_subplot(1,1,1)<br \/>\nax.plot(vz.index, vz, label=&#8217;VZ&#8217;)<br \/>\n#ax.plot(spy.index, spy, label=&#8217;SPY&#8217;)<br \/>\nax.plot(short_rolling_vz.index, short_rolling_vz, label=&#8217;20 days rolling&#8217;)<br \/>\nax.plot(long_rolling_vz.index, long_rolling_vz, label=&#8217;100 days rolling&#8217;)<br \/>\nax.set_xlabel(&#8216;Date&#8217;)<br \/>\nax.set_ylabel(&#8216;Closing price ($)&#8217;)<br \/>\nax.legend()<br \/>\nplt.show()<\/p>\n<p style=\"padding-left: 30px;\">\n","protected":false},"excerpt":{"rendered":"<p>Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial very useful for these purposes. The use of Pandas for reading data from yahoo, google&#8230; and matplotlib to build an easy chart is key to take &#8230; <a title=\"Quantitative trading strategy: Pandas and matplotlib\" class=\"read-more\" href=\"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/\" aria-label=\"Read more about Quantitative trading strategy: Pandas and matplotlib\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104,105],"tags":[123,122,106],"class_list":["post-3246","post","type-post","status-publish","format-standard","hentry","category-python","category-trading","tag-matplotlib","tag-pandas","tag-quantitative-trading"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Quantitative trading strategy: Pandas and matplotlib -<\/title>\n<meta name=\"description\" content=\"Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial - joapen projects\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Quantitative trading strategy: Pandas and matplotlib -\" \/>\n<meta property=\"og:description\" content=\"Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial - joapen projects\" \/>\n<meta property=\"og:url\" content=\"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/\" \/>\n<meta property=\"og:site_name\" content=\"joapen projects\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-11T15:20:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-10T18:09:23+00:00\" \/>\n<meta name=\"author\" content=\"joapen\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"joapen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/#article\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/\"},\"author\":{\"name\":\"joapen\",\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/#\\\/schema\\\/person\\\/23919df2312175fe9c4609203595b217\"},\"headline\":\"Quantitative trading strategy: Pandas and matplotlib\",\"datePublished\":\"2017-10-11T15:20:18+00:00\",\"dateModified\":\"2017-11-10T18:09:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/\"},\"wordCount\":474,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/#\\\/schema\\\/person\\\/23919df2312175fe9c4609203595b217\"},\"keywords\":[\"matplotlib\",\"Pandas\",\"quantitative trading\"],\"articleSection\":[\"Python\",\"Trading\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/\",\"url\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/\",\"name\":\"Quantitative trading strategy: Pandas and matplotlib -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-10-11T15:20:18+00:00\",\"dateModified\":\"2017-11-10T18:09:23+00:00\",\"description\":\"Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial - joapen projects\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/joapen.com\\\/blog\\\/2017\\\/10\\\/11\\\/quantitative-trading-strategy-pandas-matplotlib\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/joapen.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Quantitative trading strategy: Pandas and matplotlib\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/joapen.com\\\/blog\\\/\",\"name\":\"joapen projects\",\"description\":\"Just a place to write\",\"publisher\":{\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/#\\\/schema\\\/person\\\/23919df2312175fe9c4609203595b217\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/joapen.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/#\\\/schema\\\/person\\\/23919df2312175fe9c4609203595b217\",\"name\":\"joapen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/joapen-mini.jpeg\",\"url\":\"https:\\\/\\\/joapen.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/joapen-mini.jpeg\",\"contentUrl\":\"https:\\\/\\\/joapen.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/joapen-mini.jpeg\",\"width\":400,\"height\":400,\"caption\":\"joapen\"},\"logo\":{\"@id\":\"https:\\\/\\\/joapen.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/joapen-mini.jpeg\"},\"sameAs\":[\"http:\\\/\\\/www.joapen.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Quantitative trading strategy: Pandas and matplotlib -","description":"Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial - joapen projects","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":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/","og_locale":"en_US","og_type":"article","og_title":"Quantitative trading strategy: Pandas and matplotlib -","og_description":"Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial - joapen projects","og_url":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/","og_site_name":"joapen projects","article_published_time":"2017-10-11T15:20:18+00:00","article_modified_time":"2017-11-10T18:09:23+00:00","author":"joapen","twitter_misc":{"Written by":"joapen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/#article","isPartOf":{"@id":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/"},"author":{"name":"joapen","@id":"https:\/\/joapen.com\/blog\/#\/schema\/person\/23919df2312175fe9c4609203595b217"},"headline":"Quantitative trading strategy: Pandas and matplotlib","datePublished":"2017-10-11T15:20:18+00:00","dateModified":"2017-11-10T18:09:23+00:00","mainEntityOfPage":{"@id":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/"},"wordCount":474,"commentCount":0,"publisher":{"@id":"https:\/\/joapen.com\/blog\/#\/schema\/person\/23919df2312175fe9c4609203595b217"},"keywords":["matplotlib","Pandas","quantitative trading"],"articleSection":["Python","Trading"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/","url":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/","name":"Quantitative trading strategy: Pandas and matplotlib -","isPartOf":{"@id":"https:\/\/joapen.com\/blog\/#website"},"datePublished":"2017-10-11T15:20:18+00:00","dateModified":"2017-11-10T18:09:23+00:00","description":"Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code. I found this tutorial - joapen projects","breadcrumb":{"@id":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/joapen.com\/blog\/2017\/10\/11\/quantitative-trading-strategy-pandas-matplotlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/joapen.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Quantitative trading strategy: Pandas and matplotlib"}]},{"@type":"WebSite","@id":"https:\/\/joapen.com\/blog\/#website","url":"https:\/\/joapen.com\/blog\/","name":"joapen projects","description":"Just a place to write","publisher":{"@id":"https:\/\/joapen.com\/blog\/#\/schema\/person\/23919df2312175fe9c4609203595b217"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/joapen.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/joapen.com\/blog\/#\/schema\/person\/23919df2312175fe9c4609203595b217","name":"joapen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/joapen.com\/blog\/wp-content\/uploads\/2021\/04\/joapen-mini.jpeg","url":"https:\/\/joapen.com\/blog\/wp-content\/uploads\/2021\/04\/joapen-mini.jpeg","contentUrl":"https:\/\/joapen.com\/blog\/wp-content\/uploads\/2021\/04\/joapen-mini.jpeg","width":400,"height":400,"caption":"joapen"},"logo":{"@id":"https:\/\/joapen.com\/blog\/wp-content\/uploads\/2021\/04\/joapen-mini.jpeg"},"sameAs":["http:\/\/www.joapen.com"]}]}},"_links":{"self":[{"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/posts\/3246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/comments?post=3246"}],"version-history":[{"count":3,"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/posts\/3246\/revisions"}],"predecessor-version":[{"id":3249,"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/posts\/3246\/revisions\/3249"}],"wp:attachment":[{"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/media?parent=3246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/categories?post=3246"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/joapen.com\/blog\/wp-json\/wp\/v2\/tags?post=3246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}