下面是一些Python代碼,用于分析股票的技術(shù)面指標(biāo)。
首先,我們需要安裝一些必需的Python庫(kù),包括pandas、numpy、matplotlib和pandas-datareader。我們可以使用pip install命令進(jìn)行安裝。
``` python
!pip install pandas numpy matplotlib pandas-datareader
```
接下來(lái),我們將使用pandas-datareader庫(kù)中的DataReader函數(shù)從雅虎財(cái)經(jīng)中獲取股票數(shù)據(jù)。我們將使用AAPL(Apple Inc.)作為我們的例子。我們將獲取該股票的歷史股票價(jià)格,并將其存儲(chǔ)在DataFrame對(duì)象中。
``` python
import pandas_datareader as web
# Get historical data for AAPL from Yahoo Finance
df = web.DataReader('AAPL', data_source='yahoo', start='2010-01-01')
```
我們還將使用pandas庫(kù)來(lái)計(jì)算股票的移動(dòng)平均線(xiàn)(MA)和指數(shù)移動(dòng)平均線(xiàn)(EMA)。我們將計(jì)算10天和50天的MA和EMA。
``` python
# Calculate the moving averages and exponential moving averages
ma10 = df['Adj Close'].rolling(10).mean()
ma50 = df['Adj Close'].rolling(50).mean()
ema10 = df['Adj Close'].ewm(span=10).mean()
ema50 = df['Adj Close'].ewm(span=50).mean()
# Add the MA and EMA to the DataFrame
df['MA10'] = ma10
df['MA50'] = ma50
df['EMA10'] = ema10
df['EMA50'] = ema50
```
我們可以使用matplotlib庫(kù)來(lái)繪制股票價(jià)格和MA、EMA。
``` python
import matplotlib.pyplot as plt
# Plot the stock price and MA, EMA
plt.plot(df.index, df['Adj Close'], label='Price')
plt.plot(df.index, ma10, label='MA10')
plt.plot(df.index, ma50, label='MA50')
plt.plot(df.index, ema10, label='EMA10')
plt.plot(df.index, ema50, label='EMA50')
plt.legend()
plt.show()
```
最后,我們將計(jì)算股票的相對(duì)強(qiáng)弱指數(shù)(RSI)和移動(dòng)平均散度(MACD),用于更全面的技術(shù)面分析。我們將使用talib庫(kù)提供的函數(shù)來(lái)計(jì)算這些指標(biāo)。
``` python
import talib
# Calculate the RSI and MACD
rsi = talib.RSI(df['Adj Close'], timeperiod=14)
macd, macdsignal, macdhist = talib.MACD(df['Adj Close'], fastperiod=12, slowperiod=26, signalperiod=9)
# Add the RSI and MACD to the DataFrame
df['RSI'] = rsi
df['MACD'] = macd
df['MACD_Signal'] = macdsignal
df['MACD_Hist'] = macdhist
```
現(xiàn)在我們已經(jīng)計(jì)算出這些技術(shù)指標(biāo)了,我們可以用相同的方式繪制它們,以更好地理解股票的技術(shù)面。
``` python
# Plot the RSI and MACD
plt.subplot(2, 1, 1)
plt.plot(df.index, df['RSI'])
plt.title('RSI')
plt.subplot(2, 1, 2)
plt.plot(df.index, df['MACD'], label='MACD')
plt.plot(df.index, df['MACD_Signal'], label='MACD Signal')
plt.bar(df.index, df['MACD_Hist'], label='MACD Hist')
plt.legend()
plt.title('MACD')
plt.show()
```
這是一個(gè)簡(jiǎn)單的技術(shù)面分析過(guò)程。您可以對(duì)這些函數(shù)進(jìn)行修改和擴(kuò)展,以計(jì)算其他技術(shù)面指標(biāo)。然,在使用這些指標(biāo)時(shí),需要謹(jǐn)慎。單一的技術(shù)面指標(biāo)并不能準(zhǔn)確地預(yù)測(cè)未來(lái)的股票價(jià)格。這些指標(biāo)可以與基本面分析和市場(chǎng)研究相結(jié)合,提供更全面、更準(zhǔn)確的股票分析結(jié)果。
下面是完整的代碼:
``` python
import pandas_datareader as web
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import talib
# Get historical data for AAPL from Yahoo Finance
df = web.DataReader('AAPL', data_source='yahoo', start='2010-01-01')
# Calculate the moving averages and exponential moving averages
ma10 = df['Adj Close'].rolling(10).mean()
ma50 = df['Adj Close'].rolling(50).mean()
ema10 = df['Adj Close'].ewm(span=10).mean()
ema50 = df['Adj Close'].ewm(span=50).mean()
# Add the MA and EMA to the DataFrame
df['MA10'] = ma10
df['MA50'] = ma50
df['EMA10'] = ema10
df['EMA50'] = ema50
# Plot the stock price and MA, EMA
plt.plot(df.index, df['Adj Close'], label='Price')
plt.plot(df.index, ma10, label='MA10')
plt.plot(df.index, ma50, label='MA50')
plt.plot(df.index, ema10, label='EMA10')
plt.plot(df.index, ema50, label='EMA50')
plt.legend()
plt.show()
# Calculate the RSI and MACD
rsi = talib.RSI(df['Adj Close'], timeperiod=14)
macd, macdsignal, macdhist = talib.MACD(df['Adj Close'], fastperiod=12, slowperiod=26, signalperiod=9)
# Add the RSI and MACD to the DataFrame
df['RSI'] = rsi
df['MACD'] = macd
df['MACD_Signal'] = macdsignal
df['MACD_Hist'] = macdhist
# Plot the RSI and MACD
plt.subplot(2, 1, 1)
plt.plot(df.index, df['RSI'])
plt.title('RSI')
plt.subplot(2, 1, 2)
plt.plot(df.index, df['MACD'], label='MACD')
plt.plot(df.index, df['MACD_Signal'], label='MACD Signal')
plt.bar(df.index, df['MACD_Hist'], label='MACD Hist')
plt.legend()
plt.title('MACD')
plt.show()
```
聯(lián)系客服