Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 def show_trades_by_account_and_ticker(self, ticker):
     trades = Trade.from_account_and_ticker(self.pk, ticker)
     if trades == []:
         view.never_traded_invalid(
         )  #TODO: run it through the quote function and include try/except for invalid ticker
     else:
         for trade in trades:
             if trade.quantity < 0:
                 trade_type = "Sell"
             else:
                 trade_type = "Buy"
             print(
                 f"Trade Type: {trade_type},  Ticker: {trade.ticker.upper()},  Quantity: {abs(trade.quantity)},  Price: ${trade.price},  Market Value: ${round(abs(trade.price*trade.quantity), 2)},  Created At: {ctime(trade.created_at)}"
             )
     print("\n")