Stock Chart Pattern Recognition in Python

I have been following Part Time Larry’s Youtube channel and learning a lot about Python and trading. Here I was doing this video series tutorial. It was missing source code. It is always good idea to type in the code, so I can see why Larry didn’t publish the code but here is mine.

pizza.py

import yfinance as yf

pizza = yf.Ticker("pzza")

dataframe = pizza.history(period="1y")

print(dataframe.to_csv())

pizza_pattern.py

import csv

def is_bearish_candlestick(candle):
    return candle['Close'] < candle['Open']

def is_bullish_engulfing(candles, index):
    current_day = candles[index]
    previous_day = candles[index-1]

    if is_bearish_candlestick(previous_day) \
        and current_day['Close'] > previous_day['Open'] \
        and current_day['Open'] < previous_day['Close']:
        return True

    return False

with open('pizza.csv') as f:
    reader = csv.DictReader(f)
    candles = list(reader)

for i in range(1, len(candles)):
    print(candles[i])

    if is_bullish_engulfing(candles, i):
        print("{} is a bullish engulfing".format(candles[i]['Date']))

download_sp500_history.py

import yfinance as yf
import csv

companies = csv.reader(open('sp500_companies.csv'))

for company in companies:
    print(company)

    symbol, name = company

    history_filename = 'history/{}.csv'.format(symbol)

    f = open(history_filename, 'w')
    ticker = yf.Ticker(symbol)
    df = ticker.history(period="1y")
    f.write(df.to_csv())
    f.close()

spy_pattern.py

import csv

def is_bullish_candlestick(candle):
    return float(candle['Close']) > float(candle['Open'])

def is_bearish_candlestick(candle):
    return float(candle['Close']) < float(candle['Open'])

def is_bullish_engulfing(candles, index):
    current_day = candles[index]
    previous_day = candles[index-1]

    if is_bearish_candlestick(previous_day) \
        and float(current_day['Close']) > float(previous_day['Open']) \
        and float(current_day['Open']) < float(previous_day['Close']):
        return True

    return False

def is_bearish_engulfing(candles, index):
    current_day = candles[index]
    previous_day = candles[index-1]

    if is_bullish_candlestick(previous_day) \
        and float(current_day['Open']) > float(previous_day['Close']) \
        and float(current_day['Close']) < float(previous_day['Open']):
        return True

    return False

sp500_file = open('sp500_companies.csv')
sp500_companies = csv.reader(sp500_file)

for company in sp500_companies:
    ticker, company_name = company
    history_file = open('history/{}.csv'.format(ticker))
    reader = csv.DictReader(history_file)
    candles = list(reader)
    candles = candles[-2:]

    if len(candles) > 1:
        if is_bullish_engulfing(candles, 1):
            print('{} - {} is bullish engulfing'.format(ticker, candles[1]['Date']))
        if is_bearish_engulfing(candles, 1):
            print('{} - {} is bearish engulfing'.format(ticker, candles[1]['Date']))