gjdatac-api 0.0.6__tar.gz → 0.0.7__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.
@@ -1,14 +1,10 @@
1
- Metadata-Version: 2.1
2
- Name: gjdatac_api
3
- Version: 0.0.6
4
- Summary: 主动管理项目第一版(部分接口)
5
- Home-page: UNKNOWN
6
- Author: yin
7
- Author-email: 2018209921@qq.com
8
- License: UNKNOWN
9
- Platform: UNKNOWN
10
- Requires-Python: >3.6
11
- Description-Content-Type: text/markdown
12
-
13
- 第一次提交,测试是否可用
14
-
1
+ Metadata-Version: 2.1
2
+ Name: gjdatac_api
3
+ Version: 0.0.7
4
+ Summary: 主动管理项目第一版(部分接口)
5
+ Author: yin
6
+ Author-email: 2018209921@qq.com
7
+ Requires-Python: >3.6
8
+ Description-Content-Type: text/markdown
9
+
10
+ 第一次提交,测试是否可用
@@ -42,7 +42,7 @@ class GJDataC:
42
42
  self.db_session.close()
43
43
 
44
44
  ''' get_price 接口相关属性 '''
45
- self.get_price_data = None # 用于存放get_price接口数据
45
+ # self.get_price_data = None # 用于存放get_price接口数据
46
46
 
47
47
  ''' get_instruments 接口相关属性 '''
48
48
  self.get_instruments_data = None # 用于存放get_instruments接口数据
@@ -319,19 +319,19 @@ class GJDataC:
319
319
 
320
320
  ''' 参数校验完成后,开始根据参数从服务器获取数据 '''
321
321
  # 此处获取数据时已经根据order_book_ids筛选过一次数据了,所以后续不需要再对该字段进行筛选
322
- self.get_price_data_get_data(order_book_ids, asset_type, frequency)
322
+ data = self.get_price_data_get_data(order_book_ids, asset_type, frequency, start_date, end_date)
323
323
 
324
324
  ''' 数据处理 '''
325
325
  # 根据frequency进行处理
326
- self.get_price_by_type_frequency(asset_type, frequency)
326
+ data = self.get_price_by_type_frequency(asset_type, frequency, data)
327
327
 
328
328
  # 根据start_date和end_date筛选数据
329
- self.get_price_filter_by_date(start_date, end_date, order_book_ids)
329
+ data = self.get_price_filter_by_date(start_date, end_date, order_book_ids, data)
330
330
 
331
331
  # 对fields进行处理
332
- self.get_price_validate_fields(fields)
332
+ data = self.get_price_validate_fields(fields, data)
333
333
 
334
- return self.get_price_data
334
+ return data
335
335
 
336
336
  # @staticmethod
337
337
  def get_price_validate_order_book_ids(self, order_book_ids):
@@ -419,7 +419,7 @@ class GJDataC:
419
419
  else:
420
420
  return False
421
421
 
422
- def get_price_filter_by_date(self, start_date, end_date, order_book_ids):
422
+ def get_price_filter_by_date(self, start_date, end_date, order_book_ids, data):
423
423
  """
424
424
  根据start_date和end_date筛选数据
425
425
  此处order_book_ids参数用于处理未传递start_date和end_date的情况(根据order_book_ids返回距离当前时间最近的一条数据)
@@ -431,32 +431,34 @@ class GJDataC:
431
431
  """
432
432
  # 将'date_time'列转换为datetime类型
433
433
  try:
434
- if self.get_price_data['datetime'].dtype != 'datetime64[ns]':
435
- self.get_price_data['datetime'] = pd.to_datetime(self.get_price_data['datetime'])
434
+ if data['datetime'].dtype != 'datetime64[ns]':
435
+ data['datetime'] = pd.to_datetime(data['datetime'])
436
436
  except ValueError:
437
437
  raise ValueError("source data's datetime format is not correct")
438
438
 
439
439
  # 如果传递了start_date 和 end_date, 则返回时间段内的数据
440
440
  if start_date and end_date:
441
- self.get_price_data = self.get_price_data[
442
- (self.get_price_data['datetime'] >= start_date) & (self.get_price_data['datetime'] <= end_date)]
441
+ data = data[
442
+ (data['datetime'] >= start_date) & (data['datetime'] <= end_date)]
443
443
 
444
444
  # 如果没有传递start_date 和 end_date, 则根据传入的order_book_ids返回每个合约距离当前时间最近的一条数据
445
445
  elif not start_date and not end_date:
446
446
  date_time = datetime.datetime.now()
447
- self.get_price_data = self.get_price_data[
448
- self.get_price_data["datetime"] <= date_time]
447
+ data = data[
448
+ data["datetime"] <= date_time]
449
449
 
450
450
  # 根据传入的order_book_ids进行处理
451
451
  if isinstance(order_book_ids, str):
452
- self.get_price_data = self.get_price_data.tail(1)
452
+ data = data.tail(1)
453
453
  else:
454
454
  # 将order_book_id作为分组键,并找到每个分组中距离当前时间最近的数据
455
- nearest_data = self.get_price_data.groupby('order_book_ids')
455
+ nearest_data = data.groupby('order_book_ids')
456
456
  # 使用 tail(1) 获取每个组的最后一条数据
457
- self.get_price_data = nearest_data.apply(lambda x: x.tail(1)).reset_index(drop=True)
457
+ data = nearest_data.apply(lambda x: x.tail(1)).reset_index(drop=True)
458
+
459
+ return data
458
460
 
459
- def get_price_validate_fields(self, fields):
461
+ def get_price_validate_fields(self, fields, data):
460
462
  """
461
463
  根据用户选择的字段返回数据
462
464
  :param fields: 字段列表 -- 用户传入的需要返回的字段
@@ -464,9 +466,11 @@ class GJDataC:
464
466
  :return : None
465
467
 
466
468
  """
467
- self.get_price_data = self.general_validate_fields(self.get_price_data, fields)
469
+ data = self.general_validate_fields(data, fields)
470
+
471
+ return data
468
472
 
469
- def get_price_by_type_frequency(self, asset_type, frequency):
473
+ def get_price_by_type_frequency(self, asset_type, frequency, data):
470
474
  """
471
475
  根据合约类型和frequency筛选数据
472
476
 
@@ -486,7 +490,9 @@ class GJDataC:
486
490
  # frequency存在再根据频率对数据做进一步处理
487
491
  else:
488
492
  bar_or_tick = get_price_frequency_dict[asset_type][suffix] # 获取数据类型是bar还是tick,根据值处理字段
489
- self.get_price_data_rename_columns(bar_or_tick) # 重命名字段
493
+ data = self.get_price_data_rename_columns(bar_or_tick, data) # 重命名字段
494
+
495
+ return data
490
496
 
491
497
  @staticmethod
492
498
  def _ends_with(variable, suffixes):
@@ -500,7 +506,7 @@ class GJDataC:
500
506
  return True, suffix
501
507
  return False, ""
502
508
 
503
- def get_price_data_rename_columns(self, bar_or_tick):
509
+ def get_price_data_rename_columns(self, bar_or_tick, data):
504
510
  """
505
511
  将从数据库中获取的数据列名重命名
506
512
 
@@ -509,9 +515,10 @@ class GJDataC:
509
515
  """
510
516
 
511
517
  new_columns = get_price_data_type_dict.get(bar_or_tick)
512
- self.get_price_data = self.get_price_data.rename(columns=new_columns)
518
+ data = data.rename(columns=new_columns)
519
+ return data
513
520
 
514
- def _get_price_get_data(self, order_book_ids, table_name, db_path):
521
+ def _get_price_get_data(self, order_book_ids, table_name, db_path, start_date, end_date):
515
522
  """
516
523
  用于接收数据表以及数据库,获取数据
517
524
  : params order_book_ids: 合约代码
@@ -520,6 +527,7 @@ class GJDataC:
520
527
 
521
528
  : return filtered_data: 从数据库获取并根据合约代码筛选后的数据
522
529
  """
530
+
523
531
  get_price_data, db_session = self.connect_db(table_name, db_path)
524
532
 
525
533
  # 使用 DolphinDB 的 where 子句筛选数据
@@ -528,9 +536,15 @@ class GJDataC:
528
536
  else:
529
537
  limit_data = get_price_data.where(f"instrument_id in {order_book_ids}")
530
538
 
539
+ # 将日期转换成 DolphinDB 识别的日期格式,用于筛选数据
540
+ format_start_date = self.get_price_format_date(start_date)
541
+ format_end_date = self.get_price_format_date(end_date)
542
+
543
+ # 根据日期对数据再次进行筛选,最大程度减轻数据库压力
544
+ limit_data = limit_data.where(f"trade_time >= {format_start_date} and trade_time <= {format_end_date}")
545
+
531
546
  print("开始处理数据\n")
532
- if limit_data.rows > 1000000:
533
- print(f"当前获取的数据量较大,总数据量为: {limit_data.rows}, 执行时间可能较久,建议调整参数范围,多次获取")
547
+ print("总数据量: ", limit_data.rows)
534
548
 
535
549
  # 分批处理
536
550
  filtered_data = self._get_price_get_data_todf(limit_data)
@@ -540,6 +554,18 @@ class GJDataC:
540
554
 
541
555
  return filtered_data
542
556
 
557
+ @staticmethod
558
+ def get_price_format_date(date_str):
559
+ """将日期字符串格式化为 'YYYY.MM.DD' 格式。
560
+ : params date_str: 日期字符串,例如 '2018-01-04 09:01:00'。
561
+ : return :
562
+ 格式化后的日期字符串,例如 '2018.01.04'。
563
+ """
564
+ if isinstance(date_str, str):
565
+ date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
566
+ return date_obj.strftime('%Y.%m.%d')
567
+ return date_str.strftime('%Y.%m.%d')
568
+
543
569
  @staticmethod
544
570
  def _get_price_get_data_todf(data):
545
571
  """
@@ -559,52 +585,7 @@ class GJDataC:
559
585
  return result
560
586
  return data.toDF()
561
587
 
562
- # @staticmethod
563
- # def _get_price_get_data_todf_in_batches(self, data, order_book_ids):
564
- # """
565
- # 用于接收从数据库获取的数据,并分批处理,将其转换成dataframe格式的数据返回
566
- # : params data: 从数据库获取的数据
567
- # : params order_book_ids: 合约代码
568
- # : return : 转换成dataframe格式后的数据
569
- # """
570
-
571
- # result = pd.DataFrame()
572
- # if isinstance(order_book_ids, str):
573
- # if data.rows > 1000000:
574
- # chunk_size, start = 1000000, 0
575
- # while start < data.rows:
576
- # limit_data = data.limit([start, chunk_size]).toDF()
577
- # result = limit_data if result.empty else pd.concat([result, limit_data], ignore_index=True)
578
- # start += chunk_size
579
-
580
- # return result
581
- # return data.toDF()
582
- # elif isinstance(order_book_ids, list):
583
- # for order_book_id in order_book_ids:
584
- # instrument_id_df = self._get_price_get_data_todf_by_instrument_id(data, order_book_id, result)
585
- # result = instrument_id_df if result.empty else pd.concat([result, instrument_id_df], ignore_index=True)
586
- # return result
587
- # def _get_price_get_data_todf_by_instrument_id(self, data, order_book_id, result_df):
588
- # """
589
- # 此方法用于批量获取数据(数据量过大)
590
- # :param data: 从dolphindb查询到的数据
591
- # :param order_book_ids: 合约代码列表
592
- # :param result_df: 处理后的dataframe结果集
593
- # """
594
-
595
- # data.where(f"instrument_id='{order_book_id}'")
596
-
597
- # if data.rows > 1000000:
598
- # chunk_size, start = 1000000, 0
599
- # while start < data.rows:
600
- # limit_data = data.limit([start, chunk_size]).toDF()
601
- # result_df = limit_data if result_df.empty else pd.concat([result_df, limit_data], ignore_index=True)
602
- # start += chunk_size
603
-
604
- # return result_df
605
- # return data.toDF()
606
-
607
- def get_price_data_get_data(self, order_book_ids, asset_type, frequency):
588
+ def get_price_data_get_data(self, order_book_ids, asset_type, frequency, start_date, end_date):
608
589
  """
609
590
  根据参数获取数据库和表名,调用接口获取数据
610
591
 
@@ -636,7 +617,8 @@ class GJDataC:
636
617
  if db_table_name is None:
637
618
  raise Exception("frequency is not valid for this asset_type")
638
619
 
639
- self.get_price_data = self._get_price_get_data(order_book_ids, *db_table_name)
620
+ data = self._get_price_get_data(order_book_ids, *db_table_name, start_date, end_date)
621
+ return data
640
622
 
641
623
  """ get_instruments(合约基础信息) """
642
624
 
@@ -1,14 +1,10 @@
1
- Metadata-Version: 2.1
2
- Name: gjdatac-api
3
- Version: 0.0.6
4
- Summary: 主动管理项目第一版(部分接口)
5
- Home-page: UNKNOWN
6
- Author: yin
7
- Author-email: 2018209921@qq.com
8
- License: UNKNOWN
9
- Platform: UNKNOWN
10
- Requires-Python: >3.6
11
- Description-Content-Type: text/markdown
12
-
13
- 第一次提交,测试是否可用
14
-
1
+ Metadata-Version: 2.1
2
+ Name: gjdatac-api
3
+ Version: 0.0.7
4
+ Summary: 主动管理项目第一版(部分接口)
5
+ Author: yin
6
+ Author-email: 2018209921@qq.com
7
+ Requires-Python: >3.6
8
+ Description-Content-Type: text/markdown
9
+
10
+ 第一次提交,测试是否可用
@@ -1,4 +1,4 @@
1
- [egg_info]
2
- tag_build =
3
- tag_date = 0
4
-
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -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.0.6', # 版本号
8
+ version='0.0.7', # 版本号
9
9
  description='主动管理项目第一版(部分接口)', # 包的简要描述
10
10
  long_description=long_description, # 包的详细描述
11
11
  long_description_content_type='text/markdown', # 描述文件的类型