diff --git a/README.md b/README.md index e49fc04..acd4530 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/src/interface.py b/src/interface.py index 65d331e..7c0c513 100644 --- a/src/interface.py +++ b/src/interface.py @@ -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 \ No newline at end of file