#!/usr/bin/env python # coding: utf-8 import datetime import csv import sys #from datetime import datetime #from datetime import timedelta global stocks global k_ri global list_code global conn_list, conn_k quotes = [] def read_file(name, quotes): try: csvfile = file(name, 'rb') reader = csv.reader(csvfile) for line in reader: try: # 忽略第一行 if reader.line_num == 1: continue # list_code.append(line[0]) # print(' said: ', '') i = 0 #print line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4] #deal_data(line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4]) quotes.append((line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4])) except ValueError: pass #print "name --", name, locals() # csvfile.close() except IOError as err: # 使用as将异常对象,并将其赋值给一个标识符 print('File Error:' + str(err)) # ‘+’用于字符串直接的连接 finally: if 'csvfile' in locals(): csvfile.close() print "close" def judge_rise(price): #print price if (0 == price): return 0.00 price *= 1.1 price += 0.005 return float('{:.2f}'.format(price)) def plot(quotes): date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0 flg_rise, high_max, flg_buy, days, cnt_win, cnt_lost = 0, 0, 0, 0, 0, 0 total = 100 for q in quotes: date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4]) rise = judge_rise(close_old) #print rise, close, close_old if (0 == flg_buy): if (close == rise): print "hi", rise, close, close_old, date flg_rise += 1 high_max = high else: if (2 < flg_rise): if (high_max <= high): high_max = high print "3 dan" else: flg_buy = 1 days = 10 else: flg_rise = 0 elif (1 == flg_buy): if (0 >= days): print "days die" flg_rise, flg_buy, = 0, 0 if (high_max < high): buy = high_max + 0.01 flg_buy = 2 print "buy" days -= 1 elif (2 == flg_buy): if (high_max <= high): high_max = high if (0.9 >= close / buy): total = total * close / buy flg_buy = 0 cnt_win += 1 else: if (0.9 >= close / high_max): total = total * close / buy flg_buy = 0 cnt_lost += 1 else: flg_rise, flg_buy, = 0, 0 date_old, open_old, high_old, low_old, close_old = date, open, high, low, close print total, cnt_win, cnt_lost #print date, open, high, low, close def main(argv=None): read_file('csv_test2.csv', quotes) if (0 == len(quotes)): return plot(quotes) if __name__ == '__main__': sys.exit(main())