Technical analysis is a vital tool for traders and investors to evaluate financial market trends. In this article, we'll explore six widely used technical analysis methods and demonstrate their Python implementations using pandas and ta libraries. We'll analyze Kweichow Moutai (Stock Code: 600519) as our example.
1. Importing Libraries and Fetching Stock Data
First, let’s import the necessary libraries and retrieve the stock data:
import akshare as ak
import pandas as pd
stock_code = "600519" # Kweichow Moutai stock code
start_date = "20240101"
end_date = "20241018"
df = ak.stock_zh_a_hist(symbol=stock_code, start_date=start_date, end_date=end_date, adjust="qfq")
df['日期'] = pd.to_datetime(df['日期'])
df.set_index('日期', inplace=True)
print(df.head())2. Fast & Slow Moving Averages
This strategy uses a 20-day SMA (fast line) and a 50-day SMA (slow line) to generate buy/sell signals.
import matplotlib.pyplot as plt
from ta.trend import SMAIndicator
plt.rcParams["font.sans-serif"] = ["STHeiti"]
plt.rcParams["axes.unicode_minus"] = False
df['SMA20'] = SMAIndicator(close=df['收盘'], window=20).sma_indicator()
df['SMA50'] = SMAIndicator(close=df['收盘'], window=50).sma_indicator()
df['Signal'] = 0
df.loc[df['SMA20'] > df['SMA50'], 'Signal'] = 1 # Buy signal
df.loc[df['SMA20'] < df['SMA50'], 'Signal'] = -1 # Sell signal
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['收盘'], label='Closing Price')
plt.plot(df.index, df['SMA20'], label='20-day SMA')
plt.plot(df.index, df['SMA50'], label='50-day SMA')
plt.title('Kweichow Moutai - Fast & Slow Moving Averages')
plt.legend()
plt.show()
print(df[df['Signal'] != df['Signal'].shift(1)])Key Takeaways:
👉 Learn how to optimize moving averages for better trading signals
- A golden cross (20 > 50) indicates bullish momentum.
- A death cross (20 < 50) suggests bearish momentum.
3. Moving Average + MACD Strategy
Combining trend-following (SMA) and momentum (MACD), this method enhances trade signals.
from ta.trend import MACD
df['SMA50'] = SMAIndicator(close=df['收盘'], window=50).sma_indicator()
macd = MACD(close=df['收盘'])
df['MACD'] = macd.macd()
df['MACD_Signal'] = macd.macd_signal()
df['MACD_Hist'] = macd.macd_diff()
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['收盘'], label='Closing Price')
plt.plot(df.index, df['SMA50'], label='50-day SMA')
plt.bar(df.index, df['MACD_Hist'], label='MACD Histogram', color='gray', alpha=0.3)
plt.title('Kweichow Moutai - Moving Average + MACD Strategy')
plt.legend()
plt.show()Interpretation:
- A MACD crossover above zero confirms bullish trends.
- A MACD crossover below zero signals bearish reversals.
FAQ Section
Q1: What’s the best SMA period for short-term trading?
A: For short-term trading, 5-20 day SMAs work well, while 50-200 day SMAs suit long-term trends.
Q2: How does MACD improve trend analysis?
A: MACD combines trend strength (SMA) and momentum (histogram divergence), reducing false signals.
Q3: Can these methods be automated for algorithmic trading?
A: Yes! Platforms like 👉 OKX API support Python-based algorithmic trading strategies.
Conclusion
By combining moving averages, MACD, and trend analysis, traders gain actionable insights into market trends. These Python implementations provide a foundation for refining strategies further.
Would you like additional methods like RSI or Bollinger Bands? Let us know in the comments!