def __init__(self, prgname, meigaras=[]): ''' Constructor ''' rootd = f.get_json_data(prgname) d = rootd["play_fields"] self.chart_prg = d["prg_name"] self.ml_algorithm = d["ml_algorithm"] startd = d["startd"] endd = d["endd"] self.table_name = d["table"] #self.sd_table = d["sd_table"] self.meigaras = meigaras self.startd = startd self.endd = endd tbl.create_table_from_tmpl("pf", [self.table_name]) #if self.sd_table != "": # tbl.create_table_from_tmpl("sd", [self.sd_table]) #self.kabuka_generator = kf.g_get_kabuka(meigaras, startd, endd) classifier = None if self.ml_algorithm == "mini_batch_kmeans": self.classifier = kmeans.KMeans(self.chart_prg, meigaras)
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 train(self): f.log("Start training..") self._set_params() # Generate coaches for each play fields f.log("Getting coaches") self._get_coaches(self.pf_table) # Create table if not exists tbl.create_table_from_tmpl(self.tp_template, [self.tp_table]) # Train f.log("Training with GA") self._train_proc() # Insert report into table f.log("Creating report") self._report() f.log("Finished training..")
def create_pf(prgname, meigaras=[]): f.log("Start creating Play Fields..") params = f.get_json_data("ml_params") rootd = f.get_json_data(prgname) r = params["num_of_meigaras_per_octproc"] d = rootd["play_fields"] jsonpath = "%s/%s.json" % (JSON_DIR, prgname) startd = d["startd"] endd = d["endd"] table_name = d["table"] tmpl = d["table_template"] if len(meigaras) == 0: meigaras = kf.get_meigaras() if tmpl != "": tbl.create_table_from_tmpl(tmpl, [table_name]) i = 0 while i < len(meigaras): f.log("Processing from %d to %d.." % (i,i+r-1)) submeigaras = meigaras[i:i+r] outfile = "%s/%s_data.csv" % (TMP_DIR, prgname) f.log("Outputting file " + outfile) kf.kabuka2csv(outfile, "w", submeigaras, startd, endd) oc = octave.get_octave() f.log("Making %s data(octave)" % (prgname)) f.log("oc.ml_kabuka(\'%s\',\'%s\')" % (jsonpath, outfile)) impfile = oc.ml_kabuka(jsonpath, outfile) f.log("Importing from file %s to table %s" % (impfile, table_name)) tbl.imp_csv2table(impfile, "trade.%s" % table_name) i += r f.log("Finished creating Play Fields..")
def train(prgname, tm_obj, meigaras_arg=[]): global tm global meigaras tm = tm_obj meigaras = meigaras_arg f.log("Start training..") _set_params(prgname) # Generate coaches for each play fields f.log("Getting coaches") _get_coaches(pf_table) # Create table if not exists tbl.create_table_from_tmpl(tp_template, [tp_table]) # Train f.log("Training with GA") _train_proc() # Insert report into table f.log("Creating report") _report() f.log("Finished training..")
def _run_cicle(self, startd, endd, tp_name="", geneid=""): self._init() kls = {} meigaras = self.meigaras if meigaras == []: meigaras = kf.get_meigaras() for code in meigaras: kl = KabukaLines(code, startd, endd) kls[code] = kl self.kls = kls f.log("Start training from %s to %s" % (startd, endd)) #self._set_params() # Generate coaches for each play fields f.log("Getting coaches") self._get_coaches(self.pf_table, startd, endd) # Create table if not exists if tp_name != "": tp_table = "%s_%s" % (self.tp_table, tp_name) else: tp_table = self.tp_table tbl.create_table_from_tmpl(self.tp_template, [tp_table]) # Train f.log("Entering train_proc") if tp_name == "": self._train_proc() else: self._calc_score_proc(geneid) # Insert report into table f.log("Creating report") self._report(tp_name) f.log("Finished training from %s to %s" % (startd, endd))
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)