量化交易之python篇 - 读取、解析、存入json文件(新增多个json文件 清仓、加总、按倍数调仓的功能 )

import json
import math


class PositionJsonOperator:
    """
     操作 json文件 的 类
     ps: 使用时, 需要导入 json、math库
    """

    @staticmethod
    def tqz_load_jsonfile(filename=""):
        if "" == filename:
            exception = Exception("Error: filename is empty")
            raise exception
        else:
            with open(filename, "r", encoding="utf-8") as fp:
                return json.load(fp=fp)

    @staticmethod
    def tqz_write_jsonfile(content=None, filename=""):
        if "" == filename:
            exception = Exception("Error: filename is empty")
            raise exception
        else:
            with open(filename, "w", encoding="utf-8") as fp:
                json.dump(content, fp=fp, ensure_ascii=False, indent=4)  # 参数indent: json文件按格式写入, 距行首空4格;

    @staticmethod
    def _get_newData_with_sumPosition(first_dic=None, second_dic=None):
        new_dic = {}
        for (key, value) in first_dic.items():
            value['pos'] = first_dic[key]['pos'] + second_dic[key]['pos']
            new_dic[key] = value
        return new_dic

    def _sum_position(self, source_filename1, source_filename2, target_filename):
        # 读取json文件数据
        cta_hla_dic = self.tqz_load_jsonfile(filename=source_filename1)
        cta_hsr_dic = self.tqz_load_jsonfile(filename=source_filename2)

        # 获取新的要写入json文件的数据
        new_dic = self._get_newData_with_sumPosition(cta_hla_dic, cta_hsr_dic)

        # 写入目标文件夹
        self.tqz_write_jsonfile(content=new_dic, filename=target_filename)

    def tqz_sum_position_all_jsonfile(self, *jsonfile_list, target_jsonfile):
        """
        加总多个 json文件的 持仓, 并写入新的目json标文件中
        :param jsonfile_list: 字符串数组
        :param target_jsonfile: 要存入的 json文件名
        """
        jsonfile_content_list = []
        [jsonfile_content_list.append(self.tqz_load_jsonfile(jsonfile)) for jsonfile in jsonfile_list]

        new_dic = {}
        for dic in jsonfile_content_list:
            for key, value in dic.items():

                if key not in new_dic:
                    new_dic[key] = dic[key]
                else:
                    value['pos'] = value['pos'] + new_dic[key]['pos']
                    new_dic[key] = value

        self.tqz_write_jsonfile(content=new_dic, filename=target_jsonfile)

    def _multi_position(self, source_jsonfile, multi):
        source_content = self.tqz_load_jsonfile(filename=source_jsonfile)

        for key, value in source_content.items():
            if value['pos'] > 0:
                value['pos'] = math.floor(value['pos']*multi)
            else:
                value['pos'] = math.floor(-1*value['pos']*multi) * -1
            source_content[key] = value

        self.tqz_write_jsonfile(content=source_content, filename=source_jsonfile)

    def tqz_multi_position_all(self, *jsonfile_list, multi):
        """
        按倍数调整 多个json文件的 持仓
        :param jsonfile_list: 需要调整持仓的 json文件数组
        :param multi: 倍数
        """
        [self._multi_position(source_jsonfile=jsonfile, multi=multi) for jsonfile in jsonfile_list]

    def _empty_position(self, source_jsonfile):
        self._multi_position(source_jsonfile=source_jsonfile, multi=0)

    def tqz_empty_position_all(self, *jsonfile_list):
        """
        清空 多个json文件的 持仓
        :param jsonfile_list: 需要清空的 json文件数组
        """
        self.tqz_multi_position_all(*jsonfile_list, multi=0)


def main_engine():
    list = []
    list.append("symbol_2.json")
    list.append("symbol_3.json")
    list.append("symbol_4.json")
    list.append("symbol_5.json")

    PositionJsonOperator().tqz_sum_position_all_jsonfile(*list, target_jsonfile="test.json")

    # PositionJsonOperator().tqz_multi_position_all("test.json", multi=0.5)
    # PositionJsonOperator().tqz_empty_position_all("test.json")


if __name__ == '__main__':
    main_engine()

 

你可能感兴趣的:(python)