期货日数据维护与使用_日数据维护_主力合约计算逻辑

2024-01-10 07:04

本文主要是介绍期货日数据维护与使用_日数据维护_主力合约计算逻辑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

主力合约换月规则(文化财经)

主力合约计算逻辑 

数据准备

代码

​下载


主力合约换月规则(文化财经)

主力合约计算逻辑 

数据准备

本文以沪银为例,将沪银所有日数据文件放入一个文件夹中,文件名命名方式为 合约名_交割年份.csv

代码

def caculate_main_from_zero():main_column_list = ['ticker','deliYear','tradeDate','openPrice','highestPrice','lowestPrice','closePrice','settlePrice','turnoverVol','turnoverValue','openInt']# 放置品种所有日数据文件,文件名 合约名_交割年份.csvpre_dir = r'E:/temp000/'file_list = os.listdir(pre_dir)# 将合约日文件合并到一个pd.DataFrame()中df = pd.DataFrame()for item in file_list:file_path = pre_dir + itemitem_str = item.split('.')[0]ticker = item_str.split('_')[0]deliYear = item_str.split('_')[1]df_one = pd.read_csv(file_path,encoding='utf-8')df_one['ticker'] = tickerdf_one['deliYear'] = deliYeardf = pd.concat([df,df_one])pass# 去除数据为空的数据df.dropna(inplace=True)if len(df)<=0:print('所有合约数据为空')return# 按日期分组df['o_date'] = pd.to_datetime(df['tradeDate'])df.sort_values(by='o_date',ascending=True,inplace=True)df['row_i'] = [i for i in range(len(df))]df_group = df.groupby(by='o_date',as_index=False)df_main = pd.DataFrame()cur_main_ticker = Nonecur_main_deliYear = Nonepre_next_ticker = Nonepre_next_deliYear = Nonenext_change_yeah = Falsefor name,group in df_group:if len(group)<=1:# 当日只有一条日数据,那该数据对应的合约即为主力合约df_main = pd.concat([df_main,group.iloc[[0]]])cur_main_ticker = group.iloc[0]['ticker']cur_main_deliYear = group.iloc[0]['deliYear']passelse:# 当日有多条日数据,分别计算成交量最大和持仓量最大的合约# 成交量最大合约df_vol = group.sort_values(by='turnoverVol',ascending=False)# 持仓量最大合约df_inte = group.sort_values(by='openInt',ascending=False)# 如果成交量最大与持仓量最大为同一合约if df_vol.iloc[0]['row_i'] == df_inte.iloc[0]['row_i']:if not cur_main_ticker:# 不存在前主力合约,那该合约即为主力合约df_main = pd.concat([df_main,df_vol.iloc[[0]]])cur_main_ticker = df_vol.iloc[0]['ticker']cur_main_deliYear = df_vol.iloc[0]['deliYear']passelse:if next_change_yeah:# 有【预备主力合约】if df_vol.iloc[0]['ticker'] == pre_next_ticker and df_vol.iloc[0]['deliYear']==pre_next_deliYear:# 【预备主力合约】继昨日是成交量和持仓量同时最大后,今日还是成交量和持仓量最大,切换df_main = pd.concat([df_main, df_vol.iloc[[0]]])cur_main_ticker = pre_next_tickercur_main_deliYear = pre_next_deliYearnext_change_yeah = Falsepasselse:# 【预备主力合约】继昨日是成交量和持仓量同时最大后,今日不济,【预备主力合约】撤销next_change_yeah = False# ----------- 【当日成交量最大和持仓量最大 为同一个合约】 延续当前合约 start# 存在前主力合约,判断该合约是否与前主力合约一致if df_vol.iloc[0]['ticker'] == cur_main_ticker and df_vol.iloc[0]['deliYear'] == cur_main_deliYear:# 一致,主力合约延续,不切换df_main = pd.concat([df_main, df_vol.iloc[[0]]])passelse:# 不一致,主力合约延续,不切换;预备下一交易日切换one_df = group.loc[(group['ticker'] == cur_main_ticker) & (group['deliYear'] == cur_main_deliYear)].copy()df_main = pd.concat([df_main, one_df.iloc[[0]]])next_change_yeah = Truepre_next_ticker = df_vol.iloc[0]['ticker']pre_next_deliYear = df_vol.iloc[0]['deliYear']pass# ----------- 【当日成交量最大和持仓量最大 为同一个合约】 延续当前合约 endpasspasselse:# 无【预备主力合约】# ----------- 【当日成交量最大和持仓量最大 为同一个合约】 延续当前合约 start# 存在前主力合约,判断该合约是否与前主力合约一致if df_vol.iloc[0]['ticker'] == cur_main_ticker and df_vol.iloc[0]['deliYear'] == cur_main_deliYear:# 一致,主力合约延续,不切换df_main = pd.concat([df_main, df_vol.iloc[[0]]])passelse:# 不一致,主力合约延续,不切换;预备下一交易日切换one_df = group.loc[(group['ticker'] == cur_main_ticker) & (group['deliYear'] == cur_main_deliYear)].copy()df_main = pd.concat([df_main, one_df.iloc[[0]]])next_change_yeah = Truepre_next_ticker = df_vol.iloc[0]['ticker']pre_next_deliYear = df_vol.iloc[0]['deliYear']pass# ----------- 【当日成交量最大和持仓量最大 为同一个合约】 延续当前合约 endpasspasselse:# 成交量最大和持仓量最大不是同一合约if not cur_main_ticker:df_main = pd.concat([df_main,df_vol.iloc[[0]]])cur_main_ticker = df_vol.iloc[0]['ticker']cur_main_deliYear = df_vol.iloc[0]['deliYear']passelse:if df_vol.iloc[0]['ticker']==cur_main_ticker and df_vol.iloc[0]['deliYear']==cur_main_deliYear:df_main = pd.concat([df_main,df_vol.iloc[[0]]])elif df_inte.iloc[0]['ticker'] == cur_main_ticker and df_inte.iloc[0]['deliYear']==cur_main_deliYear:df_main = pd.concat([df_main,df_inte.iloc[[0]]])else:df_main = pd.concat([df_main,df_vol.iloc[[0]]])cur_main_ticker = df_vol.iloc[0]['ticker']cur_main_deliYear = df_vol.iloc[0]['deliYear']passpasspasspasspassif len(df_main) <=0:print('主力合约条数为0')returndf_main = df_main.loc[:,main_column_list].copy()df_main.to_csv(pre_dir + 'AG.csv',encoding='utf-8')pass

结果存储为 AG.csv

下载

链接:https://pan.baidu.com/s/1X0O4ZtwX8_ZmdDJB4DJXTA 
提取码:jjdz

这篇关于期货日数据维护与使用_日数据维护_主力合约计算逻辑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/589957

相关文章

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2