Search code examples
pythonpandasdataframepandas-groupbymaxima

Finding Pivot Points for stock price, after grouping by symbol. Pivot Point is high for 10 values before and after point


     Date   Symbol  Close   Volume
1259    2021-10-29  AA  45.950  6350815.000
1260    2021-10-28  AA  46.450  10265029.000
1261    2021-10-27  AA  45.790  12864700.000
1262    2021-10-26  AA  49.442  6153100.000
1263    2021-10-25  AA  51.058  11070100.000
1264    2021-10-22  AA  49.143  7453300.000
1265    2021-10-21  AA  49.881  9066900.000
1266    2021-10-20  AA  52.396  7330400.000
1267    2021-10-19  AA  53.563  10860800.000
1268    2021-10-18  AA  57.115  9883800.000

Looking for result similar too...

    Date    Symbol  Close   Volume  High Points Pivot Point
1379    2021-05-11  AA  41.230  9042100.000 41.230  True
1568    2020-08-10  AA  15.536  8087800.000 15.536  True
1760    2019-11-04  AA  22.860  3741000.000 22.860  True
1934    2019-02-27  AA  30.912  2880100.000 30.912  True
2149    2018-04-19  AA  60.099  11779200.00 60.099  True
2213    2018-01-17  AA  56.866  8189700.000 56.866  True
2445    2017-02-14  AA  38.476  3818600.000 38.476  True
5406    2021-06-02  AAL 25.820  58094598.00 25.820  True
5461    2021-03-15  AAL 25.170  93746800.00 25.170  True
5654    2020-06-08  AAL 20.310  175418900.0 20.310  True
5734    2020-02-12  AAL 30.470  9315400.000 30.470  True
5807    2019-10-28  AAL 31.144  10298500.00 31.144  True
5874    2019-07-24  AAL 34.231  7315300.000 34.231  True
6083    2018-09-21  AAL 42.788  10743100.00 42.788  True
6257    2018-01-12  AAL 56.989  7505800.000 56.989  True
6322    2017-10-10  AAL 51.574  9387100.000 51.574  True
6383    2017-07-14  AAL 52.624  4537900.000 52.624  True
   

I'm newer to programming and have been struggling on this one. I'm trying to find points that are a local max which must be higher than the 10 closes before and after. The data frame has about 320 and stocks on it and needs to be grouped by symbol. I have tried a few different approaches to solving this but haven't been able to find something that will work. Any insight would be greatly appreciated.

#read in data, vol_list is an exsting screen to reduce number of stock that didn't meet volume critria

df_prices = pd.read_csv('/Users/kylemerrick/Desktop/Stock Screener/price_data.csv')
include_pivot_points = df_prices[df_prices['Symbol'].isin(vol_list)]
n=10
pivot_points = include_pivot_points.groupby('Symbol')['Close'].apply(lambda x : iloc[argrelextrema(x.values, np.greater_equal, axis=1, order=n)

I have also tried writing my own function to do this but can't figure out how to compare the current to the 10 values before and after

include_pivot_points.groupby('Symbol').iloc['Close'] + 10:['Close'] -10]

Solution

  • I was able to solve with the following code eventually and wanted to share as I didn't receive a reply. Many other solutions existed for pivot points or (support/resistance points) appending each price point to a list or just for one symbol. I had wanted to keep data frame with multiple symbols.

    First used apply, 21 then shift so that there are even number on each side

    include_pivot_points['High Points'] = include_pivot_points.groupby('Symbol').rolling(21)['Close'].max().shift(-11).reset_index(level = 'Symbol', drop = True)
    

    If the high point equaled the current close I then knew this was the pivot point and added column true or false for a pivot point

    include_pivot_points['Pivot Point'] = include_pivot_points['High Points'] == include_pivot_points['Close']
    

    Then removed false values to get all past pivot points for all stocks

    pivot_points = include_pivot_points[include_pivot_points['Pivot Point'] == True]