Incredible and useful tools to become a better Financial Data Scientist

Alan Figueroa
MCD-UNISON
Published in
6 min readApr 4, 2021

--

Nowdays, Finance and Data Science are intimately related. I’m studying both topics in graduate school, because I love them and I’m very curious about practical applications of maths and both of these subjects are math-related in many ways. Through my postgraduate studies I noticed that Data Science and Finance are like high school sweethearts, each day they become more co-dependent. By writting this article, I want to share some tools that were useful and exciting for me troughout my studies and projects; like Data APIs and Financial Analysis libraries in Python.

Data APIs

Yahoo Finance (Yfinance & Yahoo-Fin)

Library link: https://pypi.org/project/yfinance/

Library link: https://pypi.org/project/yahoo-fin/

This library offer a practical way to obtain data from stock market, like historical prices data and information of Financial Statements such as Balance Sheet, Earnings and Cashflows. The best advantage of this API is that it’s totally FREE!. Another advantage I’ve found when using this library is that you can get the data easily in a fast way and organized in a dataframe, so is easy to work and process data. But, one disadvantage is that maybe you can’t find specific information of low cap tickers, funds or bonds. Also, it doesn’t have technical indicators, but it can be solved applying other libraries (like finta, I’ll share it later) and tools to have them.

Examples:

Obtaining Historical Prices from Tickers

Getting specific data from history and make a dict with a loop

Know about options contracts of a ticker (Put & Calls Contracts)

Financial Statements with Yahoo-Fin

Financial Modeling Prep and pyfmpcloud

Link Data API: https://financialmodelingprep.com/developer/docs/

Link Library: https://pypi.org/project/pyfmpcloud/

This Data API and library is my favorite of all that I have used. Is AMAIZING!! The data is well-organized, by topic and with complete and full information of all financial markets in the world. Also, it has info of Cryptos, Indexes, Commodities, Stock Markets, Funds, Forex, Calendars, Press Release about companies and markets, recommendations, Ratings, Technical Indicators and a complete section of Company Valuation. Depending on what data you need, it can be for different periods: intraday, daily, montly, quarterly and annual. The only detail is that there’s a cost for an API Key(montly or annual). But for the Data APIs I’ve found, Financial Modeling Prep has very competitive price for all the information that offers. The only tiny disadvantage it has is that not all the info is in the library, but you can handle it with requests and json formats.

Setting FMP

Examples

Getting tickers of interest with requests

Filtering and getting historical data for 45 stocks with pyfmpcloud and a loop

Getting technical indicators of json file for a list of 20 stocks and concatenating in a DataFrame: ex. SMA 10 periods

Financial Statment for a stock with pyfmpcloud

Financial Analysis Libraries

PyPortfolioOpt

Link of User Guide: https://pyportfolioopt.readthedocs.io/en/latest/

Robert Martin GitHub: https://github.com/robertmartin8/PyPortfolioOpt

PyPortfolioOpt is a Library that you can use to create Diversified Portfolios. All you need is Historical Asset Price Data or Propietary Model. With these inputs you can get easily Expected Returns of assets or/and portfolios, develop Risk Models and use optimisers like Efficient Frontier, Black-Litterman Model and Hierachical Risk Parity.

Covariance Matrix

Comparison of differents Covariances Matrix

Exponential Covariance Matrix seems to be more efficient than other, with a mean absolute error of 2%

Efficient Frontier: Max Sharpe with sector constraints

by Robert Martin

If you have your own model for returns (or have read the warnings and want to proceed anyways), you may consider maximising the Sharpe ratio. This theoretically gives the optimal portfolio in terms of risks-returns.

In this section, we construct a long-only max-sharpe portfolio, but also incorporate sector constraints. Sector constraints require three things. A sector_mapper, your sector_lower bounds, and your sector_upper bounds.

We then set up the optimizer and add our constraints. We can use ef.add_objective() to add other constraints. For example, let's say that in addition to the above sector constraints, I specifically want:

  • 10% of the portfolio in AMZN
  • Less than 5% of my portfolio in TSLA

We can immediately see that our explicit constraints were satisfied, and can check all the sector constraints as follows:

Complex Plots

by: Robert Martin

The plotting module allows you to pass in an ax, on top of which the plots are added. This allows you to build complex plots.

In this example, we will plot the efficient frontier as well as 10,000 simulated portfolios.

To generate the simulated portfolios, we will sample random weights from the Dirichlet distribution (these are already normalised):

Sample portfolio returns: [0.21286206 0.21820475 0.22013716 ... 0.21642456 0.23530824 0.24139073]
Sample portfolio volatilities: [0.14446284 0.16609061 0.14438936 ... 0.14572046 0.16494929 0.13686405]

Note that the above code is equivalent to generating samples via a for loop (just more efficient):

Continuing with the plot code:

Black & Litterman Returns and Expected Returns

Finta

Link: https://pypi.org/project/finta/

Finta is a project in continues improvements, with the lastest version Released on Apr 3, 2021. It offers multiples Technical Indicators.

Example of a BBand Plot with Finta

Hope you find these tools interesting and useful for a Financial Data Scientist Career or for personal usage on portfolios or assets investments.

Thank you for reading and your interest!!

--

--

Alan Figueroa
MCD-UNISON

BI Engineering & Developer, Data Financial Scientist. Msc. in Finance. Msc. in Data Science. Mentor for Growth and Development.