{"id":4571,"date":"2012-09-21T19:47:04","date_gmt":"2012-09-21T18:47:04","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=4571"},"modified":"2012-09-21T19:47:04","modified_gmt":"2012-09-21T18:47:04","slug":"some-basic-stock-analysis-with-mathematica-8","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=4571","title":{"rendered":"Some Basic Stock Analysis with Mathematica 8"},"content":{"rendered":"<p>One of my favourite investment news sites is <a href=\"http:\/\/www.fool.co.uk\/\">The Motley Fool<\/a> which frequently run articles such as <a href=\"http:\/\/www.fool.co.uk\/news\/investing\/2012\/09\/07\/10-shares-trading-near-52-week-lows.aspx\">10 Shares Trading Near 52 week lows<\/a> and <a href=\"http:\/\/www.fool.co.uk\/news\/investing\/2012\/09\/03\/15-shares-trading-near-52-week-highs.aspx\">15 Shares Trading Near 52 week Highs<\/a>.\u00a0 The idea behind such filtering is to seek out shares that have done particularly badly (or well) over the last year and then subject them to further analysis in order to find opportunities.\u00a0 Thanks to Mathematica&#8217;s <a href=\"http:\/\/reference.wolfram.com\/mathematica\/ref\/FinancialData.html\">FinancialData<\/a> command, it is rather easy to generate these lists yourself whenever you like.<\/p>\n<p><strong>15 Shares Trading Near 52 Week Highs<\/strong><\/p>\n<p>The <a href=\"http:\/\/www.fool.co.uk\/news\/investing\/2012\/09\/03\/15-shares-trading-near-52-week-highs.aspx\">original article<\/a> selected the 15 largest cap shares from the FTSE All Share Index that were trading within 3% of their 52 week high at the time of publication.\u00a0 Let&#8217;s see how to do that using Mathematica.<\/p>\n<p>The following code returns the tickers of<strong> all<\/strong> shares from the FTSE All Share Index that are trading within 3% of their 52 week high.<\/p>\n<pre>percentage = 3;\r\nall52weekHighs =\r\n Select[FinancialData[\"^FTAS\", \"Members\"],\r\n Abs[FinancialData[#, \"FractionalChangeHigh52Week\"]] &lt; (percentage\/100.) &amp;];<\/pre>\n<p>The variable <strong>all52weekHighs<\/strong> contains a list of stock tickers (e.g. LLOY.L) that meet our criteria.\u00a0 The next thing to do is to find the market cap of each one:<\/p>\n<pre>all52WeekHighsWithCaps =\r\n Map[{#, FinancialData[#, \"MarketCap\"]} &amp;, all52weekHighs];<\/pre>\n<p>This works fine for most shares.  LloydsTSB for example returns {&#8220;LLOY.L&#8221;, 2.7746*10^10} at the time of writing but the MarketCap query fails for some tickers.  For example, the Market Cap for HSL.L is not available and we get {&#8220;HSL.L&#8221;, Missing[&#8220;NotAvailable&#8221;]}.\u00a0 Let&#8217;s discard these by insisting that we only consider stocks that have a numeric market cap.<\/p>\n<pre>Goodall52WeekHighsWithCaps =\r\n  Select[all52WeekHighsWithCaps, NumberQ[#[[2]]] &amp;];<\/pre>\n<p>We sort the list according to MarketCap:<\/p>\n<pre>sorted = Sort[Goodall52WeekHighsWithCaps, #1[[2]] &gt; #2[[2]] &amp;];<\/pre>\n<p>Let&#8217;s prettify the list a little by iterating over all tickers and replacing the ticker with the associated stock name.  Also, let&#8217;s divide the market cap by 1 million to make it more readable<\/p>\n<pre>finallist =\r\n Map[{FinancialData[#[[1]], \"Name\"], #[[2]]\/1000000} &amp;, sorted];<\/pre>\n<p>Now, you may be wondering why I haven&#8217;t been showing you the output of these commands.  This is simply because even this final list is rather large at 118 entries at the time of writing<\/p>\n<pre>Length[finallist]\r\n\r\n118<\/pre>\n<p>The original article only considered the top 15 sorted by Market Cap so let&#8217;s show those. Market Caps are given in millions.<\/p>\n<pre>top15 = finallist[[1;;15]]\/\/Grid\r\n\r\nHSBC Holdings PLC\u00a0\u00a0 \u00a094159.\r\nNational Grid\u00a0\u00a0 \u00a024432.\r\nPrudential PLC\u00a0\u00a0 \u00a021775.\r\nCentrica PLC\u00a0\u00a0 \u00a017193.\r\nRolls Royce Group\u00a0\u00a0 \u00a016363.\r\nWPP Plc\u00a0\u00a0 \u00a010743.\r\nExperian PLC\u00a0\u00a0 \u00a010197.\r\nOld Mutual PLC\u00a0\u00a0 \u00a08400.\r\nLegal &amp; General Group PLC\u00a0\u00a0 \u00a08036.\r\nWolseley PLC\u00a0\u00a0 \u00a07955.\r\nStandard Life\u00a0\u00a0 \u00a06662.\r\nJ Sainsbury plc\u00a0\u00a0 \u00a06401.\r\nAggreko PLC\u00a0\u00a0 \u00a06391.\r\nLand Securities Group PLC\u00a0\u00a0 \u00a06180.\r\nBritish Land Co PLC\u00a0\u00a0 \u00a04859.<\/pre>\n<p>and we are done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my favourite investment news sites is The Motley Fool which frequently run articles such as 10 Shares Trading Near 52 week lows and 15 Shares Trading Near 52 week Highs.\u00a0 The idea behind such filtering is to seek out shares that have done particularly badly (or well) over the last year and then [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[58,8,7],"tags":[],"class_list":["post-4571","post","type-post","status-publish","format-standard","hentry","category-financial-math","category-mathematica","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1bJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4571"}],"version-history":[{"count":16,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4571\/revisions"}],"predecessor-version":[{"id":4587,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4571\/revisions\/4587"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}