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 import2kdb(_datestr): ddate = dtf.datestr2fdate(_datestr) cnt = 0 while cnt < RETRY_CNT: try: response = urllib2.urlopen("http://k-db.com/stocks/" + ddate + "?download=csv") html = response.read() f = StringIO.StringIO(html) reader = csv.reader(f, delimiter=',') break except Exception as e: print "date:%s type:%s args:%s e:%s" % (_datestr, str(type(e)), str(e.args), str(e)) cnt += 1 if cnt >= RETRY_CNT: return strsql = "" i = 0 for row in reader: if i == 0: i += 1 continue #d = row[0] #d = d[0:4] + d[6:8] + d[10:12] #if d != _datestr: # i = i + 1 # return code = row[CODE_POS] if i == 1: if len(code) != 6: return print _datestr tmp = code.split("-") if len(tmp) == 2 and tmp[1] == "T": #Use only Tosho codes code = code.split("-")[0] else: i = i + 1 continue openv = row[OPEN_POS] highv = row[HIGH_POS] lowv = row[LOW_POS] closev = row[CLOSE_POS] volv = row[VOL_POS] amtv = row[AMT_POS] if openv == "-" or highv == "-" or lowv == "-" or closev == "-" or volv == "-" or amtv == "-": continue if openv == "" or highv == "" or lowv == "" or closev == "" or volv == "" or amtv == "": continue if openv <= 0 or highv <= 0 or lowv <= 0 or closev <= 0 or volv <= 0 or amtv <= 0: continue strsql = strsql + "REPLACE INTO trade.kabuka VALUES(\'" strsql = strsql + row[CODE_POS].split("-")[0] + "\'" strsql = strsql + ", \'" + _datestr + "\'" strsql = strsql + ", \'" + str(openv) + "\'" strsql = strsql + ", \'" + str(highv) + "\'" strsql = strsql + ", \'" + str(lowv) + "\'" strsql = strsql + ", \'" + str(closev) + "\'" strsql = strsql + ", \'" + str(volv) + "\'" strsql = strsql + ", \'" + str(amtv) + "\'" strsql = strsql + ");\n" i = i + 1 if strsql != "": sql.exec_updsql(strsql)