Commit c2f950d3 authored by Dr.李's avatar Dr.李

update example

parent 8ae8ce45
......@@ -12,6 +12,7 @@ import numpy as np
import pandas as pd
import sqlalchemy as sa
from alphamind.data.engines.universe import Universe
from PyFin.api import advanceDateByCalendar
risk_styles = ['BETA',
'MOMENTUM',
......@@ -102,10 +103,21 @@ class SqlEngine(object):
return sorted(codes_set)
def fetch_d1_return(self, ref_date, codes):
def fetch_dx_return(self, ref_date, codes, expiry_date=None, horizon=1):
start_date = ref_date
if not expiry_date and horizon:
end_date = advanceDateByCalendar('china.sse', ref_date, str(horizon) + 'b').strftime('%Y%m%d')
elif expiry_date:
end_date = expiry_date
codes_str = ','.join(str(c) for c in codes)
sql = "select Code, d1 from daily_return where Date = '{ref_date}' and Code in ({codes})".format(ref_date=ref_date,
codes=codes_str)
sql = "select Code, sum(d1) as dx from daily_return " \
"where Date >= '{start_date}' and Date < '{end_date}'" \
" and Code in ({codes}) GROUP BY Code".format(start_date=start_date,
end_date=end_date,
codes=codes_str)
return pd.read_sql(sql, self.engine)
def fetch_data(self, ref_date,
......
......@@ -9,49 +9,55 @@ import pandas as pd
from alphamind.analysis.factoranalysis import factor_analysis
from alphamind.data.engines.sqlengine import SqlEngine
from alphamind.data.engines.universe import Universe
from alphamind.data.engines.sqlengine import risk_styles
from alphamind.data.engines.sqlengine import industry_styles
from PyFin.api import bizDatesList
engine = SqlEngine('mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha')
universe = Universe('custom', ['zz500'])
total_risks = risk_styles + industry_styles
type = 'risk_neutral'
used_risk_styles = ['SIZE']
def calculate_one_day(ref_date, factors, weights):
total_risks = used_risk_styles + industry_styles
build_type = 'risk_neutral'
def calculate_one_day(ref_date, factors, factor_weights):
print(ref_date)
codes = engine.fetch_codes(ref_date, universe)
total_data = engine.fetch_data(ref_date, factors, codes, 905)
factor_data = total_data['factor']
factor_df = factor_data[['Code', 'industry', 'd1', 'weight', 'isOpen'] + total_risks + factors].dropna()
factor_df = factor_data[['Code', 'industry', 'weight', 'isOpen'] + total_risks + factors].dropna()
dx_return = engine.fetch_dx_return(ref_date, codes)
factor_df = pd.merge(factor_df, dx_return, on=['Code'])
weights, _ = factor_analysis(factor_df[factors],
weights,
factor_weights,
factor_df.industry.values,
factor_df.d1.values,
None,
detail_analysis=False,
benchmark=factor_df.weight.values,
risk_exp=factor_df[total_risks].values,
is_tradable=factor_df.isOpen.values,
method=type)
return ref_date, (weights.weight - factor_df.weight).dot(factor_df.d1)
is_tradable=factor_df.isOpen.values.astype(bool),
method=build_type)
return ref_date, (weights.weight - factor_df.weight).dot(factor_df.dx)
if __name__ == '__main__':
from matplotlib import pyplot as plt
factors = ['EPS']#, 'FY12P', 'VAL', 'CFinc1', 'BDTO', 'RVOL']
weights = [1.]#, 1., 1., 1., 0.5, 0.5]
biz_dates = bizDatesList('china.sse', '2013-01-01', '2017-07-05')
factors = ['BDTO', 'CFinc1', 'DivP', 'EPS', 'RVOL', 'DROEAfterNonRecurring']
factor_weights = [0.10, 0.30, 0.15, 0.18, 0.11, 0.35]
biz_dates = bizDatesList('china.sse', '2017-01-01', '2017-07-07')
ers = []
dates = []
for ref_date in biz_dates:
ref_date, er = calculate_one_day(ref_date, factors, weights)
ref_date, er = calculate_one_day(ref_date, factors, factor_weights)
dates.append(ref_date)
ers.append(er)
......
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