反转 OHLC 数据(无负值)

问题描述 投票:0回答:1

这个问题已在这里得到解答(如何反转 OHLC 数据?),但是该人提出的方法在我的情况下不起作用。我需要积极的价值观。

我需要使用以下方法反转数据集:

提取收益,将其乘以负一,然后得到具有适当起始值的累积收益。

考虑以下 OHLC 数据集:

   data = pd.DataFrame({
    'Open': [5133.5, 5135.0, 5136.0, 5135.5, 5133.5, 5133.5, 5133.0, 5134.0, 5133.5, 5131.0, 5128.5, 5127.5, 5126.0, 5126.0, 5124.0, 5124.5, 5125.0, 5122.0, 5120.0, 5119.5, 5120.0, 5122.5, 5120.0, 5121.0, 5118.5, 5119.5],
    'High': [5135.5, 5136.0, 5137.0, 5135.5, 5134.0, 5134.0, 5134.0, 5134.5, 5133.5, 5131.5, 5128.5, 5128.0, 5127.5, 5126.0, 5125.0, 5125.5, 5125.0, 5122.5, 5121.0, 5121.0, 5122.5, 5122.5, 5121.5, 5121.0, 5119.5, 5123.0],
    'Low': [5133.0, 5134.0, 5135.0, 5132.5, 5132.0, 5132.5, 5132.0, 5133.5, 5129.5, 5127.5, 5125.0, 5125.5, 5125.5, 5122.5, 5123.5, 5123.5, 5121.0, 5119.5, 5118.5, 5118.5, 5120.0, 5120.0, 5119.5, 5118.5, 5118.5, 5118.5],
    'Close': [5135.0, 5135.5, 5135.0, 5134.0, 5133.5, 5133.5, 5134.0, 5134.0, 5131.0, 5128.5, 5127.5, 5126.0, 5125.5, 5124.0, 5124.0, 5124.5, 5122.0, 5120.5, 5120.0, 5120.5, 5122.5, 5120.5, 5121.0, 5118.5, 5119.5, 5121.5],
    'Volume': [3431.0, 3622.0, 2635.0, 5244.0, 1805.0, 1525.0, 3248.0, 1236.0, 9807.0, 8502.0, 10235.0, 3573.0, 3088.0, 8762.0, 3795.0, 3716.0, 10130.0, 8171.0, 5370.0, 5323.0, 4170.0, 4100.0, 2623.0, 3436.0, 1879.0, 7425.0]
    }, index=pd.date_range(start='2022-08-15 15:25:00', periods=26, freq='T'))

如果我使用 mplfinance 进行绘图,我将得到以下图表:

#pip install --upgrade mplfinance
import mplfinance as mpf
mpf.plot(data, type="candle")

enter image description here

我尝试过这种方法:

# Reset index
df = data.reset_index()
df.rename(columns={'index': 'Time'}, inplace=True)

# Calculate percentage changes and cumulative products
df["Close_percent"] = df['Close'].pct_change() * -1
df["High_percent"] = df['High'].pct_change() * -1
df["Low_percent"] = df['Low'].pct_change() * -1
df["Open_percent"] = df['Open'].pct_change() * -1

df['Cum_Close_percent'] = (1 + df['Close_percent']).cumprod()
df['Cum_High_percent'] = (1 + df['High_percent']).cumprod()
df['Cum_Low_percent'] = (1 + df['Low_percent']).cumprod()
df['Cum_Open_percent'] = (1 + df['Open_percent']).cumprod()

# Swap Open and Close for the first row
df.at[0, 'Open'], df.at[0, 'Close'] = df.at[0, 'Close'], df.at[0, 'Open']

# Calculate upper and lower shadows
upper_shadow = df.at[0, 'High'] - max(df.at[0, 'Open'], df.at[0, 'Close'])
lower_shadow = min(df.at[0, 'Open'], df.at[0, 'Close']) - df.at[0, 'Low']

df.at[0, 'High'] = max(df.at[0, 'Open'], df.at[0, 'Close']) + lower_shadow
df.at[0, 'Low'] = min(df.at[0, 'Open'], df.at[0, 'Close']) - upper_shadow

# Create inverted DataFrame
inverted_df = pd.DataFrame()
inverted_df["Close"] = df.at[0, 'Close'] * df["Cum_Close_percent"]
inverted_df["High"] = df.at[0, 'Low'] * df["Cum_High_percent"]
inverted_df["Low"] = df.at[0, 'High'] * df["Cum_Low_percent"]
inverted_df["Open"] = df.at[0, 'Open'] * df["Cum_Open_percent"]

# Copy Time and Volume columns
inverted_df["Time"] = df["Time"].copy()
inverted_df["Volume"] = df["Volume"].values.copy()

# Swap High and Low columns
inverted_df["High"], inverted_df["Low"] = inverted_df["Low"], inverted_df["High"]

# Set index and drop Time column
inverted_df = inverted_df.set_index(inverted_df["Time"])
inverted_df = inverted_df.drop(columns=["Time"])

# Reorder columns
inverted_df = inverted_df[['Open', 'High', 'Low', 'Close', 'Volume']].copy()

mpf.plot(inverted_df, type="candle")

但是,当开盘价和收盘价相同时,蜡烛不会正确反转,并且在某些情况下,阴影会反转。

enter image description here

你们能帮我找到解决这个问题的方法吗?

python pandas numpy stockquotes
1个回答
0
投票

代码

# return fig and ax using returnfig=True in mpf chart
fig, ax = mpf.plot(data, type="candle", returnfig=True)

# invert y-axis using ax
ax[0].invert_yaxis()

invert graph

© www.soinside.com 2019 - 2024. All rights reserved.