def cal_earning(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt stock_quote = StockAsset.get_stock(stock_id).quote p = stock_quote.tclose pe = stock_fdmt.pettm eps = p / pe return eps
def cal_halpha(self, stock_id): stock_quote = StockAsset.get_stock(stock_id).quote close = stock_quote.tcloseaf close = close.replace(0.0, method='pad') ret = close.pct_change() sz = Asset.load_nav_series('120000016') bret = sz.pct_change() ret = ret.resample('m').sum().iloc[:-1] bret = bret.resample('m').sum().iloc[:-1] common_index = ret.index.intersection(bret.index) ret = ret.loc[common_index] bret = bret.loc[common_index] ser = pd.Series() if len(common_index) < 60: return ser for i in range(60, len(common_index)): tmp_dates = common_index[i - 59:i + 1] y = ret.loc[tmp_dates].values x = bret.loc[tmp_dates].values.reshape(-1, 1) mod = LinearRegression().fit(x, y) ser.loc[tmp_dates[-1]] = mod.intercept_ today = get_today() ser.loc[today] = np.nan ser = ser.resample('d').last().fillna(method='pad') return ser
def cal_dastd(self, stock_id, period=23): stock_quote = StockAsset.get_stock(stock_id).quote close = stock_quote.tclose close = close.replace(0.0, method='pad') ret = close.pct_change() ret = ret.rolling(period).apply(lambda x: pow(pow(x, 2).mean(), 0.5)) return ret
def cal_mom(self, stock_id, period=23): stock_quote = StockAsset.get_stock(stock_id).quote close = stock_quote.tclose close = close.replace(0.0, method='pad') ret = close.pct_change() tr = stock_quote.turnrate ret_tr = (ret * tr).rolling(period).sum() weight = tr.rolling(period).sum() # mom = ret.rolling(period).mean() mom = ret_tr / weight return mom
def cal_hilo(self, stock_id, period=23): stock_quote = StockAsset.get_stock(stock_id).quote high = stock_quote.thigh high = high.replace(0.0, method='pad') hi = high.rolling(period).max() low = stock_quote.tlow low = low.replace(0.0, method='pad') lo = low.rolling(period).min() hilo = np.log(hi / low) return hilo
def cal_indexposure(self, stock_id): stock_quote = StockAsset.get_stock(stock_id).quote stock_info = StockAsset.all_stock_info() sf = pd.DataFrame(index=stock_quote.index) sf_ind = stock_info.loc[stock_id].sk_swlevel1code if sf_ind == self.sf_ind: sf_exposure = 1 else: sf_exposure = 0 sf['exposure'] = sf_exposure return sf.exposure
def cal_egro(self, stock_id): def cal_egro_single(x): mod = LinearRegression().fit(np.arange(5).reshape(-1, 1), x) return mod.coef_[0] / np.mean(x) stock_fdmt = StockAsset.get_stock(stock_id).fdmt stock_quote = StockAsset.get_stock(stock_id).quote p = stock_quote.tclose pe = stock_fdmt.pettm eps = p / pe eps = eps[eps.diff() > 0.001] eps_y = pd.Series() for k, v in eps.groupby(eps.index.strftime('%Y')): eps_y.loc[v.index[-1]] = v.values[-1] eps_y = eps_y.rolling(5).apply(cal_egro_single) today = datetime.now() today_idx = pd.tslib.Timestamp(today.year, today.month, today.day) eps_y.loc[today_idx] = np.nan eps_y = eps_y.resample('d').last().fillna(method='pad').dropna() return eps_y
def cal_cmra(self, stock_id): stock_quote = StockAsset.get_stock(stock_id).quote close = stock_quote.tcloseaf close = close.replace(0.0, method='pad') nav = close / close.iloc[0] zt = np.log(nav) zt_max = zt.rolling(window=252 * 5).max() zt_min = zt.rolling(window=252 * 5).min() cmra = np.log((1 + zt_max) / (1 + zt_min)) cmra = cmra.fillna(method='pad') cmra = cmra.dropna() return cmra
def cal_btsg(self, stock_id): stock_quote = StockAsset.get_stock(stock_id).quote close = stock_quote.tcloseaf close = close.replace(0.0, method='pad') ret = close.pct_change() sz = Asset.load_nav_series('120000016') bret = sz.pct_change() ret = ret.resample('m').sum().iloc[:-1] bret = bret.resample('m').sum().iloc[:-1] common_index = ret.index.intersection(bret.index) ret = ret.loc[common_index] bret = bret.loc[common_index] ser = pd.Series() if len(common_index) < 60: return ser for i in range(60, len(common_index)): tmp_dates = common_index[:i + 1] y = ret.loc[tmp_dates].values x = bret.loc[tmp_dates].values.reshape(-1, 1) x = sm.add_constant(x) mod = sm.OLS(y, x).fit() beta = mod.params[1] sigma = mod.resid.std() btsg = pow(beta * sigma, 0.5) ser.loc[tmp_dates[-1]] = btsg today = get_today() ser.loc[today] = np.nan ser = ser.resample('d').last().fillna(method='pad') return ser
def cal_cashrt(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt cashrt = stock_fdmt.cashrt return cashrt
def cal_ltmliabtota(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt ltmliabtota = stock_fdmt.ltmliabtota return ltmliabtota
def cal_sgpmargin(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt sgpmargin = stock_fdmt.sgpmargin return sgpmargin
def cal_currentrt(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt currentrt = stock_fdmt.currentrt return currentrt
def cal_roe(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt roe = stock_fdmt.roedilutedcut return roe
def cal_roa(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt roa = stock_fdmt.roa return roa
def cal_bp(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt bp = 1 / stock_fdmt.pb return bp
def cal_turnover(self, stock_id, period=23): stock_quote = StockAsset.get_stock(stock_id).quote tr = stock_quote.turnrate tr = tr.rolling(period).mean() return tr
def cal_equtotliab(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt equtotliab = stock_fdmt.equtotliab return equtotliab
def cal_size(self, stock_id): stock_quote = StockAsset.get_stock(stock_id).quote totmktcap = stock_quote.totmktcap return totmktcap
def cal_ep(self, stock_id): stock_fdmt = StockAsset.get_stock(stock_id).fdmt ep = 1 / stock_fdmt.pettm return ep