pythonpandasfinancestockquotes

How to fiind the earliest value in OHLC data if condition satisfies and fetch it's index?


I am dealing with OHLC data of a stock in pandas. I have set 'date' column as index. Other columns are - open, high, low and close.

low = df['2021']['low'][0] shall give first value in low column/first low of year 2021

How do I find the earliest value in 'high' column which is 1000 pts away & falls after the date of low. For example, if such first low is 12000, How do I fetch first high that's bigger than 13000(12000+1000) ?

Also, how to fetch index of such high when found?

I'm a beginner in python/pandas. Any suggestions would be welcome. Thank you.


Solution

  • The first low in 2021

    first_low = df.loc["2021","Low"][0]
    

    The first occurrence High is higher than +1000:

    df.loc["2021":].query(f"High>{first_low+1000}").head(1).index
    

    The "2021": has the : incase you would have to look even further in time for such a condition, irrelevant for 2021, but it could still be helpful.