Insider Trading: Scoring Stock Options with Congressional Data

Politicians always seem to be one step ahead in the financial markets. Whether that be due to lucky predictions, or maybe the fact they write legislation that actively affects the companies themselves or their family members invest in, we may never know. But the one thing I am certain of, is when congress moves their money, it's good to take note, because odds are they know something we don't.

print("This is Insider Trading")

This project was built to identify high potential stock options based on congressional trading trends. The algorithm I designed calculates a score for a stock based on various factors from publically available STOCK Act info. In short, when a government official buys a stock, the score increases, when they sell the stock, the score decreases. Here is the Live Link to the project.

App Development

Built with:

  • Python - Using Pandas library for data processing and manipulation
  • JavaScript - For dynamically scraping STOCK Act info from the web
  • SQLite - As database and RDBMS
  • Flask - Web framework
  • Tailwind CSS - For frontend styling and aesthetics

The Scoring Algorithm

The scoring algorithm takes into account a variety of factors. The first and most notable being whether it is a buy or sell of the stock. Purchasing a stock positively affects its score while selling it negatively affects the score. The monetary value of the purchase also increases the score. Additionally, the relation to the congress member as well as the reporting gap also makes a difference.

One of the challenges of creating legitimate scores was taking into consideration that larger companies had higher market caps and more money was running through them. To account for this, a multiplier was added to the algorithm that multiplies the score based on the number of unique congressional officials that purchased the stock within the time period, so that way popular stock options would still be identified even if they were smaller market caps.

# Create or update the rows in the Score table
for trade in trades:
        # Get the ticker for this transaction
        ticker = trade.traded_issuer_ticker

        # if this ticker is not already in score_df, add it
        if ticker not in score_df.index:
                score_df.loc[ticker] = [0, 0, set(), trade.price]

        # add the politician to the set of officials
        score_df.loc[ticker, 'num_officials'].add(trade.politician_name)

        # calculate the score for this transaction
        score = calculate_score(trade, len(score_df.loc[ticker, 'num_officials']))

        # update the total score and number of transactions
        score_df.loc[ticker, 'total_score'] += score
        score_df.loc[ticker, 'num_transactions'] += 1
        score_df.loc[ticker, 'purchase_price'] = min(trade.price, score_df.loc[ticker, 'purchase_price'])

# calculate the average score for each ticker
score_df['average_score'] = score_df['total_score'] / score_df['num_transactions']
score_df.sort_values('average_score', inplace=True)
# convert the set of officials to the number of unique officials for each ticker
score_df['num_officials'] = score_df['num_officials'].apply(len)

App Features

The app simply ranks the highest top 100 stocks and easily displays them to the user. Different tickers can be clicked into, where you can see the recent trades for that stock, who bought/sold it, and when. Additionally, you can easily search up a stock by its ticker to see the recent trades for it.

Key Takeaways and Future Work

This project exposed me to a variety of data science concepts, such as algorithm design and normalization. Moving forward, I would like to optimize the data ingestion process, and automate it through a CRON job. Additionally, more live data visualizations for each specific stock would be nice as well. Lastly, and most importantly, would be to validate the scoring algorithm through a paper trading bot and assess whether the top options are in fact the best choices.