2017年11月5日 星期日 晴

最近也不知道干啥,然后在网上瞎看,研究了一下tushare,又看了看别人根据MA20买入卖出回测的例子,于是我也写了一个感受一下。

写程序真的也不算啥,关键是要有策略,我没有策略,还得继续研究。下面程序的主要思想是当天是涨的,然后成交量大于前面N天一定比例,如果没买过的,就可以买入;当天是跌的,然后成交量大于前面N天一定比例,如果有持有的,需要卖出。最后算一下能赚多少钱,选出一个最大比例和最小比例。(程序是瞎写的,真实肯定用不了,只是练习)

[code] #! /usr/bin/env python #coding=utf-8 #author = “yyobin@gmail.com

import tushare as ts

def parse(stock, start_date,end_date): print ‘—parse()—’ df = ts.get_h_data(stock,start=start_date,end=end_date,autype=‘qfq’, index=False, retry_count=3, pause=0.001, drop_factor=True ) df = df[::-1] open = df[u’open’] close = df[u’close’] volume = df[u’volume’] amount = df[u’amount’] high = df[u’high’] low = df[u’low’]

total  = len(amount)
print 'total = %d' % (total)
maxrate = 1.0
max_buybili  = 1.0
max_sellbili = 1.0
minrate = 1.0
min_buybili  = 1.0
min_sellbili = 1.0


BUY_BILI  = 1.0 #系数应该通过如阶梯法这样的逼近求解
for offset in xrange(50):
    BUY_BILI += 0.1
    SELL_BILI = 1.0 #系数应该通过如阶梯法这样的逼近求解
    for offset2 in xrange(50):
        SELL_BILI += 0.1
        is_buy    = 0
        buy_val   = []
        buy_date  = []
        sell_val  = []
        sell_date = []
        rate = 1.0
        CYCLE = 30

        AMOUNT7 = []
        VOL7    = []
        AV7     = []
        idx    = 0

        while idx < total:
            #处理VOL7
            count = idx
            loop  = 0
            tmp_amount = 0.0
            tmp_vol    = 0.0
            while count >= 0 and loop < CYCLE:
                tmp_amount += float(amount[count])
                tmp_vol    += float(volume[count])
                count -= 1
                loop  += 1
            am7 = float('%.2f' % (tmp_amount / loop))
            AMOUNT7.append(am7)
            vol7 = float('%.2f' % (tmp_vol / loop))
            VOL7.append(vol7)
            av7 = float('%.2f' % (am7 / vol7))
            AV7.append(av7)

            close_val = close[idx]

            if idx == 0:
                idx += 1
                continue

            if av7 > open[idx]:#如果上涨
                if float(amount[idx]) / AMOUNT7[idx-1]  >= BUY_BILI:
                    if is_buy == 0:
                        is_buy = 1
                        buy_val.append(close[idx])
                        buy_date.append(close.keys()[idx])
            elif av7 < open[idx]:
                if float(amount[idx]) / AMOUNT7[idx-1]  >= SELL_BILI:
                    if is_buy == 1:
                        is_buy = 0
                        sell_val.append(close[idx])
                        sell_date.append(close.keys()[idx])
            idx += 1
        print ("stock number: %s" %stock)
        print ("buy count   : %d" %len(buy_val))
        print ("sell count  : %d" %len(sell_val) )

        loop = 0
        for i in range(len(sell_val)):
            rate = rate * (sell_val[i] * (1 - 0.002) / buy_val[i])
            print ("buy date : %s, buy price : %.2f" %(buy_date[i], buy_val[i]))
            print ("sell date: %s, sell price: %.2f" %(sell_date[i], sell_val[i]))
            loop = i

        if len(buy_date) > len(sell_date):
            print ("buy date : %s, buy price : %.2f" %(buy_date[loop], buy_val[loop]))

        print ("BUY_BILI=%.2f,SELL_BILI=%.2f,rate: %.2f" % (BUY_BILI,SELL_BILI,rate))
        print ("min_buybili=%.2f,min_sellbili=%.2f,rate: %.2f" % (min_buybili,min_sellbili,minrate))
        print ("max_buybili=%.2f,max_sellbili=%.2f,rate: %.2f" % (max_buybili,max_sellbili,maxrate))

        if rate > maxrate:
            maxrate = rate
            max_buybili = BUY_BILI
            max_sellbili = SELL_BILI
        if rate < minrate:
            minrate = rate
            min_buybili = BUY_BILI
            min_sellbili = SELL_BILI
print ("min_buybili=%.2f,min_sellbili=%.2f,rate: %.2f" % (min_buybili,min_sellbili,minrate))
print ("max_buybili=%.2f,max_sellbili=%.2f,rate: %.2f" % (max_buybili,max_sellbili,maxrate))

if name == ‘main’: stock_pool = [‘600406’] start_date_list = [‘2015-11-02’,] end_date = ‘2017-11-02’ for stock in stock_pool: for start_date in start_date_list: parse(stock, start_date,end_date) [/code]