SERVING THE QUANTITATIVE FINANCE COMMUNITY

JinhuaColin
Topic Author
Posts: 33
Joined: May 30th, 2013, 4:33 pm

### how is HFT possible

Hi guys, i was working on a small automated trading project myself and this crazy question popped up in my head. How do people achieve a millisecond trading strategy?  A C++ program takes 0.2s even i only program it to initialize a class object and run a 100-cycle for loop. i guess most trading strategies have a optimization process leaving alone those more complex machine learning strategies; also some strategies need to handle hundreds of stocks. Is it really true a program can finish all this within milliseconds, or is "millisecond" just a saying?  i understand there are more advanced tools/techniques out there like GUP computing, parallel computing and distributed computing etc which can greatly reduce the computation time. But it is still difficult for me to believe this millisecond run time. Appreciate your help!

Alan
Posts: 9171
Joined: December 19th, 2001, 4:01 am
Location: California
Contact:

### Re: how is HFT possible

Not my area, but it is hardware-based acceleration, pushing the trading logic to the network card with FPGA's -- for example see here for a circa 2012 discussion, probably with quite outdated timings by now. An excerpt:

For these products, precise latency figures are application dependent and difficult to obtain. However, Accelize claims their smart NICs can turn around financial trades on the FPGA in under 2µs. This is significantly lower than the minimum latency achievable with software, as discussed earlier.

Billy7
Posts: 255
Joined: March 30th, 2016, 2:12 pm

### Re: how is HFT possible

Apart from the specialized hardware technologies/architectures such as the one Alan mentioned (which I was not aware of), even on the software side the initial premise for your disbelief (" A C++ program takes 0.2s even i only program it to initialize a class object and run a 100-cycle for loop") is false. Maybe when something starts and has to load a few things into memory it can take 0.2 secs, or maybe you're just starting the debugger? But as soon as the application is up and running, what you mention can be done in less than a microsecond (though you don't specify what every cycle loop does, but I'm assuming you're not talking about simulations of the universe!)

LocalVolatility
Posts: 124
Joined: May 27th, 2009, 10:07 am
Location: Amsterdam
Contact:

### Re: how is HFT possible

ISayMoo
Posts: 351
Joined: September 30th, 2015, 8:30 pm

### Re: how is HFT possible

You can get microsecond latencies on Intel CPUs, no need to go for FPGA (which are a must for sub-microsecond latencies).

Principles:
1. keep it simple (no fancy stuff in the main loop)
2. pin it to the core (no core switching)
4. overclock
6. buy expensive, fast network cards

snufkin
Posts: 45
Joined: January 25th, 2017, 9:05 am
Location: Cambridge

### Re: how is HFT possible

"millisecond" latency is easy even if you relax and program in Python:

Python 2.7.13 (default, Apr  4 2017, 08:47:57) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> def cycle():...     for x in range(100):...         print x... >>> import time>>> def measure():...     t1 = time.time()...     cycle()...     t2 = time.time()...     return t2 - t1... >>> print measure()01# ... skipped ...98990.000275135040283>>> 

Here you go, sub-milisecond timing in Python with an awful output. Just don't run your Monte Carlo, and you're likely to be fine for a draft project.

outrun
Posts: 3913
Joined: April 29th, 2016, 1:40 pm

### Re: how is HFT possible

Your cycle() time is roughly e*10E-5.

That would mean that the code below runs in 148.4123 seconds?

def measure():  t1 = time.time()  for _ in range(10):    for _ in range(10):      for _ in range(10):        for _ in range(10):          for _ in range(10):            cycle()  t2 = time.time()  return t2 - t1

snufkin
Posts: 45
Joined: January 25th, 2017, 9:05 am
Location: Cambridge

### Re: how is HFT possible

outrun wrote:
Your cycle() time is roughly e*10E-5.

That would mean that the code below runs in 148.4123 seconds?

32.5 second on my laptop, why? (Using the highly scientific approach of only taking one measure).

outrun
Posts: 3913
Joined: April 29th, 2016, 1:40 pm

### Re: how is HFT possible

snufkin wrote:
outrun wrote:
Your cycle() time is roughly e*10E-5.

That would mean that the code below runs in 148.4123 seconds?

32.5484418869 on my laptop, why?

There is an obsession in the brainteasersforum discussing weird method of computing $e^5$. I though this was a new method but it's not very accurate!

snufkin
Posts: 45
Joined: January 25th, 2017, 9:05 am
Location: Cambridge

### Re: how is HFT possible

outrun wrote:
snufkin wrote:
outrun wrote:
Your cycle() time is roughly e*10E-5.

That would mean that the code below runs in 148.4123 seconds?

32.5484418869 on my laptop, why?

There is an obsession in the brainteasersforum discussing weird method of computing $e^5$. I though this was a new method but it's not very accurate!

That's a JIT optimised e! When you do HFT, you need to tune your constants, too.

outrun
Posts: 3913
Joined: April 29th, 2016, 1:40 pm

### Re: how is HFT possible

snufkin wrote:
outrun wrote:
snufkin wrote:
32.5484418869 on my laptop, why?

There is an obsession in the brainteasersforum discussing weird method of computing $e^5$. I though this was a new method but it's not very accurate!

That's a JIT optimised e! When you do HFT, you need to tune your constants, too.

Ah, yes, I forgot the HFT JIT correction factor of $\sqrt{21}$!

ISayMoo
Posts: 351
Joined: September 30th, 2015, 8:30 pm

### Re: how is HFT possible

People were running low-latency strategies from Perl or KDB, nothing new here.

outrun
Posts: 3913
Joined: April 29th, 2016, 1:40 pm

### Re: how is HFT possible

ISayMoo wrote:
People were running low-latency strategies from Perl or KDB, nothing new here.

What can possibly go wrong!..

ISayMoo
Posts: 351
Joined: September 30th, 2015, 8:30 pm

### Re: how is HFT possible

They made money, which is #1 criteria of success in finance.