如何在 Matplotlib 中制作订单簿深度图?

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

我正在尝试从订单簿创建一个“深度图”,如下所示:

我发现将使用直方图,但无法设置正确的箱,因此产生了不正确的图表。

'ASK/QTY'
如下:

[49579.41, 49580.66, 49581.39, 49585.42, 49585.43, 49586.99, 49588.0, 49588.74, 49589.62, 49589.8, 49589.81, 49590.0, 49590.37, 49590.38, 49591.38, 49591.87, 49592.84, 49593.32, 49593.86, 49594.17, 49594.36, 49595.32, 49595.54, 49595.92, 49595.97, 49596.87, 49596.88, 49596.99, 49597.06, 49597.2, 49597.21, 49597.52, 49597.64, 49597.79, 49598.08, 49598.46, 49598.69, 49598.98, 49599.52, 49600.0, 49600.14, 49600.26, 49600.34, 49600.47, 49600.62, 49600.73, 49601.0, 49601.25, 49601.3, 49601.71, 49601.85, 49601.93, 49602.2, 49602.7, 49602.93, 49603.12, 49603.15, 49603.23, 49603.56, 49604.27, 49604.31, 49604.32, 49604.89, 49604.9, 49605.12, 49605.21, 49605.5, 49605.59, 49606.0, 49606.13, 49606.39, 49606.62, 49607.2, 49607.5, 49607.68, 49607.69, 49607.71, 49607.76, 49608.05, 49608.11, 49608.67, 49609.71, 49610.0, 49610.07, 49610.18, 49610.53, 49610.54, 49611.72, 49612.1, 49612.25, 49612.42, 49612.71, 49612.99, 49613.0, 49613.42, 49613.45, 49613.53, 49613.62, 49613.84, 49613.92]
[4.514622, 1.0, 1.0, 0.014002, 0.04, 0.012384, 0.005441, 0.04, 0.254507, 0.219849, 0.559163, 0.14, 0.219849, 0.02, 0.010085, 0.399503, 0.50407, 0.05, 0.133538, 0.000952, 0.03, 0.023, 0.1205, 0.063524, 0.038323, 0.371299, 0.03, 0.041181, 0.182644, 0.088, 0.022, 0.040338, 0.137032, 0.464753, 0.05, 0.893, 1.248, 0.022, 0.3, 4.683597, 0.797, 0.004717, 0.504112, 0.009785, 0.03, 0.006436, 0.005641, 0.2937, 0.116174, 0.0183, 0.853, 0.041181, 0.03, 0.005512, 0.4, 0.24, 0.040373, 0.364929, 0.008, 0.005515, 0.163396, 0.03, 0.00377, 0.03943, 0.02386, 0.765352, 0.46, 0.115285, 0.251659, 0.049, 0.111, 0.4, 0.4, 0.364734, 0.022, 0.319989, 0.141003, 0.426611, 0.022, 0.06, 0.626486, 0.5, 0.004243, 0.059612, 0.3, 4.747239, 0.04, 0.2406, 0.021046, 0.237078, 0.428943, 0.000626, 0.000932, 0.001094, 1.906807, 0.03, 0.349154, 0.040366, 0.004549, 0.038309]

我的代码如下:

import matplotlib
from utilities import get_order_book
import matplotlib.pyplot as plt

if __name__ == '__main__':
    pair = 'BTC/USDT'
    asks, bids = get_order_book(pair)
    ask_x = []
    ask_y = []
    for ask in asks:
        ask_x.append(ask[0])
        ask_y.append(ask[1])

    print(ask_x)
    print(ask_y)
    plt.bar(ask_x, ask_y, 2)
    plt.hist(ask_x, bins=len(ask_x))
    plt.show()
python matplotlib plot data-visualization visualization
2个回答
1
投票

如果您对曲线下区域的着色不感兴趣,可以使用

matplotlib.pyplot.step
(注意
where
参数,本例中我使用默认的
pre
值):

x = [49579.41, 49580.66, 49581.39, 49585.42, 49585.43, 49586.99, 49588.0, 49588.74, 49589.62, 49589.8, 49589.81, 49590.0, 49590.37, 49590.38, 49591.38, 49591.87, 49592.84, 49593.32, 49593.86, 49594.17, 49594.36, 49595.32, 49595.54, 49595.92, 49595.97, 49596.87, 49596.88, 49596.99, 49597.06, 49597.2, 49597.21, 49597.52, 49597.64, 49597.79, 49598.08, 49598.46, 49598.69, 49598.98, 49599.52, 49600.0, 49600.14, 49600.26, 49600.34, 49600.47, 49600.62, 49600.73, 49601.0, 49601.25, 49601.3, 49601.71, 49601.85, 49601.93, 49602.2, 49602.7, 49602.93, 49603.12, 49603.15, 49603.23, 49603.56, 49604.27, 49604.31, 49604.32, 49604.89, 49604.9, 49605.12, 49605.21, 49605.5, 49605.59, 49606.0, 49606.13, 49606.39, 49606.62, 49607.2, 49607.5, 49607.68, 49607.69, 49607.71, 49607.76, 49608.05, 49608.11, 49608.67, 49609.71, 49610.0, 49610.07, 49610.18, 49610.53, 49610.54, 49611.72, 49612.1, 49612.25, 49612.42, 49612.71, 49612.99, 49613.0, 49613.42, 49613.45, 49613.53, 49613.62, 49613.84, 49613.92]
y = [4.514622, 1.0, 1.0, 0.014002, 0.04, 0.012384, 0.005441, 0.04, 0.254507, 0.219849, 0.559163, 0.14, 0.219849, 0.02, 0.010085, 0.399503, 0.50407, 0.05, 0.133538, 0.000952, 0.03, 0.023, 0.1205, 0.063524, 0.038323, 0.371299, 0.03, 0.041181, 0.182644, 0.088, 0.022, 0.040338, 0.137032, 0.464753, 0.05, 0.893, 1.248, 0.022, 0.3, 4.683597, 0.797, 0.004717, 0.504112, 0.009785, 0.03, 0.006436, 0.005641, 0.2937, 0.116174, 0.0183, 0.853, 0.041181, 0.03, 0.005512, 0.4, 0.24, 0.040373, 0.364929, 0.008, 0.005515, 0.163396, 0.03, 0.00377, 0.03943, 0.02386, 0.765352, 0.46, 0.115285, 0.251659, 0.049, 0.111, 0.4, 0.4, 0.364734, 0.022, 0.319989, 0.141003, 0.426611, 0.022, 0.06, 0.626486, 0.5, 0.004243, 0.059612, 0.3, 4.747239, 0.04, 0.2406, 0.021046, 0.237078, 0.428943, 0.000626, 0.000932, 0.001094, 1.906807, 0.03, 0.349154, 0.040366, 0.004549, 0.038309]


fig, ax = plt.subplots()

ax.step(x, y)

plt.show()

否则,如果你想给曲线下的区域着色,你可以使用

matplotlib.pyplot.stairs
,正如 BigBen 在评论中已经建议的那样。注意,您需要向
x
数组添加一个值,因为这次它代表
edges
,而不是 数据点

x.append(x[-1])

fig, ax = plt.subplots()

ax.stairs(edges = x, values = y, fill = True)

plt.show()

这两个图似乎不同:这是因为在

matplotlib.pyplot.step
中我使用了
where
参数的默认值;相反,
matplotlib.pyplot.stairs
使用 post 插值:

x.append(x[-1])

fig, ax = plt.subplots()

ax.step(x[:-1], y, where = 'post')
ax.stairs(edges = x, values = y, fill = True)

plt.show()


0
投票

接受的答案可以让您达到目标,但实际的深度图表对于买价/卖价单调递减/递增,因为它包含限价订单的“累积求和”。理由是深度图将向您显示“给定价格有多少出价/要价或更好”: 例如,假设您对 TKR 有以下出价:

价格10 美元5 美元
数量
3
4
如果您以 5 美元的价格下达 TKR 卖单,那么您会发现该价格的买家不是 4 个,而是
7

; 4 人会以 5 美元购买,而 3 人(愿意以 10 美元购买)肯定会以更具吸引力的 5 美元购买。因此,深度图应显示交易量为 7,而不是 4。 回到你的问题/示例,我发现的最简单的方法(所有功劳都归于

本笔记本/文章

)是利用seaborn的ecdfplot,如下所示: import random import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # your original data ask_price = [49579.41, 49580.66, 49581.39, 49585.42, 49585.43, 49586.99, 49588.0, 49588.74, 49589.62, 49589.8, 49589.81, 49590.0, 49590.37, 49590.38, 49591.38, 49591.87, 49592.84, 49593.32, 49593.86, 49594.17, 49594.36, 49595.32, 49595.54, 49595.92, 49595.97, 49596.87, 49596.88, 49596.99, 49597.06, 49597.2, 49597.21, 49597.52, 49597.64, 49597.79, 49598.08, 49598.46, 49598.69, 49598.98, 49599.52, 49600.0, 49600.14, 49600.26, 49600.34, 49600.47, 49600.62, 49600.73, 49601.0, 49601.25, 49601.3, 49601.71, 49601.85, 49601.93, 49602.2, 49602.7, 49602.93, 49603.12, 49603.15, 49603.23, 49603.56, 49604.27, 49604.31, 49604.32, 49604.89, 49604.9, 49605.12, 49605.21, 49605.5, 49605.59, 49606.0, 49606.13, 49606.39, 49606.62, 49607.2, 49607.5, 49607.68, 49607.69, 49607.71, 49607.76, 49608.05, 49608.11, 49608.67, 49609.71, 49610.0, 49610.07, 49610.18, 49610.53, 49610.54, 49611.72, 49612.1, 49612.25, 49612.42, 49612.71, 49612.99, 49613.0, 49613.42, 49613.45, 49613.53, 49613.62, 49613.84, 49613.92] ask_qty = [4.514622, 1.0, 1.0, 0.014002, 0.04, 0.012384, 0.005441, 0.04, 0.254507, 0.219849, 0.559163, 0.14, 0.219849, 0.02, 0.010085, 0.399503, 0.50407, 0.05, 0.133538, 0.000952, 0.03, 0.023, 0.1205, 0.063524, 0.038323, 0.371299, 0.03, 0.041181, 0.182644, 0.088, 0.022, 0.040338, 0.137032, 0.464753, 0.05, 0.893, 1.248, 0.022, 0.3, 4.683597, 0.797, 0.004717, 0.504112, 0.009785, 0.03, 0.006436, 0.005641, 0.2937, 0.116174, 0.0183, 0.853, 0.041181, 0.03, 0.005512, 0.4, 0.24, 0.040373, 0.364929, 0.008, 0.005515, 0.163396, 0.03, 0.00377, 0.03943, 0.02386, 0.765352, 0.46, 0.115285, 0.251659, 0.049, 0.111, 0.4, 0.4, 0.364734, 0.022, 0.319989, 0.141003, 0.426611, 0.022, 0.06, 0.626486, 0.5, 0.004243, 0.059612, 0.3, 4.747239, 0.04, 0.2406, 0.021046, 0.237078, 0.428943, 0.000626, 0.000932, 0.001094, 1.906807, 0.03, 0.349154, 0.040366, 0.004549, 0.038309] # generate fake bid data for illustration purposes bid_price = [p-(0.001*p) for p in ask_price] bid_qty = [q*(2*random.random()) for q in ask_qty] ask_df = pd.DataFrame({'price': ask_price, 'quantity': ask_qty}) bid_df = pd.DataFrame({'price': bid_price, 'quantity': bid_qty}) fig, ax = plt.subplots() ax.set_title(f"Order Book Depth Chart") sns.ecdfplot(x="price", weights="quantity", stat="count", data=ask_df, ax=ax, color="red") sns.ecdfplot(x="price", weights="quantity", stat="count", complementary=True, data=bid_df, ax=ax, color="green") # complementary=True allows reflects that lower bids are "better" ax.set_xlabel("Price") ax.set_ylabel("Quantity")

结果如下:

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