Home > Net >  python comparing values of previous rows with current row
python comparing values of previous rows with current row

Time:04-01

I have a data frame like this:

|     | Date                |    Open |    High |     Low |   Close_DAILY |   SMA200 |      4H |      1H | BUY   |
|----- --------------------- --------- --------- --------- --------------- ---------- --------- --------- -------|
|   0 | 2021-02-26 00:00:00 | 234.761 | 239.4   | 209.105 |       221.717 |  nan     | 247.751 | 253.4   | nan   |
|   1 | 2021-02-27 00:00:00 | 221.959 | 237.77  | 219.011 |       225.385 |  nan     | 247.886 | 251.655 | nan   |
|   2 | 2021-02-28 00:00:00 | 225.457 | 229.4   | 195     |       210.214 |  nan     | 254.707 | 244.716 | nan   |
|   3 | 2021-03-01 00:00:00 | 210.118 | 260     | 209.12  |       254.964 |  nan     | 246.808 | 250.706 | nan   |
|   4 | 2021-03-02 00:00:00 | 254.949 | 264.9   | 227     |       239.684 |  nan     | 234.915 | 248.799 | nan   |
|   5 | 2021-03-03 00:00:00 | 239.72  | 254.848 | 236.002 |       240.57  |  nan     | 228.379 | 247.751 | nan   |
|   6 | 2021-03-04 00:00:00 | 240.48  | 249.988 | 225.531 |       229.637 |  nan     | 215.144 | 241.73  | nan   |
....
| 214 | 2021-09-28 00:00:00 | 335.5   | 344.6   | 330     |       333     |  395.051 | 334.799 | 226.795 | nan   |
| 215 | 2021-09-29 00:00:00 | 332.9   | 375.4   | 331.2   |       367.7   |  395.508 | 343.926 | 225.282 | nan   |
| 216 | 2021-09-30 00:00:00 | 367.7   | 388.8   | 366.4   |       387.5   |  396.13  | 351.528 | 225.025 | nan   |
| 217 | 2021-10-01 00:00:00 | 387.5   | 423.4   | 381.5   |       421.5   |  396.965 | 346.839 | 223.853 | nan   |
| 218 | 2021-10-02 00:00:00 | 421.4   | 438.2   | 410.6   |       427.1   |  397.807 | 346.885 | 227.023 | nan   |
| 219 | 2021-10-03 00:00:00 | 427     | 437.2   | 421.5   |       430.5   |  398.611 | 337.361 | 228.571 | nan   |
| 220 | 2021-10-04 00:00:00 | 430.5   | 430.9   | 410.9   |       426.3   |  399.435 | 338.293 | 228.754 | nan   |
| 221 | 2021-10-05 00:00:00 | 426.4   | 444     | 424     |       442.1   |  400.331 | 339.057 | 229.552 | nan   |
| 222 | 2021-10-06 00:00:00 | 442     | 442.8   | 414.7   |       434.9   |  401.184 | 342.6   | 225.848 | nan   |
| 223 | 2021-10-07 00:00:00 | 434.9   | 450     | 423.6   |       438.5   |  402.053 | 346     | 224.936 | YESSS |

in the BUY column i want to get a yesss if the close of that row is lower than the Low of 7 days before and if the Close_DAILY is above the SMA200. I am using the code below but as you can see in the last row of the data frame it is returning a YESSS although the low of 7 days before is higher than that days close.

 daily.loc[(daily['Low'].shift(-7)>daily['Close_DAILY']) & (daily['Close_DAILY']>daily['SMA200']), 'BUY']='YESSS'

Basically, what I want to do is that to get a YESSS in the BUY column if the Close_DAILY is lower than the low of 7 days before.

CodePudding user response:

To get the Low value of 7 days before you should use daily['Low'].shift(7).
Please check out pandas documentation for more details: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.shift.html

  • Related