Technical Indicators

๐Ÿ”ฅ Vibe Prompt

"Calculate MA, MACD, RSI, and Bollinger Bands for TSMC stock. Plot them with buy/sell signals on a single chart."

Moving Averages

import pandas as pd
import yfinance as yf

data = yf.download("2330.TW", start="2023-01-01", end="2024-12-31")

# Calculate moving averages
data['MA5'] = data['Close'].rolling(5).mean()
data['MA20'] = data['Close'].rolling(20).mean()
data['MA60'] = data['Close'].rolling(60).mean()

print(data[['Close', 'MA5', 'MA20']].tail())

MACD

from ta.trend import MACD

macd = MACD(close=data['Close'], window_slow=26, window_fast=12, window_sign=9)
data['MACD'] = macd.macd()
data['MACD_Signal'] = macd.macd_signal()
data['MACD_Diff'] = macd.macd_diff()

# MACD crossover signals
# MACD line crosses above signal โ†’ buy
# MACD line crosses below signal โ†’ sell

RSI

from ta.momentum import RSIIndicator

rsi = RSIIndicator(close=data['Close'], window=14)
data['RSI'] = rsi.rsi()

# RSI signals:
# RSI < 30 โ†’ oversold โ†’ potential buy
# RSI > 70 โ†’ overbought โ†’ potential sell

Bollinger Bands

from ta.volatility import BollingerBands

bb = BollingerBands(close=data['Close'], window=20, window_dev=2)
data['BB_Upper'] = bb.bollinger_hband()
data['BB_Middle'] = bb.bollinger_mavg()
data['BB_Lower'] = bb.bollinger_lband()

# Price touches lower band โ†’ oversold
# Price touches upper band โ†’ overbought

Combine All Indicators

import matplotlib.pyplot as plt

fig, axes = plt.subplots(4, 1, figsize=(14, 10), sharex=True)

# Price + MA
axes[0].plot(data['Close'], label='Close', alpha=0.5)
axes[0].plot(data['MA20'], label='MA20')
axes[0].legend()

# MACD
axes[1].plot(data['MACD'], label='MACD')
axes[1].plot(data['MACD_Signal'], label='Signal')
axes[1].legend()

# RSI
axes[2].plot(data['RSI'], label='RSI')
axes[2].axhline(30, color='g', linestyle='--', alpha=0.3)
axes[2].axhline(70, color='r', linestyle='--', alpha=0.3)
axes[2].legend()

# Bollinger
axes[3].plot(data['Close'], label='Close')
axes[3].plot(data['BB_Upper'], label='Upper', alpha=0.5)
axes[3].plot(data['BB_Lower'], label='Lower', alpha=0.5)
axes[3].legend()

plt.tight_layout()
plt.show()

Practice Exercise

๐Ÿ’ก Vibe Practice: Ask AI to build a "Technical Indicator Dashboard" that shows all indicators interactively with buy/sell signal markers.

Member Exclusive Free Tutorial

This chapter is free exclusive content for registered members! Please login or register to unlock immediately.

Login / Register Now