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

标签 python pandas quantitative-finance

我对 python 和 pandas 还很陌生,我想知道是否有人知道是否有任何基于 pandas 构建的 python 库需要一系列时间序列,其中包含以下列: 时间戳、id、价格、大小、交易所

每条记录都会根据大小调整每个价格和交易所的总和,以便为您提供当前 View ,即记录可能如下所示:

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 和财务数据的人来说是一种普遍的需求他们可能已经有图书馆可以做到这一点。

有什么想法,还是我自己动手?

最佳答案

我知道这已经过时了,但看到 pandas 的好处和局限性很有启发性

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

核心是一个循环,它更新订单簿的状态并将其保存以合并到 pandas Dataframe 中:

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 之外建立账簿状态,并且订单簿状态的 pandas.DataFrame 不太适合按级别优先级或深度(第 3 级数据)建模订单簿,这可能是一个主要限制,具体取决于您希望为订单簿建模的准确程度。

在现实世界中,订单簿以及更新它们的订单和报价(您将两者归为术语“请求”)具有相当复杂的交互。这些交互受管理它们的交易所规则的约束,并且这些规则一直在变化。由于这些规则需要时间来正确建模,很少有人值得理解,而且旧的规则集通常甚至没有太多的学术兴趣,所以人们往往会发现这些规则被编入图书馆的唯一地方是不太感兴趣的地方与他人分享。

要了解订单簿的简单(“程式化”)模型背后的理论,其订单和报价,请参阅论文 "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 [...]

您可以在一些库中看到人们对简单限价订单簿进行建模或可视化的尝试:

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

关于python - 使用 Pandas 从订单的时间序列创建订单簿的快照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17053425/

相关文章:

Python Pygame 蛇苹果在障碍物内生成

python - Yfinance - 替代方案?

r - 使用交易信号计算金融时间序列的返回

python - Pandas DataFrame 选择具有 NaN 值的特定列

python-3.x - 从盈透证券的 API 获取多个最后报价

python - 使用 python 更改特定 pandas 数据框列中的行值

python - Pydantic - 递归创建模型?

python - 蒙特卡罗模拟 : Underlying distribution parameter changes during simulation run based on conditions

Python - 如何使用 python pandas crosstab 创建混淆矩阵统计

python - 如何获得两个 datetime.time Pandas 列之间的绝对差?