Last active
August 26, 2019 23:25
-
-
Save janlukasschroeder/01016cbec98349f810d13a18be6f22ae to your computer and use it in GitHub Desktop.
Insider Trading Tutorial - 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Package used to execute HTTP POST request to the API | |
import json | |
import urllib.request | |
# API Key | |
TOKEN = YOUR_API_KEY # replace YOUR_API_KEY with the API key you got from sec-api.io after sign up | |
# API endpoint | |
API = "https://api.sec-api.io?token=" + TOKEN | |
# Define the filter parameters | |
filter = "formType:\"4\" AND formType:(NOT \"N-4\") AND formType:(NOT \"4/A\") AND filedAt:[2019-07-01 TO 2019-08-01]" | |
# Start with the first filing. Increase it when paginating. | |
# Set to 10000 if you want to fetch the next batch of filings. Set to 20000 for the next and so on. | |
start = 0 | |
# Return 10,000 filings per API call | |
size = 10000 | |
# Sort in descending order by filedAt | |
sort = [{ "filedAt": { "order": "desc" } }] | |
payload = { | |
"query": { "query_string": { "query": filter } }, | |
"from": start, | |
"size": size, | |
"sort": sort | |
} | |
# Format your payload to JSON bytes | |
jsondata = json.dumps(payload) | |
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes | |
# Instantiate the request | |
req = urllib.request.Request(API) | |
# Set the correct HTTP header: Content-Type = application/json | |
req.add_header('Content-Type', 'application/json; charset=utf-8') | |
# Set the correct length of your request | |
req.add_header('Content-Length', len(jsondataasbytes)) | |
# Send the request to the API | |
response = urllib.request.urlopen(req, jsondataasbytes) | |
# Read the response | |
res_body = response.read() | |
# Transform the response into JSON | |
filingsJson = json.loads(res_body.decode("utf-8")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment