I am working with some stock data in pandas and would like to count the number of times (or cycles) where the price is within a certain entry and exit price point.
For example, I have set:
entry price = 136.3
exit price = 136.6
My entry price is $136.3 and my exit price is $136.6. I want to begin a cycle every time the value approaches $136.3 from below $136.3, and close that cycle when the price approaches $136.6 from below $136.6.
For example, using the dataframe in the screenshot below, we will have:
- a cycle begins at Timestamp 1656682503 ($136.04) and ends at Timestamp 1656682802 ($137.15)
- a cycle begins at Timestamp 1656682803 ($136.24) and ends at Timestamp 1656682804 ($136.65)
So, the cycle only begins if it crosses the entry price, and only ends if it crosses the exit price. The fluctuations in between are ignored since they never cross the entry and exit price points.
Basically, I want to be able to build a count to count the number of cycles. In this case, the count = 2.
I have thought of using .sum(), .cumsum() or .groupby(), but am honestly lost in the sauce. Any help would be appreciated.
