python股票历史最低点_Python统计某一只股票每天的开盘,收盘,最高,最低价格!...
模塊:Numpy
碼字不易,轉載請注明出處!十分感謝!
準備工作:
抓取某一只股票的信息,每30min為一組數據,可以參考上一篇:
Note: 只為演示如何統計,更精準的可以抓取每5min為一組數據
PS:如有需要Python學習資料的小伙伴可以加點擊下方鏈接自行獲取
目標:輸出每天的開盤,收盤,最高,最低價格,以其中的某一周(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')
歡迎大家一起討論學習。
總結
以上是生活随笔為你收集整理的python股票历史最低点_Python统计某一只股票每天的开盘,收盘,最高,最低价格!...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查询去除空值_SQL数据处理(五):SQ
- 下一篇: python substr函数_Sql