使用pandas从订单的时间序列创建订单簿的快照?

2024-05-12 02:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和pandas还不太熟悉,我想知道是否有人知道python在pandas的基础上构建了一些库,这些库需要一系列的命令,这些命令包含以下列: 时间戳、id、价格、大小、交换

每个记录根据大小调整每个价格和交换的总金额,以提供当前视图,即记录可能如下所示:

9:00:25.123, 1, 1.02, 100, N
9:00:25.123, 2, 1.02, -50, N
9:00:25.129, 3, 1.03,  50, X
9:00:25.130, 4, 1.02, 150, X
9:00:25.131, 5, 1.02,  -5, X

我想在任何时候都能了解市场的现状。例如,如果我在9:00:25.130打电话到市场,我会得到:

1.02, N,  50
1.02, X, 150
1.03, X,  50

查询9:00:25.131将返回

1.02, N,  50
1.02, X, 145
1.03, X,  50

这些记录可能有一百万条或更多条,对每个请求的所有记录进行迭代将需要很长时间,特别是当您试图查看一天中稍后的时间时。我想人们可以在一段时间间隔内创建“快照”,并像mpeg播放中的关键帧一样使用它们,我也可以自己编写它,但我认为,图书制作/播放是使用pandas处理财务数据的人们的一个常见需求,他们可能已经是图书馆了。

有什么主意,还是我自己滚?


Tags: 命令视图idpandas间隔市场记录时间
1条回答
网友
1楼 · 发布于 2024-05-12 02:30:49

我知道这已经很古老了,但是看看熊猫的好处和局限性是很有启发性的

我构建了一个trivial jupyter notebook来展示如何构建一个像您描述的订单簿来按照您的要求使用。

核心是一个循环,用于更新订单簿的状态并将其保存以合并到pandas数据框中:

states = []
current_timestamp = None
current_state = {}

for timestamp, (id_, price, exch, size) in df.iterrows():
    if current_timestamp is None:
        current_timestamp = timestamp
    if current_timestamp != timestamp:
        for key in list(current_state):
            if current_state[key] == 0.:
                del current_state[key]
        states.append((current_timestamp, dict(**current_state)))
        current_timestamp = timestamp
    key = (exch, price)
    current_state.setdefault(key, 0.)
    current_state[key] += size
states.append((timestamp, dict(**current_state)))

order_book = pd.DataFrame.from_items(states).T

但是:请注意,必须在pandas之外建立book state,并且order book state的pandas.DataFrame不太适合按级别优先级或深度(级别3数据)对order book建模,这可能是一个主要限制,具体取决于您希望对order book建模的准确性。

在现实世界中,订购书以及更新它们的订单和报价(这两个都归为“请求”一词)具有相当复杂的交互。这些交互由管理它们的交换规则控制,并且这些规则一直在变化。由于这些规则需要时间来正确建模,值得很少人理解,而且旧的规则集通常甚至不具有多大的学术兴趣,因此人们倾向于发现这些规则被编入图书馆的唯一地方是那些不太愿意与他人共享的地方。

要了解订单簿的简单(“样式化”)模型背后的理论、订单及其引用,请参阅论文"A stochastic model for order book dynamics" by Rama Cont, Sasha Stoikov, Rishi Talreja,第2节:

2.1 Limit order books

Consider a financial asset traded in an order-driven market. Market participants can post two types of buy/sell orders. A limit order is an order to trade a certain amount of a security at a given price. Limit orders are posted to a electronic trading system and the state of outstanding limit orders can be summarized by stating the quantities posted at each price level: this is known as the limit order book. The lowest price for which there is an outstanding limit sell order is called the ask price and the highest buy price is called the bid price. [...more useful description]

2.2. Dynamics of the order book

Let us now describe how the limit order book is updated by the inflow of new orders. [...] Assuming that all orders are of unit size [...],

• a limit buy order at price level p<p_A(t) increases the quantity at level p: x → x_{p−1}

• a limit sell order at price level p>p_B(t) increases the quantity at level p: x → x_{p+1}

• a market buy order decreases the quantity at the ask price: x → x_{p_A(t)−1}

• a market sell order decreases the quantity at the bid price: x → x_{p_B(t)+1}

• a cancellation of an oustanding limit buy order at price level p<p_A(t) decreases the quantity at level p: x → x_{p+1}

• a cancellation of an oustanding limit sell order at price level p>p_B(t) decreases the quantity at level p: x → x_{p−1}

The evolution of the order book is thus driven by the incoming flow of market orders, limit orders and cancellations at each price level [...]

在某些库中,您可以看到人们尝试建模或可视化一个简单的限价单手册:

  • PyRebuildLOB是一个有效的例子,但是pandas除了作为一个奇特的数组之外,在它的实现中所起的作用相对较小。
  • 背景与视觉化
  • 来自Oculus InformationMSFT order book visualisation的常规可视化
  • 来自working4arbitrageworking4arbitrage order book vis的异常可视化

还有一个很好的quant.stackoverflow.com问答here

相关问题 更多 >