Commit 40616b3c authored by 李煜's avatar 李煜

code pdate

parent b2dea9f5
import pdb
import collections
import time
import collections
from datetime import datetime, timedelta
from factor import factor_growth, historical_value, factor_per_share_indicators, factor_cash_flow, factor_contrarian, factor_earning
from factor.ttm_fundamental import *
......@@ -11,10 +9,8 @@ from vision.file_unit.income import Income
from vision.file_unit.valuation import Valuation
from vision.file_unit.industry import Industry
from vision.file_unit.indicator import Indicator
from factor.utillities.trade_date import TradeDate
from ultron.cluster.invoke.cache_data import cache_data
from ultron.utilities.short_uuid import unique_machine, decode
def get_trade_date(trade_date, n):
......@@ -48,7 +44,6 @@ def get_basic_growth_data(trade_date):
:param trade_date: 交易日
:return:
"""
trade_date_pre_year = get_trade_date(trade_date, 1)
trade_date_pre_year_2 = get_trade_date(trade_date, 2)
trade_date_pre_year_3 = get_trade_date(trade_date, 3)
......@@ -262,7 +257,6 @@ def get_basic_history_value_data(trade_date):
ttm_factor_sets_5 = ttm_factor_sets_5.drop(columns={"trade_date"})
# ttm_factor_sets_sum = ttm_factor_sets_sum.rename(columns={"net_profit": "net_profit_5"})
ttm_factor_sets = pd.merge(ttm_factor_sets, ttm_factor_sets_3, on='symbol')
ttm_factor_sets = pd.merge(ttm_factor_sets, ttm_factor_sets_5, on='symbol')
# ttm_factor_sets = pd.merge(ttm_factor_sets, ttm_factor_sets_sum, on='symbol')
......@@ -375,12 +369,12 @@ def get_basic_cash_flow(trade_date):
Income.total_operating_cost, Income.net_profit,
Income.np_parent_company_owners]
}
ttm_factor_sets = get_ttm_fundamental([], ttm_factors, trade_date).reset_index()
ttm_factor_sets = ttm_factor_sets[-ttm_factor_sets.duplicated()]
ttm_cash_flow_sets = get_ttm_fundamental([], ttm_factors, trade_date).reset_index()
ttm_cash_flow_sets = ttm_cash_flow_sets[-ttm_cash_flow_sets.duplicated()]
# 合并
ttm_factor_sets = pd.merge(ttm_factor_sets, valuation_sets, on="symbol")
ttm_cash_flow_sets = pd.merge(ttm_cash_flow_sets, valuation_sets, on="symbol")
return tp_cash_flow, ttm_factor_sets
return tp_cash_flow, ttm_cash_flow_sets
def get_basic_constrain(trade_date):
......@@ -408,10 +402,10 @@ def get_basic_constrain(trade_date):
Income.administration_expense
]}
ttm_factors_sets = get_ttm_fundamental([], ttm_factors, trade_date).reset_index()
ttm_factors_sets = ttm_factors_sets[-ttm_factors_sets.duplicated()]
ttm_constrain_sets = get_ttm_fundamental([], ttm_factors, trade_date).reset_index()
ttm_constrain_sets = ttm_constrain_sets[-ttm_constrain_sets.duplicated()]
return balance_sets, ttm_factors_sets
return balance_sets, ttm_constrain_sets
def get_basic_earning(trade_date):
......@@ -572,9 +566,9 @@ if __name__ == '__main__':
print('per_share_cal_time:{}'.format(time3 - time2))
# cash flow
tp_cash_flow, ttm_factor_sets = get_basic_cash_flow(date_index)
tp_cash_flow, ttm_cash_flow_sets = get_basic_cash_flow(date_index)
cache_data.set_cache(session4 + "1", date_index, tp_cash_flow.to_json(orient='records'))
cache_data.set_cache(session4 + "2", date_index, ttm_factor_sets.to_json(orient='records'))
cache_data.set_cache(session4 + "2", date_index, ttm_cash_flow_sets.to_json(orient='records'))
factor_cash_flow.factor_calculate(date_index=date_index, session=session4)
time4 = time.time()
print('cash_flow_cal_time:{}'.format(time4 - time3))
......
......@@ -45,7 +45,8 @@ class FactorCashFlow(FactorBase):
super(FactorCashFlow, self)._create_tables(create_sql, drop_sql)
# 经营活动净现金流(TTM)/负债(TTM)
def nocf_to_t_liability_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def nocf_to_t_liability_ttm(ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_liability']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['OptCFToLiabilityTTM'] = np.where(
......@@ -56,7 +57,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动净现金流(TTM)/带息负债(TTM)
def nocf_to_interest_bear_debt_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def nocf_to_interest_bear_debt_ttm(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['OptCFToIBDTTM'] = np.where(
......@@ -67,7 +69,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动净现金流(TTM)/净负债(TTM)
def nocf_to_net_debt_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def nocf_to_net_debt_ttm(ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'net_liability']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['OptCFToNetDebtTTM'] = np.where(CalcTools.is_zero(cash_flow.net_liability.values), 0,
......@@ -77,7 +80,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 销售商品和提供劳务收到的现金(TTM)/营业收入(TTM)
def sale_service_cash_to_or_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def sale_service_cash_to_or_ttm(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['SaleServCashToOptReTTM'] = np.where(
......@@ -88,7 +92,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(TTM)/营业收入(TTM)
def cash_rate_of_sales_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def cash_rate_of_sales_ttm(ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'operating_revenue']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['OptCFToRevTTM'] = np.where(
......@@ -99,7 +104,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(TTM)/(营业总收入(TTM)-营业总成本(TTM))
def nocf_to_operating_ni_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def nocf_to_operating_ni_ttm(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['OptCFToNetIncomeTTM'] = np.where(
......@@ -111,7 +117,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(TTM)/流动负债(TTM)
def oper_cash_in_to_current_liability_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def oper_cash_in_to_current_liability_ttm(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['OptCFToCurrLiabilityTTM'] = np.where(
......@@ -122,7 +129,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 期末现金及现金等价物余额(TTM)/流动负债(TTM)
def cash_to_current_liability_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def cash_to_current_liability_ttm(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['CashRatioTTM'] = np.where(CalcTools.is_zero(cash_flow.total_current_assets.values),
......@@ -133,7 +141,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(TTM)/(长期借款(TTM)+ 短期借款(TTM)+ 总市值 - 期末现金及现金等价物(TTM)
def cfo_to_ev_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def cfo_to_ev_ttm(ttm_cash_flow, factor_cash_flow):
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]
......@@ -147,7 +156,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# (经营活动产生的金流量净额(TTM) - 净利润(TTM)) /总资产(TTM)
def acca_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def acca_ttm(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['OptOnReToAssetTTM'] = np.where(CalcTools.is_zero(cash_flow.total_assets.values), 0,
......@@ -158,7 +168,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(TTM)/归属于母公司所有者的净利润(TTM)
def net_profit_cash_cover_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def net_profit_cash_cover_ttm(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['NetProCashCoverTTM'] = np.where(
......@@ -169,7 +180,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(TTM)/总资产(TTM)
def oper_cash_in_to_asset_ttm(self, ttm_cash_flow, factor_cash_flow):
@staticmethod
def oper_cash_in_to_asset_ttm(ttm_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_assets']
cash_flow = ttm_cash_flow.loc[:, columns_list]
cash_flow['OptToAssertTTM'] = np.where(CalcTools.is_zero(cash_flow.total_assets.values),
......@@ -180,7 +192,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 销售商品和提供劳务收到的现金(Latest)/营业收入(Latest)
def sales_service_cash_to_or_latest(self, tp_cash_flow, factor_cash_flow):
@staticmethod
def sales_service_cash_to_or_latest(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['SalesServCashToOR'] = np.where(CalcTools.is_zero(cash_flow.operating_revenue.values),
......@@ -191,7 +204,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经验活动产生的现金流量净额 / 营业收入
def cash_rate_of_sales_latest(self, tp_cash_flow, factor_cash_flow):
@staticmethod
def cash_rate_of_sales_latest(tp_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'operating_revenue']
cash_flow = tp_cash_flow.loc[:, columns_list]
cash_flow['CashOfSales'] = np.where(CalcTools.is_zero(cash_flow.operating_revenue.values),
......@@ -202,7 +216,8 @@ class FactorCashFlow(FactorBase):
return factor_cash_flow
# 经营活动产生的现金流量净额(Latest)/(营业总收入(Latest)-营业总成本(Latest))
def nocf_to_operating_ni_latest(self, tp_cash_flow, factor_cash_flow):
@staticmethod
def nocf_to_operating_ni_latest(tp_cash_flow, factor_cash_flow):
columns_list = ['net_operate_cash_flow', 'total_operating_revenue',
'total_operating_cost']
cash_flow = tp_cash_flow.loc[:, columns_list]
......@@ -223,7 +238,6 @@ def calculate(trade_date, cash_flow_dic, cash_flow): # 计算对应因子
factor_cash_flow = pd.DataFrame()
factor_cash_flow['symbol'] = tp_cash_flow.index
# tp_cash_flow.set_index('symbol', inplace=True)
# 非TTM计算
factor_cash_flow = cash_flow.nocf_to_operating_ni_latest(tp_cash_flow, factor_cash_flow)
......@@ -248,14 +262,6 @@ def calculate(trade_date, cash_flow_dic, cash_flow): # 计算对应因子
cash_flow._storage_data(factor_cash_flow, trade_date)
def do_update(self, start_date, end_date, count):
# 读取本地交易日
trade_date_sets = self._trade_date.trade_date_sets_ago(start_date, end_date, count)
for trade_date in trade_date_sets:
self.calculate(trade_date)
print('----->')
@app.task()
def factor_calculate(**kwargs):
print("cash_flow_kwargs: {}".format(kwargs))
......
......@@ -45,7 +45,8 @@ class FactorContrarian(FactorBase):
super(FactorContrarian, self)._create_tables(create_sql, drop_sql)
# 销售成本率=营业成本(TTM)/营业收入(TTM)
def sales_cost_ratio_ttm(self, tp_contrarian, factor_contrarian):
@staticmethod
def sales_cost_ratio_ttm(tp_contrarian, factor_contrarian):
columns_list = ['operating_cost', 'operating_revenue']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['SalesCostTTM'] = np.where(
......@@ -57,7 +58,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 销售税金率=营业税金及附加(TTM)/营业收入(TTM)
def tax_ratio_ttm(self, tp_contrarian, factor_contrarian):
@staticmethod
def tax_ratio_ttm(tp_contrarian, factor_contrarian):
columns_list = ['operating_tax_surcharges', 'operating_revenue']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['TaxRTTM'] = np.where(
......@@ -69,7 +71,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 财务费用与营业总收入之比=财务费用(TTM)/营业总收入(TTM)
def financial_expense_rate_ttm(self, tp_contrarian, factor_contrarian):
@staticmethod
def financial_expense_rate_ttm(tp_contrarian, factor_contrarian):
columns_list = ['financial_expense', 'total_operating_cost']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['FinExpTTM'] = np.where(
......@@ -81,7 +84,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 营业费用与营业总收入之比=销售费用(TTM)/营业总收入(TTM)
def operating_expense_rate_ttm(self, tp_contrarian, factor_contrarian):
@staticmethod
def operating_expense_rate_ttm(tp_contrarian, factor_contrarian):
columns_list = ['sale_expense', 'total_operating_cost', 'total_operating_revenue']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['OperExpTTM'] = np.where(
......@@ -93,7 +97,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 管理费用与营业总收入之比=管理费用/营业总收入
def admini_expense_rate_ttm(self, tp_contrarian, factor_contrarian):
@staticmethod
def admini_expense_rate_ttm(tp_contrarian, factor_contrarian):
columns_list = ['administration_expense', 'total_operating_revenue']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['AdminExpTTM'] = np.where(
......@@ -105,7 +110,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 销售期间费用率 = (财务费用 + 销售费用 + 管理费用) / (营业收入)
def period_costs_rate_ttm(self, tp_contrarian, factor_contrarian):
@staticmethod
def period_costs_rate_ttm(tp_contrarian, factor_contrarian):
columns_list = ['financial_expense', 'sale_expense', 'administration_expense', 'operating_revenue']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['PeridCostTTM'] = np.where(
......@@ -118,7 +124,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 负债合计/有形资产(流动资产+固定资产)
def debt_tangible_equity_ratio_latest(self, tp_contrarian, factor_contrarian):
@staticmethod
def debt_tangible_equity_ratio_latest(tp_contrarian, factor_contrarian):
columns_list = ['total_liability', 'total_current_liability', 'fixed_assets']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['DTE'] = np.where(
......@@ -130,7 +137,8 @@ class FactorContrarian(FactorBase):
return factor_contrarian
# 债务总资产比=负债合计/资产合计
def debts_asset_ratio_latest(self, tp_contrarian, factor_contrarian):
@staticmethod
def debts_asset_ratio_latest(tp_contrarian, factor_contrarian):
columns_list = ['total_liability', 'total_assets']
contrarian = tp_contrarian.loc[:, columns_list]
contrarian['DA'] = np.where(
......@@ -142,7 +150,8 @@ class FactorContrarian(FactorBase):
# InteBearDebtToTotalCapital = 有息负债/总资本 总资本=固定资产+净运营资本 净运营资本=流动资产-流动负债
# InteBearDebtToTotalCapital = 有息负债/(固定资产 + 流动资产 - 流动负债)
def inte_bear_debt_to_total_capital_latest(self, tp_contrarian, factor_contrarian):
@staticmethod
def inte_bear_debt_to_total_capital_latest(tp_contrarian, factor_contrarian):
columns_list = ['interest_bearing_liability', 'fixed_assets', 'total_current_assets',
'total_current_liability']
contrarian = tp_contrarian.loc[:, columns_list]
......
......@@ -76,7 +76,8 @@ class FactorEarning(FactorBase):
)ENGINE=InnoDB DEFAULT CHARSET=utf8;""".format(self._name)
super(FactorEarning, self)._create_tables(create_sql, drop_sql)
def net_profit_ratio(self, ttm_earning, factor_earning):
@staticmethod
def net_profit_ratio(ttm_earning, factor_earning):
"""
销售净利率(Net profit ratio)
销售净利率=净利润/营业收入
......@@ -93,7 +94,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def operating_profit_ratio(self, ttm_earning, factor_earning):
@staticmethod
def operating_profit_ratio(ttm_earning, factor_earning):
"""
营业净利率
营业净利率=营业利润/营业收入
......@@ -110,7 +112,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def np_to_tor(self, ttm_earning, factor_earning):
@staticmethod
def np_to_tor(ttm_earning, factor_earning):
"""
净利润与营业总收入之比
净利润与营业总收入之比=净利润/营业总收入
......@@ -127,7 +130,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def operating_profit_to_tor(self, ttm_earning, factor_earning):
@staticmethod
def operating_profit_to_tor(ttm_earning, factor_earning):
"""
营业利润与营业总收入之比
营业利润与营业总收入之比=营业利润/营业总收入
......@@ -144,7 +148,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def gross_income_ratio(self, ttm_earning, factor_earning):
@staticmethod
def gross_income_ratio(ttm_earning, factor_earning):
"""
销售毛利率
销售毛利率=(营业收入-营业成本)/营业收入
......@@ -162,7 +167,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def ebit_to_tor(self, ttm_earning, factor_earning):
@staticmethod
def ebit_to_tor(ttm_earning, factor_earning):
"""
息税前利润与营业总收入之比
息税前利润与营业总收入之比=(利润总额+利息支出-利息收入)/营业总收入
......@@ -183,7 +189,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roa(self, ttm_earning, factor_earning):
@staticmethod
def roa(ttm_earning, factor_earning):
"""
资产回报率
资产回报率=净利润/总资产
......@@ -200,11 +207,12 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roa5(self, ttm_earning_5y, factor_earning):
@staticmethod
def roa5(ttm_earning_5y, factor_earning):
"""
5年权益回报率
5年权益回报率=净利润/总资产
:param ttm_earning:
:param ttm_earning_5y:
:param factor_earning:
:return:
"""
......@@ -217,7 +225,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roe(self, ttm_earning, factor_earning):
@staticmethod
def roe(ttm_earning, factor_earning):
"""
权益回报率
权益回报率=净利润/股东权益
......@@ -234,10 +243,11 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roe5(self, ttm_earning_5y, factor_earning):
@staticmethod
def roe5(ttm_earning_5y, factor_earning):
"""
5年权益回报率
:param ttm_earning:
:param ttm_earning_5y:
:param factor_earning:
:return:
"""
......@@ -250,11 +260,12 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roe_diluted(self, tp_earning, factor_earning):
@staticmethod
def roe_diluted(tp_earning, factor_earning):
"""
净资产收益率(摊薄)
净资产收益率(摊薄)=归属于母公司的净利润/期末归属于母公司的所有者权益
:param ttm_earning:
:param tp_earning:
:param factor_earning:
:return:
"""
......@@ -269,7 +280,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roe_avg(self, ttm_earning, factor_earning):
@staticmethod
def roe_avg(ttm_earning, factor_earning):
"""
资产收益率(平均)
资产收益率(平均)=归属于母公司的净利润*2/(期末归属于母公司的所有者权益
......@@ -289,7 +301,6 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def roe_weighted(self, ttm_earning, factor_earning):
"""
:param ttm_earning:
......@@ -298,7 +309,8 @@ class FactorEarning(FactorBase):
"""
pass
def roe_cut(self, tp_earning, factor_earning):
@staticmethod
def roe_cut(tp_earning, factor_earning):
"""
:param tp_earning:
:param factor_earning:
......@@ -338,7 +350,8 @@ class FactorEarning(FactorBase):
"""
pass
def roa_ebit_ttm(self, ttm_earning, factor_earning):
@staticmethod
def roa_ebit_ttm(ttm_earning, factor_earning):
"""
总资产报酬率
ROAEBIT = EBIT*2/(期初总资产+期末总资产)
......@@ -360,7 +373,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def operating_ni_to_tp_ttm(self, ttm_earning, factor_earning):
@staticmethod
def operating_ni_to_tp_ttm(ttm_earning, factor_earning):
"""
经营活动净收益/利润总额
(注,对于非金融企业 经营活动净收益=营业总收入-营业总成本;
......@@ -382,7 +396,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def operating_ni_to_tp_latest(self, tp_earning, factor_earning):
@staticmethod
def operating_ni_to_tp_latest(tp_earning, factor_earning):
"""
经营活动净收益/利润总额
(注,对于非金融企业 经营活动净收益=营业总收入-营业总成本;
......@@ -404,7 +419,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def invest_r_associates_to_tp_ttm(self, ttm_earning, factor_earning):
@staticmethod
def invest_r_associates_to_tp_ttm(ttm_earning, factor_earning):
"""
对联营和营公司投资收益/利润总额
:param ttm_earning:
......@@ -421,7 +437,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def invest_r_associates_to_tp_latest(self, tp_earning, factor_earning):
@staticmethod
def invest_r_associates_to_tp_latest(tp_earning, factor_earning):
"""
对联营和营公司投资收益/利润总额
:param tp_earning:
......@@ -438,7 +455,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def npcut_to_np(self, tp_earning, factor_earning):
@staticmethod
def npcut_to_np(tp_earning, factor_earning):
"""
扣除非经常损益后的净利润/净利润
:param tp_earning:
......@@ -455,7 +473,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def interest_cover_ttm(self, ttm_earning, factor_earning):
@staticmethod
def interest_cover_ttm(ttm_earning, factor_earning):
"""
利息保障倍数
InterestCover=(TP + INT_EXP - INT_COME)/(INT_EXP - INT_COME)
......@@ -467,21 +486,18 @@ class FactorEarning(FactorBase):
columns_list = ['total_profit', 'financial_expense', 'interest_income']
earning = ttm_earning.loc[:, columns_list]
earning['interest_cover_ttm'] = np.where(
CalcTools.is_zero(earning.financial_expense.values-
earning.interest_income.values), 0,
(earning.total_profit.values +
earning.financial_expense.values -
earning.interest_income.values)
/ (earning.financial_expense.values -
earning.interest_income.values)
)
CalcTools.is_zero(earning.financial_expense.values - earning.interest_income.values), 0,
(earning.total_profit.values + earning.financial_expense.values - earning.interest_income.values) /
(earning.financial_expense.values - earning.interest_income.values))
earning = earning.drop(columns_list, axis=1)
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def degm(self, ttm_earning, ttm_earning_p1y, factor_earning):
@staticmethod
def degm(ttm_earning, ttm_earning_p1y, factor_earning):
"""
毛利率增长,与去年同期相比
:param ttm_earning_p1y:
:param ttm_earning:
:param factor_earning:
:return:
......@@ -506,7 +522,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def net_non_oi_to_tp_ttm(self, ttm_earning, factor_earning):
@staticmethod
def net_non_oi_to_tp_ttm(ttm_earning, factor_earning):
"""
营业外收支净额/利润总额
:param ttm_earning:
......@@ -525,7 +542,8 @@ class FactorEarning(FactorBase):
factor_earning = pd.merge(factor_earning, earning, on="symbol")
return factor_earning
def net_non_oi_to_tp_latest(self, tp_earning, factor_earning):
@staticmethod
def net_non_oi_to_tp_latest(tp_earning, factor_earning):
"""
营业外收支净额/利润总额
:param tp_earning:
......
......@@ -60,27 +60,31 @@ class Growth(FactorBase):
)ENGINE=InnoDB DEFAULT CHARSET=utf8;""".format(self._name)
super(Growth, self)._create_tables(create_sql, drop_sql)
def historical_net_asset_grow_rate(self, tp_historical_growth):
@staticmethod
def historical_net_asset_grow_rate(tp_historical_growth):
"""
净资产增长率
:param tp_historical_growth:
:param factor_historical_growth:
:return:
"""
columns_lists = ['symbol', 'total_owner_equities', 'total_owner_equities_pre_year']
historical_growth = tp_historical_growth.loc[:, columns_lists]
if len(historical_growth) <= 0:
return
fun = lambda x: ((x[0] / x[1]) - 1.0 if x[1] and x[1] != 0 and x[0] is not None and x[1] is not None else None)
historical_growth['NetAsset1YChg'] = historical_growth[['total_owner_equities',
'total_owner_equities_pre_year']].apply(fun, axis=1)
historical_growth = historical_growth.drop(columns=['total_owner_equities', 'total_owner_equities_pre_year'], axis=1)
historical_growth = historical_growth.drop(columns=['total_owner_equities',
'total_owner_equities_pre_year'], axis=1)
# factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return historical_growth
def historical_total_asset_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_total_asset_grow_rate(tp_historical_growth, factor_historical_growth):
"""
总资产增长率
:param tp_historical_growth:
......@@ -99,7 +103,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_operating_revenue_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_operating_revenue_grow_rate(tp_historical_growth, factor_historical_growth):
"""
营业收入增长率
:param tp_historical_growth:
......@@ -118,7 +123,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_operating_profit_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_operating_profit_grow_rate(tp_historical_growth, factor_historical_growth):
"""
营业利润增长率
:param tp_historical_growth:
......@@ -137,7 +143,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_total_profit_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_total_profit_grow_rate(tp_historical_growth, factor_historical_growth):
"""
利润总额增长率
:param tp_historical_growth:
......@@ -156,7 +163,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_net_profit_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_net_profit_grow_rate(tp_historical_growth, factor_historical_growth):
"""
净利润增长率
:param tp_historical_growth:
......@@ -175,7 +183,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_np_parent_company_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_np_parent_company_grow_rate(tp_historical_growth, factor_historical_growth):
"""
归属母公司股东的净利润增长率
:param tp_historical_growth:
......@@ -195,7 +204,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_net_profit_grow_rate_3y(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_net_profit_grow_rate_3y(tp_historical_growth, factor_historical_growth):
"""
净利润3年复合增长率
:param tp_historical_growth:
......@@ -214,7 +224,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_net_profit_grow_rate_5y(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_net_profit_grow_rate_5y(tp_historical_growth, factor_historical_growth):
"""
净利润5年复合增长率
:param tp_historical_growth:
......@@ -233,7 +244,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_operating_revenue_grow_rate_3y(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_operating_revenue_grow_rate_3y(tp_historical_growth, factor_historical_growth):
"""
营业收入3年复合增长率
:param tp_historical_growth:
......@@ -254,7 +266,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_operating_revenue_grow_rate_5y(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_operating_revenue_grow_rate_5y(tp_historical_growth, factor_historical_growth):
"""
营业收入5年复合增长率
:param tp_historical_growth:
......@@ -275,7 +288,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_net_cash_flow_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_net_cash_flow_grow_rate(tp_historical_growth, factor_historical_growth):
"""
缺数据
净现金流量增长率
......@@ -295,7 +309,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_np_parent_company_cut_yoy(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_np_parent_company_cut_yoy(tp_historical_growth, factor_historical_growth):
"""
缺失数据
归属母公司股东的净利润(扣除)同比增长
......@@ -315,27 +330,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_egro_(self, tp_historical_growth, factor_historical_growth):
"""
5年收益增长率
:param tp_historical_growth:
:param factor_historical_growth:
:return:
"""
if len(tp_historical_growth) < 3:
return
columns_lists = ['symbol', 'net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2',
'net_profit_pre_year_3', 'net_profit_pre_year_4']
regr = linear_model.LinearRegression()
# 读取五年的时间和净利润
historical_growth = tp_historical_growth.loc[:, columns_lists]
regr.fit(tp_historical_growth['x'].values.reshape(-1, 1), tp_historical_growth['y'].values)
weight = regr.coef_
mean = tp_historical_growth['y'].mean()
def historical_egro(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_egro(tp_historical_growth, factor_historical_growth):
columns_lists = ['symbol', 'net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2',
'net_profit_pre_year_3', 'net_profit_pre_year_4']
regr = linear_model.LinearRegression()
......@@ -368,11 +364,9 @@ class Growth(FactorBase):
# range(0, 5)) else None)
historical_growth['coefficient'] = historical_growth.apply(fun2, axis=1)
historical_growth['mean'] = historical_growth[['net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2', 'net_profit_pre_year_3', 'net_profit_pre_year_4']].fillna('nan').mean(axis=1)
fun1 = lambda x: x[0] / abs(x[1]) if x[1] != 0 and x[1] is not None and x[0] is not None else None
historical_growth['NetPft5YAvgChgTTM'] = historical_growth[['coefficient', 'mean']].apply(fun1, axis=1)
# historical_growth = historical_growth.drop(
......@@ -384,7 +378,8 @@ class Growth(FactorBase):
return factor_historical_growth
def historical_sue(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_sue(tp_historical_growth, factor_historical_growth):
"""
未预期盈余
:param tp_historical_growth:
......@@ -396,8 +391,10 @@ class Growth(FactorBase):
historical_growth = tp_historical_growth.loc[:, columns_lists]
if len(historical_growth) <= 0:
return
historical_growth['mean'] = historical_growth[['net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2', 'net_profit_pre_year_3', 'net_profit_pre_year_4']].fillna(0.0).mean(axis=1)
historical_growth['std'] = historical_growth[['net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2', 'net_profit_pre_year_3', 'net_profit_pre_year_4']].fillna(0.0).std(axis=1)
historical_growth['mean'] = historical_growth[['net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2',
'net_profit_pre_year_3', 'net_profit_pre_year_4']].fillna(0.0).mean(axis=1)
historical_growth['std'] = historical_growth[['net_profit', 'net_profit_pre_year_1', 'net_profit_pre_year_2',
'net_profit_pre_year_3', 'net_profit_pre_year_4']].fillna(0.0).std(axis=1)
fun = lambda x: (x[0] - x[1]) / x[2] if x[2] !=0 and x[1] is not None and x[0] is not None and x[2] is not None else None
......@@ -409,7 +406,8 @@ class Growth(FactorBase):
return factor_historical_growth
def historical_suoi(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_suoi(tp_historical_growth, factor_historical_growth):
"""
未预期毛利
:param tp_historical_growth:
......@@ -438,21 +436,15 @@ class Growth(FactorBase):
historical_growth['std'] = historical_growth[['gi_2', 'gi_3', 'gi_4', 'gi_5']].std(axis=1)
fun = lambda x: ((x[0] - x[1]) / x[2] if x[2] != 0 and x[1] is not None and x[0] is not None and x[2] is not None else None)
# historical_growth['StdUxpGrPft1YTTM'] = (historical_growth['gi_1'] - historical_growth['mean']) / historical_growth['std']
historical_growth['StdUxpGrPft1YTTM'] = historical_growth[['gi_1', 'mean', 'std']].apply(fun, axis=1)
# historical_growth = historical_growth.drop(columns=['operating_revenue', 'operating_revenue_pre_year_1', 'operating_revenue_pre_year_2',
# 'operating_revenue_pre_year_3', 'operating_revenue_pre_year_4',
# 'operating_cost', 'operating_cost_pre_year_1', 'operating_cost_pre_year_2',
# 'operating_cost_pre_year_3', 'operating_cost_pre_year_4', 'std', 'mean',
# 'gi_1', 'gi_2', 'gi_3', 'gi_4', 'gi_5'], axis=1)
historical_growth = historical_growth[['symbol', 'StdUxpGrPft1YTTM']]
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_financing_cash_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_financing_cash_grow_rate(tp_historical_growth, factor_historical_growth):
"""
筹资活动产生的现金流量净额增长率
:param tp_historical_growth:
......@@ -472,7 +464,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_oper_cash_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_oper_cash_grow_rate(tp_historical_growth, factor_historical_growth):
"""
经营活动产生的现金流量净额
:param tp_historical_growth:
......@@ -492,7 +485,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_invest_cash_grow_rate(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_invest_cash_grow_rate(tp_historical_growth, factor_historical_growth):
"""
投资活动产生的现金流量净额
:param tp_historical_growth:
......@@ -512,7 +506,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def historical_sgro(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def historical_sgro(tp_historical_growth, factor_historical_growth):
"""
五年营业收入增长率
:param tp_historical_growth:
......@@ -539,19 +534,18 @@ class Growth(FactorBase):
return False
def fun2(x):
aa = x[['operating_revenue', 'operating_revenue_pre_year_1', 'operating_revenue_pre_year_2', 'operating_revenue_pre_year_3', 'operating_revenue_pre_year_4']].fillna('nan').values.reshape(-1, 1)
aa = x[['operating_revenue', 'operating_revenue_pre_year_1', 'operating_revenue_pre_year_2',
'operating_revenue_pre_year_3', 'operating_revenue_pre_year_4']].fillna('nan').values.reshape(-1, 1)
if has_non(aa):
return None
else:
regr.fit(aa, range(0, 5))
return regr.coef_[-1]
# fun = lambda x: (regr.coef_[-1] if regr.fit(x[['operating_revenue', 'operating_revenue_pre_year_1',
# 'operating_revenue_pre_year_2', 'operating_revenue_pre_year_3',
# 'operating_revenue_pre_year_4']].values.reshape(-1, 1),
# range(0, 5)) else None)
historical_growth['coefficient'] = historical_growth.apply(fun2, axis=1)
historical_growth['mean'] = historical_growth[['operating_revenue', 'operating_revenue_pre_year_1', 'operating_revenue_pre_year_2', 'operating_revenue_pre_year_3', 'operating_revenue_pre_year_4']].fillna(0.0).mean(axis=1)
historical_growth['mean'] = historical_growth[['operating_revenue', 'operating_revenue_pre_year_1',
'operating_revenue_pre_year_2', 'operating_revenue_pre_year_3',
'operating_revenue_pre_year_4']].fillna(0.0).mean(axis=1)
fun1 = lambda x: x[0] / abs(x[1]) if x[1] is not None and x[0] is not None and x[1] != 0 else None
historical_growth['Sales5YChgTTM'] = historical_growth[['coefficient', 'mean']].apply(fun1, axis=1)
......@@ -564,7 +558,8 @@ class Growth(FactorBase):
return factor_historical_growth
# 分析师预期增长
def fsalesg(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def fsalesg(tp_historical_growth, factor_historical_growth):
"""
未来预期营收增长
:param tp_historical_growth:
......@@ -583,7 +578,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def fearng(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def fearng(tp_historical_growth, factor_historical_growth):
"""
未来预期盈利增长
:param tp_historical_growth:
......@@ -602,7 +598,8 @@ class Growth(FactorBase):
factor_historical_growth = pd.merge(factor_historical_growth, historical_growth, on='symbol')
return factor_historical_growth
def egibs_long(self, tp_historical_growth, factor_historical_growth):
@staticmethod
def egibs_long(tp_historical_growth, factor_historical_growth):
"""
净利润增长率
:param tp_historical_growth:
......@@ -633,7 +630,6 @@ def calculate(trade_date, growth_sets, growth):
print("%s has no data" % trade_date)
return
# psindu
factor_historical_growth = growth.historical_net_asset_grow_rate(growth_sets)
factor_historical_growth = growth.historical_total_asset_grow_rate(growth_sets, factor_historical_growth)
factor_historical_growth = growth.historical_operating_revenue_grow_rate(growth_sets, factor_historical_growth)
......
......@@ -10,11 +10,10 @@
"""
import sys
sys.path.append("..")
import json
from pandas.io.json import json_normalize
sys.path.append("..")
from factor.ttm_fundamental import *
from factor.factor_base import FactorBase
from vision.fm.signletion_engine import *
......@@ -62,7 +61,8 @@ class PerShareIndicators(FactorBase):
)ENGINE=InnoDB DEFAULT CHARSET=utf8;""".format(self._name)
super(PerShareIndicators, self)._create_tables(create_sql, drop_sql)
def eps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def eps(tp_share_indicators, factor_share_indicators):
"""
基本每股收益
:param tp_share_indicators:
......@@ -77,7 +77,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def diluted_eps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def diluted_eps(tp_share_indicators, factor_share_indicators):
"""
稀释每股收益
:param tp_share_indicators:
......@@ -93,7 +94,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def cash_equivalent_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def cash_equivalent_ps(tp_share_indicators, factor_share_indicators):
"""
每股现金及现金等价物余额
:param tp_share_indicators:
......@@ -112,7 +114,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def dividend_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def dividend_ps(tp_share_indicators, factor_share_indicators):
"""
每股股利(税前)
:param tp_share_indicators:
......@@ -128,7 +131,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def eps_ttm(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def eps_ttm(tp_share_indicators, factor_share_indicators):
"""
每股收益 TTM
:param tp_share_indicators:
......@@ -147,7 +151,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def net_asset_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def net_asset_ps(tp_share_indicators, factor_share_indicators):
"""
每股净资产
:param tp_share_indicators:
......@@ -165,7 +170,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def tor_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def tor_ps(tp_share_indicators, factor_share_indicators):
"""
每股营业总收入
:param tp_share_indicators:
......@@ -184,7 +190,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def tor_ps_latest(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def tor_ps_latest(tp_share_indicators, factor_share_indicators):
"""
每股营业总收入
:param tp_share_indicators:
......@@ -202,7 +209,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def operating_revenue_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def operating_revenue_ps(tp_share_indicators, factor_share_indicators):
"""
每股营业收入
:param tp_share_indicators:
......@@ -222,7 +230,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def operating_revenue_ps_latest(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def operating_revenue_ps_latest(tp_share_indicators, factor_share_indicators):
"""
每股营业收入(最新)
:param tp_share_indicators:
......@@ -243,7 +252,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def operating_profit_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def operating_profit_ps(tp_share_indicators, factor_share_indicators):
"""
每股营业利润
:param tp_share_indicators:
......@@ -264,7 +274,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def operating_profit_ps_latest(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def operating_profit_ps_latest(tp_share_indicators, factor_share_indicators):
"""
每股营业利润(最新)
:param tp_share_indicators:
......@@ -281,7 +292,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def capital_surplus_fund_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def capital_surplus_fund_ps(tp_share_indicators, factor_share_indicators):
"""
每股资本公积金
:param tp_share_indicators:
......@@ -301,7 +313,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def surplus_reserve_fund_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def surplus_reserve_fund_ps(tp_share_indicators, factor_share_indicators):
"""
每股盈余公积金
:param tp_share_indicators:
......@@ -319,7 +332,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def undivided_pro_fit_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def undivided_pro_fit_ps(tp_share_indicators, factor_share_indicators):
"""
每股未分配利润
:param tp_share_indicators:
......@@ -339,7 +353,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def retained_earnings_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def retained_earnings_ps(tp_share_indicators, factor_share_indicators):
"""
每股留存收益
:param tp_share_indicators:
......@@ -355,7 +370,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def oper_cash_flow_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def oper_cash_flow_ps(tp_share_indicators, factor_share_indicators):
"""
每股经营活动产生的现金流量净额
:param tp_share_indicators:
......@@ -376,7 +392,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def cash_flow_ps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def cash_flow_ps(tp_share_indicators, factor_share_indicators):
"""
每股现金流量净额
:param tp_share_indicators:
......@@ -394,7 +411,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def enterprise_fcfps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def enterprise_fcfps(tp_share_indicators, factor_share_indicators):
"""
每股企业自由现金流量
缺每股企业自由现金流量
......@@ -410,7 +428,8 @@ class PerShareIndicators(FactorBase):
factor_share_indicators = pd.merge(factor_share_indicators, share_indicators, on='symbol')
return factor_share_indicators
def shareholder_fcfps(self, tp_share_indicators, factor_share_indicators):
@staticmethod
def shareholder_fcfps(tp_share_indicators, factor_share_indicators):
"""
每股股东自由现金流量
缺每股股东自由现金流量
......@@ -459,14 +478,6 @@ def calculate(trade_date, valuation_sets, per_share):
factor_share_indicators = per_share.oper_cash_flow_ps(factor_share_indicators, factor_share_indicators) # memorydrror
factor_share_indicators = per_share.cash_flow_ps(factor_share_indicators, factor_share_indicators) # memorydrror
# factor_historical_value = factor_historical_value.drop(columns=['pb', 'pe', 'ps', 'pcf', 'market_cap',
# 'circulating_market_cap', 'isymbol',
# 'np_parent_company_owners',
# 'np_parent_company_owners_3',
# 'np_parent_company_owners_5',
# 'net_operate_cash_flow', 'net_profit',
# 'goods_sale_and_service_render_cash'])
# factor_share_indicators = factor_share_indicators[['symbol',
# 'EPS',
# 'DilutedEPSTTM',
......@@ -514,15 +525,6 @@ def calculate(trade_date, valuation_sets, per_share):
per_share._storage_data(factor_share_indicators, trade_date)
def do_update(self, start_date, end_date, count):
# 读取本地交易日
trade_date_sets = self._trade_date.trade_date_sets_ago(start_date, end_date, count)
for trade_date in trade_date_sets:
print('当前交易日: %s' % trade_date)
self.calculate(trade_date)
print('----->')
# @app.task()
def factor_calculate(**kwargs):
print("per_share_kwargs: {}".format(kwargs))
......@@ -530,6 +532,6 @@ def factor_calculate(**kwargs):
session = kwargs['session']
per_share = PerShareIndicators('factor_per_share') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错
content = cache_data.get_cache(session, date_index)
total_growth_data = json_normalize(json.loads(str(content, encoding='utf8')))
print("len_total_per_share_data {}".format(len(total_growth_data)))
calculate(date_index, total_growth_data, per_share)
total_pre_share_data = json_normalize(json.loads(str(content, encoding='utf8')))
print("len_total_per_share_data {}".format(len(total_pre_share_data)))
calculate(date_index, total_pre_share_data, per_share)
......@@ -60,7 +60,8 @@ class HistoricalValue(FactorBase):
)ENGINE=InnoDB DEFAULT CHARSET=utf8;""".format(self._name)
super(HistoricalValue, self)._create_tables(create_sql, drop_sql)
def ps_indu(self, tp_historical_value, factor_historical_value):
@staticmethod
def ps_indu(tp_historical_value, factor_historical_value):
"""
PEIndu, 市销率,以及同行业所有的公司的市销率
# (PS – PS 的行业均值)/PS 的行业标准差
......@@ -84,7 +85,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def etop(self, tp_historical_value, factor_historical_value):
@staticmethod
def etop(tp_historical_value, factor_historical_value):
"""
收益市值比
# 收益市值比= 净利润/总市值
......@@ -105,7 +107,8 @@ class HistoricalValue(FactorBase):
return factor_historical_value
# 5年平均收益市值比 = 近5年净利润 / 近5年总市值
def etp5(self, tp_historical_value, factor_historical_value):
@staticmethod
def etp5(tp_historical_value, factor_historical_value):
columns_lists = ['symbol', 'net_profit_5', 'circulating_market_cap_5', 'market_cap_5']
historical_value = tp_historical_value.loc[:, columns_lists]
......@@ -117,7 +120,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def pe_indu(self, tp_historical_value, factor_historical_value):
@staticmethod
def pe_indu(tp_historical_value, factor_historical_value):
"""
# (PE – PE 的行业均值)/PE 的行业标准差
:param tp_historical_value:
......@@ -138,7 +142,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def peg_3y(self, tp_historical_value, factor_historical_value):
@staticmethod
def peg_3y(tp_historical_value, factor_historical_value):
"""
# 市盈率/归属于母公司所有者净利润 3 年复合增长率
:param tp_historical_value:
......@@ -157,7 +162,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def peg_5y(self, tp_historical_value, factor_historical_value):
@staticmethod
def peg_5y(tp_historical_value, factor_historical_value):
"""
# 市盈率/归属于母公司所有者净利润 5 年复合增长率
:param tp_historical_value:
......@@ -176,7 +182,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def pb_indu(self, tp_historical_value, factor_historical_value):
@staticmethod
def pb_indu(tp_historical_value, factor_historical_value):
"""
# (PB – PB 的行业均值)/PB 的行业标准差
:param tp_historical_value:
......@@ -198,7 +205,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def lcap(self, tp_historical_value, factor_historical_value):
@staticmethod
def lcap(tp_historical_value, factor_historical_value):
"""
总市值的对数
# 对数市值 即市值的对数
......@@ -213,7 +221,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def lflo(self, tp_historical_value, factor_historical_value):
@staticmethod
def lflo(tp_historical_value, factor_historical_value):
"""
流通总市值的对数
# 对数市值 即流通市值的对数
......@@ -229,7 +238,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def nlsize(self, tp_historical_value, factor_historical_value):
@staticmethod
def nlsize(tp_historical_value, factor_historical_value):
"""
对数市值开立方
:param tp_historical_value:
......@@ -243,7 +253,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def pcf_indu(self, tp_historical_value, factor_historical_value):
@staticmethod
def pcf_indu(tp_historical_value, factor_historical_value):
"""
# (PCF – PCF 的行业均值)/PCF 的行业标准差
:param tp_historical_value:
......@@ -267,7 +278,8 @@ class HistoricalValue(FactorBase):
factor_historical_value = pd.merge(factor_historical_value, historical_value, on="symbol")
return factor_historical_value
def cetop(self, tp_historical_value, factor_historical_value):
@staticmethod
def cetop(tp_historical_value, factor_historical_value):
"""
# 经营活动产生的现金流量净额与市值比
:param tp_historical_value:
......@@ -287,7 +299,8 @@ class HistoricalValue(FactorBase):
return factor_historical_value
# 现金流市值比 = 每股派现 * 分红前总股本/总市值
def ctop(self, tp_historical_value, factor_historical_value):
@staticmethod
def ctop(tp_historical_value, factor_historical_value):
columns_lists = ['symbol', 'pcd', 'sbd', 'circulating_market_cap', 'market_cap']
historical_value = tp_historical_value.loc[:, columns_lists]
......@@ -300,7 +313,8 @@ class HistoricalValue(FactorBase):
return factor_historical_value
# 5 年平均现金流市值比 = 近5年每股派现 * 分红前总股本/近5年总市值
def ctop5(self, tp_historical_value, factor_historical_value):
@staticmethod
def ctop5(tp_historical_value, factor_historical_value):
columns_lists = ['symbol', 'pcd', 'sbd', 'circulating_market_cap_5', 'market_cap_5']
historical_value = tp_historical_value.loc[:, columns_lists]
......@@ -318,14 +332,11 @@ class HistoricalValue(FactorBase):
def calculate(trade_date, valuation_sets, historical_value):
"""
:param historical_value:
:param valuation_sets:
:param trade_date:
:return:
"""
# valuation_sets, ttm_factor_sets, cash_flow_sets, income_sets = self.get_basic_data(trade_date)
# valuation_sets = pd.merge(valuation_sets, income_sets, on='symbol')
# valuation_sets = pd.merge(valuation_sets, ttm_factor_sets, on='symbol')
# valuation_sets = pd.merge(valuation_sets, cash_flow_sets, on='symbol')
if len(valuation_sets) <= 0:
print("%s has no data" % trade_date)
return
......@@ -377,15 +388,6 @@ def calculate(trade_date, valuation_sets, historical_value):
historical_value._storage_data(factor_historical_value, trade_date)
def do_update(self, start_date, end_date, count):
# 读取本地交易日
trade_date_sets = self._trade_date.trade_date_sets_ago(start_date, end_date, count)
for trade_date in trade_date_sets:
print('因子计算日期: %s' % trade_date)
self.calculate(trade_date)
print('----->')
@app.task()
def factor_calculate(**kwargs):
print("history_value_kwargs: {}".format(kwargs))
......
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