Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ if your manager determines the performance is too lacking, you will be asked to
Please include the following when you are writing your PR:
General things:
1. What is the purpose of this PR?
complete last level
2. What changes did you make? Why?
compiled the system.

This PR Specific:
1. How did you design your system?
basically used dict to store and retrive data. seems very basic
2. What did you struggle with?
n/a
3. Is there anything you would change about this step?
instructios seem vague?
43 changes: 33 additions & 10 deletions src/interface.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
class Exchange:
# implement this!
class TradeObject:

def __init__(self, initialBalance):
"""Initial Balance is the amount that each account should start with."""
pass
def __init__(self, seller, buyer, ticker, price, amount):
self.seller = seller #str
self.buyer = buyer #str
self.ticker = ticker #str
self.price = price #price > 0
self.amount = amount #amount > 0

def add_trade(self, trade):
"""Adds a trade to the exchange (validation required)
and returns a match if required. It is up to you on how you will
handle representing trades. """
raise NotImplementedError
class ExchangeUser:

def __init__(self, usrName = 'testname'):
self.holdings = {} # ticker: (amount, avgPrice)
self.name = usrName

def add_trade(self, trade:TradeObject):
ticker = trade.ticker
if self.name == trade.buyer:
if ticker in self.holdings:
#calculates the avg cost after buying at different prices.
self.holdings[ticker] = (self.holdings[ticker][0] + trade.amount, (self.holdings[ticker][1] * self.holdings[ticker][0] + trade.price * trade.amount) / (self.holdings[ticker][0] + trade.amount))
if self.holdings[ticker][0] == 0:
del self.holdings[ticker]
else:
self.holdings[ticker] = (trade.amount, trade.price)

elif self.name == trade.seller:
if ticker in self.holdings:
self.holdings[ticker] = (self.holdings[ticker][0] - trade.amount, self.holdings[ticker][1])
if self.holdings[ticker][0] == 0:
del self.holdings[ticker]
else:
self.holdings[ticker] = (-trade.amount, trade.price)

def getHoldings(self):
return self.holdings