Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
F
fof
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.李
fof
Commits
df441253
Commit
df441253
authored
Mar 26, 2022
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added timing
parent
517485f7
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
9 deletions
+38
-9
010 择时能力.ipynb
20220326/010 择时能力.ipynb
+0
-0
utility.py
fof/utility.py
+38
-9
No files found.
20220326/010 择时能力.ipynb
0 → 100644
View file @
df441253
This diff is collapsed.
Click to expand it.
fof/utility.py
View file @
df441253
...
...
@@ -3,6 +3,7 @@ import calendar
import
pymssql
import
sqlalchemy
as
sa
import
numpy
as
np
import
pandas
as
pd
...
...
@@ -63,7 +64,7 @@ def read_sql(query, source="mssql"):
return
pd
.
read_sql
(
query
,
con
=
engine
)
def
nearest_report_date
(
date
,
freq
=
"Q"
):
def
nearest_report_date
(
date
,
freq
=
"Q"
,
method
=
"forward"
):
if
len
(
date
)
==
10
:
date
=
date
.
replace
(
"-"
,
""
)
...
...
@@ -71,10 +72,15 @@ def nearest_report_date(date, freq="Q"):
year
=
int
(
date
[
0
:
4
])
last_year
=
year
if
method
==
"forward"
:
add
=
1
else
:
add
=
0
if
freq
==
"Q"
:
q
=
(
mm
//
3
+
1
)
*
3
-
3
q
=
(
mm
//
3
+
add
)
*
3
-
3
else
:
q
=
(
mm
//
6
+
1
)
*
6
-
6
q
=
(
mm
//
6
+
add
)
*
6
-
6
if
q
<=
0
:
q
+=
12
...
...
@@ -217,8 +223,8 @@ def fd_assetportfolio(security_ids, report_dates_begin, report_dates_end=None):
query
=
f
"""
SELECT SECODE as SECURITYID, REPORTDATE, BDRTO, CONVBDRTO, EQUITYINVERTO FROM TQ_FD_ASSETPORTFOLIO
WHERE
REPORTDATE >= '{
report_dates_begin
}' AND
REPORTDATE <= '{
report_dates_end
}' AND
REPORTDATE >= '{
_to_yyyymmdd(report_dates_begin)
}' AND
REPORTDATE <= '{
_to_yyyymmdd(report_dates_end)
}' AND
ISVALID = 1 AND
SECODE in ({sec_id_strs})
"""
...
...
@@ -277,15 +283,21 @@ def fd_skdetail(security_ids, trade_dt):
p_id_strs
=
_join_ids
(
security_ids
)
query
=
f
"""
SELECT SECODE as pid, SKCODE as security_code, SKNAME, HOLDMKTCAP, HOLDAMT, NAVRTO FROM TQ_FD_SKDETAIL
SELECT SECODE as pid,
PUBLISHDATE,
SKCODE as security_code, SKNAME, HOLDMKTCAP, HOLDAMT, NAVRTO FROM TQ_FD_SKDETAIL
WHERE
ENDDATE = '{trade_dt}' AND
ISVALID = 1 AND
SECODE in ({p_id_strs})
"""
df
=
read_sql
(
query
)
.
sort_values
(
"security_code"
)
total_value
=
df
.
HOLDMKTCAP
.
sum
()
df
[
"weight"
]
=
df
.
HOLDMKTCAP
/
total_value
groups
=
read_sql
(
query
)
.
sort_values
(
"security_code"
)
.
groupby
(
"pid"
)
dfs
=
[]
for
k
,
g
in
groups
:
g
=
g
[
g
.
PUBLISHDATE
==
np
.
max
(
g
.
PUBLISHDATE
)]
total_value
=
g
.
HOLDMKTCAP
.
sum
()
g
[
"weight"
]
=
g
.
HOLDMKTCAP
/
total_value
dfs
.
append
(
g
)
df
=
pd
.
concat
(
dfs
)
del
df
[
"PUBLISHDATE"
]
return
df
.
reset_index
(
drop
=
True
)
...
...
@@ -530,3 +542,20 @@ def read_factors(security_ids, trade_dt, factors):
flag = 1
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
.
reset_index
(
drop
=
True
)
def
index_rtn
(
security_ids
,
begin_date
,
end_date
):
sec_id_strs
=
_join_ids
(
security_ids
)
query
=
f
"""
SELECT security_code, min(trade_date) AS begin_date, max(trade_date) AS end_date, exp(sum(ln(1 + change_pct / 100.))) - 1.0 AS rtn FROM index_daily_price
WHERE
trade_date >= '{_to_yyyy_mm_dd(begin_date)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
security_code in ({sec_id_strs}) AND
flag = 1
GROUP BY
security_code
"""
df
=
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
.
reset_index
(
drop
=
True
)
return
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