pythonpandastime-series

How to invert OHLC data?


I have a OHLC (Open, High, Low, Close financial data).

An upward bar (bullish) is when the close price is higher than the open price.

A downward bar (bearish) is when the close price is lower than the open price.

I am trying to find a way to invert the dataset in order to have the following behavior:

Original data:

Original Data

Inverted data:

Inverted Data

The first steps seams:

• For an upward bar, swap the open and close prices to make it a downward bar.

• For a downward bar, swap the open and close prices to make it an upward bar.

Second step:

Preserve the range of the candlestick - Maintain the difference of High and Low:

# Function to invert the OHLC bars
def invert_ohlc(row):
  if row['Close'] > row['Open']:
    # Bullish bar, invert to bearish
    row['Open'], row['Close'] = row['Close'], row['Open']
  elif row['Close'] < row['Open']:
     # Bearish bar, invert to bullish
    row['Open'], row['Close'] = row['Close'], row['Open']
  return row

But I don't know how to continue:

Reproducible dataset (same as image):

import pandas as pd
from io import StringIO

data = """
Date,Time,Open,High,Low,Close
7/16/2024,09:00,1302000000,1303600000,1302000000,1303550000
7/16/2024,10:00,1303550000,1305650000,1301300000,1301800000
7/16/2024,11:00,1301800000,1305650000,1301150000,1302650000
7/16/2024,12:00,1302650000,1303700000,1300550000,1303600000
7/16/2024,13:00,1303600000,1304150000,1298400000,1299550000
7/16/2024,14:00,1299550000,1300900000,1297300000,1300000000
7/16/2024,15:00,1300000000,1302650000,1298700000,1301300000
7/16/2024,16:00,1301300000,1303800000,1299850000,1300500000
7/16/2024,17:00,1300550000,1301950000,1300000000,1301800000
7/16/2024,18:00,1301800000,1302800000,1301400000,1302450000
7/16/2024,19:00,1302500000,1303450000,1302300000,1303350000
7/17/2024,09:00,1299800000,1300500000,1298800000,1299650000
7/17/2024,10:00,1299650000,1301300000,1297900000,1299900000
7/17/2024,11:00,1299900000,1303600000,1296700000,1302050000
7/17/2024,12:00,1302050000,1305250000,1299000000,1303400000
7/17/2024,13:00,1303400000,1305950000,1302400000,1303750000
7/17/2024,14:00,1303800000,1304450000,1301350000,1303950000
7/17/2024,15:00,1304000000,1305800000,1302950000,1303300000
7/17/2024,16:00,1303300000,1305750000,1302950000,1305050000
7/17/2024,17:00,1305050000,1305250000,1303200000,1303350000
7/17/2024,18:00,1303350000,1304800000,1302950000,1304250000
7/17/2024,19:00,1304300000,1304750000,1302650000,1303150000
7/18/2024,09:00,1302250000,1303850000,1302250000,1303650000
7/18/2024,10:00,1303650000,1304650000,1299100000,1299600000
7/18/2024,11:00,1299600000,1301100000,1294850000,1295650000
7/18/2024,12:00,1295650000,1296850000,1291450000,1292500000
7/18/2024,13:00,1292550000,1293100000,1290400000,1291400000
7/18/2024,14:00,1291450000,1292050000,1288650000,1289250000
7/18/2024,15:00,1289250000,1289650000,1287350000,1288300000
7/18/2024,16:00,1288300000,1288300000,1284850000,1286100000
7/18/2024,17:00,1286100000,1286200000,1283800000,1285450000
7/18/2024,18:00,1285400000,1290950000,1284400000,1290400000
7/18/2024,19:00,1290400000,1292500000,1289650000,1292500000
7/19/2024,09:00,1290400000,1292050000,1289750000,1291200000
7/19/2024,10:00,1291250000,1293550000,1285300000,1287250000
7/19/2024,11:00,1287250000,1292800000,1286100000,1289950000
7/19/2024,12:00,1289900000,1292250000,1286250000,1288400000
7/19/2024,13:00,1288400000,1288950000,1284750000,1287350000
7/19/2024,14:00,1287300000,1287800000,1286150000,1287300000
7/19/2024,15:00,1287300000,1288800000,1285750000,1286900000
7/19/2024,16:00,1286950000,1287050000,1282450000,1283350000
7/19/2024,17:00,1283350000,1284950000,1283000000,1284600000
7/19/2024,18:00,1284650000,1284700000,1283050000,1283400000
7/19/2024,19:00,1283350000,1283400000,1279000000,1279000000
"""

# Use StringIO to simulate reading from a file
df = pd.read_csv(StringIO(data), parse_dates=[['Date', 'Time']])

Solution

  • You could take the negative values:

    import plotly.graph_objects as go
    
    fig = go.Figure(data=[go.Candlestick(x=df['Date_Time'],
                    open=df['Open'],
                    high=df['High'],
                    low=df['Low'],
                    close=df['Close'] )])
    fig.show()
    

    plt1

    fig = go.Figure(data=[go.Candlestick(x=df['Date_Time'],
                    open=-df['Open'],
                    high=-df['High'],
                    low=-df['Low'],
                    close=-df['Close'] )])
    fig.show()
    

    plot2