<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mbnlc</title>
    <description>The latest articles on DEV Community by mbnlc (@mbnlc).</description>
    <link>https://dev.to/mbnlc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F604833%2Fc0addab3-81bb-40e7-a931-380715624526.png</url>
      <title>DEV Community: mbnlc</title>
      <link>https://dev.to/mbnlc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mbnlc"/>
    <language>en</language>
    <item>
      <title>Live Yahoo! Finance stock prices in Python</title>
      <dc:creator>mbnlc</dc:creator>
      <pubDate>Sun, 28 Mar 2021 20:05:21 +0000</pubDate>
      <link>https://dev.to/mbnlc/live-yahoo-finance-stock-prices-in-python-2gc5</link>
      <guid>https://dev.to/mbnlc/live-yahoo-finance-stock-prices-in-python-2gc5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I am the author of the Python package presented in this article.&lt;/p&gt;

&lt;p&gt;Python is and has been one of the best and most used programming languages for data science and is especially popular in the financial space, being used for quantitative  analytics by even the biggest firms, combing through vast amounts of data, searching for new ways to make money.&lt;/p&gt;

&lt;p&gt;However, this isn't and shouldn't be reserved for big companies. None the less, most data providers charge for their service, so you might ask where you could get your hands on some quality data for your own projects. &lt;/p&gt;

&lt;h2&gt;
  
  
  Enter, Yahoo! Finance...
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://finance.yahoo.com" rel="noopener noreferrer"&gt;Yahoo! Finance&lt;/a&gt; is one of the most popular sites for any kind of financial information and better yet, it's completely free.&lt;/p&gt;

&lt;p&gt;There are multiple ways of obtaining data from Yahoo! Finance with Python. As with any webpage you can use web scraping tools like &lt;a href="https://pypi.org/project/beautifulsoup4/" rel="noopener noreferrer"&gt;beautiful soup&lt;/a&gt; or &lt;a href="https://pypi.org/project/selenium/" rel="noopener noreferrer"&gt;selenium&lt;/a&gt; to retrieve data from the Yahoo! Finance webpage. Even though this approach works, use of the unofficial Yahoo! Finance API has proven to be more efficient. A lot of open source projects such as &lt;a href="https://github.com/ranaroussi/yfinance" rel="noopener noreferrer"&gt;yfinance&lt;/a&gt; make this even easier by providing a pythonic and intuitive way for requesting data.&lt;/p&gt;

&lt;p&gt;Both these methods work perfectly for one-time requests, are, however, not ideal for live updates as they would have to be constantly requesting data / reloading the page. Web sockets allow updates to be sent by the server itself, sparing you countless unnecessary requests, which might even get you banned. &lt;/p&gt;

&lt;h2&gt;
  
  
  Live Yahoo! Finance quotes
&lt;/h2&gt;

&lt;p&gt;Live stock prices are not easy to come by for free, especially via web socket. This has lead me to develop a simple and free way to obtain real time quotes from Yahoo! Finance:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/mbnlc" rel="noopener noreferrer"&gt;
        mbnlc
      &lt;/a&gt; / &lt;a href="https://github.com/mbnlc/yflive" rel="noopener noreferrer"&gt;
        yflive
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Live Data Streamer for Yahoo! Finance
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;yflive&lt;/strong&gt; is a python package for connecting to the Yahoo! Finance web socket in a simple, pythonic way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;yflive&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;QuoteStreamer&lt;/span&gt;

&lt;span class="c1"&gt;# Get QuoteStreamer singleton
&lt;/span&gt;&lt;span class="n"&gt;qs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;QuoteStreamer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Subscribe to AAPL, TSLA
&lt;/span&gt;&lt;span class="n"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AAPL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TSLA&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The QuoteStreamer is the main component of this package. The singleton object ensures that a connection is upheld and manages communications.&lt;/p&gt;

&lt;p&gt;After retrieving the singleton, we subscribe to the tickers of interest (in this example AAPL and TSLA). Note, that we have not yet connected to Yahoo! Finance and thus won't receive any quotes for the time being.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Override on_quote callback
&lt;/span&gt;&lt;span class="n"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;on_quote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Start streaming
&lt;/span&gt;&lt;span class="n"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After defining a simple callback function for printing the quotes we receive, we open a connection to Yahoo! Finance.&lt;/p&gt;

&lt;p&gt;You should now start to receive quotes for the tickers you've previously subscribed to. If this isn't the case, you might be outside of trading hours... &lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up...
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;yflive&lt;/strong&gt; is a very young project and still needs a lot of work. If you're interested in contributing, feel free to open a pull request or issue on GitHub.&lt;/p&gt;

&lt;p&gt;I hope you find this useful and thanks for reading.&lt;/p&gt;

&lt;p&gt;Feedback is appreciated.&lt;/p&gt;

</description>
      <category>python</category>
      <category>opensource</category>
      <category>datascience</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
