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

update strategy

parent 8c79003a
...@@ -230,7 +230,7 @@ class SqlEngine(object): ...@@ -230,7 +230,7 @@ class SqlEngine(object):
risk_factors=df[neutralized_risks].values, risk_factors=df[neutralized_risks].values,
post_process=post_process) post_process=post_process)
return df[['code', 'dx']] return df[['code', 'dx']].drop_duplicates(['code'])
def fetch_dx_return_range(self, def fetch_dx_return_range(self,
universe, universe,
...@@ -271,7 +271,7 @@ class SqlEngine(object): ...@@ -271,7 +271,7 @@ class SqlEngine(object):
if dates: if dates:
df = df[df.trade_date.isin(dates)] df = df[df.trade_date.isin(dates)]
return df.sort_values(['trade_date', 'code']) return df.sort_values(['trade_date', 'code']).drop_duplicates(['trade_date', 'code'])
def fetch_dx_return_index(self, def fetch_dx_return_index(self,
ref_date: str, ref_date: str,
...@@ -373,14 +373,10 @@ class SqlEngine(object): ...@@ -373,14 +373,10 @@ class SqlEngine(object):
.set_index('trade_date') .set_index('trade_date')
res = transformer.transform('code', df).replace([-np.inf, np.inf], np.nan) res = transformer.transform('code', df).replace([-np.inf, np.inf], np.nan)
for col in res.columns: res['isOpen'] = df.isOpen.astype(bool)
if col not in set(['code', 'isOpen']) and col not in df.columns: res = res.loc[ref_date]
df[col] = res[col].values res.index = list(range(len(res)))
return df.drop_duplicates(['trade_date', 'code'])
df['isOpen'] = df.isOpen.astype(bool)
df = df.loc[ref_date]
df.index = list(range(len(df)))
return df
def fetch_factor_range(self, def fetch_factor_range(self,
universe: Universe, universe: Universe,
...@@ -441,13 +437,9 @@ class SqlEngine(object): ...@@ -441,13 +437,9 @@ class SqlEngine(object):
df.set_index('trade_date', inplace=True) df.set_index('trade_date', inplace=True)
res = transformer.transform('code', df).replace([-np.inf, np.inf], np.nan) res = transformer.transform('code', df).replace([-np.inf, np.inf], np.nan)
for col in res.columns: res['isOpen'] = df.isOpen.astype(bool)
if col not in set(['code', 'isOpen']) and col not in df.columns: res = res.reset_index()
df[col] = res[col].values return pd.merge(res, universe_df[['trade_date', 'code']], how='inner').drop_duplicates(['trade_date', 'code'])
df['isOpen'] = df.isOpen.astype(bool)
df = df.reset_index()
return pd.merge(df, universe_df[['trade_date', 'code']], how='inner')
def fetch_factor_range_forward(self, def fetch_factor_range_forward(self,
universe: Universe, universe: Universe,
...@@ -497,7 +489,7 @@ class SqlEngine(object): ...@@ -497,7 +489,7 @@ class SqlEngine(object):
df = pd.read_sql(query, self.engine) \ df = pd.read_sql(query, self.engine) \
.replace([-np.inf, np.inf], np.nan) \ .replace([-np.inf, np.inf], np.nan) \
.sort_values(['trade_date', 'code']) .sort_values(['trade_date', 'code'])
return pd.merge(df, codes[['trade_date', 'code']], how='inner') return pd.merge(df, codes[['trade_date', 'code']], how='inner').drop_duplicates(['trade_date', 'code'])
def fetch_benchmark(self, def fetch_benchmark(self,
ref_date: str, ref_date: str,
...@@ -571,7 +563,7 @@ class SqlEngine(object): ...@@ -571,7 +563,7 @@ class SqlEngine(object):
risk_exp = pd.read_sql(query, self.engine).dropna() risk_exp = pd.read_sql(query, self.engine).dropna()
return risk_cov, risk_exp return risk_cov, risk_exp.drop_duplicates(['code'])
def fetch_risk_model_range(self, def fetch_risk_model_range(self,
universe: Universe, universe: Universe,
...@@ -631,7 +623,7 @@ class SqlEngine(object): ...@@ -631,7 +623,7 @@ class SqlEngine(object):
risk_exp = pd.merge(risk_exp, codes, how='inner', on=['trade_date', 'code']).sort_values( risk_exp = pd.merge(risk_exp, codes, how='inner', on=['trade_date', 'code']).sort_values(
['trade_date', 'code']) ['trade_date', 'code'])
return risk_cov, risk_exp return risk_cov, risk_exp.drop_duplicates(['trade_date', 'code'])
def fetch_industry(self, def fetch_industry(self,
ref_date: str, ref_date: str,
...@@ -653,7 +645,7 @@ class SqlEngine(object): ...@@ -653,7 +645,7 @@ class SqlEngine(object):
) )
).distinct() ).distinct()
return pd.read_sql(query, self.engine).dropna() return pd.read_sql(query, self.engine).dropna().drop_duplicates(['code'])
def fetch_industry_matrix(self, def fetch_industry_matrix(self,
ref_date: str, ref_date: str,
...@@ -695,7 +687,7 @@ class SqlEngine(object): ...@@ -695,7 +687,7 @@ class SqlEngine(object):
if universe.is_filtered: if universe.is_filtered:
codes = universe.query(self, start_date, end_date, dates) codes = universe.query(self, start_date, end_date, dates)
df = pd.merge(df, codes, how='inner', on=['trade_date', 'code']).sort_values(['trade_date', 'code']) df = pd.merge(df, codes, how='inner', on=['trade_date', 'code']).sort_values(['trade_date', 'code'])
return df return df.drop_duplicates(['trade_date', 'code'])
def fetch_industry_matrix_range(self, def fetch_industry_matrix_range(self,
universe: Universe, universe: Universe,
...@@ -708,7 +700,7 @@ class SqlEngine(object): ...@@ -708,7 +700,7 @@ class SqlEngine(object):
df = self.fetch_industry_range(universe, start_date, end_date, dates, category, level) df = self.fetch_industry_range(universe, start_date, end_date, dates, category, level)
df['industry_name'] = df['industry'] df['industry_name'] = df['industry']
df = pd.get_dummies(df, columns=['industry'], prefix="", prefix_sep="") df = pd.get_dummies(df, columns=['industry'], prefix="", prefix_sep="")
return df.drop('industry_code', axis=1) return df.drop('industry_code', axis=1).drop_duplicates(['trade_date', 'code'])
def fetch_trade_status(self, def fetch_trade_status(self,
ref_date: str, ref_date: str,
...@@ -935,31 +927,6 @@ class SqlEngine(object): ...@@ -935,31 +927,6 @@ class SqlEngine(object):
self.engine.execute(query) self.engine.execute(query)
df.to_sql(Performance.__table__.name, self.engine, if_exists='append', index=False) df.to_sql(Performance.__table__.name, self.engine, if_exists='append', index=False)
def upsert_positions(self, ref_date, df):
universes = df.universe.unique().tolist()
benchmarks = df.benchmark.unique().tolist()
build_types = df.type.unique().tolist()
sources = df.source.unique().tolist()
portfolios = df.portfolio.unique().tolist()
query = delete(Positions).where(
and_(
Positions.trade_date == ref_date,
Positions.type.in_(build_types),
Positions.universe.in_(universes),
Positions.benchmark.in_(benchmarks),
Positions.source.in_(sources),
Positions.portfolio.in_(portfolios)
)
)
self.engine.execute(query)
df.to_sql(Positions.__table__.name,
self.engine,
if_exists='append',
index=False,
dtype={'weight': sa.types.JSON})
def fetch_outright_status(self, ref_date: str, is_open=True, ignore_internal_borrow=False): def fetch_outright_status(self, ref_date: str, is_open=True, ignore_internal_borrow=False):
table = Outright table = Outright
if is_open: if is_open:
......
...@@ -106,8 +106,6 @@ class LinearConstraints(object): ...@@ -106,8 +106,6 @@ class LinearConstraints(object):
bounds: Dict[str, BoxBoundary], bounds: Dict[str, BoxBoundary],
cons_mat: pd.DataFrame, cons_mat: pd.DataFrame,
backbone: np.ndarray=None): backbone: np.ndarray=None):
pyFinAssert(len(bounds) == cons_mat.shape[1], "Number of bounds should be same as number of col of cons_mat")
self.names = list(set(bounds.keys()).intersection(set(cons_mat.columns))) self.names = list(set(bounds.keys()).intersection(set(cons_mat.columns)))
self.bounds = bounds self.bounds = bounds
self.cons_mat = cons_mat self.cons_mat = cons_mat
......
...@@ -35,7 +35,7 @@ def linear_builder(er: np.ndarray, ...@@ -35,7 +35,7 @@ def linear_builder(er: np.ndarray,
if isinstance(ubound, float): if isinstance(ubound, float):
ubound = np.ones(n) * ubound ubound = np.ones(n) * ubound
if not turn_over_target: if not turn_over_target or current_position is None:
cons_matrix = np.concatenate((risk_constraints.T, risk_lbound, risk_ubound), axis=1) cons_matrix = np.concatenate((risk_constraints.T, risk_lbound, risk_ubound), axis=1)
opt = LPOptimizer(cons_matrix, lbound, ubound, -er, method) opt = LPOptimizer(cons_matrix, lbound, ubound, -er, method)
......
This diff is collapsed.
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