python - 第一次尝试 python,错误(“IndexError: index 8 is out of bounds for axis 0 with size 8”)和效率问题
问题描述
上周才开始学习 python,已经有 20 年没有用其他方式编码了,而且从一开始就没有那么先进。我把你好世界的事记下来了。现在我试图回测外汇对。感谢您对学习曲线的任何帮助,当然还有在我的 Lynda 视频上搜索这个网站。
遇到一个奇怪的错误,并且想知道是否有更有效的方法可以像我一样循环遍历 excel 数据列。
正在阅读的电子表格很简单...... A 列下方有 56 个外汇对,列标题为日期的位置上方有 8 行,每列中的单元格是该日期各自的外汇对收盘价。该策略从第 2 列的顶部开始(因此可以计算出相对于先前先验的回报百分比)并计算出每对的周期/周期回报百分比,确定哪个是“最大值”,然后“做多”那个表现最好的人......其在随后的时期/时期的表现被记录为投资组合的 PnL(代码中的“p”),循环直到读取当前最近的列。
该错误与使用 8 列而不是 7 列有关...当我将循环限制为 7 列而不是 8 列时有效。当我使用 8 时,我得到一堵以“IndexError:索引 8 超出轴 0 的范围”结尾的文本墙大小为 8" 当我使用太多行时,类似的错误是 56 而不是 55,我认为我错过了最后一行。
这是我的代码:,,,
enter code here
#set up imports
import pandas as pd
#import spreadsheet
x1 = pd.ExcelFile(r"C:\Users\Gamblor\Desktop\Python\test2020.xlsx")
df = pd.read_excel(x1, "Sheet1", header=1)
#define counters for loops
o = 1 # observation counter
c = 3 # column counter
r = 0 # active row counter for sorting through for max
#define identifiers for the portfolio
rpos = 0 # static row, for identifying which currency pair is in column 0 of that row
p = 100 # portfolio size starts at $100
#define the stuff we are evaluating for
pair = df.iat[r,0] # starting pair at 0,0 where each loop will begin
pair_pct_rtn = 0 # starts out at zero, becomes something at first evaluation, then gets
compared to each subsequent eval
pair_pct_rtn_calc = 0 # a second version of above, for comparison to prior return
#runs a loop starting at the top to find the max period/period % return in a specific column
while (c < 8): # manually limiting this to 5 columns left to right
while (r < 55): # i am manually limiting this to 55 data rows per the spreadsheet ... would be better if automatic
pair_pct_rtn_calc = ((df.iat[r,c])/(df.iat[r,c-1]) - 1)
if pair_pct_rtn_calc > pair_pct_rtn: # if its a higher return, it must be the "max" to that point
pair = df.iat[r,0] # identifies the max pair for this column observation, so far
pair_pct_rtn = pair_pct_rtn_calc # sets pair_pct_rtn as the new max
rpos = r # identifies the max pair's ROW for this column observation, so far
r = r + 1 # adds to r in order to jump down and calc the next row
print('in obs #', o ,', ', pair ,'did best at' ,pair_pct_rtn ,'.')
o = o + 1
# now adjust the portfolio by however well USDMXN did in the subsequent week
p = p * ( 1 + ((df.iat[rpos,c+1])/(df.iat[rpos,c]) - 1))
print('then the subsequent period it did: ',(df.iat[rpos,c+1])/(df.iat[rpos,c]) - 1)
print('resulting in portfolio value of', p)
rpos = 0
r = 0
pair_pct_rtn = 0
c = c + 1 # adds to c in order to move to the next period to the right
print(p)
解决方案
由于索引从 0 开始标记,因此您要查找的第 8 个元素的索引为 7。同样,行索引 55(第 56 行)将是您的最后一行。
推荐阅读
- c# - .NET Core 中的 HttpClientHandler UseDefaultCredentials
- performance - Neo4j 实时推荐性能
- java - 在java sha1 openssl中解密aes加密文件
- flutter - 如何在 ListView 上创建构建下一个不可见的小部件?
- python - 使用python和win32com编写outlook规则
- javascript - 按列值将数据行复制到不同工作表的更快方法
- r - 插入后 R odbc 不返回 SCOPE_IDENTITY
- postgresql - 如何使用 Postgraphile 或替代方案在 Postgres 中实现复杂的基于权限的数据访问
- influxdb - 如何获取最新的时间戳数据
- android-studio - 将Android Studio升级到4.1后,代码不全彩