def get_last_exist_close_price(self, dat): q = StockData.mgr().get_one_day_price(dat, self.get_stockinfo_code(), self.get_stockinfo_exch(), 'close')[:] if not q: q = StockData.mgr().get_interval_day_price( dat, self.get_stockinfo_code(), self.get_stockinfo_exch(), 'close') # find the last exist close price return q[0]['close']
def save_data(self, code, data): import_num = 0 cod, exch = self.split_code(code) for i in data: if 'Open' in i: continue else: try: da = i.strip().split(',') if len(da) != 7: print "the number of this line's data is wrong ", da continue else: s = StockData.new() s.code, s.exchange = cod, exch s.Date = str(da[0]) s.Open = float(da[1]) s.High = float(da[2]) s.Low = float(da[3]) s.Close = float(da[4]) s.Volume = float(da[5]) s.AdjClose = float(da[6]) #print s s.save() import_num += 1 except Exception, e: print e, code, data
def __init__(self, stock, start, end): self._stock = stock self._stock_data = {'stock_info': self._stock} q = StockData.mgr().get_stock_data_from_db( self._stock_data['stock_info'].get_stockinfo_code(), self._stock_data['stock_info'].get_stockinfo_exch(), start, end)[:] self._stock_data['stock_chart'] = q
def find_recent_trade_day(self, dat): days = StockData.mgr().get_trade_day(self._sp_start.strftime('%Y-%m-%d'),self._sp_end.strftime('%Y-%m-%d'))[:] trade_days = [i['Date'].date() for i in days] if dat in trade_days: return dat else: return trade_days[-1]
def save_data(self, code, data): import_num = 0 cod, exch = self.split_code(code) for i in data: if 'Open' in i: continue else: try: da = i.strip().split(',') if len(da) != 7: print "the number of this line's data is wrong ",da continue else: s = StockData.new() s.code,s.exchange = cod, exch s.Date = str(da[0]) s.Open = float(da[1]) s.High = float(da[2]) s.Low = float(da[3]) s.Close = float(da[4]) s.Volume = float(da[5]) s.AdjClose = float(da[6]) #print s s.save() import_num += 1 except Exception,e: print e,code,data
def find_recent_trade_day(self): days = StockData.mgr().get_trade_day( self._sim_start.strftime('%Y-%m-%d'), self._sim_end.strftime('%Y-%m-%d'))[:] trade_days = [i['Date'].date() for i in days] if self._sim_end in trade_days: return self._sim_end else: return trade_days[-1]
def init_data2db(fns, f_dir, start): #fns = ['table002367.sz','table200016.sz'] filenum = 0 num = 0 all_file_num = ASharesCodeReal.mgr().get_len_codes() for f in fns: filenum += 1 print f, "...... ", "%0.02f" % ( 100 * float(filenum) / float(all_file_num) ), "%", "......running time:", datetime.datetime.now() - start if len(f) == 21: #wget下载的文件 (table.csv?s=000002.sz) code = f.strip()[12:18] exch = f.strip()[19:] elif len(f) == 14: #urllib2下载的文件 (table600602.ss) code = f.strip()[5:11] exch = f.strip()[12:] else: print "ERROR filename doesn't match!" continue of = open('%s%s' % (f_dir, f), 'r') for i in of.readlines(): #文件行循环 #if 'Open' not in i: #去掉文件内容第一行 if 'Open' in i: continue else: try: data = i.strip().split(',') s = StockData.new() s.code = str(code) s.exchange = str(exch) s.Date = data[0] s.Open = float(data[1]) s.High = float(data[2]) s.Low = float(data[3]) s.Close = float(data[4]) s.Volume = long(data[5]) s.AdjClose = float(data[6]) #print s s.save() num += 1 except Exception, e: print f print s print e
def init_data2db(fns, f_dir, start): #fns = ['table002367.sz','table200016.sz'] filenum = 0 num = 0 all_file_num = ASharesCodeReal.mgr().get_len_codes() for f in fns: filenum += 1 print f,"...... ","%0.02f"%(100*float(filenum)/float(all_file_num)),"%","......running time:",datetime.datetime.now()-start if len(f) == 21: #wget下载的文件 (table.csv?s=000002.sz) code = f.strip()[12:18] exch = f.strip()[19:] elif len(f) == 14: #urllib2下载的文件 (table600602.ss) code = f.strip()[5:11] exch = f.strip()[12:] else: print "ERROR filename doesn't match!" continue of = open('%s%s'%(f_dir,f),'r') for i in of.readlines(): #文件行循环 #if 'Open' not in i: #去掉文件内容第一行 if 'Open' in i: continue else: try: data = i.strip().split(',') s = StockData.new() s.code = str(code) s.exchange = str(exch) s.Date = data[0] s.Open = float(data[1]) s.High = float(data[2]) s.Low = float(data[3]) s.Close = float(data[4]) s.Volume = long(data[5]) s.AdjClose = float(data[6]) #print s s.save() num += 1 except Exception,e: print f print s print e
def generate_stock_list(self): sc_type = self._stock_condition.get_stock_condition_type() sc_term = self._stock_condition.get_stock_condition_term() if sc_type == 'condition': res = [] date = sc_term['date'] conditions = sc_term['condition'] date = self.find_recent_trade_day(date) stock_list = StockData.mgr().generate_stock_list_by_condition(date,conditions)[:] for i in stock_list: res.append(StockInfo(i)) return res elif sc_type == 'total': print sc_type elif sc_type == 'stocks': res = [] stock_list = sc_term for i in stock_list: res.append(StockInfo(i)) return res
def generate_stock_list(self): sc_type = self._stock_condition.get_stock_condition_type() sc_term = self._stock_condition.get_stock_condition_term() if sc_type == 'condition': res = [] date = sc_term['date'] conditions = sc_term['condition'] date = self.find_recent_trade_day(date) stock_list = StockData.mgr().generate_stock_list_by_condition( date, conditions)[:] for i in stock_list: res.append(StockInfo(i)) return res elif sc_type == 'total': print sc_type elif sc_type == 'stocks': res = [] stock_list = sc_term for i in stock_list: res.append(StockInfo(i)) return res
def merge_trade(self, trade): m = re.match(r'^(\w+):(\w+)', trade.get_price()) if m: date_order = m.group(1) p_order = m.group(2) #return m.group(2) if date_order == 'today': start = trade.get_deal_time() #print StockData.mgr().get_one_day_price(start,trade.get_stock_code(),trade.get_stock_exch(),p_order) #print start,trade.get_stock_code(),trade.get_stock_exch(),p_order try: price = StockData.mgr().get_one_day_price(start,trade.get_stock_code(),trade.get_stock_exch(),p_order)[0]['Close'] trade.set_price(price) return trade except Exception, e: print e print "may be this day mysql don't have data",start,trade.get_stock_code(),trade.get_stock_exch(),p_order return False else: print "error, date_order not defined"
def merge_trade(self, trade): m = re.match(r'^(\w+):(\w+)', trade.get_price()) if m: date_order = m.group(1) p_order = m.group(2) #return m.group(2) if date_order == 'today': start = trade.get_deal_time() #print StockData.mgr().get_one_day_price(start,trade.get_stock_code(),trade.get_stock_exch(),p_order) #print start,trade.get_stock_code(),trade.get_stock_exch(),p_order try: price = StockData.mgr().get_one_day_price( start, trade.get_stock_code(), trade.get_stock_exch(), p_order)[0]['Close'] trade.set_price(price) return trade except Exception, e: print e print "may be this day mysql don't have data", start, trade.get_stock_code( ), trade.get_stock_exch(), p_order return False else: print "error, date_order not defined"
def find_trade_day(self): days = StockData.mgr().get_trade_day(self._sim_start.strftime('%Y-%m-%d'),self._sim_end.strftime('%Y-%m-%d'))[:] return [i['Date'].date() for i in days]
def __init__(self, stock, start, end): self._stock = stock self._stock_data = {'stock_info':self._stock} q = StockData.mgr().get_stock_data_from_db(self._stock_data['stock_info'].get_stockinfo_code(),self._stock_data['stock_info'].get_stockinfo_exch(),start,end)[:] self._stock_data['stock_chart'] = q
def get_last_exist_close_price(self, dat): q = StockData.mgr().get_one_day_price(dat,self.get_stockinfo_code(),self.get_stockinfo_exch(),'close')[:] if not q: q = StockData.mgr().get_interval_day_price(dat,self.get_stockinfo_code(),self.get_stockinfo_exch(),'close') # find the last exist close price return q[0]['close']
def get_full_stock_list(self): stock_list = StockData.mgr().get_all_stock_info()[:] return stock_list
def find_trade_day(self): days = StockData.mgr().get_trade_day( self._sim_start.strftime('%Y-%m-%d'), self._sim_end.strftime('%Y-%m-%d'))[:] return [i['Date'].date() for i in days]