Object oriented design for an investment/stock and options portfolio in Python

I'm a beginner/intermediate Python programmer but I haven't written an application, just scripts. I don't currently use a lot of object oriented design, so I would like this project to help build my OOD skills. The problem is, I don't know where to start from a design perspective (I know how to create the objects and all that stuff). For what it's worth, I'm also self taught, no formal CS education.

I'd like to try writing a program to keep track of a portfolio stock/options positions.

I have a rough idea about what would make good object candidates (Portfolio, Stock, Option, etc.) and methods (Buy, Sell, UpdateData, etc.).

A long position would buy-to-open, and sell-to-close while a short position has a sell-to-open and buy-to-close.

portfolio.PlaceOrder(type="BUY", symbol="ABC", date="01/02/2009", price=50.00, qty=100)
portfolio.PlaceOrder(type="SELL", symbol="ABC", date="12/31/2009", price=100.00, qty=25)
portfolio.PlaceOrder(type="SELLSHORT", symbol="XYZ", date="1/2/2009", price=30.00, qty=50)
portfolio.PlaceOrder(type="BUY", symbol="XYZ", date="2/1/2009", price=10.00, qty=50)

Then, once this method is called how do I store the information? At first I thought I would have a Position object with attributes like Symbol, OpenDate, OpenPrice, etc. but thinking about updating the position to account for sales becomes tricky because buys and sells happen at different times and amounts.

  • Buy 100 shares to open, 1 time, 1 price. Sell 4 different times, 4 different prices.
  • Buy 100 shares. Sell 1 share per day, for 100 days.
  • Buy 4 different times, 4 different prices. Sell entire position at 1 time, 1 price.

A possible solution would be to create an object for each share of stock, this way each share would have a different dates and prices. Would this be too much overhead? The portfolio could have thousands or millions of little Share objects. If you wanted to find out the total market value of a position you'd need something like:

sum([trade.last_price for trade in portfolio.positions if trade.symbol == "ABC"])

If you had a position object the calculation would be simple:

position.last * position.qty

Thanks in advance for the help. Looking at other posts it's apparent SO is for "help" not to "write your program for you". I feel that I just need some direction, pointing down the right path.

ADDITIONAL INFO UPON REFLECTION The Purpose Программа будет отслеживать все позиции, как открытые, так и закрытые; с возможностью видеть подробную информацию о прибылях и убытках.

Когда я думаю о подробных отчетах о прибылях и убытках, я хочу видеть ... - все открытые даты (и закрытые) - время проведено - цена открытия (дата закрытия) - Прибыли и убытки с момента открытия - P&L per day

@Senderle

I think perhaps you're taking the "object" metaphor too literally, and so are trying to make a share, which seems very object-like in some ways, into an object in the programming sense of the word. If so, that's a mistake, which is what I take to be juxtapose's point.

This is my mistake. Thinking about "objects" a share object seems natural candidate. It's only until there may be millions that the idea seems crazy. I'll have some free coding time this weekend and will try creating an object with a quantity.

7
задан Jason Wirth 30 March 2011 в 01:14
поделиться