python 读取csv 简单策略

来源:互联网 发布:端口被屏蔽怎么办 编辑:程序博客网 时间:2024/06/15 09:38
#!/usr/bin/env python# coding: utf-8import datetimeimport csvimport sys#from datetime import datetime#from datetime import timedeltaglobal stocksglobal k_riglobal list_codeglobal conn_list, conn_kquotes = []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_buy3(open, high, low, close, close_old, date):    global g_flg_rise, g_high_max, g_flg_buy, g_days, g_buy    if (g_high_max <= high):        g_high_max = high    if (0.7 > close / g_high_max):        g_buy = open + 0.1        g_flg_rise = 0        print "buy", date        return 1    return 0def plot_buy2(open, high, low, close, close_old, date):    global g_flg_rise, g_high_max, g_flg_buy, g_days, g_buy    rise = judge_rise(close_old)    if (close == rise):        g_flg_rise += 1        g_high_max = high    else:        if (2 < g_flg_rise):            if (g_high_max <= high):                g_high_max = high            g_buy = open + 0.1            g_flg_rise = 0            #print "buy"            return 1        else:            g_flg_rise = 0    return 0def plot_buy(open, high, low, close, close_old, date):    global g_flg_rise, g_high_max, g_flg_buy, g_days, g_buy    if (0 == g_flg_buy):        rise = judge_rise(close_old)        if (close == rise):            g_flg_rise += 1            g_high_max = high        else:            if (2 < g_flg_rise):                if (g_high_max <= high):                    g_high_max = high                    #print "read buy"                else:                    g_flg_buy = 1                    g_days = 10            else:                g_flg_rise = 0        #print g_flg_buy, g_days    else:        if (0 >= g_days):            #print "g_days die"            g_flg_rise, g_flg_buy, = 0, 0        if (g_high_max < high):            g_buy = g_high_max            g_flg_rise, g_flg_buy, = 0, 0            #print "buy"            return 1        g_days -= 1        #print g_flg_buy, g_days,"g_buy"    return 0def plot_sale(open, high, low, close, close_old, date):    global g_high_max, g_cnt_win, g_cnt_lost, g_buy    global g_total    if (high == low):        return 1    if (g_high_max <= high):        g_high_max = high    if (0.9 >= close / g_buy):        total_old = g_total        g_total = total_old * close / g_buy        print g_buy, g_high_max, close, total_old, g_total, date, "lost----------------------------------------------------------"        g_cnt_lost += 1        return 2    else:        gain = g_buy / close        if (1.3 > gain):            line = 0.8        elif (1.2 > gain):            line = 0.85        else:            line = 0.9        if (line >= close / g_high_max or 1.05 > gain or 2 > close - g_buy):            total_old = g_total            g_total = total_old * close / g_buy            print g_buy, g_high_max, close, total_old, g_total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++"            g_cnt_win += 1            return 2    return 1def plot_sale2(open, high, low, close, close_old, date):    global g_high_max, g_cnt_win, g_cnt_lost, g_buy    global g_total    if (1.2 < high / g_buy):        total_old = g_total        g_total = total_old * close /g_buy        print g_buy, g_high_max, close, total_old, g_total, date, "win ++++++++++++++++++++++++++++++++++++++++++++++++++++++"        g_cnt_win += 1        return 2    return 1dict_g_buy_sale_plot = {"plot_buy":plot_buy, "plot_buy2":plot_buy2, "plot_sale":plot_sale, "plot_buy3":plot_buy3, "plot_sale2":plot_sale2}def calculate_g_buy_sale(open, high, low, close, close_old, date, symbol):    return dict_g_buy_sale_plot.get(symbol)(open, high, low, close, close_old, date)    global g_flg_rise, g_high_max, g_flg_buy, g_days, g_cnt_win, g_cnt_lost, g_buyglobal g_totaldef plot(quotes):    global g_flg_rise, g_high_max, g_flg_buy, g_days, g_cnt_win, g_cnt_lost, g_buy    g_flg_rise, g_high_max, g_flg_buy, g_days, g_cnt_win, g_cnt_lost, g_buy= 0, 0, 0, 0, 0, 0, 0    global g_total    g_total = 100    date_old, open_old, high_old, low_old, close_old = 0, 0, 0, 0, 0    flg_buy = 0    for q in quotes:        date, open, high, low, close = q[0], float(q[1]), float(q[2]), float(q[3]), float(q[4])        #print flg_buy        if (0 == flg_buy):            flg_buy = calculate_g_buy_sale(open, high, low, close, close_old, date, "plot_buy3")        elif (1 == flg_buy):            flg_buy = calculate_g_buy_sale(open, high, low, close, close_old, date, "plot_sale2")        if (2 == flg_buy):            g_flg_rise, g_flg_buy, = 0, 0            flg_buy = 0        date_old, open_old, high_old, low_old, close_old = date, open, high, low, close    print g_total, g_cnt_win, g_cnt_lost    return g_totaldef plot2(quotes):    print "222"calculateDict = {"plot":plot, "plot2":plot2}def calculate(x, symbol):    calculateDict.get(symbol)(x)def main(argv=None):    read_file('csv_test600149.csv', quotes)    if (0 == len(quotes)):        return    calculate(quotes, "plot")if __name__ == '__main__':    sys.exit(main())
0 0