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

added timing

parent 517485f7
This diff is collapsed.
......@@ -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
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