分享

用Python语言对股票进行技术分析

 cxm54666 2023-03-15 发布于吉林

下面是一些Python代码,用于分析股票的技术面指标。

首先,我们需要安装一些必需的Python库,包括pandas、numpy、matplotlib和pandas-datareader。我们可以使用pip install命令进行安装。

``` python

!pip install pandas numpy matplotlib pandas-datareader

```

接下来,我们将使用pandas-datareader库中的DataReader函数从雅虎财经中获取股票数据。我们将使用AAPL(Apple Inc.)作为我们的例子。我们将获取该股票的历史股票价格,并将其存储在DataFrame对象中。

``` 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库来计算股票的移动平均线(MA)和指数移动平均线(EMA)。我们将计算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库来绘制股票价格和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()

```

最后,我们将计算股票的相对强弱指数(RSI)和移动平均散度(MACD),用于更全面的技术面分析。我们将使用talib库提供的函数来计算这些指标。

``` 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

```

现在我们已经计算出这些技术指标了,我们可以用相同的方式绘制它们,以更好地理解股票的技术面。

``` 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()

```

这是一个简单的技术面分析过程。您可以对这些函数进行修改和扩展,以计算其他技术面指标。然,在使用这些指标时,需要谨慎。单一的技术面指标并不能准确地预测未来的股票价格。这些指标可以与基本面分析和市场研究相结合,提供更全面、更准确的股票分析结果。

下面是完整的代码:

``` 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()

```

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多