This is How I Implemented Benjamin Graham’s Teachings into an Automated Investing Strategy in Python

Trevor Thackston
Nov 27, 2018 · 13 min read
Image for post
Image for post
Photo by Chris Ried on Unsplash.

Image for post
Image for post
Benjamin Graham, the man with the value-based plan.

The Theory

Image for post
Image for post
I don’t even think I own a calculator anymore. Photo credit: stevepb at Pixabay

The Strategy


The Tools

>>> from iexfinance import Stock>>> stock_batch = Stock(['AAPL', 'TSLA', 'MSFT'])>>> stock_batch.get_financials()
total_market_cap = 0
for stock_symbol in symbol_batch:
total_market_cap += fundamental_df[stock_symbol]['market_cap']
total_market_cap = fundamental_df['market_cap'].sum()
high_pe = fundamental_df['pe_ratio'] > 15
high_pe_market_cap = fundamental_df[high_pe]['market_cap'].sum()
def initialize(context):
# This method will be called every time the script is run.
# From here, we can schedule when other methods will be called.
# context is simply an object containing useful information passed around to the different methods pylivetrader accesses.
pass
def before_market_open(context, data):
# This method will be run every day before market open as long as the script is running.
passed
def handle_data(context, data):
# This method is called whenever there's new data coming in - once a minute, when the market's open.
pass

The Code

Our script’s very first function. Go ahead and put this into GrahamFundamentals.py if you want to follow along.
Simple enough.
Now we’re really getting into it.
Here’s where the money moves.
Data collection’s a dirty job, but some code’s got to do it.
Not the most interesting code in the world, but it makes sure we’re not investing based on bad data.
The finishing touch — the rest of Graham’s rules for our investment model.

Image for post
Image for post

Running Our Script

pylivetrader run GrahamFundamentals.py
Image for post
Image for post
Here’s to finding some success in the market. Photo by Lukas from Pexels.

Image for post
Image for post

Automation Generation

News and thought leadership on the changing landscape of…