Purpose:
to screen stockers that have bearish trend
Definition:
- bearish trend: The stock's short-term simple moving average (SMA) is less than its medium-term SMA and its medium-term SMA is less than its long-term SMA.
- simple moving average: an average of close price for a given window.
- short-term SMA: SMA for 5 trading days (1 week)
- medium-term SMA: SMA for 20 trading days (1 month)
- long-term SMA: SMA for 60 trading days (3 month)
Theory:
Stocks that have a bearish trend have 80% possibility of going down
Application:
Buying a short for a stock that has a bearish trend
Getting Simple Moving Averages for Each Ticker:
data = yf.download(ticker, period=period)
short_window = 5 # Short-term SMA (e.g., 20 days)
medium_window = 20 # Medium-term SMA (e.g., 60 days)
long_window = 60 # Long-term SMA (e.g., 200 days)
Close = 'Close'
SMA_short = data[Close].tail(short_window).mean().item()
SMA_medium = data[Close].tail(medium_window).mean().item()
SMA_long = data[Close].tail(long_window).mean().item()
explanation: tail is used to calculate SMA for the last trading day only
Checking Whether Bearish or Not:
condition_one = SMA_short < SMA_medium
condition_two = SMA_medium < SMA_long
is_bearish = condition_one and condition_two
# append if bearish
if is_bearish:
quotes[bearish].append(ticker)
Printing the result:
bearish_tickers = quotes[bearish]
for i in range(0, len(bearish_tickers), 5):
# Join the next five tickers into a string
line = ", ".join(bearish_tickers[i:i + 5])
print(line)
Result:
Full code:
import pandas as pd
import json
nasdaq100_tickers = pd.read_html('https://en.wikipedia.org/wiki/Nasdaq-100#Components')[4]['Symbol']
#nasdaq100_tickers = nasdaq100_tickers[0:10]
bearish = "bearish"
quotes = {}
quotes[bearish] = []
period = "6mo"
for idx, ticker in enumerate(nasdaq100_tickers):
try:
remaining = len(nasdaq100_tickers) - idx - 1
print(f"checking quote {idx + 1}. remainding = {remaining}")
data = yf.download(ticker, period=period)
short_window = 5 # Short-term SMA (e.g., 20 days)
medium_window = 20 # Medium-term SMA (e.g., 60 days)
long_window = 60 # Long-term SMA (e.g., 200 days)
Close = 'Close'
SMA_short = data[Close].tail(short_window).mean().item()
SMA_medium = data[Close].tail(medium_window).mean().item()
SMA_long = data[Close].tail(long_window).mean().item()
condition_one = SMA_short < SMA_medium
condition_two = SMA_medium < SMA_long
is_bearish = condition_one and condition_two
if is_bearish:
quotes[bearish].append(ticker)
except Exception as e:
print(f"Exception: {e}")
raise
print(f"total {len(quotes[bearish])} quotes found")
bearish_tickers = quotes[bearish]
for i in range(0, len(bearish_tickers), 5):
# Join the next five tickers into a string
line = ", ".join(bearish_tickers[i:i + 5])
print(line)