分类
关于股票基本知识书籍

移动平均线(MACD)

代码输出的数据截图如下:

移动平均线(MACD)

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

  • Open with Desktop
  • View raw
  • Copy raw contents Copy raw contents

Copy raw contents

Copy raw contents

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

移动平均线(MACD)

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

  • Open with Desktop
  • View raw
  • Copy raw contents Copy raw contents

Copy raw contents

Copy raw contents

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

【量化投资-python & pandas技巧系列】使用python计算移动平均线

【量化小讲堂 – python & pandas技巧系列】使用python计算各类移动平均线

计算移动平均线是最常见的需求,下面这段代码将完成以下三件事情:
1. 从csv格式的文件中导入股票数据,数据例图如下:

—————- —————- —————- —————- —————- —————- —————- —————- —————- —————- —————- —————- —————-
# -*- 移动平均线(MACD) coding: utf-8 -*-
“””
@author: yucezhe
@contact: QQ:2089973054 email:[email protected]
“””
import pandas as pd

# ========== 从原始csv文件中导入股票数据,以浦发银行sh600000为例

# 导入数据 – 注意:这里请填写数据文件在您电脑中的路径
stock_data = pd.read_csv( ‘stock data/sh600000.csv’ , parse_dates =[ 1 ])

# 将数据按照交易日期从远到近排序
stock_data.sort( ‘date’ , inplace = True )

# 移动平均线(MACD) ========== 计算移动平均线

# 分别计算5日、20日、60日的移动平均线
ma_list = [ 5 , 20 , 60 ]

# 计算简单算术移动平均线MA – 注意:stock_data[‘close’]为股票每天的收盘价
for ma in ma_list:
stock_data[ ‘MA_’ + str (ma)] = pd.rolling_mean(stock_data[ ‘close’ ] , ma)

# 计算指数平滑移动平均线EMA
for ma in ma_list:
stock_data[ ‘EMA_’ + str (ma)] = pd.ewma(stock_data[ ‘close’ ] , span =ma)

# 将数据按照交易日期从近到远排序
stock_data.sort( ‘date’ , ascending = False , inplace = True )

【量化投资-python & pandas技巧系列】使用python计算移动平均线

代码输出的数据截图如下:

现在想到的之后几期会讲的内容:
【量化小讲堂 – python & pandas技巧系列】如何在windows环境安装python和pandas
【量化小讲堂 – python & pandas技巧系列】如何在mac OSX环境安装python和pandas
【量化小讲堂 – python & pandas技巧系列】使用python计算KDJ指标
【量化小讲堂 – python & pandas技巧系列】使用python计算MACD指标
【量化小讲堂 – 投资策略系列】KDJ、MACD指标双金叉选股效果
【量化小讲堂 – python & pandas技巧系列】使用pytho将日线数据转换成周线、月线数据

【量化投资-python & pandas技巧系列】使用python计算移动平均线

【量化小讲堂 – python & pandas技巧系列】使用python计算各类移动平均线

计算移动平均线是最常见的需求,下面这段代码将完成以下三件事情:
1. 从csv格式的文件中导入股票数据,数据例图如下:

—————- —————- —————- —————- —————- —————- —————- —————- —————- —————- —————- —————- —————-
# -*- coding: utf-8 -*-
“””
@author: yucezhe
@contact: QQ:2089973054 email:[email protected]
“””
import pandas as 移动平均线(MACD) pd

# ========== 从原始csv文件中导入股票数据,以浦发银行sh600000为例

# 导入数据 – 注意:这里请填写数据文件在您电脑中的路径
stock_data = pd.read_csv( ‘stock data/sh600000.csv’ , parse_dates =[ 1 ])

# 将数据按照交易日期从远到近排序
stock_data.sort( ‘date’ , inplace = True )

# ========== 计算移动平均线

# 分别计算5日、20日、60日的移动平均线
ma_list = [ 5 , 20 , 60 ]

# 计算简单算术移动平均线MA – 注意:stock_data[‘close’]为股票每天的收盘价
for ma in ma_list:
stock_data[ 移动平均线(MACD) ‘MA_’ + str (ma)] = pd.rolling_mean(stock_data[ ‘close’ ] , ma)

# 计算指数平滑移动平均线EMA
for ma in ma_list:
stock_data[ ‘EMA_’ + str (ma)] = pd.ewma(stock_data[ ‘close’ ] , span =ma)

# 将数据按照交易日期从近到远排序
stock_data.sort( ‘date’ , ascending = False , inplace = True )

【量化投资-python & pandas技巧系列】使用python计算移动平均线

代码输出的数据截图如下:

现在想到的之后几期会讲的内容:
【量化小讲堂 – python & pandas技巧系列】如何在windows环境安装python和pandas
【量化小讲堂 – python & pandas技巧系列】如何在mac OSX环境安装python和pandas
【量化小讲堂 – python & pandas技巧系列】使用python计算KDJ指标
【量化小讲堂 – python & pandas技巧系列】使用python计算MACD指标
【量化小讲堂 – 投资策略系列】KDJ、MACD指标双金叉选股效果
【量化小讲堂 – python & pandas技巧系列】使用pytho将日线数据转换成周线、月线数据