gjdatac-api 0.3.9__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/PKG-INFO +1 -1
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api/__init__.py +1 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api/data_api_class.py +171 -133
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api.egg-info/PKG-INFO +1 -1
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/setup.py +2 -2
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api/dict_data.py +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api/dolphindb_info.py +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api/instrument_class.py +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api.egg-info/SOURCES.txt +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api.egg-info/dependency_links.txt +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api.egg-info/requires.txt +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/gjdatac_api.egg-info/top_level.txt +0 -0
- {gjdatac_api-0.3.9 → gjdatac_api-0.4.0}/setup.cfg +0 -0
|
@@ -1078,7 +1078,7 @@ class GJDataC:
|
|
|
1078
1078
|
option_contract_db_table_name,
|
|
1079
1079
|
option_contract_db_path)
|
|
1080
1080
|
data["type"] = "option"
|
|
1081
|
-
data["exercise_type"] = data["option_type"]
|
|
1081
|
+
data["exercise_type"] = data["option_type"] if "option_type" in data.columns else ""
|
|
1082
1082
|
else:
|
|
1083
1083
|
raise Exception("asset_type is not valid")
|
|
1084
1084
|
data["commodity"] = ""
|
|
@@ -1132,8 +1132,11 @@ class GJDataC:
|
|
|
1132
1132
|
try:
|
|
1133
1133
|
unique_ids = data['order_book_id'].unique().tolist()
|
|
1134
1134
|
|
|
1135
|
+
if len(unique_ids) == 0:
|
|
1136
|
+
return data
|
|
1137
|
+
|
|
1135
1138
|
# 根据合约类型选择基础信息表
|
|
1136
|
-
if 'type' in data.columns and data['type'].iloc[0] == 'option':
|
|
1139
|
+
if 'type' in data.columns and len(data) > 0 and data['type'].iloc[0] == 'option':
|
|
1137
1140
|
table_name = option_contract_db_table_name
|
|
1138
1141
|
else:
|
|
1139
1142
|
table_name = future_contract_db_table_name
|
|
@@ -1393,7 +1396,11 @@ class GJDataC:
|
|
|
1393
1396
|
|
|
1394
1397
|
"""
|
|
1395
1398
|
|
|
1396
|
-
#
|
|
1399
|
+
# 获取开始日期和结束日期(先确保是字符串格式)
|
|
1400
|
+
if not isinstance(start_date, str):
|
|
1401
|
+
start_date = str(int(start_date)) if not pd.isna(start_date) else ""
|
|
1402
|
+
if not isinstance(end_date, str):
|
|
1403
|
+
end_date = str(int(end_date)) if not pd.isna(end_date) else ""
|
|
1397
1404
|
start_date = datetime.datetime.strptime(start_date, "%Y%m%d").date()
|
|
1398
1405
|
end_date = datetime.datetime.strptime(end_date, "%Y%m%d").date()
|
|
1399
1406
|
|
|
@@ -1417,32 +1424,25 @@ class GJDataC:
|
|
|
1417
1424
|
|
|
1418
1425
|
cn_holidays = holidays.CN() # 创建中国节假日对象
|
|
1419
1426
|
|
|
1427
|
+
# 提取当前日期的匹配行,避免重复筛选
|
|
1428
|
+
matched_row = trading_params_data.loc[trading_params_data["trading_day_str"] == _date]
|
|
1429
|
+
if matched_row.empty:
|
|
1430
|
+
raise ValueError(f"Date {_date} not found in trading_params_data")
|
|
1431
|
+
|
|
1420
1432
|
time_period_mapping_dict = {
|
|
1421
1433
|
"0": _date in cn_holidays,
|
|
1422
|
-
"1":
|
|
1423
|
-
"2":
|
|
1434
|
+
"1": matched_row["next_trading_day"].iloc[0],
|
|
1435
|
+
"2": matched_row["pre_trading_day"].iloc[0],
|
|
1424
1436
|
"3": self.get_date_get_week_month_year(trading_params_data,
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
"first_trading_day_week"].iloc[0],
|
|
1428
|
-
trading_params_data.loc[
|
|
1429
|
-
trading_params_data["trading_day_str"] == _date][
|
|
1430
|
-
"last_trading_day_week"].iloc[0]),
|
|
1437
|
+
matched_row["first_trading_day_week"].iloc[0],
|
|
1438
|
+
matched_row["last_trading_day_week"].iloc[0]),
|
|
1431
1439
|
"4": self.get_date_get_week_month_year(trading_params_data,
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
"first_trading_day_month"].iloc[0],
|
|
1435
|
-
trading_params_data.loc[
|
|
1436
|
-
trading_params_data["trading_day_str"] == _date][
|
|
1437
|
-
"last_trading_day_month"].iloc[0]),
|
|
1440
|
+
matched_row["first_trading_day_month"].iloc[0],
|
|
1441
|
+
matched_row["last_trading_day_month"].iloc[0]),
|
|
1438
1442
|
"5": self.get_date_get_week_month_year(trading_params_data,
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
trading_params_data.loc[
|
|
1443
|
-
trading_params_data["trading_day_str"] == _date][
|
|
1444
|
-
"last_trading_day_year"].iloc[0]),
|
|
1445
|
-
"6": trading_params_data.loc[trading_params_data["trading_day_str"] == _date]["night_day"].iloc[0],
|
|
1443
|
+
matched_row["first_trading_day_year"].iloc[0],
|
|
1444
|
+
matched_row["last_trading_day_year"].iloc[0]),
|
|
1445
|
+
"6": matched_row["night_day"].iloc[0],
|
|
1446
1446
|
}
|
|
1447
1447
|
|
|
1448
1448
|
if _n not in time_period_mapping_dict:
|
|
@@ -1889,24 +1889,116 @@ class GJDataC:
|
|
|
1889
1889
|
|
|
1890
1890
|
return new_df
|
|
1891
1891
|
|
|
1892
|
-
"""
|
|
1892
|
+
""" get_main_price 主力/次主力合约 """
|
|
1893
1893
|
|
|
1894
|
-
def
|
|
1895
|
-
|
|
1896
|
-
fields=None, adjust_type=None, adjust_method=None, source=None):
|
|
1894
|
+
def get_main(self, commodity=None, start_date=None, end_date=None,
|
|
1895
|
+
compute_type=None, source=None, fields=None):
|
|
1897
1896
|
"""
|
|
1898
|
-
|
|
1899
|
-
:param commodity: str or str list
|
|
1900
|
-
:param start_date: date
|
|
1901
|
-
:param end_date: date
|
|
1902
|
-
:param
|
|
1903
|
-
:param
|
|
1897
|
+
获取主力合约(qc_gtja_future_maincontract)
|
|
1898
|
+
:param commodity: str or str list--必填,品种代码,如 'CU'
|
|
1899
|
+
:param start_date: date--选填,开始时间
|
|
1900
|
+
:param end_date: date--选填,结束时间
|
|
1901
|
+
:param compute_type: str--必填,'main'=主力 / 'sub_main'=次主力
|
|
1902
|
+
:param source: str--选填,返回source字段里的值,默认 gtja
|
|
1903
|
+
:param fields: str or str list--选填,返回字段筛选,默认全部
|
|
1904
|
+
|
|
1905
|
+
:return: DataFrame with columns: trading_day, exchange_id, commodity, order_book_id, compute_type, source, fields(float)
|
|
1906
|
+
"""
|
|
1907
|
+
""" 校验参数 """
|
|
1908
|
+
self.general_validate_params_required(commodity, "commodity")
|
|
1909
|
+
self.general_validate_params_required(compute_type, "compute_type")
|
|
1910
|
+
self.general_validate_field_str_or_list(commodity, "commodity")
|
|
1911
|
+
|
|
1912
|
+
if start_date and end_date:
|
|
1913
|
+
if not self.validate_dates(start_date, end_date):
|
|
1914
|
+
return None
|
|
1915
|
+
elif not start_date and not end_date:
|
|
1916
|
+
start_date = datetime.date.today()
|
|
1917
|
+
end_date = datetime.date.today()
|
|
1918
|
+
|
|
1919
|
+
# 校验 compute_type 值
|
|
1920
|
+
valid_compute_types = {"main": "MAIN", "sub_main": "SUB_MAIN"}
|
|
1921
|
+
if compute_type not in valid_compute_types:
|
|
1922
|
+
raise ValueError(f"compute_type must be one of {list(valid_compute_types.keys())}, got '{compute_type}'")
|
|
1923
|
+
rank_value = valid_compute_types[compute_type]
|
|
1924
|
+
|
|
1925
|
+
# 默认 source
|
|
1926
|
+
source = source or "gtja"
|
|
1927
|
+
|
|
1928
|
+
""" 查询主表 qc_gtja_future_maincontract """
|
|
1929
|
+
table_name, db_path = main_contract_db_table_name, main_contract_db_path
|
|
1930
|
+
data, db_session = self.connect_db(table_name, db_path)
|
|
1931
|
+
|
|
1932
|
+
# 按品种筛选 (unify_product_id)
|
|
1933
|
+
code_data = data.where(f"unify_product_id in {commodity}") if isinstance(commodity, list) else \
|
|
1934
|
+
data.where(f"unify_product_id = '{commodity}'")
|
|
1935
|
+
|
|
1936
|
+
# 按日期筛选
|
|
1937
|
+
start_dolphin_date = self.format_date(start_date)
|
|
1938
|
+
end_dolphin_date = self.format_date(end_date)
|
|
1939
|
+
date_data = code_data.where(f"trading_day >= {start_dolphin_date} and trading_day <= {end_dolphin_date}")
|
|
1940
|
+
|
|
1941
|
+
# 按 compute_type 筛选
|
|
1942
|
+
result_data = date_data.where(f"compute_type = '{rank_value}'")
|
|
1943
|
+
|
|
1944
|
+
result = result_data.toDF()
|
|
1945
|
+
db_session.close()
|
|
1946
|
+
|
|
1947
|
+
""" 筛选字段并重命名(按截图出参)"""
|
|
1948
|
+
# 截图出参字段:trading_day, exchange_id, commodity(=unify_product_id),
|
|
1949
|
+
# order_book_id(=instrument_id), compute_type, source, fields(=open/收盘等数值列)
|
|
1950
|
+
rename_map = {
|
|
1951
|
+
"unify_product_id": "commodity",
|
|
1952
|
+
"instrument_id": "order_book_id",
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
# 确定返回列:固定字段 + 数值(fields)列
|
|
1956
|
+
fixed_cols = ["trading_day", "exchange_id", "unify_product_id", "instrument_id", "compute_type", "source"]
|
|
1957
|
+
numeric_cols = ["open", "high", "low", "close", "volume", "amount", "oi",
|
|
1958
|
+
"post_adj_factor_of_prev_close_spread", "post_adj_factor_of_open_spread",
|
|
1959
|
+
"post_adj_factor_of_prev_close_ratio", "post_adj_factor_of_open_ratio_ratio"]
|
|
1960
|
+
|
|
1961
|
+
available_numeric = [c for c in numeric_cols if c in result.columns]
|
|
1962
|
+
all_return_cols = [c for c in fixed_cols if c in result.columns] + available_numeric
|
|
1963
|
+
|
|
1964
|
+
if fields:
|
|
1965
|
+
# 用户指定字段:处理别名映射
|
|
1966
|
+
field_list = [fields] if isinstance(fields, str) else list(fields)
|
|
1967
|
+
resolved_fields = []
|
|
1968
|
+
for f in field_list:
|
|
1969
|
+
if f in rename_map.values():
|
|
1970
|
+
# 反向查找原始列名
|
|
1971
|
+
orig = [k for k, v in rename_map.items() if v == f][0]
|
|
1972
|
+
resolved_fields.append(orig)
|
|
1973
|
+
else:
|
|
1974
|
+
resolved_fields.append(f)
|
|
1975
|
+
result = result[resolved_fields]
|
|
1976
|
+
else:
|
|
1977
|
+
result = result[all_return_cols]
|
|
1978
|
+
|
|
1979
|
+
# 重命名
|
|
1980
|
+
for orig, new in rename_map.items():
|
|
1981
|
+
if orig in result.columns:
|
|
1982
|
+
result.rename(columns={orig: new}, inplace=True)
|
|
1983
|
+
|
|
1984
|
+
return result
|
|
1985
|
+
|
|
1986
|
+
def get_main_price(self, commodity=None, start_date=datetime.date.today(),
|
|
1987
|
+
end_date=datetime.date.today(), frequency=None, compute_type=None,
|
|
1988
|
+
fields=None, adjust_type=None, adjust_method=None, source=None):
|
|
1989
|
+
"""
|
|
1990
|
+
获取主力/次主力合约行情数据
|
|
1991
|
+
:param commodity: str or str list--必填,品种代码
|
|
1992
|
+
:param start_date: date--必填,开始日期,默认当天
|
|
1993
|
+
:param end_date: date--必填,结束日期,默认当天
|
|
1994
|
+
:param frequency: str--必填,频率,支持 1d / 1min / tick_l1 / tick_l2
|
|
1995
|
+
:param compute_type: str--必填,主力类型:main - 主力,sub_main - 次主力
|
|
1904
1996
|
:param fields: str or str list--选填,返回字段,默认全部
|
|
1905
|
-
:param adjust_type: str
|
|
1997
|
+
:param adjust_type: str--选填,复权方式:none - 不复权,post - 后复权
|
|
1906
1998
|
:param adjust_method: str--选填,复权方法
|
|
1907
|
-
:param source: str
|
|
1999
|
+
:param source: str--选填,数据源(暂未使用)
|
|
1908
2000
|
|
|
1909
|
-
:return:
|
|
2001
|
+
:return: 主力/次主力合约行情数据(DataFrame)
|
|
1910
2002
|
"""
|
|
1911
2003
|
|
|
1912
2004
|
""" 校验参数 """
|
|
@@ -1914,38 +2006,34 @@ class GJDataC:
|
|
|
1914
2006
|
if not self.validate_dates(start_date, end_date):
|
|
1915
2007
|
return None
|
|
1916
2008
|
|
|
1917
|
-
self.
|
|
1918
|
-
|
|
2009
|
+
self.get_main_price_validate_params(commodity, start_date, end_date, frequency, compute_type, adjust_type,
|
|
2010
|
+
adjust_method)
|
|
1919
2011
|
|
|
1920
2012
|
""" 获取、筛选数据 """
|
|
1921
2013
|
if frequency in ['1min', '1d', '1min_gtja']:
|
|
1922
|
-
data, db_session = self.
|
|
2014
|
+
data, db_session = self.get_main_price_get_data(commodity, start_date, end_date, frequency,
|
|
2015
|
+
compute_type,
|
|
2016
|
+
source)
|
|
2017
|
+
else:
|
|
2018
|
+
data, db_session = self.get_main_price_get_data_tick(commodity, start_date, end_date, frequency,
|
|
1923
2019
|
compute_type,
|
|
1924
2020
|
source)
|
|
1925
|
-
|
|
1926
|
-
data, db_session = self.get_active_contract_get_data_tick(commodity, start_date, end_date, frequency,
|
|
1927
|
-
compute_type,
|
|
1928
|
-
source)
|
|
1929
|
-
else:
|
|
1930
|
-
data, db_session = self.get_active_contract_get_data_tick(commodity, start_date, end_date, frequency,
|
|
1931
|
-
compute_type,
|
|
1932
|
-
source)
|
|
1933
|
-
data = self.get_active_contract_tick_data(data, frequency, start_date, end_date)
|
|
2021
|
+
data = self.get_main_price_tick_data(data, frequency, start_date, end_date)
|
|
1934
2022
|
db_session.close()
|
|
1935
2023
|
|
|
1936
2024
|
""" 处理字段名 """
|
|
1937
|
-
data = self.
|
|
2025
|
+
data = self.get_main_price_rename(data, adjust_method, frequency)
|
|
1938
2026
|
|
|
1939
2027
|
""" 根据是否复权计算价格数据 """
|
|
1940
|
-
data = self.
|
|
2028
|
+
data = self.get_main_price_adjust_price(data, adjust_type, adjust_method, frequency)
|
|
1941
2029
|
|
|
1942
2030
|
""" 处理返回字段 """
|
|
1943
|
-
data = self.
|
|
2031
|
+
data = self.get_main_price_process_return_fields(data, fields)
|
|
1944
2032
|
|
|
1945
2033
|
return data
|
|
1946
2034
|
|
|
1947
|
-
def
|
|
1948
|
-
|
|
2035
|
+
def get_main_price_validate_params(self, code, start_date, end_date, frequency, asset_type, adjust_type,
|
|
2036
|
+
adjust_method):
|
|
1949
2037
|
"""
|
|
1950
2038
|
校验参数
|
|
1951
2039
|
Args:
|
|
@@ -1961,19 +2049,21 @@ class GJDataC:
|
|
|
1961
2049
|
"""
|
|
1962
2050
|
|
|
1963
2051
|
# 校验必填参数
|
|
1964
|
-
self.
|
|
2052
|
+
self.get_main_price_validate_required_params(code, start_date, end_date, frequency, asset_type)
|
|
1965
2053
|
|
|
1966
2054
|
# 校验日期参数类型
|
|
1967
|
-
self.
|
|
2055
|
+
self.get_main_price_validate_date_type(start_date, end_date)
|
|
1968
2056
|
|
|
1969
2057
|
# 校验code类型
|
|
1970
|
-
self.
|
|
2058
|
+
self.get_main_price_validate_code(code)
|
|
1971
2059
|
|
|
1972
|
-
# 校验 adjust_type, adjust_method
|
|
1973
|
-
|
|
1974
|
-
|
|
2060
|
+
# 校验 adjust_type, adjust_method(仅当非 None 时校验)
|
|
2061
|
+
if adjust_type is not None:
|
|
2062
|
+
self.general_validate_param_is_str("adjust_type", adjust_type)
|
|
2063
|
+
if adjust_method is not None:
|
|
2064
|
+
self.general_validate_param_is_str("adjust_method", adjust_method)
|
|
1975
2065
|
|
|
1976
|
-
def
|
|
2066
|
+
def get_main_price_validate_required_params(self, code, start_date, end_date, frequency, asset_type):
|
|
1977
2067
|
"""
|
|
1978
2068
|
校验参数是否必填
|
|
1979
2069
|
:param code: str or str list--必填,品种
|
|
@@ -1994,7 +2084,7 @@ class GJDataC:
|
|
|
1994
2084
|
for key, value in params_dict.items():
|
|
1995
2085
|
self.general_validate_params_required(value, key) # 校验必填参数
|
|
1996
2086
|
|
|
1997
|
-
def
|
|
2087
|
+
def get_main_price_validate_code(self, code):
|
|
1998
2088
|
"""
|
|
1999
2089
|
校验code 类型
|
|
2000
2090
|
:param code: str or str list--必填,品种
|
|
@@ -2003,7 +2093,7 @@ class GJDataC:
|
|
|
2003
2093
|
self.general_validate_field_str_or_list(code, "code")
|
|
2004
2094
|
|
|
2005
2095
|
@staticmethod
|
|
2006
|
-
def
|
|
2096
|
+
def get_main_price_get_table(frequency):
|
|
2007
2097
|
"""
|
|
2008
2098
|
获取表信息
|
|
2009
2099
|
:param frequency: 频率
|
|
@@ -2017,7 +2107,7 @@ class GJDataC:
|
|
|
2017
2107
|
}.get(frequency, (None, None))
|
|
2018
2108
|
|
|
2019
2109
|
@staticmethod
|
|
2020
|
-
def
|
|
2110
|
+
def get_main_price_get_table_tick(frequency):
|
|
2021
2111
|
"""
|
|
2022
2112
|
获取表信息
|
|
2023
2113
|
:param frequency: 频率
|
|
@@ -2029,7 +2119,7 @@ class GJDataC:
|
|
|
2029
2119
|
'tick_l2': (tick_main_contract_db_table_name, tick_main_contract_db_path),
|
|
2030
2120
|
}.get(frequency, (None, None))
|
|
2031
2121
|
|
|
2032
|
-
def
|
|
2122
|
+
def get_main_price_get_data(self, code, start_date, end_date, frequency, compute_type, source):
|
|
2033
2123
|
"""
|
|
2034
2124
|
获取数据
|
|
2035
2125
|
:param code: str or str list--必填,品种
|
|
@@ -2043,7 +2133,7 @@ class GJDataC:
|
|
|
2043
2133
|
"""
|
|
2044
2134
|
|
|
2045
2135
|
# 根据频率获取对应的表
|
|
2046
|
-
table_name, db_path = self.
|
|
2136
|
+
table_name, db_path = self.get_main_price_get_table(frequency)
|
|
2047
2137
|
|
|
2048
2138
|
# 先从数据库获取数据
|
|
2049
2139
|
data, db_session = self.connect_db(table_name, db_path)
|
|
@@ -2054,8 +2144,8 @@ class GJDataC:
|
|
|
2054
2144
|
|
|
2055
2145
|
# 根据日期筛选数据
|
|
2056
2146
|
# 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
|
|
2057
|
-
# start_dolphin_date = self.
|
|
2058
|
-
# end_dolphin_date = self.
|
|
2147
|
+
# start_dolphin_date = self.get_main_price_datetime_to_dolphin_time(start_date)
|
|
2148
|
+
# end_dolphin_date = self.get_main_price_datetime_to_dolphin_time(end_date)
|
|
2059
2149
|
|
|
2060
2150
|
start_dolphin_date = self.format_date(start_date)
|
|
2061
2151
|
end_dolphin_date = self.format_date(end_date)
|
|
@@ -2063,16 +2153,16 @@ class GJDataC:
|
|
|
2063
2153
|
date_data = code_data.where(f"trading_day >= {start_dolphin_date} and trading_day <= {end_dolphin_date}")
|
|
2064
2154
|
|
|
2065
2155
|
if compute_type:
|
|
2066
|
-
#
|
|
2156
|
+
# 根据 compute_type 字段筛选主力/次主力合约,MAIN 是主力合约,SUB_MAIN 是次主力合约
|
|
2067
2157
|
rank = 'MAIN' if compute_type == "main" else 'SUB_MAIN'
|
|
2068
2158
|
rank_data = date_data.where(f"compute_type = '{rank}'")
|
|
2069
2159
|
|
|
2070
2160
|
return rank_data.toDF(), db_session
|
|
2071
2161
|
return date_data.toDF(), db_session
|
|
2072
2162
|
|
|
2073
|
-
def
|
|
2163
|
+
def get_main_price_get_data_tick(self, code, start_date, end_date, frequency, compute_type, source):
|
|
2074
2164
|
"""
|
|
2075
|
-
|
|
2165
|
+
从 qc_gtja_future_maincontract 获取主力合约列表(用于后续联查tick表)
|
|
2076
2166
|
:param code: str or str list--必填,品种
|
|
2077
2167
|
:param start_date: str--必填,开始日期
|
|
2078
2168
|
:param end_date: str--必填,结束日期
|
|
@@ -2080,7 +2170,7 @@ class GJDataC:
|
|
|
2080
2170
|
:param frequency: str--必填
|
|
2081
2171
|
:param source: str--选填,数据来源
|
|
2082
2172
|
|
|
2083
|
-
:return:
|
|
2173
|
+
:return: 主力/次主力合约列表
|
|
2084
2174
|
"""
|
|
2085
2175
|
|
|
2086
2176
|
table_name, db_path = main_contract_db_table_name, main_contract_db_path
|
|
@@ -2093,15 +2183,11 @@ class GJDataC:
|
|
|
2093
2183
|
data.where(f"product_id = '{code}'")
|
|
2094
2184
|
|
|
2095
2185
|
# 根据日期筛选数据
|
|
2096
|
-
# 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
|
|
2097
|
-
# start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
|
|
2098
|
-
# end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
|
|
2099
2186
|
start_dolphin_date = self.format_date(start_date)
|
|
2100
2187
|
end_dolphin_date = self.format_date(end_date)
|
|
2101
2188
|
|
|
2102
2189
|
date_data = code_data.where(f"trading_day >= {start_dolphin_date} and trading_day <= {end_dolphin_date}")
|
|
2103
2190
|
|
|
2104
|
-
# 根据表中的 rank 字段筛选主力/次主力合约,rank == 1是主力合约, 2 是次主力合约
|
|
2105
2191
|
if compute_type:
|
|
2106
2192
|
rank = 'MAIN' if compute_type == "main" else 'SUB_MAIN'
|
|
2107
2193
|
rank_data = date_data.where(f"compute_type = '{rank}'")
|
|
@@ -2109,7 +2195,7 @@ class GJDataC:
|
|
|
2109
2195
|
|
|
2110
2196
|
return date_data.toDF(), db_session
|
|
2111
2197
|
|
|
2112
|
-
def
|
|
2198
|
+
def get_main_price_tick_data(self, data, frequency, start_date, end_date):
|
|
2113
2199
|
"""
|
|
2114
2200
|
根据主力合约表查询tick表
|
|
2115
2201
|
:param data: 数据
|
|
@@ -2119,7 +2205,7 @@ class GJDataC:
|
|
|
2119
2205
|
:return: 查询后的数据
|
|
2120
2206
|
"""
|
|
2121
2207
|
|
|
2122
|
-
table_name, db_path = self.
|
|
2208
|
+
table_name, db_path = self.get_main_price_get_table_tick(frequency)
|
|
2123
2209
|
# 先从数据库获取数据
|
|
2124
2210
|
tick_data, db_session = self.connect_db(table_name, db_path)
|
|
2125
2211
|
|
|
@@ -2136,7 +2222,7 @@ class GJDataC:
|
|
|
2136
2222
|
return fina_data
|
|
2137
2223
|
|
|
2138
2224
|
@staticmethod
|
|
2139
|
-
def
|
|
2225
|
+
def get_main_price_datetime_to_dolphin_time(datetime_str):
|
|
2140
2226
|
"""将日期字符串格式化为 'YYYY.MM.DD HH:mm:ss' 格式。
|
|
2141
2227
|
: params datetime_str: 日期字符串,例如 '2018-01-04 09:01:00'。
|
|
2142
2228
|
: return :
|
|
@@ -2153,7 +2239,7 @@ class GJDataC:
|
|
|
2153
2239
|
else:
|
|
2154
2240
|
return datetime_str.strftime('%Y.%m.%d %H:%M:%S')
|
|
2155
2241
|
|
|
2156
|
-
def
|
|
2242
|
+
def get_main_price_validate_date_type(self, start_date, end_date):
|
|
2157
2243
|
"""
|
|
2158
2244
|
校验日期数据的类型
|
|
2159
2245
|
:param start_date: date--必填,默认当天
|
|
@@ -2180,7 +2266,7 @@ class GJDataC:
|
|
|
2180
2266
|
# raise ValueError("begin_date type error, please enter datetime.datetime type")
|
|
2181
2267
|
|
|
2182
2268
|
@staticmethod
|
|
2183
|
-
def
|
|
2269
|
+
def get_main_price_rename(data, ex_factor, frequency):
|
|
2184
2270
|
"""
|
|
2185
2271
|
处理返回结果
|
|
2186
2272
|
:param data: 数据
|
|
@@ -2190,9 +2276,7 @@ class GJDataC:
|
|
|
2190
2276
|
"""
|
|
2191
2277
|
new_df = data.copy()
|
|
2192
2278
|
|
|
2193
|
-
if frequency
|
|
2194
|
-
return new_df
|
|
2195
|
-
elif frequency in ['1min', '1d']:
|
|
2279
|
+
if frequency in ['1min', '1d']:
|
|
2196
2280
|
return_fields = main_contract_return_fields
|
|
2197
2281
|
return_fields_rename = main_contract_return_fields_rename
|
|
2198
2282
|
elif frequency in ['1min_gtja']:
|
|
@@ -2220,7 +2304,7 @@ class GJDataC:
|
|
|
2220
2304
|
return new_df
|
|
2221
2305
|
|
|
2222
2306
|
@staticmethod
|
|
2223
|
-
def
|
|
2307
|
+
def get_main_price_adjust_price(data, adjust_type, adjust_method, frequency):
|
|
2224
2308
|
"""
|
|
2225
2309
|
根据参数计算是否复权
|
|
2226
2310
|
:param data: 数据
|
|
@@ -2230,7 +2314,7 @@ class GJDataC:
|
|
|
2230
2314
|
:return: 计算后的数据
|
|
2231
2315
|
"""
|
|
2232
2316
|
|
|
2233
|
-
if not adjust_type or frequency in ['tick_l1', 'tick_l2', '1min_gtja'
|
|
2317
|
+
if not adjust_type or frequency in ['tick_l1', 'tick_l2', '1min_gtja']:
|
|
2234
2318
|
return data
|
|
2235
2319
|
|
|
2236
2320
|
adjust_fields = ['open', 'close', 'high', 'low', 'limit_up', 'limit_down', 'settlement', 'prev_settlement',
|
|
@@ -2246,7 +2330,7 @@ class GJDataC:
|
|
|
2246
2330
|
return data
|
|
2247
2331
|
|
|
2248
2332
|
@staticmethod
|
|
2249
|
-
def
|
|
2333
|
+
def get_main_price_process_return_fields(data, fields):
|
|
2250
2334
|
"""
|
|
2251
2335
|
处理返回字段
|
|
2252
2336
|
:param data: 主力/次主力合约数据
|
|
@@ -2262,52 +2346,6 @@ class GJDataC:
|
|
|
2262
2346
|
else:
|
|
2263
2347
|
return data[fields]
|
|
2264
2348
|
|
|
2265
|
-
def get_active_contract_process_result(self, data, fields):
|
|
2266
|
-
"""
|
|
2267
|
-
处理返回字段
|
|
2268
|
-
:param data: 主力/次主力合约数据
|
|
2269
|
-
:param fields: str or str list--选填,返回字段,默认全部
|
|
2270
|
-
:return: 筛选后的主力/次主力合约数据
|
|
2271
|
-
"""
|
|
2272
|
-
|
|
2273
|
-
new_df = data.copy()
|
|
2274
|
-
|
|
2275
|
-
# 整合需求文档中所需的字段
|
|
2276
|
-
# 价格相关字段通过 "dfs://dayk"."qc_future_dayk" 获取,通过unique_instrument_id 关联查找
|
|
2277
|
-
# 前收盘价(qc_future_dayk也无)
|
|
2278
|
-
future_day_data, future_session = self.connect_db(history_option_day_db_table_name,
|
|
2279
|
-
history_option_day_db_path)
|
|
2280
|
-
|
|
2281
|
-
# 获取筛选后的主力/次主力合约数据的交易时间和合约代码,用于去qc_future_dayk表中查询其他的缺失字段
|
|
2282
|
-
unique_instrument_id_list = new_df["unique_instrument_id"].tolist()
|
|
2283
|
-
future_filter_df = future_day_data.where(f"unique_instrument_id in {unique_instrument_id_list}").toDF()
|
|
2284
|
-
future_session.close()
|
|
2285
|
-
|
|
2286
|
-
future_filter_df_unique = future_filter_df.drop_duplicates(
|
|
2287
|
-
subset=['trading_day', 'unique_instrument_id'],
|
|
2288
|
-
keep='first' # 或者 'last',取决于你要保留哪一个
|
|
2289
|
-
)
|
|
2290
|
-
|
|
2291
|
-
merged_df = new_df.merge(
|
|
2292
|
-
future_filter_df_unique[['trading_day', 'unique_instrument_id',
|
|
2293
|
-
'pre_settlement_price', 'open_price',
|
|
2294
|
-
'highest_price', 'lowest_price', 'close_price',
|
|
2295
|
-
'settlement_price', 'volume', 'turnover', 'open_interest']],
|
|
2296
|
-
on=['trading_day', 'unique_instrument_id'],
|
|
2297
|
-
how='left' # 使用 left join 保留 new_df 的所有行
|
|
2298
|
-
)
|
|
2299
|
-
|
|
2300
|
-
# 统一字段(将数据表中的字段重命名成需求文档要求的字段)
|
|
2301
|
-
merged_df.rename(columns={'instrument_id': 'order_book_id', 'product_id': 'code',
|
|
2302
|
-
'exchange_id': 'exchange', 'trading_day': 'date'}, inplace=True)
|
|
2303
|
-
merged_df["active_type"] = merged_df["rank"].map({1: "active", 2: "next_active"})
|
|
2304
|
-
|
|
2305
|
-
if fields:
|
|
2306
|
-
# 根据fields返回用户需要的字段
|
|
2307
|
-
merged_df = merged_df[fields] if isinstance(fields, str) else merged_df[[field for field in fields]]
|
|
2308
|
-
|
|
2309
|
-
return merged_df
|
|
2310
|
-
|
|
2311
2349
|
""" get_basic_data 库存/基差/现货价格-数据(日频) """
|
|
2312
2350
|
|
|
2313
2351
|
def get_basic_data(self, order_book_id, asset_type, start_date, end_date):
|
|
@@ -5,12 +5,12 @@ with open('description_info.md', 'r', encoding='utf-8') as f:
|
|
|
5
5
|
|
|
6
6
|
setup(
|
|
7
7
|
name='gjdatac_api', # 你的包名
|
|
8
|
-
version='0.
|
|
8
|
+
version='0.4.0', # 版本号
|
|
9
9
|
description='数据接口打包', # 包的简要描述
|
|
10
10
|
long_description=long_description, # 包的详细描述
|
|
11
11
|
long_description_content_type='text/markdown', # 描述文件的类型
|
|
12
12
|
include_package_data=True, # 包含包数据
|
|
13
|
-
package_data={'gjdatac_api': ['*.py', 'dict_data.py', 'dolphindb_info.py', 'instrument_class.py'
|
|
13
|
+
package_data={'gjdatac_api': ['*.py', 'dict_data.py', 'dolphindb_info.py', 'instrument_class.py']}, # 指定数据文件
|
|
14
14
|
author='fml', # 作者姓名
|
|
15
15
|
author_email='13306510996@163.com', # 作者邮箱
|
|
16
16
|
packages=find_packages(), # 自动查找包目录
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|