一、先贴上代码
# -*- coding=utf-8 -*-
# using python 3# ******************************************* Purpose & Illustration ***********************************************
# 学习numpy的通用函数
# 实现从csv文件导入数据并打印apple inc公司的股价蜡烛图
# 绘制5,15,20日均线
# 绘制5日布林带
# 建立5日线性模型,最小二乘算法预测下一交易日股票值
# **********************************************************************************************************************import os
import sys
import csv
import numpy as np
import pandas as pd
import platform
import datetime as dt
import matplotlib.pyplot as mp
import matplotlib.dates as mddef dmy2ymd(dmy):return dt.datetime.strptime(str(dmy, encoding='utf-8'),'%d-%m-%Y').date().strftime('%Y-%m-%d') # str(dmy, encoding='utf-8') 此处涉及编码问题,不这么写会报错def dmy2days(dmy):return (dt.datetime.strptime(str(dmy, encoding='utf-8'),'%d-%m-%Y').date() - dt.date.min).daysdef read_data(filename):# loadtxt(文件名, 分隔符, usecols=(列索引表), unpack=True, dtype=元素类型, converters={列索引号:转换函数})dates, opening_prices, highest_prices, lowest_prices, closing_prices, volumes = np.loadtxt(filename, delimiter=',', usecols=(1,3,4,5,6,7), unpack=True,dtype=np.dtype('M8[D], f8, f8, f8, f8, f8'),converters={1: dmy2ymd}) # dtype=np.dtype('M8[D], f8, f8, f8, f8'), converters={1: dmy2ymd}# M8[D] 代表8个字节类型的日期,[D]代表只到日,不到具体分秒,{1: dmy2ymd} 代表将第1列用dmy2ymd函数进行转换# print(dates)# print(np.shape(dates))# print(opening_prices)# print(highest_prices)# print(lowest_prices)# print(closing_prices)return dates, opening_prices, highest_prices, lowest_prices, closing_prices, volumesdef read_data1(filename):# loadtxt(文件名, 分隔符, usecols=(列索引表), unpack=True, dtype=元素类型, converters={列索引号:转换函数})days, opening_prices, highest_prices, lowest_prices, closing_prices, volumes = np.loadtxt(filename, delimiter=',', usecols=(1,3,4,5,6,7), unpack=True,converters={1: dmy2days}) # dtype=np.dtype('M8[D], f8, f8, f8, f8'), converters={1: dmy2ymd}# M8[D] 代表8个字节类型的日期,[D]代表只到日,不到具体分秒,{1: dmy2ymd} 代表将第1列用dmy2ymd函数进行转换# print(dates)# print(np.shape(dates))# print(opening_prices)# print(highest_prices)# print(lowest_prices)# print(closing_prices)return days, opening_prices, highest_prices, lowest_prices, closing_prices, volumesdef read_data2(filename):# loadtxt(文件名, 分隔符, usecols=(列索引表), unpack=True, dtype=元素类型, converters={列索引号:转换函数})weekdays, opening_prices, highest_prices, lowest_prices, closing_prices, volumes = np.loadtxt(filename, delimiter=',', usecols=(1,3,4,5,6,7), unpack=True,converters={1: dmy2weekday}) # dtype=np.dtype('M8[D], f8, f8, f8, f8'), converters={1: dmy2ymd}# M8[D] 代表8个字节类型的日期,[D]代表只到日,不到具体分秒,{1: dmy2ymd} 代表将第1列用dmy2ymd函数进行转换return weekdays, opening_prices, highest_prices, lowest_prices, closing_prices, volumesdef init_chart(first_dat, last_dat):mp.gcf().set_s(3)*240/255)mp.title('Candlestick Chart', fontsize=20)mp.xlabel('Trading Days From %s To %s' % (first_dat.astype(md.datetime.datetime).strftime('%d %b %Y'),last_dat.astype(md.datetime.datetime).strftime('%d %b %Y')), fontsize=14)mp.ylabel('Stock Price (USD) Of Apple Inc.', fontsize=14)ax = mp.gca()ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))ax.xaxis.set_minor_locator(md.DayLocator())ax.xaxis.set_major_formatter(md.DateFormatter('%d %b %Y'))mp.t
本文发布于:2024-01-31 23:27:26,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671484632166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |