Commit 757bc40e authored by 李煜's avatar 李煜

modify factor cash flow

parent 1739f414
......@@ -107,3 +107,4 @@ venv.bak/
.idea/
/ultron/
/client_bak.py
/client_.py
import pdb
from alphamind.api import *
from PyFin.api import *
from PyFin.api import makeSchedule
from sqlalchemy import create_engine, select, and_, or_
from sqlalchemy.pool import NullPool
from factors.models import Alpha191
import pandas as pd
import time
import datetime
import json
import sys
from factors import analysis
from ultron.cluster.invoke.cache_data import cache_data
from ultron.utilities.short_uuid import unique_machine,decode
def fetch_factor(engine191, factor_names, start_date, end_date):
db_columns = []
db_columns.append(Alpha191.trade_date)
db_columns.append(Alpha191.code)
for factor_name in factor_names:
db_columns.append(Alpha191.__dict__[factor_name])
query = select(db_columns).where(
and_(Alpha191.trade_date >= start_date, Alpha191.trade_date <= end_date, ))
return pd.read_sql(query, engine191)
def factor_combination(engine, factors, universe_name_list, start_date, end_date, freq):
universe = None
for name in universe_name_list:
if universe is None:
universe = Universe(name)
else:
universe += Universe(name)
dates = makeSchedule(start_date, end_date, freq, calendar='china.sse')
factor_negMkt = engine.fetch_factor_range(universe, "negMarketValue", dates=dates)
risk_cov, risk_factors = engine.fetch_risk_model_range(universe, dates=dates)
dx_returns = engine.fetch_dx_return_range(universe, dates=dates, horizon=map_freq(freq))
# data combination
total_data = pd.merge(factors, risk_factors, on=['trade_date', 'code'])
total_data = pd.merge(total_data, factor_negMkt, on=['trade_date', 'code'])
total_data = pd.merge(total_data, dx_returns, on=['trade_date', 'code'])
industry_category = engine.fetch_industry_range(universe, dates=dates)
total_data = pd.merge(total_data, industry_category, on=['trade_date', 'code']).dropna()
total_data.dropna(inplace=True)
return total_data
def fetch_factor_sets(**kwargs):
db_info = kwargs["db_info"]
factor_names = kwargs["factor_names"]
start_date = kwargs['start_date']
end_date = kwargs['end_date']
universe_name_list = kwargs['universe_name']
benchmark_code = kwargs['benchmark_code']
freq = kwargs['freq']
engine = SqlEngine(db_info) # alpha-mind engine
engine191 = create_engine(db_info, poolclass=NullPool)
factors = fetch_factor(engine191, factor_names, start_date, end_date)
total_data = factor_combination(engine, factors, universe_name_list, start_date, end_date, freq)
return total_data
#session = str('15609986886946081')
session = str(int(time.time() * 1000000 + datetime.datetime.now().microsecond))
alpha_list = []
for i in range(31,32):
alpha_name = 'alpha_' + str(i)
alpha_list.append(alpha_name)
db_info = 'postgresql+psycopg2://alpha:alpha@180.166.26.82:8889/alpha'
total_data = fetch_factor_sets(db_info=db_info,
factor_names=alpha_list, risk_styles=["SIZE"],
start_date='2010-01-01', end_date='2018-12-31',
universe_name=['zz500','hs300','ashare'],
benchmark_code=905,
freq='3b')
try:
diff_sets = set(total_data.columns) - set(alpha_list)
except:
import pdb
pdb.set_trace()
grouped_list = []
for alpha_name in alpha_list:
print(alpha_name, session)
#pdb.set_trace()
#print(cache_data.get_cache(session, alpha_name))
factors_list = list(diff_sets)
factors_list.append(alpha_name)
factors_sets = total_data[factors_list]
cache_data.set_cache(session, alpha_name, factors_sets.to_json(orient='records'))
analysis.factor_analysis(factor_name=alpha_name,risk_styles=['SIZE'],
benchmark_code=905,
session=session)
......@@ -25,21 +25,21 @@ class FactorCashFlow(FactorBase):
`id` varchar(32) NOT NULL,
`symbol` varchar(24) NOT NULL,
`trade_date` date NOT NULL,
`nocf_to_t_liability_ttm` decimal(19,4),
`nocf_to_interest_bear_debt_ttm` decimal(19,4),
`nocf_to_net_debt_ttm` decimal(19,4),
`sale_service_cash_to_or_ttm` decimal(19,4),
`cash_rate_of_sales_ttm` decimal(19,4),
`nocf_to_operating_ni_ttm` decimal(19,4),
`oper_cash_in_to_current_liability_ttm` decimal(19,4),
`cash_to_current_liability_ttm` decimal(19,4),
`cfo_to_ev_ttm` decimal(19,4),
`acca_ttm` decimal(19,4),
`net_profit_cash_cover_ttm` decimal(19,4),
`oper_cash_in_to_asset_ttm` decimal(19,4),
`sales_service_cash_to_or_latest` decimal(19,4),
`cash_rate_of_sales_latest` decimal(19,4),
`nocf_to_operating_ni_latest` decimal(19,4),
`OptCFToLiabilityTTM` decimal(19,4),
`OptCFToIBDTTM` decimal(19,4),
`OptCFToNetDebtTTM` decimal(19,4),
`SaleServCashToOptReTTM` decimal(19,4),
`OptCFToRevTTM` decimal(19,4),
`OptCFToNetIncomeTTM` decimal(19,4),
`OptCFToCurrLiabilityTTM` decimal(19,4),
`CashRatioTTM` decimal(19,4),
`OptToEnterpriseTTM` decimal(19,4),
`OptOnReToAssetTTM` decimal(19,4),
`NetProCashCoverTTM` decimal(19,4),
`OptToAssertTTM` decimal(19,4),
`SalesServCashToOR` decimal(19,4),
`CashOfSales` decimal(19,4),
`NOCFToOpt` decimal(19,4),
PRIMARY KEY(`id`,`trade_date`,`symbol`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;""".format(self._name)
super(FactorCashFlow, self)._create_tables(create_sql, drop_sql)
......@@ -48,7 +48,7 @@ class FactorCashFlow(FactorBase):
def nocf_to_t_liability_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_liability']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['nocf_to_t_liability_ttm'] = np.where(
cash_flow['OptCFToLiabilityTTM'] = np.where(
CalcTools.is_zero(cash_flow.total_liability.values), 0,
cash_flow.net_operate_cash_flow.values / cash_flow.total_liability.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -59,7 +59,7 @@ class FactorCashFlow(FactorBase):
def nocf_to_interest_bear_debt_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_liability', 'interest_bearing_liability']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['nocf_to_interest_bear_debt_ttm'] = np.where(
cash_flow['OptCFToIBDTTM'] = np.where(
CalcTools.is_zero(cash_flow.interest_bearing_liability.values), 0,
cash_flow.net_operate_cash_flow.values / cash_flow.interest_bearing_liability.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -70,7 +70,7 @@ class FactorCashFlow(FactorBase):
def nocf_to_net_debt_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'net_liability']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['nocf_to_net_debt_ttm'] = np.where(CalcTools.is_zero(cash_flow.net_liability.values), 0,
cash_flow['OptCFToNetDebtTTM'] = np.where(CalcTools.is_zero(cash_flow.net_liability.values), 0,
cash_flow.net_operate_cash_flow.values / cash_flow.net_liability.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
factor_cash_flow = pd.merge(factor_cash_flow, cash_flow, on="symbol")
......@@ -80,7 +80,7 @@ class FactorCashFlow(FactorBase):
def sale_service_cash_to_or_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['goods_sale_and_service_render_cash', 'operating_revenue']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['sale_service_cash_to_or_ttm'] = np.where(
cash_flow['SaleServCashToOptReTTM'] = np.where(
CalcTools.is_zero(cash_flow.operating_revenue.values), 0,
cash_flow.goods_sale_and_service_render_cash.values / cash_flow.operating_revenue.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -91,7 +91,7 @@ class FactorCashFlow(FactorBase):
def cash_rate_of_sales_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'operating_revenue']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['cash_rate_of_sales_ttm'] = np.where(
cash_flow['OptCFToRevTTM'] = np.where(
CalcTools.is_zero(cash_flow.operating_revenue.values), 0,
cash_flow.net_operate_cash_flow.values / cash_flow.operating_revenue.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -102,7 +102,7 @@ class FactorCashFlow(FactorBase):
def nocf_to_operating_ni_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_operating_revenue', 'total_operating_cost']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['nocf_to_operating_ni_ttm'] = np.where(
cash_flow['OptCFToNetIncomeTTM'] = np.where(
CalcTools.is_zero(cash_flow.total_operating_revenue.values - cash_flow.total_operating_cost.values),
0, cash_flow.net_operate_cash_flow.values / (
cash_flow.total_operating_revenue.values - cash_flow.total_operating_cost.values))
......@@ -114,7 +114,7 @@ class FactorCashFlow(FactorBase):
def oper_cash_in_to_current_liability_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_current_liability']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['oper_cash_in_to_current_liability_ttm'] = np.where(
cash_flow['OptCFToCurrLiabilityTTM'] = np.where(
CalcTools.is_zero(cash_flow.total_current_liability.values), 0,
cash_flow.net_operate_cash_flow.values / cash_flow.total_current_liability.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -125,7 +125,7 @@ class FactorCashFlow(FactorBase):
def cash_to_current_liability_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['cash_and_equivalents_at_end', 'total_current_assets']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['cash_to_current_liability_ttm'] = np.where(CalcTools.is_zero(cash_flow.total_current_assets.values),
cash_flow['CashRatioTTM'] = np.where(CalcTools.is_zero(cash_flow.total_current_assets.values),
0,
cash_flow.cash_and_equivalents_at_end.values / cash_flow.total_current_assets.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -137,7 +137,7 @@ class FactorCashFlow(FactorBase):
columns_list = ['net_operate_cash_flow', 'longterm_loan', 'shortterm_loan', 'market_cap',
'cash_and_equivalents_at_end']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['cfo_to_ev_ttm'] = np.where(CalcTools.is_zero(
cash_flow['OptToEnterpriseTTM'] = np.where(CalcTools.is_zero(
cash_flow.longterm_loan.values + cash_flow.shortterm_loan.values + \
cash_flow.market_cap.values - cash_flow.cash_and_equivalents_at_end.values), 0,
cash_flow.net_operate_cash_flow.values / (cash_flow.longterm_loan.values + cash_flow.shortterm_loan.values + \
......@@ -150,7 +150,7 @@ class FactorCashFlow(FactorBase):
def acca_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'net_profit', 'total_assets']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['acca_ttm'] = np.where(CalcTools.is_zero(cash_flow.total_assets.values), 0,
cash_flow['OptOnReToAssetTTM'] = np.where(CalcTools.is_zero(cash_flow.total_assets.values), 0,
(cash_flow.net_operate_cash_flow.values - cash_flow.net_profit.values) / (
cash_flow.total_assets.values))
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -161,7 +161,7 @@ class FactorCashFlow(FactorBase):
def net_profit_cash_cover_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'np_parent_company_owners']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['net_profit_cash_cover_ttm'] = np.where(
cash_flow['NetProCashCoverTTM'] = np.where(
CalcTools.is_zero(cash_flow.np_parent_company_owners.values), 0,
cash_flow.net_operate_cash_flow.values / cash_flow.np_parent_company_owners.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -172,7 +172,7 @@ class FactorCashFlow(FactorBase):
def oper_cash_in_to_asset_ttm(self, ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_assets']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['oper_cash_in_to_asset_ttm'] = np.where(CalcTools.is_zero(cash_flow.total_assets.values),
cash_flow['OptToAssertTTM'] = np.where(CalcTools.is_zero(cash_flow.total_assets.values),
0,
cash_flow.net_operate_cash_flow.values / cash_flow.total_assets.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -183,7 +183,7 @@ class FactorCashFlow(FactorBase):
def sales_service_cash_to_or_latest(self, tp_cash_flow, factor_cash_flow):
columns_list = ['goods_sale_and_service_render_cash', 'operating_revenue']
cash_flow = tp_cash_flow.loc[:, columns_list]
cash_flow['sales_service_cash_to_or_latest'] = np.where(CalcTools.is_zero(cash_flow.operating_revenue.values),
cash_flow['SalesServCashToOR'] = np.where(CalcTools.is_zero(cash_flow.operating_revenue.values),
0,
cash_flow.goods_sale_and_service_render_cash.values / cash_flow.operating_revenue.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -194,7 +194,7 @@ class FactorCashFlow(FactorBase):
def cash_rate_of_sales_latest(self, tp_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'operating_revenue']
cash_flow = tp_cash_flow.loc[:, columns_list]
cash_flow['cash_rate_of_sales_latest'] = np.where(CalcTools.is_zero(cash_flow.operating_revenue.values),
cash_flow['CashOfSales'] = np.where(CalcTools.is_zero(cash_flow.operating_revenue.values),
0,
cash_flow.net_operate_cash_flow.values / cash_flow.operating_revenue.values)
cash_flow = cash_flow.drop(columns_list, axis=1)
......@@ -206,7 +206,7 @@ class FactorCashFlow(FactorBase):
columns_list = ['net_operate_cash_flow', 'total_operating_revenue',
'total_operating_cost']
cash_flow = tp_cash_flow.loc[:, columns_list]
cash_flow['nocf_to_operating_ni_latest'] = np.where(
cash_flow['NOCFToOpt'] = np.where(
CalcTools.is_zero((cash_flow.total_operating_revenue.values - cash_flow.total_operating_cost.values)), 0,
cash_flow.net_operate_cash_flow.values / (
cash_flow.total_operating_revenue.values - cash_flow.total_operating_cost.values))
......
......@@ -33,6 +33,10 @@ from ultron.cluster.invoke.cache_data import cache_data
class FactorEarning(FactorBase):
"""
收益质量
--盈利相关
"""
def __init__(self, name):
super(FactorEarning, self).__init__(name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment