i25959341 / orderbook

Matching Engine for Limit Order Book in Golang
MIT License
442 stars 154 forks source link

Adding a tabular representation of the orderbook #16

Closed d-fal closed 1 year ago

d-fal commented 3 years ago

In some cases, users should see a brief overview of the orderbook. For instance, user A should see the following representation of both the queues in the orderbook:

         Asks (  qty,   price)                              Bids ( qty,   price)
                      2       1.5                                             4     1
                      3       1.8                                              3     0.8
                      3        2                                                2     0.75

In the suggested changes, I have added a MarketView method that delivers the expected info in the map[string]decimal.Decimal in which keys are prices and the values are quantities.

Why strings as the keys? I used built-in string type rather than decimal.Decimal since the latter creates two objects for very same numbers:

    a := decimal.NewFromFloat(1)
    b := decimal.NewFromFloat(1)
    fmt.Printf("%v and %v are equal? %v\n", a, b, a == b)
        //  1 and 1 are equal? false