Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a functional financial market data book  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 561
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am in a project related to electronic trading. I am creating a sort of market data book where stock prices and volumes are stored in sorted manner and matched up with matching quotes. Here is a link to information which might be relevant to my question.

Now I have created a basic functional market data book which I think is good start for a newbie like me. But as I am getting more advanced requirements, I am reaching the limits to my thinking on how to make a generic design which will work in most common scenarios. Here is a class level design of what I have made:



Here StockDepth is a class with two treemaps BidDepth and AskDepth(reverse sorted). The key to maps is stock price and value is a custom object containing volume. Would someone please tell me how I can improve it to add positions (index) and faster updation and access time using core java. One of the requirements I have is that when I find a matching stock, I have to send this information to a service too. That is even more hard to workout, as the place I am matching the stocks, I only have price and quantity, no stock information.

Thanks
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:


Here StockDepth is a class with two treemaps BidDepth and AskDepth(reverse sorted). The key to maps is stock price and value is a custom object containing volume. Would someone please tell me how I can improve it to add positions (index) and faster updation and access time using core java. One of the requirements I have is that when I find a matching stock, I have to send this information to a service too. That is even more hard to workout, as the place I am matching the stocks, I only have price and quantity, no stock information.


Hello, Ravi -- here are some questions to help move this along.

Is this a learning exercise to help develop basic java skills, or do you anticipate building an industrial-strength financial application? If you're just here to learn, you're in the right place.

How strong is your understanding of the problem domain -- i.e. practical knowledge of how electronic trading works? The deeper the better -- if you're not already very up to speed (well beyond the info contained on the linked Wikipedia page), that is a good place to start increasing your knowledge. It might change how you draw your entity diagram. For example, I have basic knowledge of the business needs of simple capital markets software, and I am not intuitively clear about the difference between the InstrumentDepth and StockDepth entities. A StockDepth feels like a type of InstrumentDepth. You may not need both; or if you need both, you might also need entities representing Currency, FixedIncome, Commodity, and other non-Equity tradable instruments.

What is the significance of the word "Depth" in the class names?

Here StockDepth is a class with two treemaps BidDepth and AskDepth(reverse sorted). The key to maps is stock price and value is a custom object containing volume.

If the custom object just represents volume, it's definitely missing information -- i.e. the account (investor) that is making the ask or bid at that price level and volume. Is that already part of your design? You'll need it when you think about representing positions, unless I am misunderstanding things.

Very minor point: you say they are "treemaps", but I am reading that they are "sorted maps". Forgive me for picking a nit, but from a design point of view, you have decided you need maps whose price key is sorted (i.e. java.util.SortedMap<Price, CustomObject> interface) -- and you create them using the java.util.TreeMap concrete class. Again, that's assuming I haven't misunderstood -- tell me if I have.

Something I highly recommend even if this is just for fun -- don't use the double (or Double) data type to represent prices or money, because of limits to the precision of floating point data classes. It's a bad practice and you should avoid falling into that habit. Do a little research to select a good library class for money, or just use java.math.BigDecimal.

Let us know how this is going. And thumbs up for posting about this at a design level rather than throwing code at us.

--Scott
 
s ravi chandran
Ranch Hand
Posts: 561
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Scott. I have actually understood the business usecase of Market Data Book while working on this excercise. This is for learning purpose(finance domain) and with that knowledge I will create a design which should work in environment similar to actual scenario ( hopefully :-)) . Now I know how it should work, but not completely. For instance, matching of stock can happen at many levels, it can be based on price alone, price and quantity, quantity and range of price(spreads). Many other behaviors based on strategies, but I do not want to complicate my current task with too much of advanced behaviors.

Now talking about the code, here InstrumentDepth represents Asset Classes like Interest Rate Swaps, Bond, Treasuries, FX etc. StockDepth represents stocks from one of the asset classes like I can have treasury note or treasury bill. Depth for me represents the entities being stored in particular position based on one of the criteria.

The custom object ( key) is having price and volume. I am using BigDecimal for price and double for volume. Volume is not being used for sorting, I thought of it, but did not get how to use price and quantity both to sort. Eventually I will take a pre-defined depth of data from each side, but that will be after I have something concrete. The technical part of using interface in reference variables and parameters is taken care of.

Let me know any other information that is missing.
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! I see you were already way ahead of me, apologies for going to such a basic level of commentary. And I totally agree with your learning strategy of not complicating your design with advanced business logic up front.

Thanks for clarifying the terms and design. At this point, is the price-based matching functional? Whether or not it is working, could you walk us through the algorithm of matching a Bid and Ask?

Have you envisioned another data structure to store positions (investor account holdings)?

--Scott
 
s ravi chandran
Ranch Hand
Posts: 561
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic mechanism is functional . Here is the working.
I will be getting a market snapshop at the service start. I will load the snapshot data into the market data book BID and ASK side based on instrumentId and price. Now when I get an stock quote from some client, I will match against the counter side ( Bid/Ask ) and if a match is found, I will cross it and updated the balance volume for the market data. Now the BID tree is sorted ascending and ASK tree is sorted descending. So when I have to match the counter party request, I will take the first entry from the opposite BID/ASK tree and match it. If a match is 100%, I will remove that stock from the book. If there is no match, I will add it to the book for future matching. Here I do not have positions information, also I do not have information as to which stock matched the current request. I do have the stock information in another book storing all the stocks process by market data book.

This is the only data structure I was able to come up with, as of now I am not able to come up with a better data structure using just java library.
 
s ravi chandran
Ranch Hand
Posts: 561
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any change required at my end in terms of design?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!