Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
F
fund_report
Project
Project
Details
Activity
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
彭熊
fund_report
Commits
39a9a9d0
Commit
39a9a9d0
authored
Dec 03, 2020
by
李宗熹
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
评价修改
parent
fc60482a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
203 additions
and
113 deletions
+203
-113
engine.py
app/api/engine.py
+12
-0
config.yaml
app/config/config.yaml
+1
-0
portfolio_diagnose.py
app/service/portfolio_diagnose.py
+96
-50
fund_rank.py
app/utils/fund_rank.py
+94
-63
No files found.
app/api/engine.py
View file @
39a9a9d0
...
@@ -57,6 +57,18 @@ tamp_user_engine = create_engine(
...
@@ -57,6 +57,18 @@ tamp_user_engine = create_engine(
),
),
echo
=
True
echo
=
True
)
)
tamp_fund_engine
=
create_engine
(
'mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset={charset}'
.
format
(
db
=
config
[
env
][
'MySQL'
][
'tamp_fund_db'
],
host
=
config
[
env
][
'MySQL'
][
'host'
],
port
=
config
[
env
][
'MySQL'
][
'port'
],
user
=
config
[
env
][
'MySQL'
][
'user'
],
password
=
config
[
env
][
'MySQL'
][
'password'
],
charset
=
"utf8"
),
echo
=
True
)
# tamp_product_session = scoped_session(sessionmaker(bind=tamp_product_engine))()
# tamp_product_session = scoped_session(sessionmaker(bind=tamp_product_engine))()
# tamp_order_session = scoped_session(sessionmaker(bind=tamp_order_engine))()
# tamp_order_session = scoped_session(sessionmaker(bind=tamp_order_engine))()
# tamp_user_session = scoped_session(sessionmaker(bind=tamp_user_engine))()
# tamp_user_session = scoped_session(sessionmaker(bind=tamp_user_engine))()
...
...
app/config/config.yaml
View file @
39a9a9d0
...
@@ -43,6 +43,7 @@ prod:
...
@@ -43,6 +43,7 @@ prod:
tamp_product_db
:
tamp_product
tamp_product_db
:
tamp_product
tamp_order_db
:
tamp_order
tamp_order_db
:
tamp_order
tamp_user_db
:
tamp_user
tamp_user_db
:
tamp_user
tamp_fund_db
:
tamp_fund
host
:
tamper.mysql.polardb.rds.aliyuncs.com
host
:
tamper.mysql.polardb.rds.aliyuncs.com
port
:
3306
port
:
3306
user
:
tamp_admin
user
:
tamp_admin
...
...
app/service/portfolio_diagnose.py
View file @
39a9a9d0
This diff is collapsed.
Click to expand it.
app/utils/fund_rank.py
View file @
39a9a9d0
# import pymysql
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
create_engine
db
=
create_engine
(
'mysql+pymysql://tamp_fund:@imeng408@tamper.mysql.polardb.rds.aliyuncs.com:3306/tamp_fund?charset=utf8mb4'
,
pool_size
=
50
,
pool_recycle
=
3600
,
pool_pre_ping
=
True
)
con
=
db
.
connect
()
import
logging
# db = create_engine(
# 'mysql+pymysql://tamp_fund:@imeng408@tamper.mysql.polardb.rds.aliyuncs.com:3306/tamp_fund?charset=utf8mb4',
# pool_size=50,
# pool_recycle=3600,
# pool_pre_ping=True)
# con = db.connect()
import
logging
logging
.
basicConfig
(
level
=
logging
.
INFO
)
logging
.
basicConfig
(
level
=
logging
.
INFO
)
from
app.api.engine
import
tamp_fund_engine
,
TAMP_SQL
from
app.utils.week_evaluation
import
*
from
app.utils.week_evaluation
import
*
...
@@ -35,32 +36,39 @@ def get_nav(fund, start_date, rollback=False, invest_type='public'):
...
@@ -35,32 +36,39 @@ def get_nav(fund, start_date, rollback=False, invest_type='public'):
Returns:df[DataFrame]: 索引为净值公布日, 列为复权净值的净值表; 查询失败则返回None
Returns:df[DataFrame]: 索引为净值公布日, 列为复权净值的净值表; 查询失败则返回None
"""
"""
if
invest_type
==
'public'
:
with
TAMP_SQL
(
tamp_fund_engine
)
as
tamp_product
:
sql
=
"SELECT ts_code, end_date, adj_nav FROM public_fund_nav "
\
tamp_product_session
=
tamp_product
.
session
"WHERE ts_code='{}'"
.
format
(
fund
)
if
invest_type
==
'public'
:
df
=
pd
.
read_sql
(
sql
,
con
)
.
dropna
(
how
=
'any'
)
sql
=
"SELECT ts_code, end_date, adj_nav FROM public_fund_nav "
\
df
.
rename
({
'ts_code'
:
'fund_id'
},
axis
=
1
,
inplace
=
True
)
"WHERE ts_code='{}'"
.
format
(
fund
)
else
:
cur
=
tamp_product_session
.
execute
(
sql
)
sql
=
"SELECT fund_id, price_date, cumulative_nav FROM fund_nav "
\
data
=
cur
.
fetchall
()
"WHERE fund_id='{}'"
.
format
(
fund
)
df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'ts_code'
,
'end_date'
,
'adj_nav'
])
.
dropna
(
how
=
'any'
)
df
=
pd
.
read_sql
(
sql
,
con
)
.
dropna
(
how
=
'any'
)
df
.
rename
({
'ts_code'
:
'fund_id'
},
axis
=
1
,
inplace
=
True
)
df
.
rename
({
'price_date'
:
'end_date'
,
'cumulative_nav'
:
'adj_nav'
},
axis
=
1
,
inplace
=
True
)
else
:
sql
=
"SELECT fund_id, price_date, cumulative_nav FROM fund_nav "
\
if
df
[
'adj_nav'
]
.
count
()
==
0
:
"WHERE fund_id='{}'"
.
format
(
fund
)
logging
.
log
(
logging
.
ERROR
,
"CAN NOT FIND {}"
.
format
(
fund
))
# df = pd.read_sql(sql, con).dropna(how='any')
return
None
cur
=
tamp_product_session
.
execute
(
sql
)
data
=
cur
.
fetchall
()
df
[
'end_date'
]
=
pd
.
to_datetime
(
df
[
'end_date'
])
df
=
pd
.
DataFrame
(
data
,
columns
=
[
'fund_id'
,
'price_date'
,
'cumulative_nav'
])
.
dropna
(
how
=
'any'
)
df
.
rename
({
'price_date'
:
'end_date'
,
'cumulative_nav'
:
'adj_nav'
},
axis
=
1
,
inplace
=
True
)
if
rollback
and
df
[
'end_date'
]
.
min
()
<
start_date
<
df
[
'end_date'
]
.
max
():
while
start_date
not
in
list
(
df
[
'end_date'
]):
if
df
[
'adj_nav'
]
.
count
()
==
0
:
start_date
-=
datetime
.
timedelta
(
days
=
1
)
logging
.
log
(
logging
.
ERROR
,
"CAN NOT FIND {}"
.
format
(
fund
))
return
None
df
=
df
[
df
[
'end_date'
]
>=
start_date
]
df
.
drop_duplicates
(
subset
=
'end_date'
,
inplace
=
True
,
keep
=
'first'
)
df
[
'end_date'
]
=
pd
.
to_datetime
(
df
[
'end_date'
])
df
.
set_index
(
'end_date'
,
inplace
=
True
)
df
.
sort_index
(
inplace
=
True
,
ascending
=
True
)
if
rollback
and
df
[
'end_date'
]
.
min
()
<
start_date
<
df
[
'end_date'
]
.
max
():
return
df
while
start_date
not
in
list
(
df
[
'end_date'
]):
start_date
-=
datetime
.
timedelta
(
days
=
1
)
df
=
df
[
df
[
'end_date'
]
>=
start_date
]
df
.
drop_duplicates
(
subset
=
'end_date'
,
inplace
=
True
,
keep
=
'first'
)
df
.
set_index
(
'end_date'
,
inplace
=
True
)
df
.
sort_index
(
inplace
=
True
,
ascending
=
True
)
return
df
def
get_frequency
(
df
):
def
get_frequency
(
df
):
...
@@ -97,11 +105,16 @@ def get_trade_cal():
...
@@ -97,11 +105,16 @@ def get_trade_cal():
Returns:df[DataFrame]: 索引为交易日, 列为交易日的上交所交易日历表
Returns:df[DataFrame]: 索引为交易日, 列为交易日的上交所交易日历表
"""
"""
sql
=
'SELECT cal_date FROM stock_trade_cal WHERE is_open=1'
with
TAMP_SQL
(
tamp_fund_engine
)
as
tamp_product
:
df
=
pd
.
read_sql
(
sql
,
con
)
tamp_product_session
=
tamp_product
.
session
df
[
'end_date'
]
=
pd
.
to_datetime
(
df
[
'cal_date'
])
sql
=
'SELECT cal_date FROM stock_trade_cal WHERE is_open=1'
df
.
set_index
(
'end_date'
,
drop
=
False
,
inplace
=
True
)
cur
=
tamp_product_session
.
execute
(
sql
)
return
df
data
=
cur
.
fetchall
()
df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'cal_date'
])
.
dropna
(
how
=
'all'
)
# df = pd.read_sql(sql, con)
df
[
'end_date'
]
=
pd
.
to_datetime
(
df
[
'cal_date'
])
df
.
set_index
(
'end_date'
,
drop
=
False
,
inplace
=
True
)
return
df
def
get_manager
(
invest_type
):
def
get_manager
(
invest_type
):
...
@@ -113,13 +126,21 @@ def get_manager(invest_type):
...
@@ -113,13 +126,21 @@ def get_manager(invest_type):
Returns:
Returns:
"""
"""
if
invest_type
==
'public'
:
with
TAMP_SQL
(
tamp_fund_engine
)
as
tamp_product
:
sql
=
'SELECT ts_code, name FROM public_fund_manager WHERE end_date IS NULL'
tamp_product_session
=
tamp_product
.
session
df
=
pd
.
read_sql
(
sql
,
con
)
if
invest_type
==
'public'
:
else
:
sql
=
'SELECT ts_code, name FROM public_fund_manager WHERE end_date IS NULL'
sql
=
'SELECT fund_id, fund_manager_id FROM fund_manager_mapping'
# df = pd.read_sql(sql, con)
df
=
pd
.
read_sql
(
sql
,
con
)
cur
=
tamp_product_session
.
execute
(
sql
)
return
df
data
=
cur
.
fetchall
()
df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'ts_code'
,
'name'
])
else
:
sql
=
'SELECT fund_id, fund_manager_id FROM fund_manager_mapping'
# df = pd.read_sql(sql, con)
cur
=
tamp_product_session
.
execute
(
sql
)
data
=
cur
.
fetchall
()
df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'fund_id'
,
'fund_manager_id'
])
return
df
def
get_fund_info
(
end_date
,
invest_type
):
def
get_fund_info
(
end_date
,
invest_type
):
...
@@ -132,23 +153,33 @@ def get_fund_info(end_date, invest_type):
...
@@ -132,23 +153,33 @@ def get_fund_info(end_date, invest_type):
Returns:
Returns:
[type]: [description]
[type]: [description]
"""
"""
if
invest_type
==
'public'
:
with
TAMP_SQL
(
tamp_fund_engine
)
as
tamp_product
:
sql
=
"SELECT ts_code, fund_type, management FROM public_fund_basic "
\
tamp_product_session
=
tamp_product
.
session
"WHERE delist_date IS NULL AND (due_date IS NULL OR due_date>'{}')"
.
format
(
end_date
.
strftime
(
'
%
Y
%
m
%
d'
))
if
invest_type
==
'public'
:
df
=
pd
.
read_sql
(
sql
,
con
)
.
dropna
(
how
=
'all'
)
sql
=
"SELECT ts_code, fund_type, management FROM public_fund_basic "
\
manager_info
=
get_manager
(
invest_type
)
"WHERE delist_date IS NULL AND (due_date IS NULL OR due_date>'{}')"
.
format
(
end_date
.
strftime
(
'
%
Y
%
m
%
d'
))
# df = pd.read_sql(sql, con).dropna(how='all')
df
.
rename
({
'ts_code'
:
'fund_id'
},
axis
=
1
,
inplace
=
True
)
cur
=
tamp_product_session
.
execute
(
sql
)
df
=
pd
.
merge
(
df
,
manager_info
,
how
=
"left"
,
on
=
'fund_id'
)
data
=
cur
.
fetchall
()
else
:
sql
=
"SELECT id, substrategy FROM fund_info WHERE delete_tag=0 "
\
df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'ts_code'
,
'fund_type'
,
'management'
])
"AND substrategy!=-1"
manager_info
=
get_manager
(
invest_type
)
df
=
pd
.
read_sql
(
sql
,
con
)
.
dropna
(
how
=
'all'
)
df
.
rename
({
'ts_code'
:
'fund_id'
},
axis
=
1
,
inplace
=
True
)
df
=
pd
.
merge
(
df
,
manager_info
,
how
=
"left"
,
on
=
'fund_id'
)
else
:
df
.
rename
({
'id'
:
'fund_id'
},
axis
=
1
,
inplace
=
True
)
sql
=
"SELECT id, substrategy FROM fund_info WHERE delete_tag=0 "
\
manager_info
=
get_manager
(
invest_type
)
"AND substrategy!=-1"
df
=
pd
.
merge
(
df
,
manager_info
,
how
=
"inner"
,
on
=
'fund_id'
)
cur
=
tamp_product_session
.
execute
(
sql
)
return
df
data
=
cur
.
fetchall
()
df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'id'
,
'substrategy'
])
# df = pd.read_sql(sql, con).dropna(how='all')
df
.
rename
({
'id'
:
'fund_id'
},
axis
=
1
,
inplace
=
True
)
manager_info
=
get_manager
(
invest_type
)
df
=
pd
.
merge
(
df
,
manager_info
,
how
=
"inner"
,
on
=
'fund_id'
)
return
df
def
resample
(
df
,
trading_cal
,
freq
,
simple_flag
=
True
):
def
resample
(
df
,
trading_cal
,
freq
,
simple_flag
=
True
):
...
@@ -301,4 +332,4 @@ if __name__ == '__main__':
...
@@ -301,4 +332,4 @@ if __name__ == '__main__':
# fund_rank.to_csv("fund_rank.csv", encoding='gbk')
# fund_rank.to_csv("fund_rank.csv", encoding='gbk')
# df = pd.read_csv('fund_rank.csv')
# df = pd.read_csv('fund_rank.csv')
# df.to_sql("fund_rank", con, if_exists='replace')
# df.to_sql("fund_rank", con, if_exists='replace')
con
.
close
()
#
con.close()
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