Python股票历史涨跌幅数据获取
股票涨跌幅数据是量化投资学习的基本数据资料之一,下面以Python代码编程为工具,获得所需要的历史数据。主要步骤有:
(1) #按照市值从小到大的顺序活得N支股票的代码;
(2) #分别对这一百只股票进行100支股票操作;
(3) #获取从2016.05.01到2016.11.17的涨跌幅数据;
(4) #选取记录大于40个的数据,去除次新股;
(5) #将文件名名为“股票代码.csv”。
具体代码如下:
# -*- coding: utf-8 -*- """ Created on Thu Nov 17 23:04:33 2016 获取股票的历史涨跌幅,并分别存为csv格式 @author: yehxqq151376026 """ import numpy as np import pandas as pd #按照市值从小到大的顺序活得100支股票的代码 df = get_fundamentals ( query (fundamentals. eod_derivative_indicator. market_cap ) . order_by (fundamentals. eod_derivative_indicator. market_cap. asc ( ) ) . limit ( 100 ) , '2016-11-17' , '1y' ) #分别对这一百只股票进行100支股票操作 #获取从2016.05.01到2016.11.17的涨跌幅数据 #选取记录大于40个的数据,去除次新股 #将文件名名为“股票代码.csv” for stock in range ( 100 ): priceChangeRate = get_price_change_rate (df [ 'market_cap' ]. columns [stock ] , '20160501' , '20161117' ) if priceChangeRate is None: openDays = 0 else: openDays = len (priceChangeRate ) if openDays > 40: tempPrice = priceChangeRate [ 39: (openDays - 1 ) ] for rate in range ( len (tempPrice ) ): tempPrice [rate ] = "%.3f" %tempPrice [rate ] fileName = '' fileName = fileName. join (df [ 'market_cap' ]. columns [i ]. split ( '.' ) ) + '.csv' fileName tempPrice. to_csv (fileName )