How to calculate the session change of daily bars

I have a DF that looks like:

date volume open close high low previous close
2022-05-02 1756159.0 118.38 119.57 120.34 116.49
2022-05-03 3217838.0 119.72 122.4 123.98 119.09 119.57
2022-05-04 2460350.0 121.69 126.3 126.69 121.44 122.4
2022-05-05 2123645.0 124.62 122.15 125.21 120.8 126.3
2022-05-06 1629034.0 120.88 121.08 121.88 118.0 122.15
2022-05-09 1861704.0 119.13 113.11 119.13 112.64 121.08
2022-05-10 2141753.0 115.44 116.64 117.94 113.14 113.11
2022-05-11 1607013.0 115.7 113.99 118.0 113.84 116.64
2022-05-12 1338023.0 113.61 116.13 116.25 112.78 113.99
2022-05-13 1328411.0 117.38 119.38 120.715 117.27 116.13

I am trying to create a column which finds the difference between the previous close and the open:

def get_change(current, previous):
    print(current, 'current')
    print(previous, 'previous')
    if current == previous:
        return 0
    try:
        return (abs(current - previous) / previous) * 100.0
    except ZeroDivisionError:
        return float('inf')

final_df['change'] = get_change(df['o'], final_df['previous close'])

Like so (where 2 is the difference between previous close and open):

date volume open close high low previous close change
2022-05-02 1756159.0 118.38 119.57 120.34 116.49 2
2022-05-03 3217838.0 119.72 122.4 123.98 119.09 119.57 2
2022-05-04 2460350.0 121.69 126.3 126.69 121.44 122.4 2
2022-05-05 2123645.0 124.62 122.15 125.21 120.8 126.3 2
2022-05-06 1629034.0 120.88 121.08 121.88 118.0 122.15 2
2022-05-09 1861704.0 119.13 113.11 119.13 112.64 121.08 2
2022-05-10 2141753.0 115.44 116.64 117.94 113.14 113.11 2
2022-05-11 1607013.0 115.7 113.99 118.0 113.84 116.64 2
2022-05-12 1338023.0 113.61 116.13 116.25 112.78 113.99 2
2022-05-13 1328411.0 117.38 119.38 120.715 117.27 116.13 2

How do I get the change value?

>Solution :

IUUC, you can do

df['change'] = df['open'].sub(df['previous close']).abs().div(df['previous close']).mul(100.0)
print(df)

         date     volume    open   close     high     low  previous close    change
0  2022-05-02  1756159.0  118.38  119.57  120.340  116.49             NaN       NaN
1  2022-05-03  3217838.0  119.72  122.40  123.980  119.09          119.57  0.125450
2  2022-05-04  2460350.0  121.69  126.30  126.690  121.44          122.40  0.580065
3  2022-05-05  2123645.0  124.62  122.15  125.210  120.80          126.30  1.330166
4  2022-05-06  1629034.0  120.88  121.08  121.880  118.00          122.15  1.039705
5  2022-05-09  1861704.0  119.13  113.11  119.130  112.64          121.08  1.610505
6  2022-05-10  2141753.0  115.44  116.64  117.940  113.14          113.11  2.059942
7  2022-05-11  1607013.0  115.70  113.99  118.000  113.84          116.64  0.805898
8  2022-05-12  1338023.0  113.61  116.13  116.250  112.78          113.99  0.333363
9  2022-05-13  1328411.0  117.38  119.38  120.715  117.27          116.13  1.076380

Leave a Reply