Exemple #1
0
 def play(self, fields):
     scorelist = [self.fieldid, self.geneid]
     spanlist = [self.fieldid, self.geneid]
     p = mp.current_process()
     for field in fields:
         code = field[0]
         datestr = field[1]
         kl = self.kls[code]
         trader = LineTrader(kl, self.geneid)
         datei = kl.get_datei(datestr)
         if datei < self.start_datei:
             continue
         #if trader.judge_trade_goodness(datei) == False:
         #    continue
         
         datelimit = trader.get_near_date(str(int(datestr)+1), "f")
         (interest, interest_rate, trade_mode_str, from_date, 
             to_date, spent, start_price, end_price, enter_price_avg, release_price_avg, endi) = \
                 trader.trade(datestr, "", datelimit)
         scorelist.append(interest_rate)
         spanlist.append(spent)
     pid = p.pid
     scorefile = "%s.%s" % (SCOREFILE, str(pid))
     spanfile = "%s.%s" % (SPANFILE, str(pid))
     f.list2tmpfs(scorelist, scorefile)
     f.list2tmpfs(spanlist, spanfile)
 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
Exemple #3
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 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)