def test_buy(self):  #buy is trade withe positive quantity
     acct = Account.from_pk(jimid)
     result = acct.trade("aapl", 3, 100)  #result is mv of trade
     result2 = acct.trade("pton", 1, 100)
     trade_hist = Trade.from_account_and_ticker(jimid, "aapl")
     trade_hist2 = Trade.from_account_and_ticker(jimid, "pton")
     pos_test = Position.from_account_and_ticker(jimid, "aapl")
     quant = pos_test.total_quantity
     self.assertAlmostEqual(result, 300, "mv = px * quantity")
     self.assertAlmostEqual(
         acct.balance, 600,
         "balance started at 1000 trades cost 400 so new bal should be 600")
     self.assertEqual(
         len(trade_hist), 2,
         "there shouold only be 2 trades with aapl one trade here and one from seed"
     )
     self.assertEqual(
         len(trade_hist2), 1,
         "there shouold only be 1 trade with pton from here and one from seed"
     )
     self.assertAlmostEqual(
         quant, 8,
         "seed gave 5 shs of aapl and this test bought 3 total position is now 8"
     )
     with self.assertRaises(
             InsufficientFundsError
     ):  #"tring buy shs that cost more then your balance causes an error"
         result3 = acct.trade("pton", 500, 100)
 def test_sell(self):
     #sell is the same function, trade, as buy with negtive quantity
     # it has opposite effect on cash balance and position
     acct = Account.from_pk(jimid)
     result = acct.trade("aapl", -2, 100)  #result is mv of trade
     pos_test = Position.from_account_and_ticker(jimid, "aapl")
     quant = pos_test.total_quantity
     trade_hist = Trade.from_account_and_ticker(jimid, "aapl")
     self.assertAlmostEqual(result, 200, "mv = quantity * px")
     self.assertAlmostEqual(
         quant, 3,
         "seed gave 5 shs of aapl and this test sold 2; total position is now 3"
     )
     self.assertAlmostEqual(acct.balance, 1200,
                            "started with 1000 and sell give +200")
     self.assertEqual(
         len(trade_hist), 2,
         "there shouold only be 2 trades with aapl one trade here and one from seed"
     )
     with self.assertRaises(
             InsufficientSharesError
     ):  #"tring sell shs that you don't have causes an error"
         result3 = acct.trade("pton", -10, 100)
     with self.assertRaises(
             InsufficientSharesError
     ):  #"tring sell more shs than you have causes an error"
         result3 = acct.trade("aapl", -20, 100)
Exemple #3
0
 def trade(self, ticker, quantity, price=None):
     quantity = int(quantity)
     quote = get_quote(ticker)
     if price == None:
         if quote['iexAskPrice'] != 0 and quote[
                 'iexAskPrice'] is not None:  ##added the or
             price = float(quote['iexAskPrice'])
         else:
             price = float(quote['latestPrice'])
     else:  #price != None
         price = price
     market_value = price * quantity
     position = Position.from_account_and_ticker(self.pk, ticker)
     if market_value > 0:  #BUY
         if self.balance < market_value:
             # return None
             raise InsufficientFundsError
         else:
             self.balance -= market_value
             self.save()
     else:  #SELL so market_value < 0
         if position.total_quantity < (quantity * -1):  #
             raise InsufficientSharesError
             # return None
         else:  #where we have more shares than whe are selling
             self.balance += market_value * -1
             self.save()
     position.total_quantity += quantity  #position.quantity is total quantity
     position.save()
     # now, create a new Trade object
     trade = Trade(ticker=ticker,
                   account_pk=self.pk,
                   quantity=quantity,
                   price=price)
     trade.save()
     return abs(market_value)  #had return True
 def test_from_account_and_ticker(self):
     result = Position.from_account_and_ticker(jimid, 'aapl')
     self.assertEqual(result.ticker,'aapl',"there was one position added by seed")
     self.assertEqual(result.total_quantity,5)
     self.assertIsInstance(result,Position,"func returns position object")