实现从csv文件导入数据并打印apple inc公司的股价蜡烛图

本文展示了如何从CSV文件中导入数据,并利用这些数据绘制Apple Inc.的股价蜡烛图。通过代码示例和效果展示,帮助读者理解数据导入及可视化过程。
摘要由CSDN通过智能技术生成

 一、先贴上代码

# -*- 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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值