Site icon R-bloggers

Consuming yfinance data in Excel – Part II

[This article was first published on Adam’s Software Lab, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

The first part of this two-part blog described the YFinanceService. This is a Flask service that wrapped calls to the Python yfinance library and which served up some of the data via the APIs (/IntrinsicValue, /TickerData and so on). Part II of the blog describes how to make use of the APIs in an Excel spreadsheet, specifically using the Power Query Web Connector to retrieve the data.

Consuming the APIs from Excel

One of the main objectives of this project was to be able to obtain the data from yfinance via the YFinanceService and use it in Excel. For this, we use the Stock Valuation Spreadsheet.

Calculate Intrinsic Value

The spreadsheet uses the Power Query Web Connector. However, rather than using the Web Connector directly to call our YFinanceService APIs, we use Power Query to create blank queries and write the Power Query M language code to obtain the parameters from the worksheets and to process the results into a table.

The spreadsheet consists of the following worksheets:

Ticker Data Request

After opening the spreadsheet, select the Setup worksheet and from the Data menu select Queries and Connections. This will list the queries that are used in this sheet. The three main queries are:

KeyIndicators Query

The TickerData worksheet sets up two tables: TableSymbols and the TableFields. These are used as the inputs to the KeyIndicators query. The Power Query M code builds up the API query string from these inputs and processes the result to produce a table. The query can be refreshed using the refresh symbol on the right-hand side of the Queries & Connections panel.

Building a KeyIndicators Table

IntrinsicValues Query

The IntrinsicValues worksheet sets up a table called TickerList. Additionally the query obtains the values for the average yield (avg_yield) and the current yield (cur_yield) from the parameters table defined in the Setup worksheet. With these values it constructs the API query string and calls the appropriate YFinanceService. If the query succeeds, the resulting string is processed into a table with two columns: Ticker and Intrinsic Value.

CalcIntrinsicValue Query

The CalcIntrinsicValue query depends on the fnCalcIntrinsicValue query/function. This in turn depends on a query (IntrinsicValue) which has had a parameter (Ticker) added to it. The parameter is added using the Manage Parameters menu on the Home menu strip. Once the parameter is added we can convert the original query to a function – fnCalcIntrinsicValue. The function can be invoked either by double clicking it or by entering a parameter, the ticker symbol in this case, and pressing “Invoke”. However, we will not use this method of calling the function.

Instead, we create a further query CalcIntrinsicValue. This creates a new table. The table uses the tickers defined in the TableTickers (input) table and adds a new custom column. The custom column invokes the intrinsic value function for each ticker value.

	// Add custom IntrinsicValue column based on the function fnCalcIntrinsicValue parameterised with each of the Tickers
    InvokedCustomFunction = Table.AddColumn(TickerSource, "IntrinsicValue", each fnCalcIntrinsicValue([Tickers])),

The function fnCalcIntrinsicValue returns a key-value pair { value: nnnn }, so we expand the result and use only the second component:

    ExpandedIntrinsicValue = Table.ExpandRecordColumn(InvokedCustomFunction, "IntrinsicValue", {"value"}, {"IntrinsicValue.value"}),

The resulting table is processed to display on the Intrinsic Value column (which is what appears in the worksheet).

A word about performance

Overall the performance of the queries is slow. Assuming the YFinanceService is already running, and the Excel spreadsheet is open. Then, pressing the refresh button on any of the queries needs to 1) construct a query string, 2) call the API via Web.Contents, and 3) process the results. The call via Web.Contents calls a Flask API which wraps Python code, and in this case calls via yfinance and yahoo_fin. Both these use web scraping and this is what determines the slow performance.

Wrap Up

In Part I we described the YFinanceService and the API calls to the Python yfinance library. Part II described how to make use of the APIs in an Excel spreadsheet, specifically using the Power Query Web Connector to retrieve the data.

Overall the arrangement that we have is:

Advantages and Disadvantages

There are advantages and disadvantages to this approach. The main advantages are:

To leave a comment for the author, please follow the link and comment on their blog: Adam’s Software Lab.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version