Esempio n. 1
0
 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")
Esempio n. 2
0
    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")
Esempio n. 3
0
 def _lm2trader(self):
     meigaras = self.traders.keys()
     if len(meigaras) >= self.division:
         f.log("Already holds %d stocks." % len(meigaras))
     for i in range(0, self.division):
         if len(meigaras) == 0:
             wherestr = ""
         else:
             wherestr = "where %s" % (kf.where_code_in(meigaras, True))
         strsql = "select name, code, fieldid, geneid, points, holding_days, trade_mode \
              from trade.learned_memory %s order by points desc limit 100;" % (wherestr)
         data = sql.exec_selsql(strsql)
         if len(data) == 0:
             f.log("No good meigaras to get this day.")
             break
         
         for row in data:
             i = iter(row)
             name = next(i)
             code = next(i)
             fieldid = next(i)
             geneid = next(i)
             points = next(i)
             holding_days = int(next(i))
             trade_mode = next(i)
             
             if self.traders.has_key(code) == False:
                 return Trader(code, geneid, self.tm, trade_mode, holding_days, 
                               "name=%s, fieldid=%s, geneid=%s, points=%s, holding_days=%s, trade_mode=%s" \
                               % (name, fieldid, geneid, str(points), str(holding_days), trade_mode))
Esempio n. 4
0
 def _lm2trader(self):
     meigaras = self.traders.keys()
     if len(meigaras) >= self.division:
         f.log("Already holds %d stocks." % len(meigaras))
         return None
     target_codes = {}
     #for i in range(0, self.division):
     if len(meigaras) == 0:
         wherestr = ""
     else:
         wherestr = "where %s" % (kf.where_code_in(meigaras, True))
     strsql = "select name, code, fieldid, geneid, score, holding_days \
          from trade.learned_memory2 %s order by score desc limit 100;" % (wherestr)
     data = sql.exec_selsql(strsql)
     if len(data) == 0:
         f.log("No good meigaras to get this day.")
         return None
     
     for row in data:
         i = iter(row)
         name = next(i)
         code = next(i)
         fieldid = next(i)
         geneid = next(i)
         score = next(i)
         holding_days = int(next(i))
         
         if self.traders.has_key(code):
             continue
         
         if target_codes.has_key(code):
             continue
         target_codes[code] = code
         #if self.traders.has_key(code) == False:
         if self.kls.has_key(code):
             kl = self.kls[code]
         else:
             kl = KabukaLines(code, dtf.datestrsub(self.startd, self.buffer_days), self.endd)
             self.kls[code] = kl
             
         trader = LineTrader(kl, geneid, fieldid, self.tfl, self.trade_mode)
         (traded_cnt, traded_price, trade_mode_str) = \
         trader.trade_1step(self.date, 0)
         if trade_mode_str != "":
             trader.reset()
             return trader
     f.log("No trader entered.")
     return None