I'm trying to backtest a trading strategy.
Columns that I already have saved as a dataframe: 'Date', 'A', 'B', 'C'
I am trying to create columns D, E, F, and G to the existing dataframe by performing operations row by row.
I'm trying to:
- Start with a value of 1000 for D0.
- Calculate E0 by D0 * C0.
- Calculate F0 by E0 / A0.
- Calculate G0 by (B0 * F0)-(A0 * F0)
- Calculate D1 by D0 G0
- Repeat for each row. For example, E1 = (D1 * C1). F1 = E1 / A1 and so on.
Date | A | B | C | D | E | F | G |
---|---|---|---|---|---|---|---|
0 | 100 | 200 | 0.05 | 1000 | 1000*0.05 | (1000*0.05)/100 | (B0 * F0)-(A0 * F0) |
1 | 200 | 150 | 0.01 | 1000 (B0 * F0)-(A0 * F0) | |||
2 | 150 | 160 | 0.03 |
There are 500 rows in total and doing all these manually would obviously be impossible, which made me think I need to use for loop some how but I'm not sure how. How would I go about this?
df.at[0, 'D'] = 1000
for i in range(1, 500):
df.at[i, 'D'] = df.at[i-1, 'D'] df.at[i-1, 'G']
df.at[i, 'E'] = df.at[i, 'D'] * df.at[i, 'C']
df.at[i, 'F'] = df.at[i, 'E'] / df.at[i, 'A']
df.at[i, 'G'] = (df.at[i, 'B'] - df.at[i, 'A']) * df.at[i, 'F']
CodePudding user response:
To compute an iterative operation on 500 rows, using an explicit for loop is the easiest solution.:
for i in range(1, n):
df.at[i, "D"] = df.at[i-1, "D"] df.at[i-1, "G"]
df.at[i, "E"] = df.at[i, "D"]*df.at[i, "C"]
df.at[i, "F"] = df.at[i, "E"]/df.at[i, "A"]
df.at[i, "G"] = (df.at[i, "B"]-df.at[i, "A"])*df.at[i, "F"]