gjdatac-api 0.2.4__tar.gz → 0.2.6__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.

Potentially problematic release.


This version of gjdatac-api might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gjdatac_api
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: 数据接口打包
5
5
  Author: yin
6
6
  Author-email: 2018209921@qq.com
@@ -295,6 +295,116 @@ class GJDataC:
295
295
  bar_db_session.close()
296
296
  return 'option'
297
297
 
298
+ @staticmethod
299
+ def format_date(input_date):
300
+ """
301
+ 将输入的日期转换为指定格式的字符串。
302
+
303
+ 参数:
304
+ input_date: 输入的日期,可以是 datetime、date 或 str 类型。
305
+ 支持的字符串格式包括:
306
+ - "YYYY-MM-DD HH:MM:SS"
307
+ - "YYYY-MM-DD"
308
+ - "YYYY.MM.DD HH:MM:SS"
309
+ - "YYYY.MM.DD"
310
+
311
+ 返回:
312
+ str: 转换后的日期字符串,格式为 "YYYY.MM.DD HH:MM:SS" 或 "YYYY.MM.DD"。
313
+ """
314
+ # 如果输入是 datetime 类型
315
+ if isinstance(input_date, datetime.datetime):
316
+ if input_date.hour == 0 and input_date.minute == 0 and input_date.second == 0:
317
+ return input_date.strftime("%Y.%m.%d")
318
+ else:
319
+ return input_date.strftime("%Y.%m.%d %H:%M:%S")
320
+
321
+ # 如果输入是 date 类型
322
+ elif isinstance(input_date, datetime.date):
323
+ return input_date.strftime("%Y.%m.%d")
324
+
325
+ # 如果输入是字符串类型
326
+ elif isinstance(input_date, str):
327
+ try:
328
+ # 尝试解析包含时间的格式
329
+ dt = datetime.datetime.strptime(input_date, "%Y-%m-%d %H:%M:%S")
330
+ return dt.strftime("%Y.%m.%d %H:%M:%S")
331
+ except ValueError:
332
+ try:
333
+ # 尝试解析不包含时间的格式
334
+ dt = datetime.datetime.strptime(input_date, "%Y-%m-%d")
335
+ return dt.strftime("%Y.%m.%d")
336
+ except ValueError:
337
+ try:
338
+ # 尝试解析包含时间的点分隔格式
339
+ dt = datetime.datetime.strptime(input_date, "%Y.%m.%d %H:%M:%S")
340
+ return dt.strftime("%Y.%m.%d %H:%M:%S")
341
+ except ValueError:
342
+ try:
343
+ # 尝试解析不包含时间的点分隔格式
344
+ dt = datetime.datetime.strptime(input_date, "%Y.%m.%d")
345
+ return dt.strftime("%Y.%m.%d")
346
+ except ValueError:
347
+ raise ValueError("输入的日期格式不支持")
348
+
349
+ # 如果输入类型不支持
350
+ else:
351
+ raise TypeError("输入类型必须是 datetime、date 或 对应格式的str")
352
+
353
+ @staticmethod
354
+ def format_date_split_by_(input_date):
355
+ """
356
+ 将输入的日期转换为指定格式的字符串。
357
+
358
+ 参数:
359
+ input_date: 输入的日期,可以是 datetime、date 或 str 类型。
360
+ 支持的字符串格式包括:
361
+ - "YYYY-MM-DD HH:MM:SS"
362
+ - "YYYY-MM-DD"
363
+ - "YYYY.MM.DD HH:MM:SS"
364
+ - "YYYY.MM.DD"
365
+
366
+ 返回:
367
+ str: 转换后的日期字符串,格式为 "YYYY.MM.DD HH:MM:SS" 或 "YYYY.MM.DD"。
368
+ """
369
+ # 如果输入是 datetime 类型
370
+ if isinstance(input_date, datetime.datetime):
371
+ if input_date.hour == 0 and input_date.minute == 0 and input_date.second == 0:
372
+ return input_date.strftime("%Y-%m-%d")
373
+ else:
374
+ return input_date.strftime("%Y-%m-%d %H:%M:%S")
375
+
376
+ # 如果输入是 date 类型
377
+ elif isinstance(input_date, datetime.date):
378
+ return input_date.strftime("%Y-%m-%d")
379
+
380
+ # 如果输入是字符串类型
381
+ elif isinstance(input_date, str):
382
+ try:
383
+ # 尝试解析包含时间的格式
384
+ dt = datetime.datetime.strptime(input_date, "%Y-%m-%d %H:%M:%S")
385
+ return dt.strftime("%Y-%m-%d %H:%M:%S")
386
+ except ValueError:
387
+ try:
388
+ # 尝试解析不包含时间的格式
389
+ dt = datetime.datetime.strptime(input_date, "%Y-%m-%d")
390
+ return dt.strftime("%Y-%m-%d")
391
+ except ValueError:
392
+ try:
393
+ # 尝试解析包含时间的点分隔格式
394
+ dt = datetime.datetime.strptime(input_date, "%Y.%m.%d %H:%M:%S")
395
+ return dt.strftime("%Y-%m-%d %H:%M:%S")
396
+ except ValueError:
397
+ try:
398
+ # 尝试解析不包含时间的点分隔格式
399
+ dt = datetime.datetime.strptime(input_date, "%Y.%m.%d")
400
+ return dt.strftime("%Y-%m-%d")
401
+ except ValueError:
402
+ raise ValueError("输入的日期格式不支持")
403
+
404
+ # 如果输入类型不支持
405
+ else:
406
+ raise TypeError("输入类型必须是 datetime、date 或 对应格式的str")
407
+
298
408
  """ get_price(获取行情数据接口) """
299
409
 
300
410
  def get_price(self, order_book_ids=None, asset_type="future", frequency=None, start_date=None, end_date=None,
@@ -411,18 +521,20 @@ class GJDataC:
411
521
  if (start_date and not end_date) or (not start_date and end_date):
412
522
  raise ValueError("start_date and end_date should be both provided or not provided at all")
413
523
  if start_date and end_date:
524
+ self.format_date(start_date)
525
+ self.format_date(end_date)
414
526
  # 如果提供了,则进行进一步校验
415
- is_start_datetime = self._is_convertible_datetime(start_date)
416
- is_end_datetime = self._is_convertible_datetime(end_date)
417
- if not is_end_datetime or not is_start_datetime:
418
- raise ValueError(
419
- "start_date and end_date should be datetime.datetime objects or "
420
- "convertible to datetime.datetime objects")
421
-
422
- # 如果传入的数据类型符合要求,进行转换
423
- start_date, end_date = self._get_price_convert_start_end_date(start_date, end_date)
424
- if start_date > end_date:
425
- raise ValueError("start_date should be earlier than end_date")
527
+ # is_start_datetime = self._is_convertible_datetime(start_date)
528
+ # is_end_datetime = self._is_convertible_datetime(end_date)
529
+ # if not is_end_datetime or not is_start_datetime:
530
+ # raise ValueError(
531
+ # "start_date and end_date should be datetime.datetime objects or "
532
+ # "convertible to datetime.datetime objects")
533
+ #
534
+ # # 如果传入的数据类型符合要求,进行转换
535
+ # start_date, end_date = self._get_price_convert_start_end_date(start_date, end_date)
536
+ # if start_date > end_date:
537
+ # raise ValueError("start_date should be earlier than end_date")
426
538
 
427
539
  @staticmethod
428
540
  def _get_price_convert_start_end_date(start_date, end_date):
@@ -618,8 +730,10 @@ class GJDataC:
618
730
  limit_data = get_price_data.where(f"instrument_id in {order_book_ids}")
619
731
 
620
732
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
621
- format_start_date = self.get_price_format_date(start_date)
622
- format_end_date = self.get_price_format_date(end_date)
733
+ # format_start_date = self.get_price_format_date(start_date)
734
+ # format_end_date = self.get_price_format_date(end_date)
735
+ format_start_date = self.format_date(start_date)
736
+ format_end_date = self.format_date(end_date)
623
737
 
624
738
  # 根据日期对数据再次进行筛选,最大程度减轻数据库压力
625
739
  limit_data = limit_data.where(f"trade_time >= {format_start_date} and trade_time <= {format_end_date}")
@@ -1041,6 +1155,7 @@ class GJDataC:
1041
1155
  raise ValueError("date parameter error, type should be str or datetime.date")
1042
1156
 
1043
1157
  if isinstance(date, str):
1158
+ date = self.format_date_split_by_(date)
1044
1159
  date = datetime.datetime.strptime(date, "%Y-%m-%d").date()
1045
1160
 
1046
1161
  return self.get_dates_in_n(date, n)
@@ -1069,6 +1184,8 @@ class GJDataC:
1069
1184
  start_date = self._get_trading_dates_by_start_end(start_date)
1070
1185
  if isinstance(end_date, str):
1071
1186
  end_date = self._get_trading_dates_by_start_end(end_date)
1187
+
1188
+
1072
1189
  # 筛选 trading_day_str 列在 start_date 和 end_date 之间的数据
1073
1190
  # filtered_df = trading_params_data.loc[
1074
1191
  # (trading_params_data['trading_day_str'] >= start_date) & (
@@ -1304,7 +1421,8 @@ class GJDataC:
1304
1421
 
1305
1422
  # 根据 date 筛选数据
1306
1423
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
1307
- dolphin_date = self.get_margin_ratio_date_to_dolphin_time(date)
1424
+ # dolphin_date = self.get_margin_ratio_date_to_dolphin_time(date)
1425
+ dolphin_date = self.format_date(date)
1308
1426
  date_data = data.where(f"trading_day = {dolphin_date}")
1309
1427
 
1310
1428
  # 根据 exchange 筛选数据
@@ -1369,11 +1487,13 @@ class GJDataC:
1369
1487
  :return: None
1370
1488
  """
1371
1489
 
1372
- if isinstance(date, (str, datetime.date, datetime.datetime)):
1373
- if isinstance(date, str) and not self.general_validate_date(date):
1374
- raise ValueError("date is not a valid date string format")
1375
- else:
1376
- raise ValueError("date type error, please pass in str, datetime.date or datetime.datetime type")
1490
+ self.format_date(date)
1491
+
1492
+ # if isinstance(date, (str, datetime.date, datetime.datetime)):
1493
+ # if isinstance(date, str) and not self.general_validate_date(date):
1494
+ # raise ValueError("date is not a valid date string format")
1495
+ # else:
1496
+ # raise ValueError("date type error, please pass in str, datetime.date or datetime.datetime type")
1377
1497
 
1378
1498
  """ get_fee 期货交割手续费 """
1379
1499
 
@@ -1470,7 +1590,8 @@ class GJDataC:
1470
1590
 
1471
1591
  # 根据 date 筛选数据
1472
1592
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
1473
- dolphin_date = self.get_fee_datetime_to_dolphin_time(date)
1593
+ # dolphin_date = self.get_fee_datetime_to_dolphin_time(date)
1594
+ dolphin_date = self.format_date(date)
1474
1595
 
1475
1596
  date_data = get_fee_data.where(f"trading_day = {dolphin_date}")
1476
1597
 
@@ -1502,12 +1623,14 @@ class GJDataC:
1502
1623
  # 校验参数是否必填
1503
1624
  self.general_validate_params_required(date, "date")
1504
1625
 
1505
- # 校验参数类型格式
1506
- if isinstance(date, (str, datetime.date, datetime.datetime)):
1507
- if isinstance(date, str) and not self.general_validate_date(date):
1508
- raise ValueError("date is not a valid date string format")
1509
- else:
1510
- raise ValueError("date type error, please pass in str, datetime.date or datetime.datetime type")
1626
+ self.format_date(date)
1627
+
1628
+ # # 校验参数类型格式
1629
+ # if isinstance(date, (str, datetime.date, datetime.datetime)):
1630
+ # if isinstance(date, str) and not self.general_validate_date(date):
1631
+ # raise ValueError("date is not a valid date string format")
1632
+ # else:
1633
+ # raise ValueError("date type error, please pass in str, datetime.date or datetime.datetime type")
1511
1634
 
1512
1635
  @staticmethod
1513
1636
  def get_fee_handle_data(data):
@@ -1590,7 +1713,8 @@ class GJDataC:
1590
1713
 
1591
1714
  # 根据 date 筛选数据
1592
1715
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
1593
- dolphin_date = self.get_limit_position_date_to_dolphin_time(date)
1716
+ # dolphin_date = self.get_limit_position_date_to_dolphin_time(date)
1717
+ dolphin_date = self.format_date(date)
1594
1718
  date_data = data.where(f"trading_day = {dolphin_date}")
1595
1719
 
1596
1720
  # 根据 order_book_id 和 commodity 获取数据
@@ -1627,11 +1751,13 @@ class GJDataC:
1627
1751
  :return: None
1628
1752
  """
1629
1753
 
1630
- if isinstance(date, (str, datetime.date, datetime.datetime)):
1631
- if isinstance(date, str) and not self.general_validate_date(date):
1632
- raise ValueError("date is not a valid date string format")
1633
- else:
1634
- raise ValueError("date type error, please pass in str, datetime.date or datetime.datetime type")
1754
+ self.format_date(date)
1755
+
1756
+ # if isinstance(date, (str, datetime.date, datetime.datetime)):
1757
+ # if isinstance(date, str) and not self.general_validate_date(date):
1758
+ # raise ValueError("date is not a valid date string format")
1759
+ # else:
1760
+ # raise ValueError("date type error, please pass in str, datetime.date or datetime.datetime type")
1635
1761
 
1636
1762
  @staticmethod
1637
1763
  def get_limit_position_handle_data(data):
@@ -1808,8 +1934,12 @@ class GJDataC:
1808
1934
 
1809
1935
  # 根据日期筛选数据
1810
1936
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
1811
- start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
1812
- end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
1937
+ # start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
1938
+ # end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
1939
+
1940
+ start_dolphin_date = self.format_date(start_date)
1941
+ end_dolphin_date = self.format_date(end_date)
1942
+
1813
1943
  date_data = code_data.where(f"trading_day >= {start_dolphin_date} and trading_day <= {end_dolphin_date}")
1814
1944
 
1815
1945
  if compute_type:
@@ -1844,8 +1974,11 @@ class GJDataC:
1844
1974
 
1845
1975
  # 根据日期筛选数据
1846
1976
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
1847
- start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
1848
- end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
1977
+ # start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
1978
+ # end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
1979
+ start_dolphin_date = self.format_date(start_date)
1980
+ end_dolphin_date = self.format_date(end_date)
1981
+
1849
1982
  date_data = code_data.where(f"trading_day >= {start_dolphin_date} and trading_day <= {end_dolphin_date}")
1850
1983
 
1851
1984
  # 根据表中的 rank 字段筛选主力/次主力合约,rank == 1是主力合约, 2 是次主力合约
@@ -1875,8 +2008,10 @@ class GJDataC:
1875
2008
 
1876
2009
  # 根据日期筛选数据
1877
2010
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
1878
- start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
1879
- end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
2011
+ # start_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(start_date)
2012
+ # end_dolphin_date = self.get_active_contract_datetime_to_dolphin_time(end_date)
2013
+ start_dolphin_date = self.format_date(start_date)
2014
+ end_dolphin_date = self.format_date(end_date)
1880
2015
  date_data = tick_data.where(f"trading_day >= {start_dolphin_date} and trading_day <= {end_dolphin_date}")
1881
2016
 
1882
2017
  instrument_data = date_data.where(f"instrument_id in {data['instrument_id'].tolist()}")
@@ -1912,13 +2047,18 @@ class GJDataC:
1912
2047
  :return: None
1913
2048
  """
1914
2049
  # 校验start_date 和 end_date 是否为datetime.date类型或者可以转换成该类型的str
1915
- if isinstance(start_date, (str, datetime.date)) and isinstance(end_date, (str, datetime.date)):
1916
- if isinstance(start_date, str) and not self.general_validate_date_str_is_date_type(start_date):
1917
- raise ValueError("start_date type error, please enter datetime.date type")
1918
- if isinstance(end_date, str) and not self.general_validate_date_str_is_date_type(end_date):
1919
- raise ValueError("start_date type error, please enter datetime.date type")
1920
- else:
1921
- raise ValueError("start_date or end_date type error, please enter datetime.date type")
2050
+ # if isinstance(start_date, (str, datetime.date)) and isinstance(end_date, (str, datetime.date)):
2051
+ # if isinstance(start_date, str) and not self.general_validate_date_str_is_date_type(start_date):
2052
+ # raise ValueError("start_date type error, please enter datetime.date type")
2053
+ # if isinstance(end_date, str) and not self.general_validate_date_str_is_date_type(end_date):
2054
+ # raise ValueError("start_date type error, please enter datetime.date type")
2055
+ # else:
2056
+ # raise ValueError("start_date or end_date type error, please enter datetime.date type")
2057
+
2058
+
2059
+
2060
+ self.format_date(start_date)
2061
+ self.format_date(end_date)
1922
2062
 
1923
2063
  # 校验begin_date 是否为datetime.datetime类型或者为可以转换为该类型的字符串
1924
2064
  # if isinstance(begin_date, (str, datetime.datetime)):
@@ -2135,17 +2275,21 @@ class GJDataC:
2135
2275
  :param end_date: 结束日期
2136
2276
  :return: None
2137
2277
  """
2138
- error_msg = "start_date or end_date type error, datetime.date, datetime.datetime, pandasTimestamp type or can be converted to the type"
2139
2278
 
2140
- # 校验start_date 和 end_date 是否为datetime.date, datetime.datetime, pandasTimestamp类型或者可以转换成该类型的str
2141
- if isinstance(start_date, (str, datetime.date, datetime.datetime, pd.Timestamp)) and isinstance(end_date, (
2142
- str, datetime.date, datetime.datetime, pd.Timestamp)):
2143
- if isinstance(start_date, str) and not self.general_validate_date(start_date):
2144
- raise ValueError(error_msg)
2145
- if isinstance(end_date, str) and not self.general_validate_date(end_date):
2146
- raise ValueError(error_msg)
2147
- else:
2148
- raise ValueError(error_msg)
2279
+ self.format_date(start_date)
2280
+ self.format_date(end_date)
2281
+
2282
+ # error_msg = "start_date or end_date type error, datetime.date, datetime.datetime, pandasTimestamp type or can be converted to the type"
2283
+
2284
+ # # 校验start_date 和 end_date 是否为datetime.date, datetime.datetime, pandasTimestamp类型或者可以转换成该类型的str
2285
+ # if isinstance(start_date, (str, datetime.date, datetime.datetime, pd.Timestamp)) and isinstance(end_date, (
2286
+ # str, datetime.date, datetime.datetime, pd.Timestamp)):
2287
+ # if isinstance(start_date, str) and not self.general_validate_date(start_date):
2288
+ # raise ValueError(error_msg)
2289
+ # if isinstance(end_date, str) and not self.general_validate_date(end_date):
2290
+ # raise ValueError(error_msg)
2291
+ # else:
2292
+ # raise ValueError(error_msg)
2149
2293
 
2150
2294
  def get_basic_data_get_data(self, order_book_id, asset_type, start_date, end_date):
2151
2295
  """
@@ -2176,12 +2320,15 @@ class GJDataC:
2176
2320
  库存表的 data_date 字段为 STRING 类型,筛选日期时,需要提供和表中格式一致的数据,才能筛选,例如:2024-12-01
2177
2321
  '''
2178
2322
  # 将日期转换为 DolphinDB 的时间格式
2179
- start_dolphin_date = self._get_basic_data_date_to_dolphin_time(start_date, asset_type)
2180
- end_dolphin_date = self._get_basic_data_date_to_dolphin_time(end_date, asset_type)
2323
+ # start_dolphin_date = self._get_basic_data_date_to_dolphin_time(start_date, asset_type)
2324
+ # end_dolphin_date = self._get_basic_data_date_to_dolphin_time(end_date, asset_type)
2325
+ start_dolphin_date = self.format_date(start_date)
2326
+ end_dolphin_date = self.format_date(end_date)
2327
+
2181
2328
 
2182
2329
  # 此处还需要判断的原因是,因为对 dolphindb 中不同的日期数据类型查询数据的语法不同
2183
2330
  asset_type_data = product_data.where(
2184
- f"data_date>='{start_dolphin_date}' and data_date<='{end_dolphin_date}'") if asset_type == '库存' \
2331
+ f"data_date>={start_dolphin_date} and data_date<={end_dolphin_date}") if asset_type == '库存' \
2185
2332
  else product_data.where(f"date>={start_dolphin_date} and date<={end_dolphin_date}")
2186
2333
 
2187
2334
  return asset_type_data.toDF(), db_session
@@ -2243,10 +2390,10 @@ class GJDataC:
2243
2390
  }, inplace=True)
2244
2391
  # 处理库存
2245
2392
  else:
2246
- new_df.rename(columns={'product_id': 'order_book_id', ' data_date': 'date', 'inventory_value': 'value'},
2393
+ new_df.rename(columns={'product_id': 'order_book_id', 'data_date': 'date', 'inventory_value': 'value'},
2247
2394
  inplace=True)
2248
2395
 
2249
- return new_df[['order_book_id', 'asset_type', 'data_date', 'value']]
2396
+ return new_df[['order_book_id', 'asset_type', 'date', 'value']]
2250
2397
 
2251
2398
  """ get_warehouse_stocks_future 仓单数据 """
2252
2399
 
@@ -2265,7 +2412,11 @@ class GJDataC:
2265
2412
  self.get_warehouse_stocks_future_validate_required_params(commodity, start_date, end_date)
2266
2413
 
2267
2414
  # 校验commodity, start_date, end_date 是否为str类型
2268
- self.get_warehouse_stocks_future_validate_type_str(commodity, start_date, end_date)
2415
+ self.get_warehouse_stocks_future_validate_type_str(commodity)
2416
+
2417
+ # 校验start_date, end_date
2418
+ start_date = self.format_date_split_by_(start_date)
2419
+ end_date = self.format_date_split_by_(end_date)
2269
2420
 
2270
2421
  """ 获取数据 """
2271
2422
  # 从数据库中获取数据
@@ -2296,7 +2447,7 @@ class GJDataC:
2296
2447
  for key, value in params_dict.items():
2297
2448
  self.general_validate_params_required(value, key)
2298
2449
 
2299
- def get_warehouse_stocks_future_validate_type_str(self, commodity, start_date, end_date):
2450
+ def get_warehouse_stocks_future_validate_type_str(self, commodity):
2300
2451
  """
2301
2452
  校验参数类型是否为str
2302
2453
  :param commodity: str--必填,期货合约品种
@@ -2307,8 +2458,8 @@ class GJDataC:
2307
2458
 
2308
2459
  params_dict = {
2309
2460
  "commodity": commodity,
2310
- "start_date": start_date,
2311
- "end_date": end_date
2461
+ # "start_date": start_date,
2462
+ # "end_date": end_date
2312
2463
  }
2313
2464
 
2314
2465
  for key, value in params_dict.items():
@@ -2354,6 +2505,8 @@ class GJDataC:
2354
2505
  :return: 过滤后的数据
2355
2506
  """
2356
2507
 
2508
+
2509
+
2357
2510
  start_date = self._get_warehouse_stocks_future_date_conversion(start_date)
2358
2511
  end_date = self._get_warehouse_stocks_future_date_conversion(end_date)
2359
2512
 
@@ -2495,8 +2648,10 @@ class GJDataC:
2495
2648
  :return: None
2496
2649
  """
2497
2650
 
2498
- self._get_vwap_validate_date_type(start_date, "start_date")
2499
- self._get_vwap_validate_date_type(end_date, "end_date")
2651
+ # self._get_vwap_validate_date_type(start_date, "start_date")
2652
+ # self._get_vwap_validate_date_type(end_date, "end_date")
2653
+ self.format_date(start_date)
2654
+ self.format_date(end_date)
2500
2655
 
2501
2656
  def _get_vwap_validate_date_type(self, date_str, date_str_name):
2502
2657
  """
@@ -2587,8 +2742,10 @@ class GJDataC:
2587
2742
  limit_data = get_vwap_data.where(f"instrument_id in {order_book_ids}")
2588
2743
 
2589
2744
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
2590
- format_start_date = self.get_vwap_format_date(start_date)
2591
- format_end_date = self.get_vwap_format_date(end_date)
2745
+ # format_start_date = self.get_vwap_format_date(start_date)
2746
+ # format_end_date = self.get_vwap_format_date(end_date)
2747
+ format_start_date = self.format_date(start_date)
2748
+ format_end_date = self.format_date(end_date)
2592
2749
 
2593
2750
  # 根据日期对数据再次进行筛选,最大程度减轻数据库压力
2594
2751
  limit_data = limit_data.where(f"trade_time >= {format_start_date} and trade_time <= {format_end_date}").where(
@@ -2736,8 +2893,11 @@ class GJDataC:
2736
2893
  :return: None
2737
2894
  """
2738
2895
 
2739
- self._get_twap_validate_date_type(start_date, "start_date")
2740
- self._get_twap_validate_date_type(end_date, "end_date")
2896
+ self.format_date(start_date)
2897
+ self.format_date(end_date)
2898
+
2899
+ # self._get_twap_validate_date_type(start_date, "start_date")
2900
+ # self._get_twap_validate_date_type(end_date, "end_date")
2741
2901
 
2742
2902
  def _get_twap_validate_date_type(self, date_str, date_str_name):
2743
2903
  """
@@ -2801,8 +2961,11 @@ class GJDataC:
2801
2961
  limit_data = get_vwap_data.where(f"instrument_id in {order_book_ids}")
2802
2962
 
2803
2963
  # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
2804
- format_start_date = self.get_twap_format_date(start_date)
2805
- format_end_date = self.get_twap_format_date(end_date)
2964
+ # format_start_date = self.get_twap_format_date(start_date)
2965
+ # format_end_date = self.get_twap_format_date(end_date)
2966
+
2967
+ format_start_date = self.format_date(start_date)
2968
+ format_end_date = self.format_date(end_date)
2806
2969
 
2807
2970
  # 根据日期对数据再次进行筛选,最大程度减轻数据库压力
2808
2971
  limit_data = limit_data.where(f"trade_time >= {format_start_date} and trade_time <= {format_end_date}").where(
@@ -2950,11 +3113,10 @@ class GJDataC:
2950
3113
  self._get_history_bar_validate_params(datetime_str, instrument_id, bar_count, frequency, fields)
2951
3114
 
2952
3115
  # 转换日期格式
2953
- # query_time = self._get_history_bar_parse_datetime(datetime_str)
3116
+ query_time = self._get_history_bar_parse_datetime(datetime_str)
2954
3117
 
2955
- # 1d ['v2505'] 2025.01.21 1
2956
3118
  # 执行查询并筛选数据
2957
- result, bar_db_session = self._get_history_bar_execute_query(frequency, instrument_id, datetime_str,
3119
+ result, bar_db_session = self._get_history_bar_execute_query(frequency, instrument_id, query_time,
2958
3120
  bar_count)
2959
3121
  # 此处在所有的查询筛选都在数据库层面进行完后,再关闭连接
2960
3122
  # 如果将dolphindb的Table对象转换成DataFrame时,如果数据量较大,那么需要耗费的时间较长,所以尽可能将所有的筛选在数据库层面完成
@@ -2963,7 +3125,6 @@ class GJDataC:
2963
3125
  # 处理结果
2964
3126
  return self._get_history_bar_process_result(result, instrument_id, fields, bar_count)
2965
3127
  else:
2966
- print("22222")
2967
3128
  return self.get_history_bar(instrument_id, start_date, end_date, frequency, fields)
2968
3129
 
2969
3130
  def _get_history_bar_validate_params(self, datetime_str, instrument_id, bar_count,
@@ -3022,8 +3183,8 @@ class GJDataC:
3022
3183
  else:
3023
3184
  return False
3024
3185
 
3025
- @staticmethod
3026
- def _get_history_bar_parse_datetime(datetime_str):
3186
+ # @staticmethod
3187
+ def _get_history_bar_parse_datetime(self, datetime_str):
3027
3188
  """
3028
3189
  解析日期时间字符串
3029
3190
  Args:
@@ -3035,11 +3196,14 @@ class GJDataC:
3035
3196
 
3036
3197
  """
3037
3198
 
3038
- try:
3039
- return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S') if isinstance(datetime_str, str) \
3040
- else datetime_str
3041
- except ValueError:
3042
- raise ValueError("Invalid datetime format, should be 'YYYY-MM-DD HH:MM:SS'")
3199
+ return self.format_date(datetime_str)
3200
+
3201
+ # try:
3202
+ # return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S') if isinstance(datetime_str, str) \
3203
+ # else datetime_str
3204
+ # except ValueError:
3205
+ # raise ValueError("Invalid datetime format, should be 'YYYY-MM-DD HH:MM:SS'")
3206
+
3043
3207
 
3044
3208
  def _get_history_bar_execute_query(self, frequency, instrument_id, datetime_str, bar_count):
3045
3209
  """
@@ -3055,7 +3219,6 @@ class GJDataC:
3055
3219
 
3056
3220
  # 获取表信息
3057
3221
  table_info = self._get_history_bar_get_table_info(frequency, instrument_id)
3058
- print(table_info)
3059
3222
 
3060
3223
  # 获取数据
3061
3224
  table_data, bar_db_session = self.connect_db(table_info[0], table_info[1])
@@ -3094,7 +3257,8 @@ class GJDataC:
3094
3257
  else:
3095
3258
  raise Exception("frequency is not valid or frequency is not valid for this frequency")
3096
3259
 
3097
- def _get_history_bar_filter_data(self, table_data, instrument_id, datetime_str, bar_count):
3260
+ @staticmethod
3261
+ def _get_history_bar_filter_data(table_data, instrument_id, datetime_str, bar_count):
3098
3262
  """
3099
3263
  筛选数据
3100
3264
  Args:
@@ -3112,22 +3276,17 @@ class GJDataC:
3112
3276
  # dolphin_date = self._get_history_bar_datetime_to_dolphin_time(datetime_str)
3113
3277
  dolphin_date = datetime_str
3114
3278
 
3115
- # 使用 DolphinDB 的 where 子句筛选数据(这里因为实际需求的原因需要对每个instrument_id进行筛选,所以需要使用 contextby)
3116
- # 根据bar_count 分别对不同的instrument_id 进行筛选
3117
- # 此处的逻辑是,筛选出每个instrument_id距离给定日期最近的前bar_count条数据
3118
-
3119
3279
  if isinstance(instrument_id, str):
3120
- if bar_count != 0:
3121
- query_data = (table_data.where(f"instrument_id='{instrument_id}'").where(f"trading_day <= {dolphin_date}")
3122
- .contextby("instrument_id").sort("trading_day", ascending=True).limit(-bar_count))
3123
- else:
3124
- query_data = table_data.where(f"instrument_id='{instrument_id}'").where(f"trading_day = {dolphin_date}")
3280
+ query_data = (table_data.where(f"instrument_id='{instrument_id}'")
3281
+ .where(f"trading_day <= {dolphin_date}")
3282
+ .sort("trading_day", ascending=False)
3283
+ .limit(bar_count))
3284
+
3125
3285
  else:
3126
- if bar_count != 0:
3127
- query_data = (table_data.where(f"instrument_id in {instrument_id}").where(f"trading_day <= {dolphin_date}")
3128
- .contextby("instrument_id").sort("trading_day", ascending=True).limit(-bar_count))
3129
- else:
3130
- query_data = table_data.where(f"instrument_id in {instrument_id}").where(f"trading_day = {dolphin_date}")
3286
+ query_data = (table_data.where(f"instrument_id in {instrument_id}")
3287
+ .where(f"trading_day <= {dolphin_date}")
3288
+ .sort("trading_day", ascending=False)
3289
+ .limit(bar_count))
3131
3290
 
3132
3291
  # 根据日期对数据再次进行筛选,最大程度减轻数据库压力
3133
3292
  query_data = query_data.where(f"trade_time <= {dolphin_date}")
@@ -3291,8 +3450,13 @@ class GJDataC:
3291
3450
  # 获取数据
3292
3451
  table_data, bars_db_session = self.connect_db(table_info[0], table_info[1])
3293
3452
 
3453
+ # 转换日期格式
3454
+ conv_start_date = self.format_date(start_date)
3455
+ conv_end_date = self.format_date(end_date)
3456
+
3457
+
3294
3458
  # 筛选数据
3295
- query_data = self._get_history_bars_filter_data(table_data, instrument_id, start_date, end_date)
3459
+ query_data = self._get_history_bars_filter_data(table_data, instrument_id, conv_start_date, conv_end_date)
3296
3460
 
3297
3461
  return query_data.toDF(), bars_db_session
3298
3462
 
@@ -3644,7 +3808,8 @@ class GJDataC:
3644
3808
  :return: 查询到的交易日或时间点列表
3645
3809
  """
3646
3810
  # 将 trade_time 转换为 datetime 对象
3647
- trade_time = datetime.datetime.strptime(trade_time, '%Y.%m.%d %H:%M:%S')
3811
+ trade_time = self.format_date(trade_time)
3812
+
3648
3813
 
3649
3814
  # 获取交易日历数据
3650
3815
  trading_dates = self.get_trading_dates_data()
@@ -3755,6 +3920,8 @@ class GJDataC:
3755
3920
  """
3756
3921
 
3757
3922
  data, db_session = self.connect_db(trading_dates_db_table_name, trading_dates_db_path)
3923
+ date = self.format_date(date)
3924
+
3758
3925
 
3759
3926
  # 根据条件筛选数据
3760
3927
  # 筛选 trade_flag 列等于 T 的数据(是交易日的数据)
@@ -3765,13 +3932,13 @@ class GJDataC:
3765
3932
 
3766
3933
  """ 节假日判断 """
3767
3934
 
3768
- @staticmethod
3769
- def is_holiday(date):
3935
+ def is_holiday(self, date):
3770
3936
  """
3771
3937
  判断给定日期是否为节假日
3772
3938
  :param date: 给定的日期
3773
3939
  :return: True 或 False
3774
3940
  """
3941
+ date = self.format_date(date)
3775
3942
 
3776
3943
  # 将输入日期字符串转换为 datetime 对象
3777
3944
  date = datetime.datetime.strptime(date, '%Y.%m.%d').date()
@@ -3780,13 +3947,13 @@ class GJDataC:
3780
3947
 
3781
3948
  """ 周末判断 """
3782
3949
 
3783
- @staticmethod
3784
- def is_weekend(date):
3950
+ def is_weekend(self, date):
3785
3951
  """
3786
3952
  判断给定日期是否为周末
3787
3953
  :param date: 给定日期
3788
3954
  :return: True 或 False
3789
3955
  """
3956
+ date = self.format_date(date)
3790
3957
 
3791
3958
  # 将输入日期字符串转换为 datetime 对象
3792
3959
  date = datetime.datetime.strptime(date, '%Y.%m.%d')
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gjdatac-api
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: 数据接口打包
5
5
  Author: yin
6
6
  Author-email: 2018209921@qq.com
@@ -5,7 +5,7 @@ with open('description_info.md', 'r', encoding='utf-8') as f:
5
5
 
6
6
  setup(
7
7
  name='gjdatac_api', # 你的包名
8
- version='0.2.4', # 版本号
8
+ version='0.2.6', # 版本号
9
9
  description='数据接口打包', # 包的简要描述
10
10
  long_description=long_description, # 包的详细描述
11
11
  long_description_content_type='text/markdown', # 描述文件的类型
File without changes