Commit 74ec7081 authored by 李煜's avatar 李煜

code update

parent cf988166
...@@ -338,22 +338,26 @@ def get_basic_cash_flow(trade_date): ...@@ -338,22 +338,26 @@ def get_basic_cash_flow(trade_date):
:return: :return:
""" """
cash_flow_sets = get_fundamentals(add_filter_trade(query(CashFlow.__name__, cash_flow_sets = get_fundamentals(add_filter_trade(query(CashFlow.__name__,
[CashFlow.symbol, CashFlow.net_operate_cash_flow, [CashFlow.symbol,
CashFlow.net_operate_cash_flow,
CashFlow.goods_sale_and_service_render_cash]) CashFlow.goods_sale_and_service_render_cash])
, [trade_date])) , [trade_date]))
income_sets = get_fundamentals(add_filter_trade(query(Income.__name__, income_sets = get_fundamentals(add_filter_trade(query(Income.__name__,
[Income.symbol, Income.operating_revenue, [Income.symbol,
Income.operating_revenue,
Income.total_operating_cost, Income.total_operating_cost,
Income.total_operating_revenue]), [trade_date])) Income.total_operating_revenue]), [trade_date]))
valuation_sets = get_fundamentals(add_filter_trade(query(Valuation.__name__, valuation_sets = get_fundamentals(add_filter_trade(query(Valuation.__name__,
[Valuation.symbol, Valuation.market_cap, [Valuation.symbol,
Valuation.market_cap,
Valuation.circulating_market_cap]), [trade_date])) Valuation.circulating_market_cap]), [trade_date]))
# 合并 # 合并
tp_cash_flow = pd.merge(cash_flow_sets, income_sets, on="symbol") tp_cash_flow = pd.merge(cash_flow_sets, income_sets, on="symbol")
tp_cash_flow = tp_cash_flow[-tp_cash_flow.duplicated()] tp_cash_flow = tp_cash_flow[-tp_cash_flow.duplicated()]
ttm_factors = {Balance.__name__: [Balance.symbol, Balance.total_liability, ttm_factors = {Balance.__name__: [Balance.symbol,
Balance.total_liability,
Balance.shortterm_loan, Balance.shortterm_loan,
Balance.longterm_loan, Balance.longterm_loan,
Balance.total_current_liability, Balance.total_current_liability,
...@@ -365,8 +369,11 @@ def get_basic_cash_flow(trade_date): ...@@ -365,8 +369,11 @@ def get_basic_cash_flow(trade_date):
CashFlow.net_operate_cash_flow, CashFlow.net_operate_cash_flow,
CashFlow.goods_sale_and_service_render_cash, CashFlow.goods_sale_and_service_render_cash,
CashFlow.cash_and_equivalents_at_end], CashFlow.cash_and_equivalents_at_end],
Income.__name__: [Income.symbol, Income.operating_revenue, Income.total_operating_revenue, Income.__name__: [Income.symbol,
Income.total_operating_cost, Income.net_profit, Income.operating_revenue,
Income.total_operating_revenue,
Income.total_operating_cost,
Income.net_profit,
Income.np_parent_company_owners] Income.np_parent_company_owners]
} }
ttm_cash_flow_sets = get_ttm_fundamental([], ttm_factors, trade_date).reset_index() ttm_cash_flow_sets = get_ttm_fundamental([], ttm_factors, trade_date).reset_index()
...@@ -392,7 +399,8 @@ def get_basic_constrain(trade_date): ...@@ -392,7 +399,8 @@ def get_basic_constrain(trade_date):
balance_sets = balance_sets[-balance_sets.duplicated()] balance_sets = balance_sets[-balance_sets.duplicated()]
# TTM计算 # TTM计算
ttm_factors = {Income._name_: [Income.symbol, Income.operating_cost, ttm_factors = {Income._name_: [Income.symbol,
Income.operating_cost,
Income.operating_revenue, Income.operating_revenue,
Income.operating_tax_surcharges, Income.operating_tax_surcharges,
Income.total_operating_revenue, Income.total_operating_revenue,
...@@ -479,13 +487,11 @@ def get_basic_earning(trade_date): ...@@ -479,13 +487,11 @@ def get_basic_earning(trade_date):
## 5年TTM数据 ## 5年TTM数据
ttm_factors = {Balance.__name__: [Balance.symbol, ttm_factors = {Balance.__name__: [Balance.symbol,
Balance.total_assets, Balance.total_assets,
Balance.total_owner_equities Balance.total_owner_equities],
],
CashFlow.__name__: [CashFlow.symbol, CashFlow.__name__: [CashFlow.symbol,
CashFlow.cash_and_equivalents_at_end], CashFlow.cash_and_equivalents_at_end],
Income.__name__: [Income.symbol, Income.__name__: [Income.symbol,
Income.net_profit, Income.net_profit,]
]
} }
# 通过cache_data.set_cache, 会使得index的name丢失 # 通过cache_data.set_cache, 会使得index的name丢失
ttm_earning_5y = get_ttm_fundamental([], ttm_factors, trade_date, year=5).reset_index() ttm_earning_5y = get_ttm_fundamental([], ttm_factors, trade_date, year=5).reset_index()
...@@ -535,11 +541,12 @@ if __name__ == '__main__': ...@@ -535,11 +541,12 @@ if __name__ == '__main__':
# earning.create_dest_tables() # earning.create_dest_tables()
for date_index in trade_date_sets: for date_index in trade_date_sets:
print('trade_date: {}'.format(date_index))
# factor_growth # factor_growth
start_time = time.time() start_time = time.time()
ttm_factor_sets, balance_sets = get_basic_growth_data(date_index) ttm_factor_sets, balance_sets = get_basic_growth_data(date_index)
growth_sets = pd.merge(ttm_factor_sets, balance_sets, on='symbol') growth_sets = pd.merge(ttm_factor_sets, balance_sets, on='symbol')
cache_data.set_cache(session1, date_index, growth_sets.to_json(orient='records')) cache_data.set_cache(session1 + str(date_index), date_index, growth_sets.to_json(orient='records'))
factor_growth.factor_calculate.delay(date_index=date_index, session=session1) factor_growth.factor_calculate.delay(date_index=date_index, session=session1)
time1 = time.time() time1 = time.time()
print('growth_cal_time:{}'.format(time1 - start_time)) print('growth_cal_time:{}'.format(time1 - start_time))
...@@ -549,7 +556,7 @@ if __name__ == '__main__': ...@@ -549,7 +556,7 @@ if __name__ == '__main__':
valuation_sets = pd.merge(valuation_sets, income_sets, on='symbol') 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, ttm_factor_sets, on='symbol')
valuation_sets = pd.merge(valuation_sets, cash_flow_sets, on='symbol') valuation_sets = pd.merge(valuation_sets, cash_flow_sets, on='symbol')
cache_data.set_cache(session2, date_index, valuation_sets.to_json(orient='records')) cache_data.set_cache(session2 + str(date_index), date_index, valuation_sets.to_json(orient='records'))
historical_value.factor_calculate.delay(date_index=date_index, session=session2) historical_value.factor_calculate.delay(date_index=date_index, session=session2)
time2 = time.time() time2 = time.time()
print('history_cal_time:{}'.format(time2 - time1)) print('history_cal_time:{}'.format(time2 - time1))
...@@ -560,7 +567,7 @@ if __name__ == '__main__': ...@@ -560,7 +567,7 @@ if __name__ == '__main__':
valuation_sets = pd.merge(valuation_sets, ttm_factor_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') valuation_sets = pd.merge(valuation_sets, cash_flow_sets, on='symbol')
valuation_sets = pd.merge(valuation_sets, balance_sets, on='symbol') valuation_sets = pd.merge(valuation_sets, balance_sets, on='symbol')
cache_data.set_cache(session3, date_index, valuation_sets.to_json(orient='records')) cache_data.set_cache(session3 + str(date_index), date_index, valuation_sets.to_json(orient='records'))
factor_per_share_indicators.factor_calculate.delay(date_index=date_index, session=session3) factor_per_share_indicators.factor_calculate.delay(date_index=date_index, session=session3)
time3 = time.time() time3 = time.time()
print('per_share_cal_time:{}'.format(time3 - time2)) print('per_share_cal_time:{}'.format(time3 - time2))
......
...@@ -687,7 +687,7 @@ def factor_calculate(**kwargs): ...@@ -687,7 +687,7 @@ def factor_calculate(**kwargs):
date_index = kwargs['date_index'] date_index = kwargs['date_index']
session = kwargs['session'] session = kwargs['session']
growth = Growth('factor_growth') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错 growth = Growth('factor_growth') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错
content = cache_data.get_cache(session, date_index) content = cache_data.get_cache(session + str(date_index), date_index)
total_growth_data = json_normalize(json.loads(str(content, encoding='utf8'))) total_growth_data = json_normalize(json.loads(str(content, encoding='utf8')))
print("len_total_growth_data {}".format(len(total_growth_data))) print("len_total_growth_data {}".format(len(total_growth_data)))
calculate(date_index, total_growth_data, growth) calculate(date_index, total_growth_data, growth)
......
...@@ -532,7 +532,7 @@ def factor_calculate(**kwargs): ...@@ -532,7 +532,7 @@ def factor_calculate(**kwargs):
date_index = kwargs['date_index'] date_index = kwargs['date_index']
session = kwargs['session'] session = kwargs['session']
per_share = PerShareIndicators('factor_per_share') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错 per_share = PerShareIndicators('factor_per_share') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错
content = cache_data.get_cache(session, date_index) content = cache_data.get_cache(session + str(date_index), date_index)
total_pre_share_data = json_normalize(json.loads(str(content, encoding='utf8'))) total_pre_share_data = json_normalize(json.loads(str(content, encoding='utf8')))
print("len_total_per_share_data {}".format(len(total_pre_share_data))) print("len_total_per_share_data {}".format(len(total_pre_share_data)))
calculate(date_index, total_pre_share_data, per_share) calculate(date_index, total_pre_share_data, per_share)
...@@ -391,9 +391,8 @@ def factor_calculate(**kwargs): ...@@ -391,9 +391,8 @@ def factor_calculate(**kwargs):
date_index = kwargs['date_index'] date_index = kwargs['date_index']
session = kwargs['session'] session = kwargs['session']
historical_value = HistoricalValue('factor_historical_value') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错 historical_value = HistoricalValue('factor_historical_value') # 注意, 这里的name要与client中新建table时的name一致, 不然回报错
content = cache_data.get_cache(session, date_index) content = cache_data.get_cache(session + str(date_index), date_index)
total_history_data = json_normalize(json.loads(str(content, encoding='utf8'))) total_history_data = json_normalize(json.loads(str(content, encoding='utf8')))
print("len_history_value_data {}".format(len(total_history_data))) print("len_history_value_data {}".format(len(total_history_data)))
calculate(date_index, total_history_data, historical_value) calculate(date_index, total_history_data, historical_value)
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