Commit 8be2302c authored by Dr.李's avatar Dr.李

FIX: added industry range and industry matrix

parent a29ba940
......@@ -26,7 +26,8 @@ from PyFin.api import advanceDateByCalendar
from alphamind.data.dbmodel.models_rl import (
Market,
Industry
Industry,
RiskExposure
)
if "DB_VENDOR" in os.environ and os.environ["DB_VENDOR"].lower() == "rl":
......@@ -188,6 +189,92 @@ class SqlEngine:
return pd.read_sql(query, self.engine).dropna().drop_duplicates(['code'])
def fetch_industry_matrix(self,
ref_date: str,
codes: Iterable[int] = None,
category: str = 'sw',
level: int = 1):
df = self.fetch_industry(ref_date, codes, category, level)
df['industry_name'] = df['industry']
df = pd.get_dummies(df, columns=['industry'], prefix="", prefix_sep="")
return df.drop('industry_code', axis=1)
def fetch_industry_range(self,
universe: Universe,
start_date: str = None,
end_date: str = None,
dates: Iterable[str] = None,
category: str = 'sw',
level: int = 1):
code_name = 'industry_code' + str(level)
category_name = 'industry_name' + str(level)
cond = universe._query_statements(start_date, end_date, dates)
query = select([Industry.code.label("code"),
Industry.trade_date,
getattr(Industry, code_name).label('industry_code'),
getattr(Industry, category_name).label('industry')]).where(
and_(
*cond,
Industry.code == UniverseTable.code,
Industry.trade_date == UniverseTable.trade_date,
Industry.flag == 1
)
).distinct()
return pd.read_sql(query, self.session.bind)
def fetch_risk_model(self,
ref_date: str,
codes: Iterable[int],
risk_model: str = 'short',
excluded: Iterable[str] = None,
model_type: str = None) -> Union[
FactorRiskModel, Tuple[pd.DataFrame, pd.DataFrame]]:
risk_cov_table, special_risk_table = _map_risk_model_table(risk_model)
cov_risk_cols = [risk_cov_table.__table__.columns[f] for f in total_risk_factors]
query = select([risk_cov_table.FactorID,
risk_cov_table.Factor]
+ cov_risk_cols).where(
risk_cov_table.trade_date == ref_date
)
risk_cov = pd.read_sql(query, self.engine).sort_values('FactorID')
if excluded:
risk_exposure_cols = [RiskExposure.__table__.columns[f] for f in total_risk_factors if
f not in set(excluded)]
else:
risk_exposure_cols = [RiskExposure.__table__.columns[f] for f in total_risk_factors]
big_table = join(RiskExposure,
special_risk_table,
and_(
RiskExposure.code == special_risk_table.code,
RiskExposure.trade_date == special_risk_table.trade_date
))
query = select(
[RiskExposure.code, special_risk_table.SRISK.label('srisk')] + risk_exposure_cols) \
.select_from(big_table).where(
and_(RiskExposure.trade_date == ref_date,
RiskExposure.code.in_(codes)
))
risk_exp = pd.read_sql(query, self.engine).dropna()
if not model_type:
return risk_cov, risk_exp
elif model_type == 'factor':
factor_names = risk_cov.Factor.tolist()
new_risk_cov = risk_cov.set_index('Factor')
factor_cov = new_risk_cov.loc[factor_names, factor_names] / 10000.
new_risk_exp = risk_exp.set_index('code')
factor_loading = new_risk_exp.loc[:, factor_names]
idsync = new_risk_exp['srisk'] * new_risk_exp['srisk'] / 10000
return FactorRiskModel(factor_cov, factor_loading, idsync), risk_cov, risk_exp
if __name__ == "__main__":
db_url = "mysql+mysqldb://reader:Reader#2020@121.37.138.1:13317/vision?charset=utf8"
......@@ -196,7 +283,7 @@ if __name__ == "__main__":
universe = Universe("hs300")
start_date = '2020-09-29'
end_date = '2020-10-10'
df = sql_engine.fetch_codes_range(start_date='start_date', end_date=end_date, universe=Universe("hs300"))
df = sql_engine.fetch_codes_range(start_date=start_date, end_date=end_date, universe=Universe("hs300"))
print(df)
df = sql_engine.fetch_dx_return("2020-10-09", codes=["2010031963"])
print(df)
......@@ -204,3 +291,7 @@ if __name__ == "__main__":
print(df)
df = sql_engine.fetch_industry(ref_date="2020-10-09", codes=["2010031963"])
print(df)
df = sql_engine.fetch_industry_matrix(ref_date="2020-10-09", codes=["2010031963"])
print(df)
df = sql_engine.fetch_industry_range(start_date=start_date, end_date=end_date, universe=Universe("hs300"))
print(df)
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