python 选择股票

#! /usr/bin/python2
# coding=utf-8
import sys
from datetime import datetime
from datetime import timedelta



def txt_read(file_name):
    file_object = open(file_name, 'r')
    ll = []
    try:
        i = 0
        for line in file_object:
            if (0 == i):
                i += 1
                continue
            i += 1
            #print line
            ll.append(line)
    finally:
         file_object.close()
    print file_name, len(ll)
    return ll

def list_to_dict_total(list):
    ll = []
    for line in list:
        row_s = line.split(" ")
        # for row_s in split_row:  ,
        ll.append(dict(code=row_s[0], total=float(row_s[1]), cnt_win=int(row_s[2]), days_diff=int(row_s[3]), days_diff_ave=int(row_s[4]), date_buy=row_s[5], price_buy=float(row_s[6]), close=float(row_s[7]),
                 price_sale_read=float(row_s[8]), rate=float(row_s[9]), high_max=float(row_s[10]), date_sale=row_s[11], price_sale=float(row_s[12]),
                 date_buy_read=row_s[13], price_buy_read=float(row_s[14]), cnt_lost=int(row_s[15]), quote_change=float(row_s[16]), date=row_s[17]))
    return ll

def list_to_dict_days(list):
    ll = []
    for line in list:
        row_s = line.split(" ")
        # for row_s in split_row:
        ll.append(dict(code=row_s[0], day_diff=int(row_s[1]), total=float(row_s[2]), date_buy=row_s[3], price_buy=float(row_s[4]), date_sale=row_s[5], price_sale=float(row_s[6]), rate=float(row_s[7]), cnt_win=int(row_s[8]), high_max=float(row_s[9])))
    return ll

def day_before(day):
    today = datetime.today()
    # day = datetime.strptime(str, "%Y-%m-%d")
    day_diff = timedelta(days=day)
    day_new = today - day_diff
    return day_new.strftime("%Y-%m-%d")

def days_find(code, days):
    ll = []
    for i in days:
        if (code == i['code'] and day_before(365*2) < i['date_buy']):
            ll.append(i)

    return ll

#sorted_x = sorted(ll2, key=lambda x: (x['name'], -x['age']), reverse=True)
def buy_choice(dl_buy, dl_days):
    ll = []
    for i in dl_buy:
        if (9 < i['cnt_win'] and i['close'] < i['price_buy'] and day_before(365) < i['date_buy'] and day_before(365*2) < i['date_sale']):
            ll2 = days_find(i['code'], dl_days)
            if (3 > len(ll2)):
                continue
            #print "ll2",ll2
            cnt = 0
            ave = 0
            for l2 in ll2:
                cnt += l2['day_diff']
            print cnt,"cnt", len(l2)
            ave=  cnt /  len(l2)
            #print cnt,ave
            if (100 > ave):
                ll.append(i)
    #print len(ll),ll

    for l in ll:
        print l



def main(argv=None):
    ll = txt_read('total.txt')
    dl_total = list_to_dict_total(ll)
    ll1 = txt_read('buy.txt')
    dl_buy = list_to_dict_total(ll1)
    ll = txt_read('buy_read.txt')
    dl_buy_read = list_to_dict_total(ll)
    ll = txt_read('days.txt')
    dl_days = list_to_dict_days(ll)
    #day_before(1)
    # print len(dl_total), len(dl_buy),len(dl_buy_read),len(dl_days)
    # for l in dl_days:
    #    print l
    buy_choice(dl_buy, dl_days)

if __name__ == '__main__':
    sys.exit(main())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值