一、先贴上代码
# -*- 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 md
def 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).days
def 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, volumes
def 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, volumes
def 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, volumes
def init_chart(first_dat, last_dat):
mp.gcf().set_facecolor(np.ones(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