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

gwo2fgha  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(157)

我试图创建一个“深度图”从订单书将看起来像下面这样:


的数据
我计算出直方图将被使用,但无法设置适当的箱,因此产生一个不正确的图表。'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()

mkh04yzy

mkh04yzy1#

如果你对曲线下的区域不感兴趣,可以使用**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()

字符串
x1c 0d1x的数据
否则,如果你想给曲线下面的区域上色,你可以使用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()


lnlaulya

lnlaulya2#

接受的答案让你走了一段路,但实际的深度图是单调减少/增加的出价/要求,因为它包括一个 * 累积总和 * 的限制订单。基本原理是,深度图将向您显示“给定价格 * 或更好的价格 * 有多少单位可用”:
例如,假设您对TKR有以下出价:
| 数量| Quantity |
| --| ------------ |
| 三个| 3 |
| 四个| 4 |
| 十个| 10 |
如果您以5美元的价格下了一个TKR卖单,那么您将能够以该价格卖出7个单位; 4将以5美元的价格购买,3(愿意以10美元购买)肯定会以更有吸引力的5美元购买。因此,Price=5处的深度图应该显示数量7,而不是4。同样地,在Price=3处,数量将是17。
回到你的问题/例子,我发现的最简单的方法(所有功劳都归功于this notebook/article)是利用seaborn's 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")

字符串
结果如下:x1c 0d1x的数据

相关问题