C++ price type to properly deal with rounding error?

Discussion in 'App Development' started by kmiklas, May 17, 2017.

  1. kmiklas

    kmiklas

    Usage is for price representation on the Ritchie Stock Exchange. How should I store prices in the order book, market data, logs... prices are everywhere.

    For U.S. equities, the price must be accurate to the penny; for forex, to the pip (0.0001). Zero tolerance for error in price!
     
    #11     May 18, 2017
  2. Simples

    Simples

    Ok, design for those use cases then, not more! :D
     
    #12     May 18, 2017
    kmiklas likes this.
  3. grashed

    grashed

    I think you should stick to decimals. Doubles are inherently problematic and have epsilon issues (https://en.wikipedia.org/wiki/Machine_epsilon). To be honest, unless you are doing somehting that requires c++, you'd be better off building this in c#, imho. But if c++ is the way to go, then yeah, get a library like boost that implements a c++ decimal (http://stackoverflow.com/a/15320495)
     
    #13     May 18, 2017
  4. Thanks for that wiki link. I couldn't remember what that was called.

    I've used GMP in the past as well for some crypto stuff. It was a total PITA to get it compiled under Windows, though it's a no-brainer in Linux.
     
    #14     May 18, 2017
    kmiklas likes this.
  5. kmiklas

    kmiklas

    Why do you think that I'd be better off building this in C#? As I see it, C++ has some clear advantages:

    1. Implement in Linux, making available an open-source, open-kernel platform for perf tweaking, and all other Linux beauty. C# can't be compiled on Linux (at least not a C# compiler that I would trust with financial data).
    2. Avoiding Windows fees, crashes, and "The Microsoft Way"
    3. Portable code that can be compiled under Linux, Windows, Mac, UNIX (Sun, AIX, HPUX), even OS/2 :D
    4. Terminal and shell scripts available for console-based applications. (I suppose you could argue for PowerShell here; it's come a long way, but the terminal is the terminal).
    5. Perf. C++ beats C# everywhere from a performance perspective:
    https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=csharpcore&lang2=gpp
    6. C++ is the standard in the financial industry

    Also, what do you mean by "Stick to decimals." You mean floating point base (2 ^ -n) internal representation?
     
    Last edited: May 18, 2017
    #15     May 18, 2017
  6. If you're doing this all yourself and you're most familiar with C++ then you should just stick with it. It's the fastest way from point A to point B.
     
    #16     May 18, 2017
    kmiklas likes this.
  7. Millionaire

    Millionaire

    I think doubles are accurate to 15 significant digits.
    I don't know of any financial instrument that needs more than 15, perhaps other posters can?
    The largest one i can think of are bonds futures that trade in 1/128 increments.
    A price like 120 1/128 = 120.0078125
    That is 10 significant digits.

    Then you just round off any rounding errors outside those 10 significant digits.

    So i think doubles might be fine.

    But perhaps you might want to define your own 'Price' class that encapsulates a double to start with, but can easily change in future if double proves problematic.

    You could also extend class 'Price' with specialised versions, eg stock prices, fx prices, bond prices if needed.
     
    #17     May 18, 2017
    kmiklas likes this.
  8. kmiklas

    kmiklas

    Thank you! That was very helpful! I really like the idea of creating and extending a "Price" class. I had thought of making price a fundamental type (int, double, some size_t, etc).
     
    #18     May 18, 2017
  9. grashed

    grashed


    Yeah those are good points. Here are the pros of using c#

    1) Perf - True, native c++ holds an advantage, but that only really materializes when you have optimized your entire network (slow processor, 1 gig switch, slow disks on your database). Honestly, with gpu's and mutithread cores, the perf difference is not as great as you would expect.

    2) Ease of maintenance - managed code is a lot easier to maintain because of all of c++ gotcha's. Also today we have more c#/java programmers than c++ programmers (don't know if that is good or bad, but as a hiring manager, I can tell you it's easier to hire a java/c#).

    3) Ease of use - c# and java are just syntactically easier to use and have a cleaner design. Building classes in c# feels much more cleaner than c++. Plus less gothca's!

    2) Memory leaks - they happen a lot more in c++ than c#

    3) Window fees - can't argue that here, but I think this is a bit overblown. Depending on your ROI expectations, a three year server running windows will cost about $800 for the OS and # -4 k for the server. But the real killer will be the maintenance. I good linux admin willl cost you about 50k over the equivalent Windows admin (agian, this is my own experience, so please don't be offended).

    4) c++ is the standard - I would argue with that. I worked at Direct edge and we implemented the exchange in c#. Bats bought us out and they are a linux/c++ shop, but I'm not sure if they had to start over, they would use c++ (Although they had better speeds of execution, but I'd argue again, hardware can remedy that). I think java/c# are the more prevalent platforms now.
     
    #19     May 18, 2017
    kmiklas likes this.
  10. sprstpd

    sprstpd

    If it were me, I would use C# (or F#) over C++ in this situation just because the .NET languages have a built in "decimal" type (and that is what you need). It will make your life easier.

    You should be searching for a language that makes your life easier, especially in the prototyping stage.

    Also, I'm pretty sure C# (or F#) runs on Linux without much problem.
     
    #20     May 18, 2017
    kmiklas and dartmus like this.