def ins_top_meigaras(self, datestr, meigaras=[], ex_meigaras=[]): strsql = "truncate table trade.learned_memory2;" sql.exec_updsql(strsql) strsql = "select pf.code, pf.fieldid, tp.geneid, tp.score, \ tp.holding_days/tp.n_plays, pf.volume from trade.%s as pf " % (self.pf_table) strsql = strsql + "inner join trade.%s as tp on pf.fieldid = tp.fieldid " % self.tp_table wherelist = ["date = \'%s\'" % datestr] if int(self.min_cnt) > 0: wherelist.append("tp.fieldcnt>=\'%s\'" % self.min_cnt) if float(self.min_score) > 0: wherelist.append("tp.score>=\'%s\'" % self.min_score) wherelist.append("tp.holding_days>0") wherelist.append("n_win/(n_lose+n_win)>=0.5") strwhere = f.list2sqlwhere(wherelist) where_code_in = kf.where_code_in(meigaras) if strwhere == "": strwhere = " where " else: if where_code_in != "": strwhere = strwhere + " and " strsql = strsql + strwhere + kf.where_code_in(meigaras) strsql = strsql + " order by tp.score desc" strsql = strsql + " limit %s;" % self.top_fieldid_num data = sql.exec_selsql(strsql) for row in data: row.insert(0, self.prgname) if len(data) > 0: tbl.arr2table(data, "learned_memory2")
def predict2tmp(self): old_code = "" dl, ol, hl, ll, cl, vl = ([], [], [], [], [], []) kabuka_generator = kf.g_get_kabuka(self.meigaras, self.startd, self.endd) for line in kabuka_generator: (code, date, open, high, low, close, volume) = line if code != old_code and old_code != "": if self.chart_prg == "gen_bollinger_field": tpl = gen_pf_bollinger(dl, ol, hl, ll, cl, vl) if len(tpl) == 4: (d, X, Y) = tpl else: continue if len(X) > 0: labels = np.array(self.classifier.predict(X)) else: continue data = [] for i in range(len(d)): data.append([old_code, d[i], "", labels[i], Y[i][0], Y[i][1]]) if len(data) > 0: tbl.arr2table(data, self.table_name) data = [] dl, ol, hl, ll, cl, vl = ([], [], [], [], [], []) dl.append(date) ol.append(open) hl.append(high) ll.append(low) cl.append(close) vl.append(volume) old_code = code
def ins_top_meigaras(self, datestr, meigaras=[], ex_meigaras=[], choose_strictly=True): strsql = "select code from trade.dow " wherelist = ["date = \'%s\'" % datestr] if choose_strictly: if float(self.volsize_limit) > 0: wherelist.append("volsize>=\'%s\'" % self.volsize_limit) if self.use_dow == 1: if self.trade_mode == "BUY": wherelist.append("dow_sign=\'1'") if self.trade_mode == "SELL": wherelist.append("dow_sign=\'-1'") if float(self.price_moving_rate_limit) > 0: wherelist.append("mv_range_avg<=\'%s\'" % self.price_moving_rate_limit) if len(meigaras) > 0: wherelist.append(kf.where_code_in(meigaras)) if len(ex_meigaras) > 0: wherelist.append(kf.where_code_in(ex_meigaras, True)) strwhere = f.list2sqlwhere(wherelist) strsql = strsql + strwhere + ";" if strwhere != "": meigaras = sql.exec_selsql(strsql, 0) else: meigaras = [] strsql = "select pf.code, pf.fieldid, tp.geneid, tp.points, \ tp.holding_days, tp.trade_mode from trade.%s as pf " % (self.pf_table) strsql = strsql + "inner join trade.%s as tp on pf.fieldid = tp.fieldid " % self.tp_table wherelist = ["date = \'%s\'" % datestr] if int(self.min_cnt) > 0: wherelist.append("tp.fieldcnt>=\'%s\'" % self.min_cnt) if float(self.min_points) > 0: wherelist.append("tp.points>=\'%s\'" % self.min_points) wherelist.append("tp.trade_mode=\'%s\'" % self.trade_mode) strwhere = f.list2sqlwhere(wherelist) where_code_in = kf.where_code_in(meigaras) if strwhere == "": strwhere = " where " else: if where_code_in != "": strwhere = strwhere + " and " strsql = strsql + strwhere + kf.where_code_in(meigaras) strsql = strsql + " order by tp.points desc" strsql = strsql + " limit %s;" % self.top_fieldid_num data = sql.exec_selsql(strsql) for row in data: row.insert(0, self.prgname) if len(data) > 0: tbl.arr2table(data, "learned_memory")
def _report(): size = 0 data = [] for k in coaches.keys(): c = coaches[k] subdata = c.get_report() size += len(subdata) if size >= MAX_DB_INSERT_RECORDS: tbl.arr2table(data, tp_table) size = 0
def create_play_field(startd, endd, meigaras=[]): f.log("Starting to create play field") (mcode, dl, X, y, report) = gen_pf_line("20150601", "20160101", meigaras) mcode = np.array(mcode) dl = np.array(dl) X = np.array(X) tablename = "pf_line" tbl.create_table_from_tmpl("pf2", [tablename]) r = np.c_[mcode, dl, X] tbl.arr2table(r, tablename) f.log("Finished creating play field")
def _report(self): size = 0 data = [] cnt = 1 keys = self.coaches.keys() for k in keys: c = self.coaches[k] subdata = c.get_report() data.extend(subdata) if len(data) >= MAX_DB_INSERT_RECORDS or cnt >= len(keys): tbl.arr2table(data, self.tp_table) data = [] size = 0 cnt +=1
def _report(self, name=""): size = 0 data = [] cnt = 1 keys = self.coaches.keys() for k in keys: c = self.coaches[k] subdata = c.get_report() data.extend(subdata) if len(data) >= MAX_DB_INSERT_RECORDS or cnt >= len(keys): if name != "": tp_table = "%s_%s" % (self.tp_table, name) else: tp_table = self.tp_table tbl.arr2table(data, tp_table) data = [] size = 0 cnt +=1
def gen_pf_line(startd, endd, meigaras=[]): if len(meigaras) == 0: meigaras = kf.get_meigaras() params = f.get_json_data("ml_params") rootd = f.get_json_data("ml_line") d = rootd["play_fields"] table_name = d["table"] y_consider_span = d["y_consider_span"] losscut_rate = d["losscut_rate"] interest_rate = d["interest_rate"] tmpl = "pf2" if len(meigaras) == 0: meigaras = kf.get_meigaras() if tmpl != "": tbl.create_table_from_tmpl(tmpl, [table_name]) trade_mode_str = "" for code in meigaras: mcode = [] dl = [] vl = [] X = [] y = [] if DEBUG: print "Processing code:%s" % (code) kl = KabukaLines(code, startd, endd) lt = LineTrader(kl) kabuka = kl.get_kabuka() if len(kabuka) == 7: (indexes, dates, open, high, low, close, volume) = kabuka else: continue starti = 60 if len(dates) <= starti+1: continue code_startd = dates[starti] code_endd = dates[-1] #(trade_signs, trade_signs_d_idxs) = lt.get_trade_signs(code_startd, code_endd) passed_d_idxs = lt.get_passed_days(code_startd, code_endd) if DEBUG: passed_dates = [] for i in passed_d_idxs: passed_dates.append(dates[i]) j = -1 for i in passed_d_idxs: j+=1 if i < starti: continue if lt.judge_trade_goodness(i) == False: continue fieldid = lt.generate_fieldid(i) #if need_y: #(trade_mode_str, interest, from_date, to_date, spent, start_price, end_price, endi) \ #= lt.test(dates[i], dates[-1]) #y.append(interest) #report.append([trade_mode_str, from_date, to_date, spent, start_price, end_price]) datei = i start_price = open[datei] if start_price <= 0: continue min_close = min(close[datei:datei+y_consider_span]) max_close = max(close[datei:datei+y_consider_span]) h = max(high[datei:datei+y_consider_span]) l = min(low[datei:datei+y_consider_span]) min_close_rate = (min_close-start_price)*1.0/start_price max_close_rate = (max_close-start_price)*1.0/start_price h_rate = (h-start_price)*1.0/start_price l_rate = (l-start_price)*1.0/start_price sign = 0 if max_close_rate >= interest_rate and abs(l_rate) < losscut_rate: sign = int(max_close_rate*100) if abs(min_close_rate) >= interest_rate and abs(h_rate) < losscut_rate: sign = int(min_close_rate*100) mcode.append(code) dl.append(dates[i]) vl.append(np.mean(volume[i-5:i+1])) X.append(fieldid) y.append(sign) mcode = np.array(mcode) dl = np.array(dl) vl = np.array(vl) if len(dl) <= 0: continue X = np.array(X) y = np.array(y) r = np.c_[mcode, dl, X, vl, y] tbl.arr2table(r, table_name)