python 读取csv文件 日k显示 最多700条

# coding=utf-8
#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator,DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc

import matplotlib
import datetime
import csv

# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
#date1 = (2004, 2, 3)
#date2 = (2004, 4, 12)




quotes = []
'''
#quotes = quotes_historical_yahoo_ohlc('INTC', date1, date2)

#quotes = [(731613.0, 21, 21.9, 21.41, 21.49), (731614.0, 21.58059381723535, 22.282458652866239, 21.495518496815286, 22.261189999999999, 62375400.0)]
#quotes = [(matplotlib.dates.date2num(datetime.datetime(2004, 02, 03)),32,43,23,33),(matplotlib.dates.date2num(datetime.datetime(2004, 02, 04)),32,43,23,36),(matplotlib.dates.date2num(datetime.datetime(2004, 02, 07)),52,45,23,33) ,(matplotlib.dates.date2num(datetime.datetime(2004, 02, 8)),52,45,23,33) ]
quotes.append((matplotlib.dates.date2num(datetime.datetime(2004, 2, 3)),32,43,23,33))
quotes.append((matplotlib.dates.date2num(datetime.datetime(2004, 2, 04)),42,65,33,43))
quotes.append((matplotlib.dates.date2num(datetime.datetime(2004, 2, 5)),33.0,63,23,53))
quotes.append((matplotlib.dates.date2num(datetime.datetime(2004, 2, 6)),34.34,43.343,23.34,33.34))
quotes.append((matplotlib.dates.date2num(datetime.datetime(2004, 2, kk)),32.34,43.34,23.3,33.3))
'''
def day_change(str):
    day = datetime.datetime.strptime(str, "%Y-%m-%d")
    return day
def read_date(quotes):
    reader = csv.reader(file('1000002.csv','rb'))
    for line in reader:
        #忽略第一行
        if reader.line_num == 1:
            continue
        day = day_change(line[0])
        quotes.append((matplotlib.dates.date2num(datetime.datetime(int(day.strftime('%Y')), int(day.strftime('%m')), int(day.strftime('%d')))), float(line[1]), float(line[2]), float(line[3]), float(line[4])))
        #print line,line[0],line[1]
        if reader.line_num == 700:
            break
def show_k_ri(quotes):
    mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
    alldays = DayLocator()              # minor ticks on the days
    weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
    dayFormatter = DateFormatter('%d')      # e.g., 12

    #print quotes
    if len(quotes) == 0:
        raise SystemExit

    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2)
    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(weekFormatter)
    ax.xaxis.set_minor_formatter(dayFormatter)

    #plot_day_summary(ax, quotes, ticksize=3)
    candlestick_ohlc(ax, quotes, width=0.6)

    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

    plt.show()

if __name__ == '__main__':
    read_date(quotes)
    show_k_ri(quotes)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值