def _save(self, fqFactorDF, codeDF): logging.info("Saving daily fq factor.") fqFactorDF.to_sql(name='t_daily_fqFactor', con=utils.getEngine(), if_exists='append', chunksize=20000) logging.info("Saved daily fq factor.") logging.info("Saving daily hfq data.") codeDF.to_sql(name='t_daily_hfq_stock', con=utils.getEngine(), if_exists='append') logging.info("Saved daily hfq data.")
def _hfq2qfq(beginCode=None): codes = pd.read_sql('select code from t_stock_basics', utils.getEngine()) if conf.DEBUG: codes = codes[:1] if not conf.DEBUG and beginCode is None: utils.executeSQL('delete from t_daily_qfq_stock') if beginCode is not None: codes = codes[codes['code']>=beginCode] codes['code'].map(_doqfq)
def _doqfq(code): logging.info("qfq for %s."%code) hfqDF = pd.read_sql('select * from t_daily_hfq_stock where code="%s"'%code, utils.getEngine()).set_index('date').sort_index(ascending=False) if hfqDF is None or hfqDF.empty: return factors = pd.read_sql('select * from t_daily_fqFactor where code="%s" and date="%s"'%(code, hfqDF.head(1).index.values[0]), utils.getEngine()) # off market if factors.empty: return factor = factors['factor'].values[0] rt = ts.get_realtime_quotes(code) if rt is None: return if ((float(rt['high']) == 0) & (float(rt['low'])==0)): preClose = float(rt['pre_close']) else: preClose = float(rt['price']) _rate = factor / preClose for label in ['open','high','low','close']: hfqDF[label] /= _rate hfqDF[label] = hfqDF[label].map(lambda x:'%.2f'%x) hfqDF[label] = hfqDF[label].astype(float) if not conf.DEBUG: logging.info("Saving %s qfq data."%code) hfqDF.to_sql(name='t_daily_qfq_stock', con=utils.getEngine(), if_exists='append') logging.info("Saved %s qfq data."%code) if conf.DEBUG: print hfqDF.tail(1) print factor print _rate
def _downloadFqFactor(codes): factorDF = DataFrame() for code in codes: logging.info("Downloading %s fq factor."%code) df = ts.stock.trading._parase_fq_factor(code,'','') df.insert(0,"code",code,True) df = df.drop_duplicates('date').set_index('date') factorDF = pd.concat([factorDF, df]) if conf.DEBUG: break logging.info("Deleting fq factor.") utils.executeSQL("delete from t_daily_fqFactor") logging.info("Saving fq factor.") factorDF.to_sql(name='t_daily_fqFactor',con=utils.getEngine(), if_exists="append",chunksize=20000) logging.info("Saved fq factor.")
def _downloadFqFactor(codes): factorDF = DataFrame() for code in codes: logging.info("Downloading %s fq factor." % code) df = ts.stock.trading._parase_fq_factor(code, '', '') df.insert(0, "code", code, True) df = df.drop_duplicates('date').set_index('date') factorDF = pd.concat([factorDF, df]) if conf.DEBUG: break logging.info("Deleting fq factor.") utils.executeSQL("delete from t_daily_fqFactor") logging.info("Saving fq factor.") factorDF.to_sql(name='t_daily_fqFactor', con=utils.getEngine(), if_exists="append", chunksize=20000) logging.info("Saved fq factor.")
def _save(self, code, codeDF,descStr): codeDF.insert(0, "code", code, True) logging.info("Saving %s."%descStr) codeDF.to_sql(name="t_daily_hfq_stock",con=utils.getEngine(), if_exists="append") logging.info("Saved %s with shape:%s."%(descStr,codeDF.shape))