Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
d0152b38
Commit
d0152b38
authored
Nov 27, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update formulas
parent
0ef22cb4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
35 deletions
+82
-35
models.py
alphamind/data/dbmodel/models.py
+8
-0
sqlengine.py
alphamind/data/engines/sqlengine.py
+39
-35
__init__.py
alphamind/formula/__init__.py
+0
-0
utilities.py
alphamind/formula/utilities.py
+35
-0
No files found.
alphamind/data/dbmodel/models.py
View file @
d0152b38
...
@@ -1883,6 +1883,14 @@ class IndexMarket(Base):
...
@@ -1883,6 +1883,14 @@ class IndexMarket(Base):
chgPct
=
Column
(
Float
(
53
))
chgPct
=
Column
(
Float
(
53
))
class
Formulas
(
Base
):
__tablename__
=
'formulas'
formula
=
Column
(
String
(
50
),
primary_key
=
True
)
formula_desc
=
Column
(
JSONB
,
nullable
=
False
)
comment
=
Column
(
Text
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
create_engine
...
...
alphamind/data/engines/sqlengine.py
View file @
d0152b38
...
@@ -14,7 +14,7 @@ import numpy as np
...
@@ -14,7 +14,7 @@ import numpy as np
import
pandas
as
pd
import
pandas
as
pd
import
sqlalchemy
as
sa
import
sqlalchemy
as
sa
import
sqlalchemy.orm
as
orm
import
sqlalchemy.orm
as
orm
from
sqlalchemy
import
select
,
and_
,
outerjoin
,
join
from
sqlalchemy
import
select
,
and_
,
outerjoin
,
join
,
delete
,
insert
from
sqlalchemy.sql
import
func
from
sqlalchemy.sql
import
func
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.dbmodel.models
import
FactorMaster
from
alphamind.data.dbmodel.models
import
FactorMaster
...
@@ -32,8 +32,11 @@ from alphamind.data.dbmodel.models import Models
...
@@ -32,8 +32,11 @@ from alphamind.data.dbmodel.models import Models
from
alphamind.data.dbmodel.models
import
Market
from
alphamind.data.dbmodel.models
import
Market
from
alphamind.data.dbmodel.models
import
IndexMarket
from
alphamind.data.dbmodel.models
import
IndexMarket
from
alphamind.data.dbmodel.models
import
Universe
as
UniverseTable
from
alphamind.data.dbmodel.models
import
Universe
as
UniverseTable
from
alphamind.data.dbmodel.models
import
Formulas
from
alphamind.data.transformer
import
Transformer
from
alphamind.data.transformer
import
Transformer
from
alphamind.model.loader
import
load_model
from
alphamind.model.loader
import
load_model
from
alphamind.formula.utilities
import
encode_formula
from
alphamind.formula.utilities
import
decode_formula
from
PyFin.api
import
advanceDateByCalendar
from
PyFin.api
import
advanceDateByCalendar
risk_styles
=
[
'BETA'
,
risk_styles
=
[
'BETA'
,
...
@@ -710,39 +713,40 @@ class SqlEngine(object):
...
@@ -710,39 +713,40 @@ class SqlEngine(object):
del
model_df
[
'model_desc'
]
del
model_df
[
'model_desc'
]
return
model_df
return
model_df
def
save_formula
(
self
,
formula_name
,
formula_obj
,
comment
=
None
):
dict_repr
=
encode_formula
(
formula
=
formula_obj
)
query
=
delete
(
Formulas
)
.
where
(
Formulas
.
formula
==
formula_name
)
self
.
engine
.
execute
(
query
)
query
=
insert
(
Formulas
,
values
=
dict
(
formula
=
formula_name
,
formula_desc
=
dict_repr
,
comment
=
comment
))
self
.
engine
.
execute
(
query
)
def
load_formula
(
self
,
formula_name
):
query
=
select
([
Formulas
])
.
where
(
Formulas
.
formula
==
formula_name
)
df
=
pd
.
read_sql
(
query
,
self
.
engine
)
if
not
df
.
empty
:
return
decode_formula
(
df
.
loc
[
0
,
'formula_desc'
][
'desc'
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
import
datetime
as
dt
from
PyFin.api
import
RES
,
LAST
from
PyFin.api
import
*
engine
=
SqlEngine
()
from
alphamind.api
import
alpha_logger
names
=
[
'eps_q'
,
'BDTO'
,
'CFinc1'
,
'CHV'
,
'IVR'
,
'VAL'
,
'GREV'
,
'DivP'
,
'Volatility'
]
# db_url = 'postgresql+psycopg2://postgres:we083826@localhost/alpha'
db_url
=
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
for
name
in
names
:
formula
=
RES
(
20
,
LAST
(
name
)
^
LAST
(
'roe_q'
))
universe
=
Universe
(
'custom'
,
[
'zz500'
])
engine
.
save_formula
(
f
'{name}_res'
,
formula
)
engine
=
SqlEngine
(
db_url
)
ref_date
=
'2017-08-02'
formula
=
RES
(
20
,
-
LAST
(
name
)
^
LAST
(
'roe_q'
))
start_date
=
'2017-01-01'
engine
.
save_formula
(
f
'{name}_neg_res'
,
formula
)
end_date
=
'2017-08-31'
\ No newline at end of file
dates
=
makeSchedule
(
start_date
,
end_date
,
'1w'
,
'china.sse'
)
alpha_logger
.
info
(
'start'
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
=
universe
)
data1
=
engine
.
fetch_data_experimental
(
ref_date
,
codes
=
codes
,
factors
=
[
'IVR'
])
alpha_logger
.
info
(
'end'
)
data2
=
engine
.
fetch_industry_range
(
universe
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
data3
=
engine
.
fetch_benchmark_range
(
905
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
data4
=
engine
.
fetch_risk_model_range
(
universe
=
universe
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
data2
=
engine
.
fetch_codes_range
(
universe
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
data2
=
engine
.
fetch_dx_return_range
(
universe
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
print
(
data1
)
alphamind/formula/__init__.py
0 → 100644
View file @
d0152b38
alphamind/formula/utilities.py
0 → 100644
View file @
d0152b38
# -*- coding: utf-8 -*-
"""
Created on 2017-11-27
@author: cheng.li
"""
import
pickle
import
base64
def
encode_formula
(
formula
):
encoded
=
base64
.
encodebytes
(
pickle
.
dumps
(
formula
))
str_repr
=
encoded
.
decode
(
'ascii'
)
return
{
'desc'
:
str_repr
,
'formula_type'
:
formula
.
__class__
.
__module__
+
"."
+
formula
.
__class__
.
__name__
,
'dependency'
:
formula
.
fields
,
'window'
:
formula
.
window
}
def
decode_formula
(
str_repr
):
encoded
=
str_repr
.
encode
(
'ascii'
)
formula
=
pickle
.
loads
(
base64
.
decodebytes
(
encoded
))
return
formula
if
__name__
==
'__main__'
:
from
PyFin.api
import
*
eps_q_res
=
RES
(
20
,
LAST
(
'eps_q'
)
^
LAST
(
'roe_q'
))
print
(
eps_q_res
)
str_repr
=
encode_formula
(
eps_q_res
)
decoded_formula
=
decode_formula
(
str_repr
)
print
(
decoded_formula
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment