Skip to content
/ marketstack-api Public template

Use Python to interact with the marketstack.com API

Notifications You must be signed in to change notification settings

wsbinette/marketstack-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Market Stack API - Python

This is a Python Client for the Market Stack API. This package will allow you to simply import the python package to use the api rather than having to write the requests yourself.

Dependencies

For this package you'll need to make sure you have the following Dependencies installed:

  • Requests
    • For running the API requests to the Market Stack API
  • Pandas
    • For dataframe handling
  • Python-dotenv
    • For loading in environment variables from your .env file

Environment

I used the Anaconda environment for development of this package, but you can use any other environment you'd like. If you want more information about Anaconda it's great for data science. You can check out their docs and installation instructions here: Anaconda

Installation

To use this package simply clone the repo and install the requirements. If you want to expand upon this project, feel free to fork the repo and open a PR!

git clone https://github.com/wsbinette/marketstack-api.git

Requirements Installation

For your convenience, I've included a configuration file, requirements.txt, that will install all the requirements for you. Simply run the following command to install the requirements:

pip install -r requirements.txt

API Key

After you've made cloned the repo and installed the requirements, you need to get your own Personal API key for the Market Stack API. You can do this by going to the Market Stack API and creating an account. Once you have your key, you can set your key by adding it to your environment variables using a .env file or by setting it with the built in method. I recommend using a .env file.

Recommended Method Set your api key by adding a .env file at the root of your directory and setting the MARKETSTACK_API_KEY variable to your personal key. Check out the included .env-example for reference.

# .env 
MARKETSTACK_API_KEY=YOUR_API_KEY

To set your api_key using the built in method if you're using something like Jupyter notebooks, use the code below.

import marketstack

marketstack.set_api_key('YOUR_API_KEY')

How to Use the API

Once you've installed the requirements, cloned the git repo, and gotten your own API key, you can simply import the package at the top of your python project and use it like you would any other package.

import marketstack

You can refer to the full documentation for the API here: Market Stack API. Below I'll outline some of the basic usage of the API.

API Features

Check the Market Stack API documentation for a full list of features. All the parameters listed there will be present in the client.

Classes as Endpoints

Each endpoint of the MarketStack API can is built into a class. Each endpoint can take the defined params in the docs as class parameters and any defined features for an endpoint can be called as a chainable function to the class instance.

Supported Endpoints

import marketstack

marketstack.EndOfDay(
    symbols: str, 
    exchange: Optional[str] = None, 
    sort: Optional[str] = None, 
    date_from: Optional[str] = None, 
    date_to: Optional[str] = None, 
    limit: Optional[str] = None, 
    offset: Optional[str] = None
)

#features as chainable functions
marketstack.EndOfDay(...params).latest()
marketstack.EndOfDay(...params).historical(
    date_from: Union[datetime, str],
    date_to: Optional[Union[datetime, str]]    
)


# Intraday 
marketstack.Intraday(
    symbols: str, 
    exchange: Optional[str] = None, 
    sort: Optional[str] = None, 
    date_from: Optional[str] = None, 
    date_to: Optional[str] = None, 
    limit: Optional[str] = None, 
    offset: Optional[str] = None
)

#Chainable features
marketstack.Intraday(...params).latest()
marketstack.Intraday(...params).historical(
    date_from: Union[datetime, str],
    date_to: Optional[Union[datetime, str]]    
)
 
#Splits
marketstack.Splits(
    symbols: str, 
    sort: Optional[str] = None, 
    date_from: Optional[str] = None, 
    date_to: Optional[str] = None, 
    limit: Optional[str] = None, 
    offset: Optional[str] = None
)

#Dividends
marketstack.Dividends(
    symbols: str, 
    sort: Optional[str] = None, 
    date_from: Optional[str] = None, 
    date_to: Optional[str] = None, 
    limit: Optional[str] = None, 
    offset: Optional[str] = None
)

#Tickers
marketstack.Tickers(
    get_symbol: Optional[str] = None, 
    exchange: Optional[str] = None, 
    search: Optional[str] = None, 
    limit: Optional[int] = None, 
    offset: Optional[int] = None, 
    get_splits: bool = False, 
    get_dividends: bool = False, 
    get_eod: bool = False, 
    get_intraday: bool = False, 
    date: Optional[str] = None, 
    get_latest: bool = False
)

marketstack.Tickers(...params)
marketstack.Tickers(...params).symbol(symbol: str)
marketstack.Tickers(...params).symbol(symbol: str).splits()
marketstack.Tickers(...params).symbol(symbol: str).dividends()
marketstack.Tickers(...params).symbol(symbol: str).eod()
marketstack.Tickers(...params).symbol(symbol: str).eod().latest()
marketstack.Tickers(...params).symbol(symbol: str).eod().hisorical(date: Union[datetime, str])
marketstack.Tickers(...params).symbol(symbol: str).intraday()
marketstack.Tickers(...params).symbol(symbol: str).intraday().latest()
marketstack.Tickers(...params).symbol(symbol: str).intraday().hisorical(date: Union[datetime, str])

#Exchanges
marketstack.Exchanges(
    search: Optional[str] = None, 
    limit: Optional[int] = None, 
    offset: Optional[int] = None, 
    mic: Optional[str] = None, 
    get_tickers: Optional[bool] = False, 
    get_eod: Optional[bool] = False, 
    get_intraday: Optional[bool] = False, 
    date: Optional[str] = None, 
    get_latest: Optional[bool] = False
)
marketstack.Exchanges(...params).exchange(mic: str)
marketstack.Exchanges(...params).exchange(mic: str).tickers()
marketstack.Exchanges(...params).exchange(mic: str).eod()
marketstack.Exchanges(...params).exchange(mic: str).eod().latest()
marketstack.Exchanges(...params).exchange(mic: str).eod().hisorical(date: Union[datetime, str])
marketstack.Exchanges(...params).exchange(mic: str).intraday()
marketstack.Exchanges(...params).exchange(mic: str).intraday().latest()
marketstack.Exchanges(...params).exchange(mic: str).intraday().hisorical(date: Union[datetime, str])


#Currencies
marketstack.Currencies(
    limit: Optional[int] = None, 
    offset: Optional[int] = None
)

#Time Zones
marketstack.TimeZones(
    limit: Optional[int] = None, 
    offset: Optional[int] = None
)

API Response Formats

For the raw API response:

marketstack.ENDPOINT.request()

For the HTTP Response code:

marketstack.ENDPOINT.get_http_response_code()

For the JSON API response:

use marketstack.ENDPOINT.get_api_response()

For the API response data:

marketstack.ENDPOINT.get_data()

For a Pandas DataFrame:

marketstack.ENDPOINT.get_data_df()

Example Usage:

import marketstack

# Ensure your API key is set in your .env 
# Using a class instance
apple_eod = marketstack.EndOfDay('AAPL', sort = 'asc').get_data_df()

# Using a class instance with chained features
apple_eod_latest = marketstack.EndOfDay('AAPL').latest().get_data_df()

The calls above return pandas dataframes with the end of day data for Apple.

#apple_eod
	open	high	low	close	volume	adj_high	adj_low	adj_close	adj_open	adj_volume	split_factor	dividend	symbol	exchange	date
0	186.83	188.05	185.23	185.27	48088700.0	187.316953	184.507946	184.5412	186.101709	48088661.0	1.0	0.00	AAPL	XNAS	2023-06-26T00:00:00+0000
1	185.89	188.39	185.67	188.06	50730800.0	187.655628	184.946231	187.3203	185.165373	50730846.0	1.0	0.00	AAPL	XNAS	2023-06-27T00:00:00+0000
2	187.93	189.90	187.60	189.25	51216800.0	189.159742	186.868707	188.5056	187.197421	51216801.0	1.0	0.00	AAPL	XNAS	2023-06-28T00:00:00+0000
3	189.08	190.07	188.94	189.59	46347300.0	189.329079	188.203484	188.8442	188.342938	46347308.0	1.0	0.00	AAPL	XNAS	2023-06-29T00:00:00+0000
4	191.63	194.48	191.26	193.97	85069600.0	193.721888	190.514440	193.2070	190.882998	85213216.0	1.0	0.00	AAPL	XNAS	2023-06-30T00:00:00+0000
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
95	182.35	183.45	181.59	182.89	49340300.0	182.981560	181.126309	182.4166	181.884369	49340282.0	1.0	0.00	AAPL	XNAS	2023-11-08T00:00:00+0000
96	182.96	184.12	181.81	182.41	53763500.0	183.649849	181.345748	181.9378	182.492811	53763540.0	1.0	0.00	AAPL	XNAS	2023-11-09T00:00:00+0000
97	183.97	186.57	183.53	186.40	66133400.0	186.328205	183.297057	186.1624	183.736498	66177922.0	1.0	0.24	AAPL	XNAS	2023-11-10T00:00:00+0000
98	185.82	186.03	184.21	184.80	43627500.0	185.793884	183.976194	184.5645	185.584150	43627519.0	1.0	0.00	AAPL	XNAS	2023-11-13T00:00:00+0000
99	187.70	188.11	186.30	187.44	60108400.0	187.871244	186.063541	187.2011	187.461764	60108378.0	1.0	0.00	AAPL	XNAS	2023-11-14T00:00:00+0000
100 rows × 15 columns

#apple_eod_latest
	open	high	low	close	volume	adj_high	adj_low	adj_close	adj_open	adj_volume	split_factor	dividend	symbol	exchange	date
0	186.83	188.05	185.23	185.27	48088700.0	187.316953	184.507946	184.5412	186.101709	48088661.0	1.0	0.00	AAPL	XNAS	2023-06-26T00:00:00+0000

About

Use Python to interact with the marketstack.com API

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages