python3.7 统计某一只股票每天的开盘,收盘,最高,最低价格

模块:Numpy

码字不易,转载请注明出处!十分感谢!

准备工作:
抓取某一只股票的信息,每30min为一组数据,可以参考上一篇:

Note: 只为演示如何统计,更精准的可以抓取每5min为一组数据

如何获取股票原始数据

目标:输出每天的开盘,收盘,最高,最低价格,以其中的某一周(5 days, 40组数据)为例

1, 从csv中导入数据,需要有 open, high, close 和low

#import numpy as np
open = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 2, delimiter=',', encoding = 'utf-8')
high = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 3, delimiter=',', encoding = 'utf-8')
close = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 4, delimiter=',', encoding = 'utf-8')
low = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 5, delimiter=',', encoding = 'utf-8')

打印测试可以得到如下数据,

2,从csv中导入date数据,需要将date转化输出成工作日的格式

from datetime import datetime
def datestr(s):
    return datetime.strptime(s, '%Y/%m/%d').isoweekday()
#print(datestr('2020/12/7'))

dates = np.loadtxt('30min.csv',dtype = str, skiprows = 1, usecols = 0, converters = { 0: datestr}, delimiter = ',', encoding = 'utf-8')

打印测试可以得到如下数据,

3, 找到某一周的40组数据,确认第一天的开盘时间和最后一天的收盘时间

close = close[0:40]
dates = dates[0:40]
first_monday = np.ravel(np.where(dates == 1))[-1] #根据csv中的排序找到某一周的第一天
last_friday = np.ravel(np.where(dates == 5))[0] #根据csv中的排序找到某一周的最后一天

打印测试得到如下某一周的40个时间

4, 创建一个数组,用于存储一周内每一天的索引值

day_indices = np.arange(last_friday, first_monday +1)[::-1]
weeks_indices = np.split(day_indices, 5)

打印测试得到如下输出,

5,编写summarize函数,返回一个元组包含这一周每天对应的open, close, high, low

def summarize(a, o, h, c, l):
    monday_open = o[a[0]]  #monday open是最后一个价格
    day_high = np.max( np.take(h, a) )  #每天的最高价格
    day_low = np.min( np.take(l, a) )	#每天的最低价格
    friday_close = c[a[-1]]  #friday close是第一个价格
    return('lux', monday_open, day_high, friday_close, day_low)

6,生成每周的数据

weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, close, low)
print(' ****** open, high, close, low \n', weeksummary)

对比一下表格中的数据信息,结果是匹配的

7, 如果有需要可以保存

np.savetxt('cw36_lux.csv', weeksummary, delimiter = ',', fmt = '%s')	 #同30min.csv在同一文件夹下

打开csv之后保存的数据如下

好了,完整的代码如下:

import numpy as np
from datetime import datetime

def datestr(s):
    return datetime.strptime(s, '%Y/%m/%d').isoweekday()

dates, open, high, close, low = np.loadtxt('30min.csv', skiprows = 1, usecols = (0, 2, 3, 4, 5), converters = { 0:datestr}, delimiter = ',', unpack = True, encoding = 'utf-8')
close = close[0:40]
dates = dates[0:40]
#print(dates)

first_monday = np.ravel(np.where(dates == 1))[-1]
#print(first_monday)
last_friday = np.ravel(np.where(dates == 5))[0]
#print(last_friday)

day_indices = np.arange(last_friday, first_monday +1)[::-1]
#print(day_indices)
weeks_indices = np.split(day_indices, 5)
#print(weeks_indices)

def summarize(a, o, h, c, l):
    monday_open = o[a[0]]
    day_high = np.max( np.take(h, a) )
    day_low = np.min( np.take(l, a) )
    friday_close = c[a[-1]]
    return('lux', monday_open, day_high, friday_close, day_low)

weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, close, low)
#print(' ****** open, high, close, low \n', weeksummary)

np.savetxt('cw36_lux.csv', weeksummary, delimiter = ',', fmt = '%s')

欢迎大家一起讨论学习。

本文地址:https://blog.csdn.net/SoftwarePM/article/details/110821752