I am making a Dollar Cost Average code where I want to choose between 2 equations. I made an excel spreadsheet that I'm trying to portover to python. I've gotten pretty far except for the last step. The last step has had me searching for a solution for 3 weeks now. The errors happen when I try a for loop in a df when looping through. I would like to check a column with an if the statement. If is true then do an equation if false do another equation. I can get the for loop to work and I can the if statements to work, but not combined. See all commented out code for whats been tried. I have tried np.where instead of the if statements as well. I have tried .loc. I have tried lamda. I have tried list comp. Nothing is working please help. FYI the code referring is ['trend bal'] column. ***see end with correct code.
What the df looks like:
    Index   timestamp         Open         High          Low  ...      rate  account bal  invested ST_10_1.0  if trend
0       0   8/16/2021  4382.439941  4444.350098  4367.729980  ...  1.000000  $10,000.00      10000         1         0
1       1   8/23/2021  4450.290039  4513.330078  4450.290039  ...  0.015242  $10,252.42      10100         1         0
2       2   8/30/2021  4513.759766  4545.850098  4513.759766  ...  0.005779  $10,411.67      10200         1         0
3       3    9/6/2021  4535.379883  4535.379883  4457.660156  ... -0.016944  $10,335.25      10300         1         0
4       4   9/13/2021  4474.810059  4492.990234  4427.759766  ... -0.005739  $10,375.93      10400         1         0
5       5   9/20/2021  4402.950195  4465.399902  4305.910156  ...  0.005073  $10,528.57      10500         1         0
6       6   9/27/2021  4442.120117  4457.299805  4288.520020  ... -0.022094  $10,395.95      10600         1         0
7       7   10/4/2021  4348.839844  4429.970215  4278.939941  ...  0.007872  $10,577.79      10700         1         0
8       8  10/11/2021  4385.439941  4475.819824  4329.919922  ...  0.018225  $10,870.57      10800         1         0
9       9  10/18/2021  4463.720215  4559.669922  4447.470215  ...  0.016445  $11,149.33      10900         1         0
10     10  10/25/2021  4553.689941  4608.080078  4537.359863  ...  0.013307  $11,397.70      11000         1         0
11     11   11/1/2021  4610.620117  4718.500000  4595.060059  ...  0.020009  $11,725.75      11100         1         0
12     12   11/8/2021  4701.479980  4714.919922  4630.859863  ... -0.003125  $11,789.11      11200         1         0
13     13  11/15/2021  4689.299805  4717.750000  4672.779785  ...  0.003227  $11,927.15      11300         1         0
14     14  11/22/2021  4712.000000  4743.830078  4585.430176  ... -0.021997  $11,764.79      11400         1         0
15     15  11/29/2021  4628.750000  4672.950195  4495.120117  ... -0.012230  $11,720.92      11500        -1       100
16     16   12/6/2021  4548.370117  4713.569824  4540.509766  ...  0.038249  $12,269.23      11600        -1       100
17     17  12/13/2021  4710.299805  4731.990234  4600.220215  ... -0.019393  $12,131.29      11700         1         0
18     18  12/20/2021  4587.899902  4740.740234  4531.100098  ...  0.022757  $12,507.36      11800         1         0
19     19  12/27/2021  4733.990234  4808.930176  4733.990234  ...  0.008547  $12,714.25      11900         1         0
20     20    1/3/2022  4778.140137  4818.620117  4662.740234  ... -0.018705  $12,576.44      12000         1         0
21     21   1/10/2022  4655.339844  4748.830078  4582.240234  ... -0.003032  $12,638.31      12100         1         0
22     22   1/17/2022  4632.240234  4632.240234  4395.339844  ... -0.056813  $12,020.29      12200         1         0
23     23   1/24/2022  4356.319824  4453.229980  4222.620117  ...  0.007710  $12,212.97      12300        -1       100
24     24   1/31/2022  4431.790039  4595.310059  4414.020020  ...  0.015497  $12,502.23      12400        -1       100
25     25    2/7/2022  4505.750000  4590.029785  4401.410156  ... -0.018196  $12,374.75      12500         1         0
26     26   2/14/2022  4412.609863  4489.549805  4327.220215  ... -0.015790  $12,279.35      12600         1         0
27     27   2/21/2022  4332.740234  4385.339844  4114.649902  ...  0.008227  $12,480.38      12700         1         0
28     28   2/28/2022  4354.169922  4416.779785  4279.540039  ... -0.012722  $12,421.61      12800         1         0
29     29    3/7/2022  4327.009766  4327.009766  4157.870117  ... -0.028774  $12,164.19      12900        -1       100
30     30   3/14/2022  4202.750000  4465.399902  4161.720215  ...  0.061558  $13,012.99      13000        -1       100
31     31   3/21/2022  4462.399902  4546.029785  4424.299805  ...  0.017911  $13,346.07      13100         1         0
32     32   3/28/2022  4541.089844  4637.299805  4507.569824  ...  0.000616  $13,454.30      13200         1         0
33     33    4/4/2022  4547.970215  4593.450195  4450.040039  ... -0.012666  $13,383.88      13300         1         0
34     34   4/11/2022  4462.640137  4471.000000  4381.339844  ... -0.021320  $13,198.53      13400         1         0
35     35   4/18/2022  4385.629883  4512.939941  4267.620117  ... -0.027503  $12,935.53      13500        -1       100
36     36   4/25/2022  4255.339844  4308.450195  4124.279785  ... -0.032738  $12,612.05      13600        -1       100
37     37    5/2/2022  4130.609863  4307.660156  4062.510010  ... -0.002079  $12,685.83      13700        -1       100
38     38    5/9/2022  4081.270020  4081.270020  3858.870117  ... -0.024119  $12,479.86      13800        -1       100
39     39   5/16/2022  4013.020020  4090.719971  3810.320068  ... -0.030451  $12,199.84      13900        -1       100
40     40   5/23/2022  3919.419922  4158.490234  3875.129883  ...  0.065844  $13,103.12      14000        -1       100
41     41   5/30/2022  4151.089844  4177.509766  4073.850098  ... -0.011952  $13,046.51      14100         1         0
42     42    6/6/2022  4134.720215  4168.779785  3900.159912  ... -0.050548  $12,487.03      14200         1         0
43     43   6/13/2022  3838.149902  3838.149902  3636.870117  ... -0.057941  $11,863.52      14300        -1       100
44     44   6/20/2022  3715.310059  3913.649902  3715.310059  ...  0.064465  $12,728.31      14400        -1       100
45     45   6/27/2022  3920.760010  3945.860107  3738.669922  ... -0.022090  $12,547.14      14500        -1       100
46     46    7/4/2022  3792.610107  3918.500000  3742.060059  ...  0.019358  $12,890.03      14600        -1       100
47     47   7/11/2022  3880.939941  3880.939941  3721.560059  ... -0.009289  $12,870.29      14700        -1       100
48     48   7/18/2022  3883.790039  4012.439941  3818.629883  ...  0.025489  $13,298.35      14800        -1       100
49     49   7/25/2022  3965.719971  4140.149902  3910.739990  ...  0.042573  $13,964.51      14900         1         0
50     50    8/1/2022  4112.379883  4167.660156  4079.810059  ...  0.003607  $14,114.88      15000         1         0
51     51    8/8/2022  4155.930176  4280.470215  4112.089844  ...  0.032558  $14,674.44      15100         1         0
52     52   8/15/2022  4269.370117  4325.279785  4253.080078  ...  0.000839  $14,786.75      15200         1         0
53     53   8/19/2022  4266.310059  4266.310059  4218.700195  ... -0.012900  $14,696.00      15300         1         0
   
What it should look like:
    Index   timestamp         Open         High          Low  ...   account bal  invested  ST_10_1.0 if trend     trend bal
0       0   8/16/2021  4382.439941  4444.350098  4367.729980  ...   $10,000.00      10000          1        0   $10,000.00        
1       1   8/23/2021  4450.290039  4513.330078  4450.290039  ...   $10,252.42      10100          1        0   $10,252.42        
2       2   8/30/2021  4513.759766  4545.850098  4513.759766  ...   $10,411.67      10200          1        0   $10,411.67        
3       3    9/6/2021  4535.379883  4535.379883  4457.660156  ...   $10,335.25      10300          1        0   $10,335.25        
4       4   9/13/2021  4474.810059  4492.990234  4427.759766  ...   $10,375.93      10400          1        0   $10,375.93        
5       5   9/20/2021  4402.950195  4465.399902  4305.910156  ...   $10,528.57      10500          1        0   $10,528.57        
6       6   9/27/2021  4442.120117  4457.299805  4288.520020  ...   $10,395.95      10600          1        0   $10,395.95        
7       7   10/4/2021  4348.839844  4429.970215  4278.939941  ...   $10,577.79      10700          1        0   $10,577.79        
8       8  10/11/2021  4385.439941  4475.819824  4329.919922  ...   $10,870.57      10800          1        0   $10,870.57        
9       9  10/18/2021  4463.720215  4559.669922  4447.470215  ...   $11,149.33      10900          1        0   $11,149.33        
10     10  10/25/2021  4553.689941  4608.080078  4537.359863  ...   $11,397.70      11000          1        0   $11,397.70        
11     11   11/1/2021  4610.620117  4718.500000  4595.060059  ...   $11,725.75      11100          1        0   $11,725.75        
12     12   11/8/2021  4701.479980  4714.919922  4630.859863  ...   $11,789.11      11200          1        0   $11,789.11        
13     13  11/15/2021  4689.299805  4717.750000  4672.779785  ...   $11,927.15      11300          1        0   $11,927.15        
14     14  11/22/2021  4712.000000  4743.830078  4585.430176  ...   $11,764.79      11400          1        0   $11,764.79        
15     15  11/29/2021  4628.750000  4672.950195  4495.120117  ...   $11,720.92      11500         -1      100   $11,720.92        
16     16   12/6/2021  4548.370117  4713.569824  4540.509766  ...   $12,269.23      11600         -1      100   $11,820.92        
17     17  12/13/2021  4710.299805  4731.990234  4600.220215  ...   $12,131.29      11700          1        0   $11,920.92        
18     18  12/20/2021  4587.899902  4740.740234  4531.100098  ...   $12,507.36      11800          1        0   $12,292.19        
19     19  12/27/2021  4733.990234  4808.930176  4733.990234  ...   $12,714.25      11900          1        0   $12,497.25        
20     20    1/3/2022  4778.140137  4818.620117  4662.740234  ...   $12,576.44      12000          1        0   $12,363.49        
21     21   1/10/2022  4655.339844  4748.830078  4582.240234  ...   $12,638.31      12100          1        0   $12,426.01        
22     22   1/17/2022  4632.240234  4632.240234  4395.339844  ...   $12,020.29      12200          1        0   $11,820.05        
23     23   1/24/2022  4356.319824  4453.229980  4222.620117  ...   $12,212.97      12300         -1      100   $12,011.19        
24     24   1/31/2022  4431.790039  4595.310059  4414.020020  ...   $12,502.23      12400         -1      100   $12,111.19        
25     25    2/7/2022  4505.750000  4590.029785  4401.410156  ...   $12,374.75      12500          1        0   $12,211.19        
26     26   2/14/2022  4412.609863  4489.549805  4327.220215  ...   $12,279.35      12600          1        0   $12,118.38        
27     27   2/21/2022  4332.740234  4385.339844  4114.649902  ...   $12,480.38      12700          1        0   $12,318.08        
28     28   2/28/2022  4354.169922  4416.779785  4279.540039  ...   $12,421.61      12800          1        0   $12,261.37        
29     29    3/7/2022  4327.009766  4327.009766  4157.870117  ...   $12,164.19      12900         -1      100   $12,008.56        
30     30   3/14/2022  4202.750000  4465.399902  4161.720215  ...   $13,012.99      13000         -1      100   $12,108.56        
31     31   3/21/2022  4462.399902  4546.029785  4424.299805  ...   $13,346.07      13100          1        0   $12,208.56        
32     32   3/28/2022  4541.089844  4637.299805  4507.569824  ...   $13,454.30      13200          1        0   $12,316.09        
33     33    4/4/2022  4547.970215  4593.450195  4450.040039  ...   $13,383.88      13300          1        0   $12,260.08        
34     34   4/11/2022  4462.640137  4471.000000  4381.339844  ...   $13,198.53      13400          1        0   $12,098.70        
35     35   4/18/2022  4385.629883  4512.939941  4267.620117  ...   $12,935.53      13500         -1      100   $11,865.95        
36     36   4/25/2022  4255.339844  4308.450195  4124.279785  ...   $12,612.05      13600         -1      100   $11,965.95        
37     37    5/2/2022  4130.609863  4307.660156  4062.510010  ...   $12,685.83      13700         -1      100   $12,065.95        
38     38    5/9/2022  4081.270020  4081.270020  3858.870117  ...   $12,479.86      13800         -1      100   $12,165.95        
39     39   5/16/2022  4013.020020  4090.719971  3810.320068  ...   $12,199.84      13900         -1      100   $12,265.95        
40     40   5/23/2022  3919.419922  4158.490234  3875.129883  ...   $13,103.12      14000         -1      100   $12,365.95        
41     41   5/30/2022  4151.089844  4177.509766  4073.850098  ...   $13,046.51      14100          1        0   $12,465.95        
42     42    6/6/2022  4134.720215  4168.779785  3900.159912  ...   $12,487.03      14200          1        0   $11,935.81        
43     43   6/13/2022  3838.149902  3838.149902  3636.870117  ...   $11,863.52      14300         -1      100   $11,344.24        
44     44   6/20/2022  3715.310059  3913.649902  3715.310059  ...   $12,728.31      14400         -1      100   $11,444.24        
45     45   6/27/2022  3920.760010  3945.860107  3738.669922  ...   $12,547.14      14500         -1      100   $11,544.24        
46     46    7/4/2022  3792.610107  3918.500000  3742.060059  ...   $12,890.03      14600         -1      100   $11,644.24        
47     47   7/11/2022  3880.939941  3880.939941  3721.560059  ...   $12,870.29      14700         -1      100   $11,744.24        
48     48   7/18/2022  3883.790039  4012.439941  3818.629883  ...   $13,298.35      14800         -1      100   $11,844.24        
49     49   7/25/2022  3965.719971  4140.149902  3910.739990  ...   $13,964.51      14900          1        0   $11,944.24        
50     50    8/1/2022  4112.379883  4167.660156  4079.810059  ...   $14,114.88      15000          1        0   $12,087.33        
51     51    8/8/2022  4155.930176  4280.470215  4112.089844  ...   $14,674.44      15100          1        0   $12,580.87        
52     52   8/15/2022  4269.370117  4325.279785  4253.080078  ...   $14,786.75      15200          1        0   $12,691.42        
53     53   8/19/2022  4266.310059  4266.310059  4218.700195  ...   $14,696.00      15300          1        0   $12,627.70        
Python Code:
from ctypes.wintypes import VARIANT_BOOL
from xml.dom.expatbuilder import FilterVisibilityController
import ccxt
from matplotlib import pyplot as plt
import config
import schedule
import pandas as pd
import pandas_ta as ta
pd.set_option('display.max_rows', None)
#pd.set_option('display.max_columns', None)
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from datetime import datetime
import time
import yfinance as yf
ticker = yf.Ticker('^GSPC')
df = ticker.history(period="1y", interval="1wk")
df.reset_index(inplace=True)
df.rename(columns = {'Date':'timestamp'}, inplace = True)
#df.drop(columns ={'Open', 'High', 'Low', 'Volume'}, inplace=True, axis=1)
df.drop(columns ={'Dividends', 'Stock Splits'}, inplace=True, axis=1)
# df['Close'].ffill(axis = 0, inplace = True)
invest = 10000
weekly = 100
fee = .15/100
fees = 1-fee 
df.loc[df.index == 0, 'rate'] = 1
df.loc[df.index > 0, 'rate'] = (df['Close'] / df['Close'].shift(1))-1
df.loc[df.index == 0, 'account bal'] = invest
for i in range(1, len(df)):
    df.loc[i, 'account bal'] = (df.loc[i-1, 'account bal'] * (1 + df.loc[i, 'rate'])) + weekly
df['invested'] = (df.index*weekly)+invest
    
#Supertrend
ATR = 10
Mult = 1.0
ST = ta.supertrend(df['High'], df['Low'], df['Close'], ATR, Mult)
df[f'ST_{ATR}_{Mult}'] = ST[f'SUPERTd_{ATR}_{Mult}']
df[f'ST_{ATR}_{Mult}'] = df[f'ST_{ATR}_{Mult}'].shift(1).fillna(1)
df.loc[df[f'ST_{ATR}_{Mult}'] == 1, 'if trend'] = 0
df.loc[df[f'ST_{ATR}_{Mult}'] == -1, 'if trend'] = weekly
# df.loc[df.index == 0, 'trend bal'] = invest
# for i in range(1, len(df)):
#     np.where(df.loc[df[f'ST_{ATR}_{Mult}'] == 1, 'trend bal'], (df.loc[i-1, 'trend bal'] * (1 + df.loc[i, 'rate'])) + weekly, df.loc[i-i, 'trend bal'] + df['if trend'])
# df.loc[df.index == 0, 'trend bal'] = invest
# for i in range(1, len(df)):
#     if df[f'ST_{ATR}_{Mult}'] == 1:
#         df.loc[i, 'trend bal'] = (df.loc[i-1, 'trend bal'] * (1 + df.loc[i, 'rate'])) + weekly
#     else:
#         df.loc[i, 'trend bal'] = df.loc[i-i, 'trend bal'] + df['if trend']
# for i in range(1, len(df)):
#     df.loc[df[f'ST_{ATR}_{Mult}'].shift(1) == 1, 'trend bal'] = (df.loc[i-1, 'trend bal'] * (1 + df.loc[i, 'rate'])) + weekly
#     df.loc[df[f'ST_{ATR}_{Mult}'].shift(1) == -1, 'trend bal'] = df.loc[i-i, 'trend bal'] + df['if trend'] 
#df.to_csv('GSPC.csv',index=False,mode='a')
# plt.plot(df['timestamp'], df['account bal'])
# plt.plot(df['timestamp'], df['invested'])
# plt.plot(df['timestamp'], df['close'])
# plt.show()
print(df)
What some errors looks like:
    np.where(df.loc[df[f'ST_{ATR}_{Mult}'] == 1, 'trend bal'], (df.loc[i-1, 'trend bal'] * (1 + df.loc[i, 'rate'])) + weekly, df.loc[i-i, 'trend bal'] + df['if trend'])
  File "<__array_function__ internals>", line 180, in where
ValueError: operands could not be broadcast together with shapes (36,) () (54,)
Another error:
line 1535, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
No error but not the correct amounts:
df['trend bal'] = 0
for i in range(1, len(df)):
    df.loc[df[f'ST_{ATR}_{Mult}'].shift(1) == 1, 'trend bal'] = (df.loc[i-1, 'trend bal'] * (1 + df.loc[i, 'rate'])) + weekly
    df.loc[df[f'ST_{ATR}_{Mult}'].shift(1) == -1, 'trend bal'] = df.loc[i-i, 'trend bal'] + df['if trend'] 
See photo of screenshot of excel formula: excel spreadsheet
*** Made correct calculations thanks to Ingwersen_erik:
from re import X
import pandas as pd
import pandas_ta as ta
import numpy as np
pd.set_option('display.max_rows', None)
df = pd.read_csv('etcusd.csv')
invest = 10000
weekly = 100
fee = .15/100
fees = 1-fee 
df.loc[df.index == 0, 'rate'] = 1
df.loc[df.index > 0, 'rate'] = (df['Close'] / df['Close'].shift(1))-1
df.loc[df.index == 0, 'account bal'] = invest
for i in range(1, len(df)):
    df.loc[i, 'account bal'] = (df.loc[i-1, 'account bal'] * (1 + df.loc[i, 'rate'])) + weekly
df['invested'] = (df.index*weekly)+invest
MDD = ((df['account bal']-df['account bal'].max()) / df['account bal'].max()).min()
#Supertrend
ATR = 10
Mult = 1.0
ST = ta.supertrend(df['High'], df['Low'], df['Close'], ATR, Mult)
df[f'ST_{ATR}_{Mult}'] = ST[f'SUPERTd_{ATR}_{Mult}']
df[f'ST_{ATR}_{Mult}'] = df[f'ST_{ATR}_{Mult}'].shift(1).fillna(1)
df.loc[df.index == 0, "trend bal"] = invest
for index, row in df.iloc[1:].iterrows():
    row['trend bal'] = np.where(
        df.loc[index - 1, f'ST_{ATR}_{Mult}'] == 1,
        (df.loc[index - 1, 'trend bal'] * (1 + row['rate'])) + weekly,
        df.loc[index - 1, 'trend bal'] + weekly,
    )
    df.loc[df.index == index, 'trend bal'] = row['trend bal']
print(df)
    
dflooks like, and what you want the final df to look like.