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

update formula expression example

parent 59cf08bb
...@@ -11,14 +11,15 @@ from PyFin.api import * ...@@ -11,14 +11,15 @@ from PyFin.api import *
from alphamind.api import * from alphamind.api import *
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
plt.style.use('ggplot')
# defind your alpha formula here import datetime as dt
base_factors = ['EPS', 'ROEDiluted', 'BTOP']#, 'ROEDiluted', 'VAL', 'CFinc1']
expression = 0. start = dt.datetime.now()
for name in base_factors: formula1 = CSRank(DIFF(LOG("turnoverValue")))
expression = expression + LAST(name) formula2 = CSRank((LAST('closePrice') - LAST('openPrice')) / LAST('openPrice'))
expression = -CORR(6, formula1 ^ formula2)
alpha_factor_name = 'alpha_factor' alpha_factor_name = 'alpha_factor'
alpha_factor = {alpha_factor_name: expression} alpha_factor = {alpha_factor_name: expression}
...@@ -29,18 +30,11 @@ engine = SqlEngine('postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha' ...@@ -29,18 +30,11 @@ engine = SqlEngine('postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
universe = Universe('custom', ['zz500']) universe = Universe('custom', ['zz500'])
benchmark_code = 905 benchmark_code = 905
neutralize_risk = ['SIZE'] + industry_styles neutralize_risk = ['SIZE'] + industry_styles
freq = '1w' freq = '1d'
n_bins = 5 n_bins = 5
if freq == '1m': dates = makeSchedule('2012-04-01',
horizon = 21 '2017-09-03',
elif freq == '1w':
horizon = 4
elif freq == '1d':
horizon = 0
dates = makeSchedule('2012-01-01',
'2012-08-01',
tenor=freq, tenor=freq,
calendar='china.sse') calendar='china.sse')
...@@ -48,7 +42,7 @@ factor_all_data = engine.fetch_data_range(universe, ...@@ -48,7 +42,7 @@ factor_all_data = engine.fetch_data_range(universe,
alpha_factor, alpha_factor,
dates=dates, dates=dates,
benchmark=905)['factor'] benchmark=905)['factor']
return_all_data = engine.fetch_dx_return_range(universe, dates=dates, horizon=horizon) return_all_data = engine.fetch_dx_return_range(universe, dates=dates, horizon=0)
factor_groups = factor_all_data.groupby('trade_date') factor_groups = factor_all_data.groupby('trade_date')
return_groups = return_all_data.groupby('trade_date') return_groups = return_all_data.groupby('trade_date')
...@@ -84,8 +78,10 @@ for i, value in enumerate(factor_groups): ...@@ -84,8 +78,10 @@ for i, value in enumerate(factor_groups):
df = pd.DataFrame(final_res, index=dates) df = pd.DataFrame(final_res, index=dates)
start_date = advanceDateByCalendar('china.sse', dates[0], '-1w') start_date = advanceDateByCalendar('china.sse', dates[0], '-1d')
df.loc[start_date] = 0. df.loc[start_date] = 0.
df.sort_index(inplace=True) df.sort_index(inplace=True)
df = df.cumsum().plot() df = df.cumsum().plot()
plt.show() plt.show()
print(dt.datetime.now() - start)
\ No newline at end of file
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