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
d4379c3a
Unverified
Commit
d4379c3a
authored
Jan 08, 2018
by
Dr.李
Committed by
GitHub
Jan 08, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2 from lion-sing/master
update outright table structure as outright_tmp
parents
b899c787
9203f363
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
4 deletions
+60
-4
models.py
alphamind/data/dbmodel/models.py
+19
-0
sqlengine.py
alphamind/data/engines/sqlengine.py
+41
-4
No files found.
alphamind/data/dbmodel/models.py
View file @
d4379c3a
...
@@ -1932,6 +1932,25 @@ class Outright(Base):
...
@@ -1932,6 +1932,25 @@ class Outright(Base):
volume
=
Column
(
Integer
,
nullable
=
False
)
volume
=
Column
(
Integer
,
nullable
=
False
)
class
OutrightTmp
(
Base
):
__tablename__
=
'outright_tmp'
__table_args__
=
(
Index
(
'outright_trade_id_trade_date_code_portfolio_name_uindex'
,
'trade_id'
,
'trade_date'
,
'code'
,
'portfolio_name'
,
unique
=
True
),
)
trade_id
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
trade_date
=
Column
(
DateTime
,
primary_key
=
True
,
nullable
=
False
)
code
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
portfolio_name
=
Column
(
String
(
50
),
primary_key
=
True
,
nullable
=
False
)
volume
=
Column
(
Integer
,
nullable
=
False
)
operation
=
Column
(
String
(
10
),
nullable
=
False
)
interest_rate
=
Column
(
Float
,
nullable
=
False
)
price_rule
=
Column
(
String
(
50
),
nullable
=
False
)
due_date
=
Column
(
DateTime
)
remark
=
Column
(
Text
,
nullable
=
True
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
create_engine
...
...
alphamind/data/engines/sqlengine.py
View file @
d4379c3a
...
@@ -33,6 +33,7 @@ from alphamind.data.dbmodel.models import Formulas
...
@@ -33,6 +33,7 @@ from alphamind.data.dbmodel.models import Formulas
from
alphamind.data.dbmodel.models
import
DailyPortfoliosSchedule
from
alphamind.data.dbmodel.models
import
DailyPortfoliosSchedule
from
alphamind.data.dbmodel.models
import
Performance
from
alphamind.data.dbmodel.models
import
Performance
from
alphamind.data.dbmodel.models
import
Positions
from
alphamind.data.dbmodel.models
import
Positions
from
alphamind.data.dbmodel.models
import
OutrightTmp
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
encode_formula
...
@@ -792,6 +793,44 @@ class SqlEngine(object):
...
@@ -792,6 +793,44 @@ class SqlEngine(object):
index
=
False
,
index
=
False
,
dtype
=
{
'weight'
:
sa
.
types
.
JSON
})
dtype
=
{
'weight'
:
sa
.
types
.
JSON
})
def
fetch_outright_status
(
self
,
ref_date
:
str
):
table
=
OutrightTmp
t
=
select
([
table
.
trade_id
])
.
\
where
(
and_
(
table
.
trade_date
<=
ref_date
,
table
.
operation
==
'withdraw'
))
.
alias
(
't'
)
query
=
select
([
table
])
.
\
where
(
and_
(
table
.
trade_id
.
notin_
(
t
),
table
.
trade_date
<=
ref_date
))
df
=
pd
.
read_sql
(
query
,
engine
.
engine
)
.
set_index
(
'trade_id'
)
# calc total volume
df
[
'total_volume'
]
=
df
.
groupby
(
'trade_id'
)[
'volume'
]
.
transform
(
sum
)
# parse price
def
parse_price_rule
(
x
:
pd
.
Series
):
code
=
x
[
'code'
]
rule
=
x
[
'price_rule'
]
.
split
(
'@'
)
if
rule
[
0
]
in
[
'closePrice'
,
'openPrice'
]:
query
=
select
([
getattr
(
Market
,
rule
[
0
])])
.
\
where
(
and_
(
Market
.
code
==
code
,
Market
.
trade_date
==
rule
[
1
]))
data
=
pd
.
read_sql
(
query
,
engine
.
engine
)
price
=
data
.
values
[
0
][
0
]
elif
rule
[
0
]
==
'fixedPrice'
:
price
=
float
(
rule
[
1
])
else
:
raise
KeyError
(
'do not have rule for
%
s'
%
x
[
'price_rule'
])
return
price
df
[
'price'
]
=
df
.
apply
(
lambda
x
:
parse_price_rule
(
x
),
axis
=
1
)
df
.
drop
([
'remark'
,
'price_rule'
],
axis
=
1
,
inplace
=
True
)
# pivot portfolio volume
total_cols
=
df
.
columns
pivot_cols
=
[
'portfolio_name'
,
'volume'
]
tmp
=
df
[
pivot_cols
]
.
pivot
(
columns
=
'portfolio_name'
)
tmp
.
columns
=
tmp
.
columns
.
droplevel
(
0
)
res
=
df
[[
c
for
c
in
total_cols
if
c
not
in
pivot_cols
]]
.
drop_duplicates
()
.
join
(
tmp
)
.
reset_index
()
return
res
.
sort_values
([
'trade_id'
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
@@ -799,9 +838,7 @@ if __name__ == '__main__':
...
@@ -799,9 +838,7 @@ if __name__ == '__main__':
engine
=
SqlEngine
()
engine
=
SqlEngine
()
df
=
engine
.
fetch_factor_range
(
universe
,
df
=
engine
.
fetch_outright_status
(
'2017-12-28'
)
[
'closePrice'
],
start_date
=
'2015-12-21'
,
end_date
=
'2017-12-25'
)
print
(
df
)
print
(
df
)
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