• 量化交易之One Piece篇 - onepiece_rsh - 按小节时间清洗全市场盘口数据


    1. import os
    2. import re
    3. import pandas
    4. # from tqz_extern.pandas_operator import pandas
    5. from tqz_extern.json_operator import TQZJsonOperator
    6. import warnings
    7. warnings.filterwarnings("ignore")
    8. class MarketDataParser:
    9. session_map = TQZJsonOperator.tqz_load_jsonfile(jsonfile='../trading_time/source_trading_time.json')
    10. source_content = None
    11. target_dir: str = ''
    12. @classmethod
    13. def dump_all_format_csv(cls, datetime_str: str):
    14. """
    15. Dump all instruments' format csv of one day.
    16. :return:
    17. """
    18. cls.__check_source_file(datetime_str=datetime_str)
    19. assert cls.source_content is not None, f'cls.source_content is None.'
    20. all_instrument_ids = list(set(cls.source_content['InstrumentID'].values))
    21. for instrumentID in all_instrument_ids:
    22. single_instrument_df = cls.source_content[cls.source_content["InstrumentID"] == instrumentID]
    23. cls.__get_format_market_data(
    24. instrument_source_df=single_instrument_df
    25. ).to_csv(f'{cls.target_dir}/{instrumentID}.csv', index=False)
    26. @classmethod
    27. def __check_source_file(cls, datetime_str: str):
    28. """
    29. Check single day's market depth data csv file.
    30. :param datetime_str: datatime of need parse, eg: 20230926.
    31. """
    32. year, month, day = datetime_str[:4], datetime_str[4:6], datetime_str[6:]
    33. source_path = f'E:/futures_market_data/market_depth_data/{year}/{month}/market_depth_data_{datetime_str}.csv'
    34. cls.target_dir = f'E:/futures_market_data/target_data/{year}/{month}/{datetime_str}'
    35. assert os.path.exists(path=source_path) is True, f'Bad source_path {source_path}.'
    36. if os.path.exists(path=cls.target_dir) is False:
    37. os.makedirs(cls.target_dir, exist_ok=True)
    38. cls.source_content = pandas.read_csv(source_path)
    39. @classmethod
    40. def __get_format_market_data(cls, instrument_source_df: pandas.DataFrame()) -> pandas.DataFrame():
    41. """
    42. Clean single instrument dataframe.
    43. :param instrument_source_df: source dataframe of single instrument
    44. :return: single instrument dataframe after clean.
    45. """
    46. assert len(instrument_source_df['ExchangeInstrument'].unique()) == 1, f'Bad ExchangeInstrument {instrument_source_df["ExchangeInstrument"].unique()}.'
    47. symbol = re.sub(r'\d+', '', instrument_source_df['ExchangeInstrument'].unique()[0])
    48. assert symbol in cls.session_map.keys(), f'Bad symbol: {symbol}.'
    49. symbol_session_map = cls.session_map[symbol]['night'] + cls.session_map[symbol]['day']
    50. instrument_source_df['trading_time'] = False
    51. instrument_format_df = None
    52. for single_session in symbol_session_map:
    53. if 2 == len(single_session):
    54. if single_session[0] < single_session[1]:
    55. instrument_source_df['trading_time'] = (instrument_source_df['UpdateTime'] >= single_session[0]) & (instrument_source_df['UpdateTime'] < single_session[1])
    56. elif single_session[1] > single_session[0]:
    57. instrument_source_df['trading_time'] = (instrument_source_df['UpdateTime'] >= single_session[0]) | (instrument_source_df['UpdateTime'] < single_session[1])
    58. if instrument_format_df is None:
    59. instrument_format_df = instrument_source_df[instrument_source_df['trading_time'] == True]
    60. else:
    61. instrument_format_df = pandas.concat([instrument_format_df, instrument_source_df[instrument_source_df['trading_time'] == True]])
    62. instrument_format_df.sort_values(by='Timestamp', ascending=True, inplace=True)
    63. instrument_format_df.reset_index(inplace=True)
    64. del instrument_format_df['index']
    65. del instrument_format_df['trading_time']
    66. return instrument_format_df
    67. if __name__ == '__main__':
    68. parser_datetime = '20230926'
    69. MarketDataParser.dump_all_format_csv(datetime_str=parser_datetime)

  • 相关阅读:
    12 个强大的现代 CSS 技术
    Python 爬虫入门
    【Proteus仿真】【Arduino单片机】RGB彩灯
    Flutter - 底部多选弹框组件
    Windos中部署 Elasticsearch(7.8.0)集群
    每日一题:跳跃游戏II
    离线数仓(10):ODS层实现之业务数据核对
    短视频seo矩阵系统源码开发搭建--代用户发布视频能力
    【Eigen】旋转方向及eulerAngles函数参数说明
    JDK,JRE,JVM之间的关系
  • 原文地址:https://blog.csdn.net/Michael_234198652/article/details/133471154