DEV Community

Cover image for Free Market Data for Python using Twelve Data API
midasSSS
midasSSS

Posted on

Free Market Data for Python using Twelve Data API

What is Twelve Data API?

Twelve Data is a company that provides high-quality financial market data. The main features are the variety of data (stocks, forex, crypto, indices, etfs), a large number of historical entries, and support of over a hundred technical indicators.

Nevertheless, Twelve Data allows us to begin using data at no cost without time constraints.

Getting Started

The Twelve Data API provides the following features:

  • OHLCV values in real-time and historical retrospective.
  • Company data, such as earnings.
  • Stream data into your app through WebSocket

To begin install the Python library

pip install twelvedata

You will also need an API key that you can obtain in seconds here.

Historical and Real-Time quotes

To gather any kind of data you have to call the TDClient module. Below is a simple example to retrieve GOOG stock OHLCV values at the weekly interval:

from twelvedata import TDClient

td = TDClient(my_api_key)
ts = td.time_series(symbol="GOOG", interval="1week").as_pandas()

This will output something like this:

GOOG Pandas Dataframe

By the way, there are a lot of parameters that might be passed to time_series, which makes it possible to retrieve any kind of data at any intervals and dates.

Plot Charts

There are two types of charting available. The first one is plotting the static charts using matplotlib module. However, my fav is the dynamic chart based on plotly library that allows the build complex charts even with technical indicators.

ts = td.time_series(symbol="GOOG", interval="1week")
ts.with_aroon().with_stoch().with_ema(time_period=24).as_plotly_figure().show()

Interactive GOOG chart

Export data

Apart from pandas.Dataframe you may also export data in json and csv formats.

ts = td.time_series(symbol="GOOG", interval="1week")
ts.as_json()
ts.as_csv()

Fundamentals

Similar logic applies when you want to request company fundamentals.

from twelvedata import TDClient

td = TDClient(my_api_key)
earnings = td.get_earnings(symbol="GOOG", outputsize=30)
earnings.as_pandas()

#             eps_estimate  eps_actual  difference  surprise_prc
# date                                                          
# 2020-07-30          8.34       10.13        1.79         21.46
# 2020-04-28         10.38        9.87       -0.51         -4.91
# 2020-02-03         12.59       15.35        2.76         21.92
# 2019-10-28         12.46       10.12       -2.34        -18.78
# 2019-07-25         11.33       14.21        2.88         25.42
# 2019-04-29         10.63        9.50       -1.13        -10.63
# 2019-02-04         10.82       12.77        1.95         18.02
# 2018-10-25         10.40       13.06        2.66         25.58
# 2018-07-23          9.54        4.54       -5.00        -52.41
# 2018-04-23          9.32       13.33        4.01         43.03
# 2018-02-01          9.96        9.70       -0.26         -2.61
# 2017-10-26          8.33        9.57        1.24         14.89
# 2017-07-24          4.47        5.01        0.54         12.08
# 2017-04-27          7.39        7.73        0.34          4.60

Wrapping up

At this post, we made a brief overview of some features that the Twelve Data Python library provides. In the next article, we will cover the topic of streaming market data with WebSockets.
Meanwhile, check out the GitHub repo.

Stay safe!

Top comments (8)

Collapse
 
mnvx profile image
Nick Mathias

Wow, 100+ technical indicators is great. How many stocks in API?

Collapse
 
midassss profile image
midasSSS

About 55k different symbols

Collapse
 
tradedude profile image
Trade Dude

Good job! Gonna give it a try.

Collapse
 
aib_kz profile image
Aib Kz

Twelve data is the best! I see that creator of this company put big effort to project and really he is professional of his work.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.