def __init__(self, startd, endd, meigaras=[]): ''' Constructor ''' if len(meigaras) == 0: self.meigaras = kf.get_meigaras() for code in meigaras: kl = KabukaLines(code, startd, endd) lt = LineTrader(kl) self.traders.append(lt)
def __init__(self, startd, endd, meigaras=[]): ''' Constructor ''' self.startd = startd self.endd = endd if meigaras == []: self.meigaras = kf.get_meigaras() else: self.meigaras = meigaras self.interest_total = 0.0 self.trade_cnt = 0 self.win_trade_cnt = 0 self.lose_trade_cnt = 0
def do_test(startd, endd, meigaras=[]): interval = 20 if len(meigaras) == 0: meigaras = kf.get_meigaras() f.log("Start making reports") report = [] for code in meigaras: f.log("Processing meigara:%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 tmp_endd = dates[-1] i = 60 old_from_date = "" while i < len(dates)-interval: if lt.judge_trade_goodness(i) == False: i += interval continue tmp_startd = dates[i] tmp_next_startd = dates[i+interval] (trade_mode_str, interest, from_date, to_date, spent, start_price, end_price, endi) \ = lt.test(tmp_startd, tmp_endd, tmp_next_startd) if old_from_date != from_date: if trade_mode_str != "": report.append([code, trade_mode_str, interest, from_date, to_date, spent, start_price, end_price]) old_from_date = from_date if endi > i: i = endi else: i += interval f.arr2csv("%s/report.csv" % (TMP_DIR), report) f.log("Finished making reports:%s/report.csv" % (TMP_DIR))
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 _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)