Page 1 of 1

how is HFT possible

Posted: September 21st, 2017, 11:38 pm
by JinhuaColin
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!

Re: how is HFT possible

Posted: September 22nd, 2017, 5:33 am
by Alan
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.

Re: how is HFT possible

Posted: September 22nd, 2017, 10:57 am
by Billy7
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!) 

Re: how is HFT possible

Posted: October 9th, 2017, 9:53 am
by LocalVolatility

Re: how is HFT possible

Posted: November 3rd, 2017, 8:33 am
by snufkin
"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 darwin
Type "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()
0
1
# ... skipped ...
98
99
0.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.

Re: how is HFT possible

Posted: November 3rd, 2017, 8:43 am
by outrun
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

Re: how is HFT possible

Posted: November 3rd, 2017, 10:50 am
by snufkin
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).

Re: how is HFT possible

Posted: November 3rd, 2017, 11:06 am
by outrun
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!

Re: how is HFT possible

Posted: November 3rd, 2017, 11:18 am
by snufkin
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.

Re: how is HFT possible

Posted: November 3rd, 2017, 11:35 am
by outrun
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}[$]!

Re: how is HFT possible

Posted: November 3rd, 2017, 9:05 pm
by outrun
People were running low-latency strategies from Perl or KDB, nothing new here.
What can possibly go wrong!..
Image

Re: how is HFT possible

Posted: January 30th, 2018, 12:13 am
by bingobangobongo
most of it is bypassing the kernel for network IO, keeping the CPU cache lines full, pinning threads to specific cpus and minimising/eliminating context switches. Any language can be used to achieve this, C++ simply gives you a little bit more control.