0

How to calculate the slope parameter of the 10th closing price on a rolling basis and add it to the last column of the table (after the close, similar to the moving average price)?

enter image description here

jinwandalaohu
  • 226
  • 1
  • 7

1 Answers1

0

Based on Python Dataframe Find n rows rolling slope without for loop

Fake data:

df = pd.DataFrame([[38611.38, '2022-03-08 22:23:00.000000'],
       [38604.02, '2022-03-08 22:24:00.000000'],
       [38611.76, '2022-03-08 22:25:00.000000'],
       [38609.75, '2022-03-08 22:26:00.000000'],
       [38601.35, '2022-03-08 22:27:00.000000']], columns = ['Close', 'Open time'])

df
        Close   Open time
    0   38611.38    2022-03-08 22:23:00.000000
    1   38604.02    2022-03-08 22:24:00.000000
    2   38611.76    2022-03-08 22:25:00.000000
    3   38609.75    2022-03-08 22:26:00.000000
    4   38601.35    2022-03-08 22:27:00.000000


    df.reset_index(drop=True)
    window = 10
    df['Close'].rolling(window).apply(lambda x: stats.linregress(x, x.index+1)[0], raw=False)

Result:

0           NaN
1           NaN
2           NaN
3           NaN
4           NaN
         ...   
995    0.017796
996    0.038905
997    0.052710
998    0.047330
999    0.043615
Name: Close, Length: 1000, dtype: float64
Nand0san
  • 473
  • 4
  • 13