Yahoo Finance Screener With TA-Lib

I wanted to show how you could create a python screener using yahoo finance data with TA-Lib. A link to the notebook can be found on my GitHub.

Sample Code

# Calculate indicators
def calculate_indicators(df):
    df['SMA'] = ta.SMA(df['close'], timeperiod=20)
    df['EMA'] = ta.EMA(df['close'], timeperiod=20)
    df['RSI'] = ta.RSI(df['close'], timeperiod=14)
    df['MACD'], df['MACD_signal'], _ = ta.MACD(df['close'], fastperiod=12, slowperiod=26, signalperiod=9)
    df['BB_upper'], df['BB_middle'], df['BB_lower'] = ta.BBANDS(df['close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
    df['TRIMAS'] = ta.TRIMA(df['close'], timeperiod=20)
    return df

# Detect candlestick patterns
def detect_patterns(df):
    df['Doji'] = ta.CDLDOJI(df['open'], df['high'], df['low'], df['close'])
    df['Hammer'] = ta.CDLHAMMER(df['open'], df['high'], df['low'], df['close'])
    df['Engulfing'] = ta.CDLENGULFING(df['open'], df['high'], df['low'], df['close'])
    return df

# Calculate flags for each indicator/pattern
def calculate_flags(df):
    # SMA Flags
    df['SMA_Flag'] = df.apply(lambda x: 1 if x['close'] > x['SMA'] else (-1 if x['close'] < x['SMA'] else 0), axis=1)
    # EMA Flags
    df['EMA_Flag'] = df.apply(lambda x: 1 if x['close'] > x['EMA'] else (-1 if x['close'] < x['EMA'] else 0), axis=1)
    # RSI Flags
    df['RSI_Flag'] = df.apply(lambda x: 1 if x['RSI'] < 30 else (-1 if x['RSI'] > 70 else 0), axis=1)
    # MACD Flags
    df['MACD_Flag'] = df.apply(lambda x: 1 if x['MACD'] > x['MACD_signal'] else (-1 if x['MACD'] < x['MACD_signal'] else 0), axis=1)
    # Bollinger Bands Flags
    df['BB_Flag'] = df.apply(lambda x: 1 if x['close'] < x['BB_lower'] else (-1 if x['close'] > x['BB_upper'] else 0), axis=1)
    # TRIMAS Flags
    df['TRIMAS_Flag'] = df.apply(lambda x: 1 if x['close'] > x['TRIMAS'] else (-1 if x['close'] < x['TRIMAS'] else 0), axis=1)
    # Candlestick Pattern Flags
    df['Doji_Flag'] = df['Doji'].apply(lambda x: -1 if x > 0 else 0)
    df['Hammer_Flag'] = df['Hammer'].apply(lambda x: 1 if x > 0 else 0)
    df['Engulfing_Flag'] = df['Engulfing'].apply(lambda x: 1 if x > 0 else (-1 if x < 0 else 0))
    return df

# Calculate buy and sell scores
def calculate_scores(df):
    # Total number of indicators and patterns
    total_signals = 6 + 3  # 6 indicators + 3 candlestick patterns

    # Calculate Buy and Sell Scores
    df['Buy_Score'] = (
        df[['SMA_Flag', 'EMA_Flag', 'RSI_Flag', 'MACD_Flag', 'BB_Flag', 'TRIMAS_Flag', 'Hammer_Flag', 'Engulfing_Flag']]
        .apply(lambda x: sum(1 for v in x if v == 1), axis=1) / total_signals
    )
    df['Sell_Score'] = (
        df[['SMA_Flag', 'EMA_Flag', 'RSI_Flag', 'MACD_Flag', 'BB_Flag', 'TRIMAS_Flag', 'Doji_Flag', 'Engulfing_Flag']]
        .apply(lambda x: sum(1 for v in x if v == -1), axis=1) / total_signals
    )
    return df

Sample Output

Next Steps

  • Schedule this python file to run on a standard cadence
  • Develop process to email results daily

Sharing

Related Articles

  • All Post
  • Articles
  • Blog Post
  • General Business Automation
  • Portfolio
  • Stock Market & Finance