I’ve read all his books multiple times. And even summarize them every now and then for myself to dive really deep into the content.
Then I don’t always have to read his writing but can read my own understanding of it.
In my trading routine I’ve incorporated checking stocks that meet his Trend Template criteria.
I used to do that in Think or Swim as explained earlier in this post. The downside of that is that I’m not able to use the IBD rating in scanning in Think or Swim.
To further improve my trading analysis I’ve started to look into programming Python lately. Since I’m in an IT job I do already have a lot of technical feeling that makes adopting it somewhat easier.
Following you’ll find the exact Python code for Mark Minervini’s Trend Template. It uses an export of IBD’s new Stock Screener as import and than scans all the stocks on the criteria.
After the scan it outputs the stocks that meet all criteria in an Excel including some additional useful data rows.
I’m always looking at improving it and already got some things on my to-do list.
- import / export using Google Drive instead of Excel
- Including some additional output e.g. pivot points that can be used as buy point
- Export stocks that meeting a subset of the criteria.
Since I use Google Drive for most of my file storage I prefer that over Excel. It also add some flexibility to run the program from other machines as well.
By including more output information I can cut out some more manual actions out of my routine. Off course I’ll always do the analysis manually afterwards because I think it is important to get more feeling and understanding of the charts than only running some code to validate.
Some of the best super performance stocks don’t match all criteria. They might lack one, or more, of the criteria but still turn out to become big movers. Currently I would miss them by only looking at the output that currently only shows stocks that match all.
These changes will help me further improve my trading.
The Trend Template
In Mark’s first book; Trade Like A Stock Market Wizard he has published his Trend Template.
He uses that to filter out stocks using some technical criteria before doing an additional research on them.
The following rules are used:
- The current stock price is above both the 150-day and 200-day moving average
- The 150-day moving average is above the 200-day moving average
- The 200-day moving average is trending up for at least 1 month
- The 50-day moving average is above both the 150-day and 200-day moving average
- The current stock price is trading above the 50-day moving average
- The current stock price is at least 30% above it’s 52-week low
- The current stock price is within at least 25% of it’s 52-week high
- The relative strength ranking (in IBD) is no less than 70, preferably in the 80’s or 90’s
They link to the exact Python code that validates the criteria.
At the bottom of this post you can download the python file itself.
The Trend Template in Python Code
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import yfinance as yf
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import os
from pandas import ExcelWriter
yf.pdr_override()
start =dt.datetime(2018,12,1)
now = dt.datetime.now()
root = Tk()
ftypes = [(".xlsm","*.xlsx",".xls")]
ttl = "Title"
dir1 = 'C:\\'
filePath = askopenfilename(filetypes = ftypes, initialdir = dir1, title = ttl)
#Pop-up screen where you can select the input file you want to scan
stocklist = pd.read_excel(filePath)
stocklist=stocklist.head()
exportList= pd.DataFrame(columns=['Stock', "RS_Rating", "50 Day MA", "150 Day Ma", "200 Day MA", "52 Week Low", "52 week High"])
for i in stocklist.index:
stock=str(stocklist["Symbol"][i])
RS_Rating=stocklist["RS Rating"][i]
try:
df = pdr.get_data_yahoo(stock, start, now)
#Define Trend Template criteria
smaUsed=[50,150,200]
for x in smaUsed:
sma=x
df["SMA_"+str(sma)]=round(df.iloc[:,4].rolling(window=sma).mean(),2)
currentClose=df["Adj Close"][-1]
moving_average_50=df["SMA_50"][-1]
moving_average_150=df["SMA_150"][-1]
moving_average_200=df["SMA_200"][-1]
low_of_52week=min(df["Adj Close"][-260:])
high_of_52week=max(df["Adj Close"][-260:])
try:
moving_average_200_20 = df["SMA_200"][-20]
except Exception:
moving_average_200_20=0
#Condition 1: Current Price > 150 SMA and > 200 SMA
if(currentClose>moving_average_150>moving_average_200):
cond_1=True
else:
cond_1=False
#Condition 2: 150 SMA and > 200 SMA
if(moving_average_150>moving_average_200):
cond_2=True
else:
cond_2=False
#Condition 3: 200 SMA trending up for at least 1 month
if(moving_average_200>moving_average_200_20):
cond_3=True
else:
cond_3=False
#Condition 4: 50 SMA> 150 SMA and 50 SMA> 200 SMA
if(moving_average_50>moving_average_150>moving_average_200):
#print("Condition 4 met")
cond_4=True
else:
#print("Condition 4 not met")
cond_4=False
#Condition 5: Current Price > 50 SMA
if(currentClose>moving_average_50):
cond_5=True
else:
cond_5=False
#Condition 6: Current Price is at least 30% above 52 week low
if(currentClose>=(1.3*low_of_52week)):
cond_6=True
else:
cond_6=False
#Condition 7: Current Price is within 25% of 52 week high
if(currentClose>=(.75*high_of_52week)):
cond_7=True
else:
cond_7=False
#Condition 8: IBD RS rating >70 and the higher the better
#Need IBD data tables including the RS rating for this, or export stocks form IBD Stock Screener with RS >70
if(RS_Rating>70):
cond_8=True
else:
cond_8=False
if(cond_1 and cond_2 and cond_3 and cond_4 and cond_5 and cond_6 and cond_7 and cond_8):
exportList = exportList.append({'Stock': stock, "RS_Rating": RS_Rating, "50 Day MA": moving_average_50, "150 Day Ma": moving_average_150, "200 Day MA": moving_average_200, "52 Week Low": low_of_52week, "52 week High": high_of_52week}, ignore_index=True)
except Exception:
print("No data on "+stock)
print(exportList)
newFile=os.path.dirname(filePath)+"/ScreenOutput.xlsx"
writer= ExcelWriter(newFile)
exportList.to_excel(writer,"Sheet1")
writer.save()
Click here to download the Python file
Hi Marcel,
Thanks for making this! Been looking for a solution for scanning this for quite a while. I copied this script and pasted it into a .py file and I keep getting all kinds of IndentationErrors and SyntaxErrors. What’s the best step-by-step way to run this script? Do you have a version of the python file you use that I could download and instructions on how to run it? Thanks in advance!
Hi Nikhil,
Thanks for your reply! Strange that is haven’t been working for you yet. I can send you the exact python file that I have working? If you’d want so, can I use the email address you provided here?
-Marcel
Hello Marc,
Thank you very much for making this! Could you please send the file you sent to Nikhil to me as well? Thank you very much in advance!
Best regards,
Pete
Hi Marcel,
Yes, that would be great! The email I used here is fine. In addition, whatever instructions I might need or packages I might need to download along with it would be great. Thanks in advance!
Hi Marcel,
Thanks for your share. I am currently finding methods to get Marks’ template and find your post. Would you mind sharing the python file with me also? That would be great if you can instruct whatever packages I might need to download to use the code. Thanks a lot.
Hello Marc,
Thank you very much for making this! Could you please send the file you sent to Nikhil to me as well? Thank you very much in advance!
Best regards,
Jeanna
Hi Nikhil,
I am also getting the following errors when running the python script:
IPython 7.16.1 — An enhanced Interactive Python.
debugfile(‘D:/Save/Stocks/The Best Stock Data 2020/Relative Strength Trend Template.py’, wdir=’D:/Save/Stocks/The Best Stock Data 2020′)
File “d:\save\stocks\the best stock data 2020\relative strength trend template.py”, line 28
stock=str(stocklist[“Symbol”][i])
^
IndentationError: expected an indented block
runfile(‘C:/Users/Kama/AppData/Local/Temp/kite_tutorial.py’, wdir=’C:/Users/Kama/AppData/Local/Temp’)
File “C:\Users\Kama\AppData\Local\Temp\kite_tutorial.py”, line 131
plt. # <–
^
SyntaxError: invalid syntax. Could you please send me the python script that works.
Hi Marcel, I am getting the same errors as Nikhil. could you please email me a copy of the Python scripts that works, and advise what packages I need to run it, ie if other than Python.
To answer all questions and errors people run in to by picking up this python screener I’m working on an improved manual to get it up and running from scratch. Will post it online asap.
Hi Marcel,
Could you please send me your Screening python program as well. My email is cheungdererk@hotmail.com
regards
Derek
Hi Marcel,
Do you have any indication of when you will be able to finish the improved manual, to get this code working ?
error python in code smaUsed=[50,150,200]