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())
相关文章推荐
- python 随手记
- Python数据分析(八)-xrange,range,arange与np.where
- Python 相关包安装pip
- 【我拼搏的2016】技术与转型并存
- 用Python写a穿过#
- 我的2016学习之路
- caffe学习笔记(六)--ipython notebook安装
- python数字图像处理(4):图像数据类型及颜色空间转换
- python数字图像处理(3):图像像素的访问与裁剪
- python数字图像处理(2):图像的读取、显示与保存
- python数字图像处理(1):环境安装与配置
- pandas中groupby和pivot_table(数据透视表)
- python中协程
- python下cv2.imread()返回NoneType错误的解决
- Python读写mat文件
- 如何在win 下安装ipython notebook
- PIL 安装与配置
- 素数的实现--python
- 【我拼搏的2016】python 学习之路
- 【python】闭包、@修饰符(装饰器)、