I have a price dataframe: Date, Open, Close, Week, Trading
I would like to express in python, for each row in the dataframe, how to calculate the max price meeting the following conditions:
- Trading is true
- Date is future (more than current row date)
- Week is the same
- MaxH is maximum open or if last trading day of the week, MaxH=Close
I know a for-loop will probably get the answer, but I am looking for a more pythonic expression.
For example,
Row, Date, Open, Close, Week, Trading,MaxHDate MaxH
0 2017.6.12 120 1 W1 True 2017.6.14 500
1 2017.6.13 100 2 W1 True 2017.6.14 500
2 2017.6.14 500 3 W1 True 2017.6.14 500
3 2017.6.15 300 350 W1 True 2017.6.15 350
4 2017.6.16 1000 4 W1 False
5 2017.7.1 2000 5 W2 True 2017.7.1 5
In row 1, it will take max(Open) in rows 2..3 since only these rows have similar Week and Trading is True and with Date later than 2017.6.13. The answer is 500 on 2017.6.14. In row 3, since it is the last row in week W1 and Trading==True, it will return max(open,close) on the same day.