Search code examples
pythonpandasfor-loopnested-if

Why does my if condition in my for loop stop validation after one iteration is true?


I am trying to backtest a trading algorithm with historical data. I want to iterate through a pandas dataframe, checking for a couple of flags (alert and order_placed) and executing trades when the conditions are met. The dataframe contains date (index), open, high, low, close and RSI columns.

I have tried to avoid nesting the if statements and using 'and' to streamline the code and still end up with the same output.


entry = 0
sl = 0
target_increment = 0
alert = False
order_placed = False

for row in df.itertuples():
    # print('in next iteration')
    if not order_placed:
        # print('in orderplaced not true')
        if alert:
            # print('\tin alert is true\n')
            if row.Close>entry:
                # print('\t\tin entry condition')
                print("Bought on date " + str(row.Index) + ' at ' + str(entry))
                print("SL: " + str(sl) + "\nTarget: " + str(target_increment+row.Close))
                order_placed = True

            else:
                # print('\t\tin entry condition is not met')
                alert = False

        else:
            # print('\tin alert is not true')
            if row.RSI>60:
                print('\t\tin alert condition check')
                entry = row.High+1
                sl = row.Low-1
                target_increment = row.Close-row.Open
                alert = True
                print("Alert at date " + str(row.Index))


    else:

        if row.Close<=sl:
            print("Sold on date " + str(row.Index) + ' at ' + str(row.Close) + "\nStop loss hit!")
            alert = False
            order_placed = False
        elif row.Close>=target_increment+row.Close:
            print("Sold on date " + str(row.Index) + ' at ' + str(row.Close) + "\nTarget reached!")
            alert = False
            order_placed = False

print("alert: ", alert)
print("order_placed: ", order_placed)

I expect the program to say "bought" when alert is true and order_placed is false. This part is working for the first tuple in the dataframe that matches these conditions. After order_placed is true however, the next iteration of the loop does not enter the 'else:' portion of the parent if statement, ie, no further logic is implemented for the rest of the rows.

Output:

building dictionary
                in alert condition check
Alert at date 2019-09-20
Bought on date 2019-09-23 at 29539.3
SL: 26850.35
Target: 32766.85
alert:  True
order_placed:  True

Dataframe for reference:

               Symbol      Expiry      Open      High       Low     Close      Last  Settle Price Number of Contracts      Turnover Open Interest Change in OI  Underlying        RSI  Previous High  Previous Low  Previous Open  Previous Close
Date
2019-07-26  BANKNIFTY  2019-10-31  29456.30  29698.00  29397.00  29677.50  29661.50      29677.50                 135  7.968000e+07          1980         1980    29325.30        NaN            NaN           NaN            NaN             NaN
2019-07-29  BANKNIFTY  2019-10-31  29714.75  29752.80  29438.85  29577.65  29582.05      29577.65                  85  5.030400e+07          2900          920    29295.90        NaN       29698.00      29397.00       29456.30        29677.50
2019-07-30  BANKNIFTY  2019-10-31  29621.35  29773.75  29000.05  29052.20  29050.00      29052.20                 223  1.304850e+08          3500          600    28791.60        NaN       29752.80      29438.85       29714.75        29577.65
2019-07-31  BANKNIFTY  2019-10-31  28900.00  29227.65  28836.65  29164.85  29155.00      29164.85                 186  1.079050e+08          4600         1100         NaN        NaN       29773.75      29000.05       29621.35        29052.20
2019-08-01  BANKNIFTY  2019-10-31  29000.00  29105.00  28355.55  28699.05  28770.55      28699.05                 598  3.434440e+08          9460         4860    28367.25        NaN       29227.65      28836.65       28900.00        29164.85
2019-08-02  BANKNIFTY  2019-10-31  28599.05  28866.95  28268.20  28518.35  28525.00      28518.35                 408  2.324140e+08          9660          200    28204.95        NaN       29105.00      28355.55       29000.00        28699.05
2019-08-05  BANKNIFTY  2019-10-31  28418.35  28418.35  27718.35  27973.05  27919.50      27973.05                 907  5.060620e+08         10980         1320    27648.05        NaN       28866.95      28268.20       28599.05        28518.35
2019-08-06  BANKNIFTY  2019-10-31  27873.05  28441.35  27873.05  28311.25  28292.00      28311.25                 413  2.328910e+08          9000        -1980    28022.10        NaN       28418.35      27718.35       28418.35        27973.05
2019-08-07  BANKNIFTY  2019-10-31  28282.25  28411.25  27900.00  27918.45  27900.00      27918.45                 405  2.278510e+08          9060           60    27702.05        NaN       28441.35      27873.05       27873.05        28311.25
2019-08-08  BANKNIFTY  2019-10-31  28018.45  28550.00  27818.45  28438.55  28550.00      28438.55                 364  2.051960e+08          9600          540    28110.45        NaN       28411.25      27900.00       28282.25        27918.45
2019-08-09  BANKNIFTY  2019-10-31  28538.55  28800.00  28538.55  28669.50  28692.00      28669.50                 241  1.382590e+08         10320          720    28431.90        NaN       28550.00      27818.45       28018.45        28438.55
2019-08-13  BANKNIFTY  2019-10-31  28497.30  28497.30  27878.25  27930.00  27925.00      27930.00                 329  1.855670e+08         11080          760    27729.10        NaN       28800.00      28538.55       28538.55        28669.50
2019-08-14  BANKNIFTY  2019-10-31  28015.15  28330.00  27990.00  28174.45  28152.15      28174.45                 173  9.743500e+07         11140           60    28019.20        NaN       28497.30      27878.25       28497.30        27930.00
2019-08-16  BANKNIFTY  2019-10-31  28177.90  28515.75  27948.20  28451.10  28444.95      28451.10                 333  1.880830e+08         10260         -880    28217.00        NaN       28330.00      27990.00       28015.15        28174.45
2019-08-19  BANKNIFTY  2019-10-31  28621.70  28663.40  28344.85  28400.95  28421.00      28400.95                 309  1.763910e+08         11220          960    28186.10  41.462145       28515.75      27948.20       28177.90        28451.10
2019-08-20  BANKNIFTY  2019-10-31  28312.10  28395.95  28050.00  28183.60  28224.65      28183.60                 376  2.119360e+08         12180          960    27982.45  38.474052       28663.40      28344.85       28621.70        28400.95
2019-08-21  BANKNIFTY  2019-10-31  28142.00  28280.50  27880.00  27919.85  27957.95      27919.85                 354  1.983950e+08         13060          880    27719.05  35.162444       28395.95      28050.00       28312.10        28183.60
2019-08-22  BANKNIFTY  2019-10-31  27833.15  27847.00  27163.15  27212.95  27202.00      27212.95                 547  3.008380e+08         11820        -1240    27034.20  28.165109       28280.50      27880.00       28142.00        27919.85
2019-08-23  BANKNIFTY  2019-10-31  26921.75  27419.05  26803.65  27201.70  27215.00      27201.70                 721  3.907550e+08         10760        -1060    26958.65  28.069376       27847.00      27163.15       27833.15        27212.95
2019-08-26  BANKNIFTY  2019-10-31  27554.95  28150.00  27030.00  28095.05  28100.00      28095.05                 948  5.246750e+08         10760            0    27951.35  44.268995       27419.05      26803.65       26921.75        27201.70
2019-08-27  BANKNIFTY  2019-10-31  28233.50  28390.10  28085.40  28278.60  28290.00      28278.60                 350  1.979000e+08         11480          720         NaN  46.914359       28150.00      27030.00       27554.95        28095.05
2019-08-28  BANKNIFTY  2019-10-31  28168.35  28271.00  27802.30  27987.05  27998.00      27987.05                 439  2.458660e+08         12340          860    27804.30  43.391190       28390.10      28085.40       28233.50        28278.60
2019-08-29  BANKNIFTY  2019-10-31  27900.00  27900.00  27437.05  27513.30  27566.60      27513.30                1312  7.251390e+08         15140         2800    27305.20  38.351222       28271.00      27802.30       28168.35        27987.05
2019-08-30  BANKNIFTY  2019-10-31  27221.10  27773.65  27221.10  27613.60  27665.40      27613.60                3065  1.686416e+09         28060        12920    27427.85  39.941728       27900.00      27437.05       27900.00        27513.30
2019-09-03  BANKNIFTY  2019-10-31  27160.55  27420.00  26910.00  26979.10  26921.65      26979.10                2015  1.093904e+09         34760         6700    26824.15  33.970914       27773.65      27221.10       27221.10        27613.60
2019-09-04  BANKNIFTY  2019-10-31  26968.00  27481.00  26825.00  27335.45  27288.55      27335.45                3714  2.023207e+09         54420        19660    27123.85  39.445866       27420.00      26910.00       27160.55        26979.10
2019-09-05  BANKNIFTY  2019-10-31  27252.95  27443.75  26980.00  27098.30  27090.35      27098.30                2127  1.155384e+09         54340          -80    26919.70  37.233245       27481.00      26825.00       26968.00        27335.45
2019-09-06  BANKNIFTY  2019-10-31  27194.40  27500.00  27186.20  27415.90  27400.65      27415.90                2163  1.184791e+09         55060          720    27247.90  41.931016       27443.75      26980.00       27252.95        27098.30
2019-09-09  BANKNIFTY  2019-10-31  27350.05  27714.00  27148.80  27665.05  27650.25      27665.05                4219  2.326629e+09         40720       -14340    27504.65  45.384392       27500.00      27186.20       27194.40        27415.90
2019-09-11  BANKNIFTY  2019-10-31  27760.20  27998.55  27711.50  27938.40  27901.50      27938.40                1886  1.052612e+09         48840         8120    27776.20  48.970041       27714.00      27148.80       27350.05        27665.05
2019-09-12  BANKNIFTY  2019-10-31  28028.00  28198.55  27885.60  27923.85  27900.00      27923.85                3564  1.999824e+09         60720        11880    27818.50  48.786439       27998.55      27711.50       27760.20        27938.40
2019-09-13  BANKNIFTY  2019-10-31  27869.55  28333.45  27700.00  28276.75  28273.05      28276.75                4487  2.511968e+09         71980        11260    28098.75  53.354495       28198.55      27885.60       28028.00        27923.85
2019-09-16  BANKNIFTY  2019-10-31  28015.85  28205.00  27938.70  27997.15  27996.90      27997.15                5198  2.916725e+09         73080         1100    27855.00  49.581096       28333.45      27700.00       27869.55        28276.75
2019-09-17  BANKNIFTY  2019-10-31  27980.00  27986.25  27192.75  27242.15  27270.75      27242.15                8466  4.665398e+09        107020        33940    27131.75  41.123510       28205.00      27938.70       28015.85        27997.15
2019-09-18  BANKNIFTY  2019-10-31  27450.00  27568.85  27240.00  27285.60  27280.00      27285.60               10375  5.685789e+09        134280        27260    27172.65  41.739442       27986.25      27192.75       27980.00        27242.15
2019-09-19  BANKNIFTY  2019-10-31  27260.30  27281.20  26790.05  26880.60  26899.00      26880.60               12977  6.995522e+09        157280        23000    26757.65  37.772828       27568.85      27240.00       27450.00        27285.60
2019-09-20  BANKNIFTY  2019-10-31  26977.05  29538.30  26851.35  29100.05  29117.05      29100.05               79189  4.521008e+10        310540       153260    28981.55  60.132563       27281.20      26790.05       27260.30        26880.60
2019-09-23  BANKNIFTY  2019-10-31  29921.75  30824.90  29745.30  30643.85  30650.00      30643.85               66636  4.039896e+10        451720       141180    30566.20  68.587639       29538.30      26851.35       26977.05        29100.05
2019-09-24  BANKNIFTY  2019-10-31  30601.70  30776.90  30055.00  30373.90  30360.10      30373.90               47956  2.911238e+10        542760        91040    30183.10  65.953644       30824.90      29745.30       29921.75        30643.85
2019-09-25  BANKNIFTY  2019-10-31  30279.65  30279.65  29667.35  29784.75  29759.95      29784.75               64327  3.844630e+10        698080       155320    29586.05  60.493489       30776.90      30055.00       30601.70        30373.90
2019-09-26  BANKNIFTY  2019-10-31  29800.00  30957.95  29800.00  30225.10  30263.95      30225.10              160567  9.732284e+10        888600       190520    30002.60  62.961656       30279.65      29667.35       30279.65        29784.75
2019-09-27  BANKNIFTY  2019-10-31  30201.00  30340.00  29882.95  30080.90  30061.30      30080.90              195904  1.179994e+11        967320        78720    29876.65  61.604376       30957.95      29800.00       29800.00        30225.10
2019-09-30  BANKNIFTY  2019-10-31  29890.05  29955.60  29103.50  29324.70  29310.00      29324.70              244117  1.437219e+11       1327640       360320    29103.15  54.918367       30340.00      29882.95       30201.00        30080.90
2019-10-01  BANKNIFTY  2019-10-31  29440.00  29724.00  28218.00  28874.65  28918.15      28874.65              366120  2.126155e+11       1513360       185720    28725.50  51.346656       29955.60      29103.50       29890.05        29324.70
2019-10-03  BANKNIFTY  2019-10-31  28655.10  28919.80  28368.60  28570.85  28579.95      28570.85              247822  1.418723e+11       1586560        73200    28414.10  49.028620       29724.00      28218.00       29440.00        28874.65
2019-10-04  BANKNIFTY  2019-10-31  28801.05  28860.00  27778.00  27853.75  27819.00      27853.75              291787  1.648944e+11       1645520        58960    27731.85  43.981386       28919.80      28368.60       28655.10        28570.85
2019-10-07  BANKNIFTY  2019-10-31  27950.00  28277.00  27652.20  27871.55  27870.00      27871.55              214766  1.203944e+11       1441060      -204460    27767.55  44.135119       28860.00      27778.00       28801.05        27853.75
2019-10-09  BANKNIFTY  2019-10-31  27870.00  28989.95  27774.00  28874.05  28894.60      28874.05              295138  1.675061e+11       1351920       -89140    28785.85  52.106930       28277.00      27652.20       27950.00        27871.55
2019-10-10  BANKNIFTY  2019-10-31  28690.00  28690.00  27980.00  28106.60  28170.15      28106.60              246371  1.392902e+11       1509280       157360    28013.45  46.622132       28989.95      27774.00       27870.00        28874.05
2019-10-11  BANKNIFTY  2019-10-31  28300.00  28723.00  27805.00  28138.60  28135.00      28138.60              301738  1.703704e+11       1527360        18080    28042.50  46.873242       28690.00      27980.00       28690.00        28106.60
2019-10-14  BANKNIFTY  2019-10-31  28317.95  28790.00  28082.85  28209.80  28210.00      28209.80              233980  1.327742e+11       1468680       -58680    28181.95  47.465433       28723.00      27805.00       28300.00        28138.60
2019-10-15  BANKNIFTY  2019-10-31  28300.00  28775.00  28251.00  28618.65  28621.00      28618.65              213025  1.214465e+11       1457480       -11200    28555.10  50.853191       28790.00      28082.85       28317.95        28209.80
2019-10-16  BANKNIFTY  2019-10-31  28720.25  28807.00  28321.15  28604.75  28615.65      28604.75              221098  1.264651e+11       1386680       -70800    28538.80  50.733407       28775.00      28251.00       28300.00        28618.65
2019-10-17  BANKNIFTY  2019-10-31  28530.50  29150.00  28530.50  29079.65  29070.35      29079.65              231824  1.337761e+11       1424740        38060    28989.45  54.662632       28807.00      28321.15       28720.25        28604.75
2019-10-18  BANKNIFTY  2019-10-31  29029.60  29299.95  28903.05  29152.35  29140.00      29152.35              192396  1.120157e+11       1295200      -129540    29120.25  55.251008       29150.00      28530.50       28530.50        29079.65
2019-10-22  BANKNIFTY  2019-10-31  29397.00  29747.00  28851.40  29492.50  29490.00      29492.50              218602  1.289482e+11       1253820       -41380    29411.15  57.997593       29299.95      28903.05       29029.60        29152.35
2019-10-23  BANKNIFTY  2019-10-31  29400.00  29725.00  29122.00  29494.50  29526.15      29494.50              202544  1.194933e+11       1198340       -55480    29459.60  58.013911       29747.00      28851.40       29397.00        29492.50
2019-10-24  BANKNIFTY  2019-10-31  29625.00  29742.55  29020.95  29180.50  29215.55      29180.50              269195  1.580991e+11       1328280       129940    29107.95  54.438115       29725.00      29122.00       29400.00        29494.50
2019-10-25  BANKNIFTY  2019-10-31  29280.10  29480.00  28912.60  29431.85  29438.00      29431.85              227255  1.327633e+11       1000880      -327400    29395.95  56.736869       29742.55      29020.95       29625.00        29180.50
2019-10-27  BANKNIFTY  2019-10-31  29638.00  29760.00  29460.60  29497.30  29473.00      29497.30               21511  1.270312e+10        997600        -3280    29516.30  57.340432       29480.00      28912.60       29280.10        29431.85

Solution

  • Both the if and elif (in the else block of the root if-else statement) are always failing their conditions. There is no way for 'order_placed' to become False or 'alert' to become True. I'm not quite sure what this it meant to be doing myself but you may want to check the logic of your code. It appears that 'sl' and 'target_increment' are never changing.