Real market data. That's what you need before you run off on your horses to learn about technical analysis. Constructing models and verifying what you've learnt against real market data makes for a realistic learning experience. I generally use Python for making API calls - while you are free to use any scripting language, I strongly recommend setting up a framework to make API calls so that you have access to live and updated datasets on a wide range of financial assets. And for the aspiring beginner? I recommend Yahoo Finance.
For beginners, Yahoo Finance suffices as a data playground, which provides sufficiently long historical prices of both traditional assets (e.g. equities) and cryptocurrencies. For the beginner who doesn't wish to invest too much upfront, it's free, and no registration is required. Most importantly, there are ready Python libraries out there that allow for data calls via Yahoo Finance's APIs.
We'll be working with three python libraries: "pandas", "yfinance" and "plotly". If you haven't installed them yet, go to your terminal and install them via the following pip commands:
pip install plotly yfinance pandasOnce installed, you'll be able to load the three libraries in the header portion of your Python code:
## Loading Libraries
import pandas as pd
import yfinance as yf from yahoofinancials
import YahooFinancials
With the libraries loaded, you now have access to the stock and crypto prices available on Yahoo Finance. For example, suppose you wanted the stock prices of Tesla Inc, which has the ticker TSLA:
## Create a TSLA ticker object
tsla_ticker=yf.Ticker('TSLA')
## Download TSLA historical prices into a df
tsla_df=tsla_ticker.history(period="ytd")
## Displaying first 5 rows of df, prices to 2 dp
tsla_df.head().round(2)
Output: Open High Low Close Adj Close Volume Date 2022-01-03 1147.75 1201.07 1136.04 1199.78 1199.78 34643800 2022-01-04 1189.55 1208.00 1123.05 1149.59 1149.59 33416100 2022-01-05 1146.65 1170.34 1081.01 1088.12 1088.12 26706600 2022-01-06 1077.00 1088.00 1020.50 1064.70 1064.70 30112200 2022-01-07 1080.37 1080.93 1010.00 1026.96 1026.96 28054900
Many prefer this way of accessing stock data, because it's more "Pythonic". Essentially, you create a ticker object with methods embedded within that you can call on to access various data beyond stock prices. Let's regenerate the same dataframe above using this method:
## Create a TSLA ticker object
tsla_ticker=yf.Ticker('TSLA')
## Download TSLA historical prices into a df
tsla_df=tsla_ticker.history(period="ytd")
## Displaying first 5 rows of df, prices to 2 dp
tsla_df.head().round(2)
Output: Open High Low Close Adj Close Volume Date 2022-01-03 1147.75 1201.07 1136.04 1199.78 1199.78 34643800 2022-01-04 1189.55 1208.00 1123.05 1149.59 1149.59 33416100 2022-01-05 1146.65 1170.34 1081.01 1088.12 1088.12 26706600 2022-01-06 1077.00 1088.00 1020.50 1064.70 1064.70 30112200 2022-01-07 1080.37 1080.93 1010.00 1026.96 1026.96 28054900
With this ticker object, you can then obtain various useful company-level data, such as its corporate actions (e.g. dividends, splits), quarterly financials, and even its annual earnings. Let's try to generate one such dataset called "recommendations, which is a historical list of recommendations made by different analysts regarding the stock and whether to buy it or not:
## Retrieve TSLA stock recommendations
tsla_ticker.recommendations
Output: Firm To Grade From Grade Action Date 2012-02-16 07:42:00 JP Morgan Overweight main 2012-02-16 13:53:00 Wunderlich Hold down 2012-02-17 06:17:00 Oxen Group Buy init 2012-03-26 07:31:00 Wunderlich Buy up 2012-05-22 05:57:00 Maxim Group Buy init ... ... ... ... ... 2022-06-01 12:08:20 Goldman Sachs Buy main 2022-06-03 11:22:15 Cowen & Co. Market Perform main 2022-06-09 08:58:31 UBS Buy Neutral up 2022-06-10 10:30:27 Barclays Underweight main 2022-06-13 09:12:55 RBC Capital Outperform Sector Perform up
For such a beginner-friendly tool, the library has its drawbacks. For one thing, some of its methods are extremely fragile. For most of its methods, yfinance works by making API calls to Yahoo Finance to access data, but some methods work by unofficially scraping the Yaho Finance webpages to consolidate data. If Yahoo changes its web structure, these methods will fail.
This library lacks specialised features as well. For example, it doesn't allow access to any of the news reports or analyses that are available on Yahoo Finance. You would have to write your own web scraper to complete yfinance. You also have to generate all of your technical analysis indicators from scratch, which may not be optimum for web developers who want ready functions to code into their web interface or trading algorithm.
In future posts, we will delve into making actual API calls with Python. This will vastly increase the quality and repertoire of data you have access to. The novice may wish to consider adding API calls to his arsenal after mastering some of the basics of technical analysis.
Now, how shall we visualise these data?
Find more Finalytics stories on my blog. Have a suggestion? Contact me at [email protected].