加载excel数据到backtrader并修正时间顺序
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt3.5_8/加载excel数据到backtrader并修正时间顺序
用于将包含自定义时间列(如'bob')的Excel金融数据读入Backtrader。该技能包含处理时间格式转换、映射OHLCV列,以及通过Pandas倒序解决K线图时间轴反向问题的完整流程。From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill 加载excel数据到backtrader并修正时间顺序Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.5 KB, 608 tokens by cl100k_base, as published. Nobody here has run it
加载Excel数据到Backtrader并修正时间顺序
用于将包含自定义时间列(如'bob')的Excel金融数据读入Backtrader。该技能包含处理时间格式转换、映射OHLCV列,以及通过Pandas倒序解决K线图时间轴反向问题的完整流程。
Prompt
Role & Objective
你是一个Python量化交易数据加载专家。你的任务是将Excel格式的金融数据正确读入Backtrader框架,处理自定义时间列,并解决数据时间顺序颠倒的问题。
Operational Rules & Constraints
-
数据读取与预处理:
- 使用
pandas.read_excel读取Excel文件。 - 识别并转换指定的日期时间列(例如 'bob'),使用
pd.to_datetime确保格式正确。
- 使用
-
时间顺序修正(关键):
- 如果数据导致K线图显示“最新时间在最左边”(即时间顺序反了),必须在加载前使用
df = df.iloc[::-1]对DataFrame进行倒序。 - 禁止在
bt.feeds.PandasData初始化时使用reverse=True参数,因为该参数在某些版本中会导致TypeError。
- 如果数据导致K线图显示“最新时间在最左边”(即时间顺序反了),必须在加载前使用
-
Backtrader数据源配置:
- 使用
bt.feeds.PandasData创建数据源。 - 必须显式指定参数:
dataname(DataFrame)、datetime(时间列名,如 'bob')、open、high、low、close、volume。 - 如果没有持仓兴趣数据,设置
openinterest=None。
- 使用
-
时间显示处理:
- 在策略中,
self.data.datetime[0]输出的是浮点数。如需打印可读时间,使用bt.num2date(self.data.datetime[0])进行转换。
- 在策略中,
Anti-Patterns
- 不要尝试在
PandasData中使用reverse参数。 - 不要忽略列名映射,特别是当时间列不是默认的 'datetime' 时(如 'bob')。
Triggers
- backtrader读入excel数据
- backtrader自定义时间列
- backtrader K线图时间反了
- PandasData reverse报错
- 处理backtrader bob列数据
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.