您的位置:首页 > 编程语言 > Python开发

python 选择股票

2016-12-16 16:45 260 查看
#! /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())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: