A Python library for fetching financial data from FT Markets
Project description
FTMarkets
A Python library for fetching financial data from Financial Times Markets, including historical stock prices, ETF holdings, fund profiles, and allocation breakdowns.
Features
- Historical Data: Fetch historical OHLCV data for stocks and ETFs
- Holdings Data: Get ETF/fund holdings, asset allocation, and sector breakdowns
- Fund Profiles: Access fund information, statistics, and investment details
- Symbol Search: Find FT Markets XIDs by ticker symbols
- Concurrent Processing: Fast data retrieval using multithreading
- Pandas Integration: Returns data as pandas DataFrames for easy analysis
Installation
pip install ftgo
Quick Start
from ftgo import search_securities, get_xid, get_historical_prices, get_holdings
# Search for a security
results = search_securities('AAPL')
print(results)
# Get XID for a ticker
xid = get_xid('AAPL')
# Fetch historical data
df = get_historical_prices(xid, "01012024", "31012024")
print(df.head())
# Get ETF holdings
spy_xid = get_xid('SPY')
holdings = get_holdings(spy_xid, "top_holdings")
print(holdings)
API Reference
Search Functions
search_securities(query)
Search for securities on FT Markets.
Parameters:
query(str): Search term for securities (ticker symbol or company name)
Returns: pandas.DataFrame with search results containing xid, name, symbol, asset_class, url
# Search for Apple
results = search_securities('Apple')
print(results)
# Search by ticker
results = search_securities('AAPL')
get_xid(ticker, display_mode="first")
Get FT Markets XID for given ticker symbol.
Parameters:
ticker(str): Ticker symboldisplay_mode(str): "first" to return first match XID, "all" to return all matches
Returns: String XID (if display_mode="first") or DataFrame (if display_mode="all")
# Get XID for Apple
xid = get_xid('AAPL')
print(xid) # Returns XID string
# Get all matches
all_results = get_xid('AAPL', display_mode='all')
print(all_results)
Historical Data
get_historical_prices(xid, date_from, date_to)
Get historical price data for a security with full OHLCV data.
Parameters:
xid(str): The FT Markets XIDdate_from(str): Start date in DDMMYYYY format (e.g., "01012024")date_to(str): End date in DDMMYYYY format (e.g., "31122024")
Returns: pandas.DataFrame with columns: date, open, high, low, close, volume
xid = get_xid('AAPL')
df = get_historical_prices(xid, "01012024", "31012024")
print(df.head())
get_multiple_historical_prices(xids, date_from, date_to)
Get historical data for multiple securities concurrently.
Parameters:
xids(list): List of FT Markets XIDsdate_from(str): Start date in DDMMYYYY formatdate_to(str): End date in DDMMYYYY format
Returns: pandas.DataFrame with concatenated data for all securities
xids = [get_xid('AAPL'), get_xid('MSFT')]
df = get_multiple_historical_prices(xids, "01012024", "31012024")
Holdings Data
get_holdings(xid, holdings_type="all")
Get holdings and allocation data for ETFs and funds.
Parameters:
xid(str): The FT Markets XIDholdings_type(str): Type of holdings data:"asset_allocation": Asset class breakdown (stocks, bonds, cash)"sector_weights": Sector allocation"geographic_allocation": Geographic allocation"top_holdings": Top holdings by weight"all": All holdings data types as a tuple
Returns: pandas.DataFrame or tuple of DataFrames
# Get top holdings for SPY ETF
spy_xid = get_xid('SPY')
top_holdings = get_holdings(spy_xid, "top_holdings")
# Get asset allocation
allocation = get_holdings(spy_xid, "asset_allocation")
# Get all holdings data
all_data = get_holdings(spy_xid, "all")
asset_alloc, sectors, regions, holdings = all_data
get_fund_breakdown(xid)
Get complete fund breakdown with all allocation data.
Parameters:
xid(str): The FT Markets XID
Returns: Dictionary with all DataFrames
qqq_xid = get_xid('QQQ')
breakdown = get_fund_breakdown(qqq_xid)
print(breakdown['asset_allocation'])
print(breakdown['top_holdings'])
Fund Profile Data
get_fund_profile(xid)
Get profile and investment information for ETFs and funds.
Parameters:
xid(str): The FT Markets XID
Returns: pandas.DataFrame with Field and Value columns
xid = get_xid('SPY')
profile = get_fund_profile(xid)
print(profile)
# Filter for specific information
fees = profile[profile['Field'].str.contains('fee', case=False)]
get_fund_stats(xid)
Get fund profile data as a dictionary for easy access.
Parameters:
xid(str): The FT Markets XID
Returns: Dictionary with all available fund fields and values
xid = get_xid('QQQ')
stats = get_fund_stats(xid)
# Access any available field safely
inception = stats.get('Inception date', 'Not available')
fees = stats.get('Ongoing charge', 'Not available')
get_available_fields(xid)
Get list of all available profile fields for a fund.
Parameters:
xid(str): The FT Markets XID
Returns: List of all field names available
xid = get_xid('SPY')
fields = get_available_fields(xid)
print("Available fields:")
for field in fields:
print(f" - {field}")
search_profile_field(xid, search_term)
Search for specific fields in the fund profile data.
Parameters:
xid(str): The FT Markets XIDsearch_term(str): Term to search for in field names (case-insensitive)
Returns: pandas.DataFrame with matching fields and values
xid = get_xid('SPY')
fees = search_profile_field(xid, 'fee')
inception = search_profile_field(xid, 'inception')
Complete Example
from ftgo import get_xid, get_historical_prices, get_holdings, get_fund_profile
import matplotlib.pyplot as plt
# Search for QQQ ETF
qqq_xid = get_xid('QQQ')
# Get 1 year of historical data
historical_data = get_historical_prices(qqq_xid, "01012023", "31122023")
# Get fund information
profile = get_fund_profile(qqq_xid)
top_holdings = get_holdings(qqq_xid, "top_holdings")
asset_allocation = get_holdings(qqq_xid, "asset_allocation")
# Plot price chart
historical_data.set_index('date')['close'].plot(title='QQQ Price History')
plt.show()
# Display fund information
print("Fund Profile:")
print(profile.head(10))
print("\nTop 10 Holdings:")
print(top_holdings.head(10))
print("\nAsset Allocation:")
print(asset_allocation)
Error Handling
The library includes logging and error handling, but you should wrap calls in try-except blocks for production use:
try:
xid = get_xid('INVALID_TICKER')
data = get_historical_prices(xid, "01012024", "31012024")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
Requirements
- Python 3.7+
- cloudscraper >= 1.2.68
- pandas >= 1.3.0
- beautifulsoup4 >= 4.11.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Disclaimer
This library is for educational and research purposes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ftgo-1.2.0.tar.gz.
File metadata
- Download URL: ftgo-1.2.0.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc437084219ce78bd2ce9eb70421f016e554f71bc422e7566b4a8b90642378f9
|
|
| MD5 |
9c33a7cdecb52f6148fcdb156428231b
|
|
| BLAKE2b-256 |
29201b17c9ffb030f1d3745e67909bedcb339de822a959f94de55aa7c582c995
|
File details
Details for the file ftgo-1.2.0-py3-none-any.whl.
File metadata
- Download URL: ftgo-1.2.0-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94b9e9dbb38b5a4c0ad3172e887adc9835fbf2c1a1ace165b90ab7a452877f7a
|
|
| MD5 |
fb7219db7bf30038d1ab6157ae93e0dc
|
|
| BLAKE2b-256 |
fe7fc5f3f6d0470179ca59fa2b052fc929009de8f1215b9318091d4448ef5409
|