diff --git a/eodhd/APIs/InsiderTransactionsV2API.py b/eodhd/APIs/InsiderTransactionsV2API.py new file mode 100644 index 0000000..e0dd521 --- /dev/null +++ b/eodhd/APIs/InsiderTransactionsV2API.py @@ -0,0 +1,39 @@ +# APIs/InsiderTransactionsV2API.py + +from urllib.parse import quote + +from .BaseAPI import BaseAPI + + +class InsiderTransactionsV2API(BaseAPI): + + def get_sec_filings_form4(self, api_token: str, symbol: str, + page_offset: int = None, page_limit: int = None): + """Insider Transactions v2 (SEC Form 4): GET /sec-filings/{symbol}/form4 + + Returns paginated SEC Form 4 filings for a US-listed symbol, parsed directly + from SEC EDGAR. Each filing contains non-derivative (direct stock) and derivative + (options, RSUs, warrants) transactions plus referenced footnotes. + + Parameters: + api_token: EODHD API token + symbol: US ticker symbol (e.g. 'AAPL' or 'AAPL.US') + page_offset: pagination offset (zero-based, default 0) + page_limit: page size (1-100, default 20) + + Cost: 10 API calls per request. + Plans: Fundamentals and All-In-One. + + For more information visit: https://eodhd.com/financial-apis/insider-transactions-api/ + """ + + endpoint = 'sec-filings' + uri = f"{quote(symbol, safe='')}/form4" + + query_string = '' + if page_offset is not None: + query_string += f"&page[offset]={page_offset}" + if page_limit is not None: + query_string += f"&page[limit]={page_limit}" + + return self._rest_get_method(api_key=api_token, endpoint=endpoint, uri=uri, querystring=query_string) diff --git a/eodhd/APIs/__init__.py b/eodhd/APIs/__init__.py index 5ce175c..3bea31b 100644 --- a/eodhd/APIs/__init__.py +++ b/eodhd/APIs/__init__.py @@ -38,6 +38,7 @@ from .SanctionsAPI import SanctionsAPI from .InterestRatesAPI import InterestRatesAPI from .RealEstate import RealEstateAPI +from .InsiderTransactionsV2API import InsiderTransactionsV2API #Marketplace endpoints from .MPIndexComponentsAPI import MPIndexComponentsAPI diff --git a/eodhd/apiclient.py b/eodhd/apiclient.py index de5c79e..728f264 100644 --- a/eodhd/apiclient.py +++ b/eodhd/apiclient.py @@ -1,6 +1,7 @@ #apiclient.py import sys +import warnings from json.decoder import JSONDecodeError from enum import Enum from datetime import datetime @@ -24,6 +25,7 @@ from eodhd.APIs import LiveExtendedQuotesAPI from eodhd.APIs import EconomicEventsDataAPI from eodhd.APIs import InsiderTransactionsAPI +from eodhd.APIs import InsiderTransactionsV2API from eodhd.APIs import FundamentalDataAPI from eodhd.APIs import BulkEodSplitsDividendsDataAPI from eodhd.APIs import UpcomgingEarningsAPI @@ -711,7 +713,9 @@ def get_insider_transactions_data( code: str = None, limit: int = None, ) -> list: - """Available args: + """Obsolete legacy endpoint. Use get_insider_transactions_v2() for SEC Form 4 data. + + Available args: date_from (not required) - date from with format Y-m-d. Example: 2000-01-01 date_to (not required) - date from with format Y-m-d. Example: 2000-01-01 code (not required) - to get the data only for Apple Inc (AAPL), use AAPL.US or AAPL ticker code. @@ -719,6 +723,12 @@ def get_insider_transactions_data( limit (not required) - the limit for entries per result, from 1 to 1000. Default value: 100. For more information visit: https://eodhistoricaldata.com/financial-apis/insider-transactions-api/ """ + warnings.warn( + "get_insider_transactions_data() is obsolete. " + "Use get_insider_transactions_v2(symbol=...) for SEC Form 4 data.", + DeprecationWarning, + stacklevel=2, + ) api_call = InsiderTransactionsAPI(session=self._session, timeout=self._timeout) return api_call.get_insider_transactions_data( @@ -729,6 +739,35 @@ def get_insider_transactions_data( limit=limit, ) + def get_insider_transactions_v2( + self, + symbol: str, + page_offset: int = None, + page_limit: int = None, + ) -> dict: + """Insider Transactions v2 (SEC Form 4): GET /sec-filings/{symbol}/form4 + + Returns paginated SEC Form 4 filings parsed directly from EDGAR. Each filing + contains non-derivative (direct stock) and derivative (options, RSUs, warrants) + transactions plus referenced footnotes. + + Args: + symbol: US ticker symbol (e.g. 'AAPL' or 'AAPL.US') + page_offset: pagination offset (zero-based, default 0) + page_limit: page size (1-100, default 20) + + Cost: 10 API calls per request. Plans: Fundamentals and All-In-One. + + For more information visit: https://eodhd.com/financial-apis/insider-transactions-api/ + """ + api_call = InsiderTransactionsV2API(session=self._session, timeout=self._timeout) + return api_call.get_sec_filings_form4( + api_token=self._api_key, + symbol=symbol, + page_offset=page_offset, + page_limit=page_limit, + ) + def get_fundamentals_data(self, ticker: str, filter: str = None, historical: int = None, from_date: str = None, to_date: str = None, version: int = None, no_cache: int = None) -> Any: diff --git a/tests/test_insider_transactions_v2.py b/tests/test_insider_transactions_v2.py new file mode 100644 index 0000000..08daaff --- /dev/null +++ b/tests/test_insider_transactions_v2.py @@ -0,0 +1,111 @@ +"""Tests for InsiderTransactionsV2API — Insider Transactions v2 (SEC Form 4).""" + +import pytest +from unittest.mock import MagicMock + +from eodhd.APIs.InsiderTransactionsV2API import InsiderTransactionsV2API + + +@pytest.fixture +def mock_session(): + return MagicMock() + + +def _make_api(session): + return InsiderTransactionsV2API(session=session) + + +def _mock_response(session, data=None): + resp = MagicMock() + resp.status_code = 200 + resp.json.return_value = data or {"data": [], "meta": {"total": 0, "page": {"offset": 0, "limit": 20}}, "links": {"next": None}} + session.get.return_value = resp + + +# --- URL construction --- + +def test_form4_constructs_correct_url(mock_session): + _mock_response(mock_session) + api = _make_api(mock_session) + api.get_sec_filings_form4(api_token="test1234567890123456", symbol="AAPL") + + call_url = mock_session.get.call_args[0][0] + assert "/sec-filings/AAPL/form4" in call_url + assert "api_token=test1234567890123456" in call_url + + +def test_form4_dotted_symbol(mock_session): + _mock_response(mock_session) + api = _make_api(mock_session) + api.get_sec_filings_form4(api_token="x", symbol="AAPL.US") + + call_url = mock_session.get.call_args[0][0] + assert "/sec-filings/AAPL.US/form4" in call_url + + +def test_form4_pagination_params(mock_session): + _mock_response(mock_session) + api = _make_api(mock_session) + api.get_sec_filings_form4(api_token="x", symbol="AAPL", page_offset=20, page_limit=50) + + call_url = mock_session.get.call_args[0][0] + assert "page[offset]=20" in call_url + assert "page[limit]=50" in call_url + + +def test_form4_omits_pagination_when_none(mock_session): + _mock_response(mock_session) + api = _make_api(mock_session) + api.get_sec_filings_form4(api_token="x", symbol="AAPL") + + call_url = mock_session.get.call_args[0][0] + assert "page[offset]" not in call_url + assert "page[limit]" not in call_url + + +# --- Response handling --- + +def test_form4_returns_dict(mock_session): + payload = { + "data": [ + { + "accession_number": "0000320193-23-000077", + "filed_at": "2023-08-04", + "period_of_report": "2023-08-02", + "non_derivative": [], + "derivative": [], + "footnotes": [], + } + ], + "meta": {"total": 594, "page": {"offset": 0, "limit": 20}}, + "links": {"next": "https://eodhd.com/api/sec-filings/AAPL/form4?page[offset]=20&page[limit]=20"}, + } + _mock_response(mock_session, data=payload) + api = _make_api(mock_session) + result = api.get_sec_filings_form4(api_token="x", symbol="AAPL") + + assert isinstance(result, dict) + assert "data" in result + assert "meta" in result + assert "links" in result + assert result["meta"]["total"] == 594 + + +def test_form4_symbol_is_path_encoded(mock_session): + _mock_response(mock_session) + api = _make_api(mock_session) + api.get_sec_filings_form4(api_token="x", symbol="A/B?C") + + call_url = mock_session.get.call_args[0][0] + assert "/sec-filings/A%2FB%3FC/form4?" in call_url + + +def test_legacy_insider_transactions_warns_deprecation(): + from eodhd import APIClient + + client = APIClient("x" * 20) + client._session = MagicMock() + client._session.get.return_value.json.return_value = [] + client._session.get.return_value.status_code = 200 + with pytest.warns(DeprecationWarning): + client.get_insider_transactions_data(code="AAPL.US")