HFT strategies and C#
SO, you like high frequency trading and you want to write strategies in C# HFT. - the speed race. Being first. HFT - faster than the next box, and C# - most people do not think this is possible. Maybe it is. Maybe not. At least I can outline a path and pitfalls. Welcome to a guie to HFT programming in C#. Most of this also applies for other programming languages - but definitely, unless you go FPGA, these are not too bad guidelines for HFT trading. So, what do you need for HFT strategies in C#? Let us start at the bottom.
Obviously, you need fast hardware. HFT means being faster than the next box, with many orders entered and cancelled. If any other box in the room is faster, your loose. C# puts you at a disadvantage for HFT. Let's not add to that by using crappy hardware. There are some white papers out about hardware for HFT - go and read them. For example the SUperMicro Low Latency WhitePaper
Processor & RAM
Intel. The newest. For HFT you need per core speed. Not too many cores. Better a higher frequency. Get adecent special motherboard. There are boards for that - like the SuperMicro C7Z87 which supports overclocking. HFT in C# needs raw core speed. Maybe a gamer motherboard, like an Asus ROG (Republic of gamers). Remember, this is about speed. Not total computation power - being as fast as possible. Speed for your algo. No xeon - this likely is an end user processor. Gamers like core speed. Anddid I say it already - overfclock. HFT means speed. C# and HFT - get all teh speed you can.
Data comes in, orderss go out. C# and HFT - be fast. Faster than the next box. You want a gaming network card maybe - like the ROG boards from Asus have on board. Server networks are often optimized for throughput. Programming for HFT is about speed, you have not too much data. But you need speed. Be faster han the neighbor. You use C# - he may use assembler.
Try linux - you can optimize the kernel. Latency. Speed. HFT is about speed. Be faster than the next box. C# runs on Linux (Mono). And you want lantecy. You can not optimize the windows kernel. You can optimize Linux. Do it - the other boxes in your datra center, all competing, all doing HFT, may do it. HFT is about speed. No easy money. Speed. Be faster. You use C#, they do HFT maybe in assembler. And it is all about speed.
So you do HFT in C#. Or want to. You must be fast. You got fast hardware for your HFT setup. Now do not waste it. HFT in C# means programming for speed. There are some tricks. You want to be fast. As do the others. You use C# - they may use assembler. HFT is about speed. So - learn from them.
You do not want this. C# has garbage collection. HFT is about speed. You do HFT in C#, so everything is about speed. Make sure the garbage collector does not interrupt you. Speed, you know. HFT is about speed. Even in C#. How does one do that? Do not allocate objects that need collection. Yes, easy as this. Preallocate objects you need, reuse them. Allcoate 500 ticks (if you use classes), put them once processed into an empty queue for the data input to reuse them. You can get away with very little garbage - and HFT is all about speed.
Even better - try using structs. Structs are not heap allocated, do not create garbage. Allocate data structures static, when you start. HFT is about speed. C# can slow you down. Processing tons of ticks takes a lot less time when your input queue is an array of 500 structs.
Optimize your data structures
That was the basic 20 years ago. Today it is arcane. But HFT in C# is about speed. Well, let's program old fashioned. Fast. An input queue for data to process is good - but do not use the queue. Look up "Circular Buffer". Implement one. Storing the ticks as structures. You are fast. HFT in C# is still about speed.
Use your cores - process in stages
HFT is about speed, and you have a dedicated computer just for trading. Use it. When data comes, have one thread looping to process it. DO not rely on events. HFT in C# is still about speed. Run a tight loop checking for new data. You burn a core. But you are faster. C# and HFT - it is all about speed. The next thread does initial analysis. You have 4-6 threads, all running tight loops. Forget locks. Learn the slim variants. Learn Interlocked. It is about speed. Latency. HFT is about being fast.
Use a special API
HFT is about speed. C# or not - HFT is about speed. Think retail data works? Delusion. Even sending an order is too slow, and again: HFT is all about speed. Ther are special interfaces by speed trading companies. Example Rithmic - nice, fast. THe tandard offer for HFT - no way. But their Diamond API. Send an order - but have it hold before the exchange. All risk management checks done. Waiting for your HFT strategy to say "NOW". No more checks. HFT in C# - all about speed. A special API sheds off the microseconds you need.
Write your own stack
Ninja Trader? Forget it. Too slow. Single threaded, running in the UI. It is retail software. As is anything else most people use and that is in the news. You pretty much need something slim. Need something optimized for speed. Did I mention it? HFT in C# is still about speed. That means a command line program or some system service. Stay out of the UI at least with the critical code paths. It is all about speed.
Is it worth it?
You decide. You invest in top hardware. Spend lot of money on hosting- HFT in C# - still all about speed. Meanwhile others like us - we do latency optimized trading. We are in the same city, but not HFT. We do not compete with you - but sometimes write keyword optimized blog posts like this. For you, trading is HFT, HFT in C#, still about speed. For others - it is not. At the end, the profits are worth he effort or not.
But if you want to do HFT in C#, then the items mentioned here are your baseline. Because the other boxes in the room want to be faster than you.