python-通过动态规划合并列表中根据某几个字段合并数据

本文介绍了一种用于合并特定交易数据的算法实现。该算法通过比较数据记录中的instrumentid、direct和hedge字段来确定是否将两条记录合并,并最终输出合并后的交易数据列表。具体实现了交易数据的高效处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据这是三个字段instrumentid、direct、hedge确定唯一的一行数据,再根据datetype字段合并两条数据。

 getData = [
        {
            'date': '20190720',
            'datetype': '2',
            'opencost': 455350.0,
            'instrumentid': 'cu2001',
            'direct': 2,
            'longfrozen': 0,
            'hedge': 3,
            'ydposition': 25,
            'closeprofit': 44,
            'positioncost': 200,
            'positionprofit': -525.0,
            'usemargin': 54663.0,
            'commission': 10.0,
            'position': 25,
            'shortfrozen': 0,
            'closeprofitbydate': 0.0,
            'closeprofitbytrade': 0.0
        },
        {
            'date': '20190720',
            'datetype': '1',
            'opencost': 455350.0,
            'instrumentid': 'cu2001',
            'direct': 2,
            'longfrozen': 0,
            'hedge': 3,
            'ydposition': 0,
            'closeprofit': 44,
            'positioncost': 200,
            'positionprofit': -525.0,
            'usemargin': 54663.0,
            'commission': 10.0,
            'position': 20,
            'shortfrozen': 0,
            'closeprofitbydate': 0.0,
            'closeprofitbytrade': 0.0
        },
        {
            'date': '20190720',
            'datetype': '2',
            'opencost': 455350.0,
            'instrumentid': 'cu2001',
            'direct': 3,
            'longfrozen': 0,
            'hedge': 3,
            'ydposition': 35,
            'closeprofit': 44,
            'positioncost': 200,
            'positionprofit': -525.0,
            'usemargin': 54663.0,
            'commission': 10.0,
            'position': 35,
            'shortfrozen': 0,
            'closeprofitbydate': 0.0,
            'closeprofitbytrade': 0.0
        },
        {
            'date': '20190720',
            'datetype': '1',
            'opencost': 455350.0,
            'instrumentid': 'cu2001',
            'direct': 3,
            'longfrozen': 0,
            'hedge': 3,
            'ydposition': 0,
            'closeprofit': 44,
            'positioncost': 200,
            'positionprofit': -525.0,
            'usemargin': 54663.0,
            'commission': 10.0,
            'position': 40,
            'shortfrozen': 0,
            'closeprofitbydate': 0.0,
            'closeprofitbytrade': 0.0
        },
        {
            'date': '20190720',
            'datetype': '1',
            'opencost': 455350.0,
            'instrumentid': 'CF003',
            'direct': 2,
            'longfrozen': 0,
            'hedge': 1,
            'ydposition': 0,
            'closeprofit': 44,
            'positioncost': 200,
            'positionprofit': -525.0,
            'usemargin': 54663.0,
            'commission': 10.0,
            'position': 5,
            'shortfrozen': 0,
            'closeprofitbydate': 0.0,
            'closeprofitbytrade': 0.0
        },
    
    ]
    
    
    class CaculateHoldingData():
        def __init__(self, data):
            self.returnList = []
            self.data = data
            while self.data.__len__() > 1:
                self.handle()
            if self.data.__len__() != 0:
                self.returnList.extend(self.data)
            print(len(self.returnList), self.returnList)
    
        @property
        def getList(self):
            return self.returnList
    
        def handle(self):
            for i in range(1, len(self.data)):
                if self.data[0]['instrumentid'] == self.data[i]['instrumentid'] and self.data[0]['direct'] == self.data[i][
                    'direct'] \
                        and self.data[0]['hedge'] == self.data[i]['hedge']:
                    res = self.caculte(self.data[i], self.data[0])
                    print(res)
                    self.returnList.append(res)
                    self.data.remove(self.data[i])
                    self.data.remove(self.data[0])
                    return
            self.returnList.append(self.data[0])
            self.data.remove(self.data[0])
    
        def caculte(self, d1_holding_data, d2_holding_data):
            # 总仓 = 今仓 + 昨仓
            sum_holding = d1_holding_data['position'] + d2_holding_data['position']
            # 开仓均价
            d1_holding_data['opencost'] = (d1_holding_data['position'] / sum_holding) * d1_holding_data[
                'opencost'] + (d2_holding_data['position'] / sum_holding) * d2_holding_data['opencost']
            # 持仓盈亏
            d1_holding_data['positionprofit'] = d1_holding_data['positionprofit'] + d2_holding_data[
                'positionprofit']
            # 平仓盈亏
            d1_holding_data['closeprofit'] = d1_holding_data['closeprofit'] + d2_holding_data[
                'closeprofit']
            # 盯市盈亏
            d1_holding_data['closeprofitbydate'] = d1_holding_data['closeprofitbydate'] + d2_holding_data[
                'closeprofitbydate']
            # 逐笔盈亏
            d1_holding_data['closeprofitbytrade'] = d1_holding_data['closeprofitbytrade'] + \
                                                    d2_holding_data['closeprofitbytrade']
            # 持仓均价
            d1_holding_data['positioncost'] = (d1_holding_data['position'] / sum_holding) * \
                                              d1_holding_data['positioncost'] + (
                                                      d2_holding_data['position'] / sum_holding) * \
                                              d2_holding_data['positioncost']
            # 保证金
            d1_holding_data['usemargin'] = d1_holding_data['usemargin'] + d2_holding_data['usemargin']
            # 手续费
            d1_holding_data['commission'] = d1_holding_data['commission'] + d2_holding_data['commission']
            d1_holding_data['shortfrozen'] = d1_holding_data['shortfrozen'] + d2_holding_data[
                'shortfrozen']
            d1_holding_data['longfrozen'] = d1_holding_data['longfrozen'] + d2_holding_data['longfrozen']
            # 今仓
            d1_holding_data['position'] = sum_holding
            # 昨仓归零
            d1_holding_data['ydposition'] = 0
    
            return d1_holding_data
    
    
    CaculateHoldingData(getData)
    
    
    def handleMSG(self, code, msg):
        '''回调'''
        caculateHoldingData = CaculateHoldingData(getData)
        print(caculateHoldingData.returnList)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天下·第二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值