def calc(code, time_day): if time_day == None: data = _init_all(code) if data != None and len(data) > 0: db_helper.batch_insert(data) return None else: return _init_day(code, time_day)
def init_data(file_name): f = open(file_name) df = pd.read_csv(f) data = df.iloc[0:, :].values data_list = [] for i in range(len(data)): data_list.append(pack_data(data[i])) db_helper.batch_insert(data_list) print('导入数据%d条' % len(data_list))
def init_kpi(kpi_name, time_day): # print('启动线程初始化【%s】' % kpi_name) data_list = [] while 1: if q.empty(): if len(data_list) > 0: db_helper.batch_insert(data_list) # print('线程结束,完成[%d]条' % len(data_list)) break code = q.get() if code == None: if len(data_list) > 0: db_helper.batch_insert(data_list) # print('任务结束') break start_ts = datetime.datetime.now() data = None try: if kpi_name == 'ma': data = ma.calc(code, time_day) elif kpi_name == 'macd': data = macd.calc(code, time_day) elif kpi_name == 'trix': data = trix.calc(code, time_day) elif kpi_name == 'kdj': data = kdj.calc(code, time_day) elif kpi_name == 'dmi': data = dmi.calc(code, time_day) elif kpi_name == 'arbr': data = arbr.calc(code, time_day) elif kpi_name == 'emv': data = emv.calc(code, time_day) elif kpi_name == 'boll': data = boll.calc(code, time_day) elif kpi_name == 'rsi': data = rsi.calc(code, time_day) elif kpi_name == 'bias': data = bias.calc(code, time_day) elif kpi_name == 'roc': data = roc.calc(code, time_day) elif kpi_name == 'wr': data = wr.calc(code, time_day) elif kpi_name == 'dma': data = dma.calc(code, time_day) elif kpi_name == 'cr': data = cr.calc(code, time_day) end_ts = datetime.datetime.now() progress.log(progress.total - q.qsize()) if time_day == None: print('完成初始化【%s】\t%s\t%ds\t剩余%d' % (kpi_name, code, (end_ts - start_ts).seconds, q.qsize())) if data != None: data_list.append(data) except Exception as e: print('计算失败【%s】\t%s\t%s' % (kpi_name, code, time_day)) print(e)
def calc(code, time_day): """ time_date is None时初始化全量 """ if time_day == None: data = _init_all(code) if data != None and len(data) > 0: pass db_helper.batch_insert(data) return None else: return _init_day(code, time_day)
def calc(code, time_day): """ time_date is None时初始化全量 查询第一天的收盘价作为第一天的EMA12和EMA26 第二天的ema = 前一日EMA×11/13+今日收盘价×2/13 DIF=EMA12-EMA26 今日DEA(MACD)=前一日DEA×8/10+今日DIF×2/10,其首日DEA为0 BAR=2* (DIF-DEA) """ if time_day == None: data = _init_all(code) if data != None and len(data) > 0: db_helper.batch_insert(data) return None else: return _init_day(code, time_day)
def elect(day=None, max_result=20): if day == None: day = time.strftime("%Y-%m-%d", time.localtime()) code_list = db_helper.select(query_code_fn, (day, )) progress.setting(len(code_list), 5) for i in range(len(code_list)): code = code_list[i][0] q.put(code) thread_list = [] for i in range(20): t = threading.Thread(target=vote, args=(day, )) t.start() thread_list.append(t) for i in range(len(thread_list)): thread_list[i].join() sort_by = lambda elem: elem[1] rst = list(filter(lambda x: x[1] > 0, result)) rst.sort(key=sort_by, reverse=True) print('') max_result = np.min([len(rst), np.int(max_result)]) print('评估结果【正分%d,输出%d】:' % (len(rst), max_result)) out = PrettyTable(['股票代码', '评分', '特征']) for i in range(max_result): out.add_row([rst[i][0], rst[i][1], rst[i][2]]) print(out) entities = [] for i in range(len(rst)): e = Evaluation() e.code = rst[i][0] e.time_date = day e.score = rst[i][1] e.feature = rst[i][2] entities.append(e) db_helper.batch_insert(entities)
def save(self, data): db.batch_insert(data) pass