用Python实现股价的简单移动平均值

前言

最近有没有朋友想买股票和基金?今天我要教你们一件神奇的事情,如何计算平均值。没有人不喜欢钱。

[En]

Are there any friends who want to buy stocks and funds recently? today I’m going to teach you a magic thing, how to calculate the average. There’s no one who doesn’t like money.

用Python绘制出股价的5日均线和20日均线。众所周知,5日均线是短线交易的生死线,而20日均线是中长线趋势的分水岭。因此,基于这两

有了移动平均线,你就可以设计一些简单的交易策略。

[En]

With a moving average, you can design some simple trading strategies.

下面是我练习的代码:

Python学习交流Q群:906715085###
import pandas as pd
import numpy as np
from pandas_datareader import data
import datetime
import matplotlib.pyplot as plt

对于库的导入部分,无需解释,请拉取以下数据:

[En]

For the import part of the library, without explanation, pull the data below:

end_date = datetime.date.today()
start_date = end_date - datetime.timedelta(days = 100)
price = data.DataReader('601127.ss','yahoo',
                       start_date,
                       end_date)
price.head()

这里我选择从yahoo拉601127这支股票过去100天的行情数据。能够看到最早的数据到2021年的10月8日:

然后我开始添加5日和20日均线

price['ma5'] = price['Adj Close'].rolling(5).mean()
price['ma20'] = price['Adj Close'].rolling(20).mean()
price.tail()

数据中就可以看到了:

为了更容易观察,我用代码画了一张图:

[En]

To make it easier to observe, I drew a picture with the code:

Python学习交流Q群:906715085###
fig = plt.figure(figsize=(16,9))
ax1 = fig.add_subplot(111, ylabel='Price')
price['Adj Close'].plot(ax=ax1, color='g', lw=2., legend=True)
price.ma5.plot(ax=ax1, color='r', lw=2., legend=True)
price.ma20.plot(ax=ax1, color='b', lw=2., legend=True)
plt.grid()
plt.show()

这使您可以直观地查看图像:

[En]

This allows you to see the image visually:

这样,就可以根据不同时期的移动平均线来设计移动平均线策略。别往下看,就在这里。记住要喜欢你最喜欢的朋友的收藏,如果你有任何问题,请及时记住。

[En]

In this way, the moving average strategy can be designed according to the moving average of different periods. Don’t look down, it’s over here. Remember to like the collection of your favorite friends, and remember in time if you have any questions.

提问哟!!笔芯~~

Original: https://www.cnblogs.com/1234567FENG/p/16372108.html
Author: 蚂蚁爱Python
Title: 用Python实现股价的简单移动平均值

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499385/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球