python 简约策略

#!/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())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值