Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks if asset already in portfolio #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions pyrobot/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,25 @@ def add_position(self, symbol: str, asset_type: str, purchase_date: Optional[str
'purchase_date': '2020-01-31'
}
"""

self.positions[symbol] = {}
self.positions[symbol]['symbol'] = symbol
self.positions[symbol]['quantity'] = quantity
self.positions[symbol]['purchase_price'] = purchase_price
self.positions[symbol]['purchase_date'] = purchase_date
self.positions[symbol]['asset_type'] = asset_type

if purchase_date:
self.positions[symbol]['ownership_status'] = True
if symbol not in self.positions:
self.positions[symbol] = {}
self.positions[symbol]['symbol'] = symbol
self.positions[symbol]['quantity'] = quantity
self.positions[symbol]['purchase_price'] = purchase_price
self.positions[symbol]['purchase_date'] = purchase_date
self.positions[symbol]['asset_type'] = asset_type

if purchase_date:
self.positions[symbol]['ownership_status'] = True
else:
self.positions[symbol]['ownership_status'] = False
else:
self.positions[symbol]['ownership_status'] = False
orig_price = self.positions[symbol].get('purchase_price', 0.00)
orig_quant = self.positions[symbol].get('quantity', 0)
avg_price = (orig_price*orig_quant + purchase_price*quantity) / (orig_quant + quantity)
new_quant = orig_quant + quantity
self.positions[symbol]['purchase_price'] = avg_price
self.positions[symbol]['quantity'] = new_quant

return self.positions[symbol]

Expand Down