문제

A database has two tables: one for stocks (equities), and one for options. These are separate table because they have different information. For example, the stocks table has a ticker symbol column, and a (primary) exchange column; the options table has a ticker symbol column, a FK column pointing to the equities table, a strike price column, an expiry date column, and a right (put/call) column. I think it is a good idea to separate them in two tables, though this is open for debate.

I need to store trades (or orders, or transactions) in the database. A single trade is like "Buy 100 shares of IBM at $200 on 2012-04-14. A trade can involve either an equity or an option. Should trades be stored as one table, with one column that specifies whether the trade involves a stock or an option, and another column that is a FK that points either to the stocks table or the options table? Or should there be two trades tables, one for trades involving stocks and one for trades involving options?

Later on, I will also need to add a positions table. A simple position would be composed of two trades, such as buy 100 shares of IBM today and sell 100 shares of IBM tomorrow. However, it could also be more complex, involving both options and stocks (a covered call, for example). It seems that if I do two tables for trades, then I face the same difficulty designing the positions table that I would if I implemented a single trades table: I would need a foreign key that points either to the stock trades table or the option trades table.

This makes me feel like there should be a single instruments table, that contains both stocks and options. However, the information required for stocks is so different than options (see first paragraph), that this also feels wrong. A row containing a stock would have all the option-specific columns null.

How should I design this database?

도움이 되었습니까?

해결책

You should probably consider making STOCKs and OPTIONs subtypes of a new table: INSTRUMENT. When you trade or record a position, it would be with respect to the supertype INSTRUMENT.

This lets you keep distinct tables for your stocks and options, which is sensible since they have different attributes. At the same time, it lets you work in a sensible way with a single set of transactions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top