Search code examples
pythonexcelpandasback-testing

Is there a way I can add rows of values to a dataframe in new columns, based on existing values in the dataframe?


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:

  1. Start with a value of 1000 for D0.
  2. Calculate E0 by D0 * C0.
  3. Calculate F0 by E0 / A0.
  4. Calculate G0 by (B0 * F0)-(A0 * F0)
  5. Calculate D1 by D0 + G0
  6. 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']

Solution

  • 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"]