Skip to content

Instantly share code, notes, and snippets.

View janlukasschroeder's full-sized avatar
🎯
Focusing

Jan janlukasschroeder

🎯
Focusing
View GitHub Profile
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-1.py
Last active August 26, 2019 23:25
Insider Trading Tutorial - 1
# 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
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-full.ipynb
Last active February 21, 2022 07:08
Insider Trading - Python Tutorial
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-2.md
Last active August 24, 2019 22:52
Insider Trading Tutorial - 2
# Print the response. Most likely this will throw an error because we fetched a
# large amount of data (10,000 filings). Reduce the number of filings and you will see a result here.
print(json.dumps(filingsJson, indent=2))
{
  "total": 21521,
  "filings": [

{

@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-3.py
Created August 24, 2019 22:57
Insider Trading Tutorial - 3
def compress_filings (filings):
store = {}
compressed_filings = []
for filing in filings:
filedAt = filing['filedAt']
if filedAt in store and store[filedAt] < 5:
compressed_filings.append(filing)
store[filedAt] += 1
elif filedAt not in store:
compressed_filings.append(filing)
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-4.py
Created August 24, 2019 23:00
Insider Trading Tutorial - 4
import xml.etree.ElementTree as ET
import re
import time
# Download the XML version of the filing. If it fails wait for 5, 10, 15, ... seconds and try again.
def download_xml (url, tries = 1):
try:
response = urllib.request.urlopen(url)
except:
print('Something went wrong. Wait for 5 seconds and try again.', tries)
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-5.py
Created August 24, 2019 23:01
Insider Trading Tutorial - 5
# Download the XML for each filing
# Calculate the total transaction amount per filing
# Save the calculate transaction value to the filing dict with key 'nonDerivativeTransactions'
def add_non_derivative_transaction_amounts ():
for filing in filings:
url = filing['linkToTxt']
xml = download_xml(url)
nonDerivativeTransactions = calculate_transaction_amount(xml)
filing['nonDerivativeTransactions'] = nonDerivativeTransactions
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-6.md
Last active August 24, 2019 23:19
Insider Trading Tutorial - 6
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from pandas.io.json import json_normalize
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-7.md
Created August 24, 2019 23:07
Insider Trading Tutorial - 7
def getBins (filings):
    bins = {}
    for index, row in filings.iterrows():
        filedAt = row['filedAt']
        nonDerivativeTransactions = row['nonDerivativeTransactions']
        value = bins[filedAt] + nonDerivativeTransactions if filedAt in bins else nonDerivativeTransactions
        bins[filedAt] = round(value, 2)
    return bins
@janlukasschroeder
janlukasschroeder / insider-trading-tutorial-8.py
Created August 24, 2019 23:09
Insider Trading Tutorial - 8
# Set size of figure
plt.rcParams['figure.figsize'] = [15, 10]
# Prettify y axis: 2000000 to $2M
def millions(x, pos):
return '${:,.0f}M'.format(x*1e-6)
fig, ax = plt.subplots()
# Define bar plot
ax.bar(range(len(bins)), list(bins.values()), align='center')
@janlukasschroeder
janlukasschroeder / index.html
Created September 26, 2019 10:32
React - static AMP example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="Description"
content="Stock Market News Aggregator | Article2Tickers | Reuters, Bloomberg, SeekingAlpha, WSJ, +1K more |
Real-Time News Feed | Custom Filters | Text-to-Audio Reader | Ad-Free & Clean Design"
/>
<meta