def fetch_cnstock_hist_to_mongo(): '''get qoute data ''' #set log LOGGER_NAME = "TONGLIAN_DATA" mylog = logger.getLogger(LOGGER_NAME) #get the stock list today = datetime.strftime(datetime.today(),"%Y%m%d") mongo = Mongo() db = mongo.getDB() cursor = db.stock_list.find({"exchangeCD":{"$in":["XSHE","XSHG"]},"listStatusCD":"L"}) for row in cursor: ticker = str(row['ticker']) mylog.info("update history data of %s"%(ticker)) exchangeCD = str(row['exchangeCD']) listDate = str(row['listDate']).replace("-", "").replace("NaN", "") if exchangeCD == 'XSHG' and not ticker.startswith("6"): continue #get hist data cursor2 = db.cn_stock_hist.find({"ticker":ticker}) if cursor2.count() > 0: continue mylog.info("insert data of %s"%(ticker)) st = ts.Market() df = st.MktEqud(ticker=ticker,beginDate=listDate, endDate=today, field="") df['ticker'] = df['ticker'].map(lambda x: str(x).zfill(6)) db.cn_stock_hist.insert(json.loads(df.to_json(orient='records'))) time.sleep(1)
def run(): ts.set_token(ct.DATA_YES_TOKEN) st = ts.Market() today = datetime.strftime(datetime.today(),"%Y%m%d") stock_list = st.MktEqud(tradeDate="20160513",field="ticker,PE,secShortName") if not isinstance(stock_list,pd.DataFrame) or stock_list.empty: return stock_list['ticker'] = stock_list['ticker'].map(lambda x: str(x).zfill(6)) result = [] mongo = Mongo() db = mongo.getDB() for i in stock_list.index: code = stock_list.loc[i,'ticker'] pe = stock_list.loc[i,'PE'] name = stock_list.loc[i,'secShortName'] if np.isnan(pe): continue cursor = db.year_min_value.find({"ticker":code}) if cursor.count() <= 0: continue pe_list = [] for row in cursor: pe_list.append(row['pe']) min_pe = min(pe_list) rate = (pe - min_pe)/min_pe result.append({"code":code,"name":name,"pe":pe,"min_pe":min_pe,"rate":rate}) df = pd.DataFrame(result) if db.lowest_pe_stock.find().count() > 0: db.lowest_pe_stock.remove() db.lowest_pe_stock.insert(json.loads(df.to_json(orient='records')))
def check_stock(code): mongo = Mongo() db = mongo.getDB() cursor = db.mainreports.find({"code":code}) report_list = dict() for row in cursor: report_list[str(row['date'])] = row if len(report_list) == 0: return False,None,None key_list = list(report_list.keys()) key_list.sort() key_list.reverse() last_two_quarter = key_list[0:2] last_date = str(key_list[0]) year = int(last_date[0:4]) last_three_year = [str(year-1)+"-12-31",str(year-2)+"-12-31",str(year-3)+"-12-31"] last_three_year2 = [v for v in last_three_year if v in key_list] "连续三年业绩增长都小于30%,最近3个季度业绩增长大于30%" year_grow = [] for key in last_three_year2: if report_list[key]['profits_yoy']: year_grow.append(report_list[key]['profits_yoy']) quarter_grow = [] for key in last_two_quarter: if report_list[key]['profits_yoy']: quarter_grow.append(report_list[key]['profits_yoy']) if (sum(year_grow)/3)!=0 and (max(quarter_grow) - sum(year_grow)/3)/abs((sum(year_grow)/3)) > 1: return True,quarter_grow,year_grow return False,quarter_grow,year_grow
def check_stock(code,forcast_rate): query_text = '业绩预增,预告业绩变动幅度排序' mongo = Mongo() db = mongo.getDB() cursor = db.mainreports.find({"code":code}) report_list = dict() for row in cursor: report_list[str(row['date'])] = row if len(report_list) == 0: return False,None key_list = report_list.keys() key_list.sort() key_list.reverse() last_four_quarter = key_list[0:4] "连续三年业绩增长都小于30%,最近3个季度业绩增长大于30%" quarter_profit_grow = [] for key in last_four_quarter: if report_list[key]['profits_yoy']: quarter_profit_grow.append(report_list[key]['profits_yoy']) if (sum(quarter_profit_grow)/4)!=0 and (forcast_rate - sum(quarter_profit_grow)/4)/abs((sum(quarter_profit_grow)/4)) > 1: return True,quarter_profit_grow return False,quarter_profit_grow
def fetch_stock_list_to_mongo(): eq = ts.Equity() df = eq.Equ(equTypeCD='A', listStatusCD='L', field='') df['ticker'] = df['ticker'].map(lambda x: str(x).zfill(6)) mongo = Mongo() db = mongo.getDB() cursor = db.stock_list.find() if cursor.count() == 0: db.stock_list.insert(json.loads(df.to_json(orient='records')))
def batchUpdateMainReport(stockCodeList): LOGGER_NAME = "UPDATE_DATA" mylog = logger.getLogger(LOGGER_NAME) mongodb = Mongo() for code in stockCodeList: time.sleep(1) mylog.info("update mainreport data of %s" % (code)) mongodb.updateMainReport(code)
def batchUpdateMainReport(stockCodeList): LOGGER_NAME = "UPDATE_DATA" mylog = logger.getLogger(LOGGER_NAME) mongodb = Mongo() for code in stockCodeList: time.sleep(1) mylog.info("update mainreport data of %s"%(code)) mongodb.updateMainReport(code)
def query_store(query,store): res = query_iwencai(query) if res is None or not res.has_key("result"): pass ticker_list = [] for item in res["result"]: ticker_list.append(dict(ticker=item[0][0:6])) mongo = Mongo() db = mongo.getDB() db[store].remove() db[store].insert(ticker_list)
def query_store(query, store): res = query_iwencai(query) if res is None or not res.has_key("result"): pass ticker_list = [] for item in res["result"]: ticker_list.append(dict(ticker=item[0][0:6])) mongo = Mongo() db = mongo.getDB() db[store].remove() db[store].insert(ticker_list)
def run(): '''get qoute data ''' #set log LOGGER_NAME = "HISTORY_DATA" mylog = logger.getLogger(LOGGER_NAME) #get the stock list today = datetime.strftime(datetime.today(),"%Y%m%d") mongo = Mongo() db = mongo.getDB() cursor = db.stock_list.find({"listStatusCD":"L"}) for row in cursor: ticker = str(row['ticker']) mylog.info("update history data of %s"%(ticker)) exchangeCD = str(row['exchangeCD']) listDate = str(row['listDate']).replace("-", "").replace("NaN", "") if exchangeCD == 'XSHG' and not ticker.startswith("6"): continue #get hist data cursor2 = db.cn_stock_hist.find({"ticker":ticker}).sort("tradeDate",pymongo.DESCENDING).limit(1) st = ts.Market() if cursor2.count() > 0: start_date = str(cursor2[0]['tradeDate']).replace("-", "") df = st.MktEqud(ticker=ticker,beginDate=start_date, endDate=today, field="") if df is not None and not df.empty: if df['accumAdjFactor'][0] == 1: mylog.info("update new data of %s from %s"%(ticker,start_date)) df = df[1:] else: mylog.info("factor change remove data of %s"%(ticker)) db.cn_stock_hist.remove({"ticker":ticker}) df = st.MktEqud(ticker=ticker,beginDate=listDate, endDate=today, field="") else: mylog.info("fisrt insert data of %s"%(ticker)) df = st.MktEqud(ticker=ticker,beginDate=listDate, endDate=today, field="") if df is not None and not df.empty: mylog.info("processing insert of %s"%(ticker)) df['ticker'] = df['ticker'].map(lambda x: str(x).zfill(6)) db.cn_stock_hist.insert(json.loads(df.to_json(orient='records'))) time.sleep(1)
def fetch_cnstock_hist_to_mongo(): '''get qoute data ''' #set log LOGGER_NAME = "TONGLIAN_DATA" mylog = logger.getLogger(LOGGER_NAME) #get the stock list today = datetime.strftime(datetime.today(), "%Y%m%d") mongo = Mongo() db = mongo.getDB() cursor = db.stock_list.find({ "exchangeCD": { "$in": ["XSHE", "XSHG"] }, "listStatusCD": "L" }) for row in cursor: ticker = str(row['ticker']) mylog.info("update history data of %s" % (ticker)) exchangeCD = str(row['exchangeCD']) listDate = str(row['listDate']).replace("-", "").replace("NaN", "") if exchangeCD == 'XSHG' and not ticker.startswith("6"): continue #get hist data cursor2 = db.cn_stock_hist.find({"ticker": ticker}) if cursor2.count() > 0: continue mylog.info("insert data of %s" % (ticker)) st = ts.Market() df = st.MktEqud(ticker=ticker, beginDate=listDate, endDate=today, field="") df['ticker'] = df['ticker'].map(lambda x: str(x).zfill(6)) db.cn_stock_hist.insert(json.loads(df.to_json(orient='records'))) time.sleep(1)
# -*- coding:utf-8 -*- from quantdata.db.mongo import Mongo from datetime import datetime from quantdata import logger import pandas as pd import json mongo = Mongo() db = mongo.getDB() #set log LOGGER_NAME = "get the PB" mylog = logger.getLogger(LOGGER_NAME) #get the stocklist stock_list = db.stock_list.find() for row in stock_list: ticker = str(row['ticker']) mylog.info("processing %s" % (ticker)) stock_hist_list = db.cn_stock_hist.find({"ticker": ticker}) hist_list = [] for stock_row in stock_hist_list: tradDate = datetime.strptime(stock_row['tradeDate'], "%Y-%m-%d") if stock_row['PE'] <= 0 or stock_row['PB'] <= 0: continue hist_list.append({ 'ticker': ticker, 'date': stock_row['tradeDate'], 'pb': stock_row['PB'], 'pe': stock_row['PE'], 'gr_year': tradDate.year,
if report_list[key]['profits_yoy']: year_grow.append(report_list[key]['profits_yoy']) quarter_grow = [] for key in last_two_quarter: if report_list[key]['profits_yoy']: quarter_grow.append(report_list[key]['profits_yoy']) if (sum(year_grow)/3)!=0 and (max(quarter_grow) - sum(year_grow)/3)/abs((sum(year_grow)/3)) > 1: return True,quarter_grow,year_grow return False,quarter_grow,year_grow if __name__ == "__main__": stokList = quotes.get_stock_hq_list() data_list = [] for i in stokList.index: try: flag, quarter_grow,year_grow = check_stock(stokList.loc[i,"code"]) if flag: data = {"code":stokList.loc[i,"code"],"quarter_grow1":quarter_grow[0],"quarter_grow2":quarter_grow[1],\ "year_grow1":year_grow[0],"year_grow2":year_grow[1],"year_grow3":year_grow[2],"mktcap":stokList.loc[i,"mktcap"],\ "name":stokList.loc[i,"name"]} data_list.append(data) except: continue mongo = Mongo() db = mongo.getDB() db["guaidian_list"].remove() db["guaidian_list"].insert(data_list)