# 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)
python 读取csv文件 日k显示 最多700条
最新推荐文章于 2023-12-08 15:40:24 发布