OHLC Doesn't have Volume Column. How to do this Volume in OHLC?

92 Views Asked by At

OHLC Full Form -> Open , High , Low , Close.

I want to calculating this volume in OHLC because It doesn't have volume column.

I have problem to calculated this Volume in OHLC.

Using Jupyter Notebook :-

ohlc_data = {
    'DateTime': ['2023-10-05 08:31:00', '2023-10-05 08:32:00', '2023-10-05 08:33:00', '2023-10-05 08:34:00', '2023-10-05 08:35:00'],
    'Open': [0.5945, 0.59445, 0.59427, 0.59427, 0.59439],
    'High': [0.5945, 0.59448, 0.59433, 0.59445, 0.59452],
    'Low': [0.5945, 0.5943, 0.59423, 0.59425, 0.59435],
    'Close': [0.5944, 0.5943, 0.5943, 0.5944, 0.59448],
}

ohlc_data = pd.DataFrame(ohlc_data)
ohlc_data
Vol = pd.DataFrame(ohlc_data,columns = ['Volume'])
Volume = pd.concat([ohlc_data,Vol],axis = 1)
Volume = Volume.set_index('DateTime')
Volume.index = pd.to_datetime(Volume.index, format='%Y-%m-%d %H:%M:%S', utc=True)

Volume.Volume = Volume.Volume.resample('1T').sum()
Volume

Is this correct to Resample code ?

Output :-

Screenshot

It says "0" number is given in Volumn column but it has problem to calculated itself.

How to calculate this Volume in OHLC ?

How to do code?

1

There are 1 best solutions below

1
Corralien On

Maybe you misunderstood what OHLC means? (Open High Low Close). If you want to compute the daily OHLC of a time series:

>>> df
             DateTime  Value
0 2023-01-01 09:00:52      2
1 2023-01-01 12:28:37      7
2 2023-01-01 14:14:28      1
3 2023-01-01 15:18:23      4
4 2023-01-01 16:45:37      3
5 2023-01-01 17:59:42      5

You can do:

# Assume DateTime is already a DatetimeIndex
>>> df.resample('D', on='DateTime')['Value'].ohlc()
            open  high  low  close
DateTime                          
2023-01-01     2     7    1      5

Which is a very similar to:

>>> (df.resample('D', on='DateTime')
       .agg(open=('Value', 'first'),
            high=('Value', 'max'),
            low=('Value', 'min'),
            close=('Value', 'last')))

            open  high  low  close
DateTime                          
2023-01-01     2     7    1      5

You can't compute the volume with this function. You don't need ohlc because your input data are already in this format.