Home  >  Article  >  Backend Development  >  Python 做高频交易系统适合哪个级别的延迟?

Python 做高频交易系统适合哪个级别的延迟?

WBOY
WBOYOriginal
2016-06-06 16:22:402164browse

latency指tick to trade. 可以容许少数核心函数用cython或直接c来实现。

回复内容:

我对 Python 不算熟,不过可以提供一些思路。

首先做一个最基本的测试,连续取两次系统时间,精度在纳秒,看看延迟如何。先来看一段纯 C 代码:
<code class="language-text">#include <sys>
uint64_t nanotime(const struct timespec *ts)
{
   return (ts->tv_sec * kT_ns_in_s) + (ts->tv_nsec);
}

uint64_t    n=50000;
uint64_t    sum=0;
uint64_t    latency=0;

for (i = 0; i </sys></code>
比较现实的说是1ms级别的,如果你用python现成的library(urlib, request)接收数据至少有100us级别的延迟,一般交易系统需要多线程,python的GIL又会增加延迟,而且交易最忙的时候因为处理大量数据,python的GC更容易发生。用C或Cython写核心部分不能提高很多,因为python的延迟是因为language design而不是computation造成的。当然这些问题可以改进,比如自己做一套tcp连接程序什么的,不过这些恐怕并不比写c++更容易。

另外上面的回答里的时间测试不一定有代表性,在一个简单的loop测时间的话compiler和CPU会做很多你想不到的事情,结果会和真实值差很多。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn