def seed(dbpath=DBPATH): ORM.dbpath = dbpath mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password('password') mike_bloom.api_key = '00000' mike_bloom.save() buy_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=10, price=100.0) sell_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=-5, price=200.0) buy_trade.save() sell_trade.save() tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk) appl_position = Position(ticker='aapl', shares=5, accounts_pk=mike_bloom.pk) tsla_position.save() appl_position.save()
def testBuy(self): alex = Account(username="******", password_hash="password", balance=20000000, first_name="Alex", last_name="C", email="*****@*****.**") alex.save() # Tests buy for an already existing position stok_position = Position(ticker="STOK", shares=100, account_id=alex.id) stok_position.save() alex.buy(ticker="STOK", volume=100) check_stok = Position.from_account_id_and_ticker(account_id=alex.id, ticker="STOK") self.assertEqual(check_stok.shares, 200, msg="buy() should increase position shares") # Tests buy trade for a position that doesn't exist alex.buy(ticker="P2P", volume=101) check_p2p = Position.from_account_id_and_ticker(account_id=alex.id, ticker="P2P") self.assertEqual(check_p2p.shares, 101, "buy() should increase position shares") # Test bad ticker symbol with self.assertRaises(NoSuchTickerError, msg="buy() should raise NoSUchTickerError"): alex.buy(ticker="xyz1234", volume=100)
def seed(dbpath=DBPATH): ORM.dbpath = dbpath mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password('password') mike_bloom.save() # trade for a purchase of 10 shares yesterday # trade for a sell of 5 shares today tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk) tsla_position.save() ms_position = Position(ticker='ms', shares=10, accounts_pk=mike_bloom.pk) ms_position.save() tsla_trade = Trade( ticker='tsla', volume=5, price=95.20, time=time.time(), accounts_pk=mike_bloom.pk, ) tsla_trade.save() ms_trade = Trade( ticker='ms', volume=10, price=25.50, time=time.time(), accounts_pk=mike_bloom.pk, ) ms_trade.save()
def testFromId(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() apple_id = apple.id apple2 = Position.from_id(apple_id) self.assertEqual( apple2.ticker, "AAPL", "Data should be loaded from database with .from_id()") orange = Position.from_id(100000000000) self.assertIsNone(orange, ".from_id() returns None for nonexistent row")
def testSaveInsert(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() self.assertIsNotNone(apple.id, "save should set an id value for new input") with sqlite3.connect(DBPATH) as connection: cursor = connection.cursor() SQL = "SELECT * FROM positions WHERE shares=100;" cursor.execute(SQL) rows = cursor.fetchall() self.assertEqual(len(rows), 1, "save should create 1 new row in the database")
def testDelete(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() orange = Position(ticker="ORNG", shares=150, account_id=3) orange.save() apple.delete() with sqlite3.connect(DBPATH) as connection: cursor = connection.cursor() SQL = "SELECT * FROM positions;" cursor.execute(SQL) rows = cursor.fetchall() self.assertEqual(len(rows), 1, "delete should delete 1 row in the database")
def seed(dbpath=DBPATH): ORM.dbpath = dbpath print("DATABASE: ", dbpath) mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password("password") print(mike_bloom.dbpath) mike_bloom.save() tsla_position = Position(ticker='tsla', shares=5, account_pk=mike_bloom.pk) tsla_position.save() fake_trade = Trade(ticker='tsla', quantity=2, type=1) fake_trade.save()
def testSaveUpdate(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() apple_id = apple.id apple2 = Position.from_id(apple_id) apple2.shares = 150 apple2.save() self.assertEqual(apple2.id, apple_id, "update should not change ID number") apple3 = Position.from_id(apple_id) self.assertEqual(apple3.ticker, "AAPL", "save update should keep same ticker if not changed") self.assertEqual(apple3.shares, 150, "save updates should increase share amt by 50") self.assertEqual( apple3.account_id, 3, "save should keep the same account_id if not changed")
def testSell(self): caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() # Tests sell for a position with SUFFICIENT shares a33a_position = Position(ticker="A33A", shares=200, account_id=caroline.id) a33a_position.save() caroline.sell(ticker="A33A", volume=100) check_a33a = Position.from_account_id_and_ticker( account_id=caroline.id, ticker="A33A") self.assertEqual(check_a33a.shares, 100, "sell() should decrease position shares") # Tests sell for a position with INSUFFICIENT shares stok_position = Position(ticker="STOK", shares=100, account_id=caroline.id) stok_position.save() with self.assertRaises( InsufficientSharesError, msg="sell() should raise InsufficicentSharesError"): caroline.sell(ticker="STOK", volume=200) # Tests sell trade for a position that doesn't exist with self.assertRaises( InsufficientSharesError, msg="sell() should raise InsufficicentSharesError"): caroline.sell(ticker="P2P", volume=202) # Test bad ticker symbol with self.assertRaises(NoSuchTickerError, msg="sell() should raise NoSUchTickerError"): caroline.sell(ticker="xyz1234", volume=100)
def get_positions(api_key): account = Account.api_authenticate(api_key) if account: res = Position.all_shares_accounts(account.pk) for pos in res: equ = pos[2] pk = pos[3] ticker = pos[0] number_shares = pos[1] equ = 0 equ = get_price_of_ticker(ticker) * int(number_shares) New_positions = Position(pk, account.pk, ticker, number_shares, equ) New_positions.save() return jsonify({ "Positions": account.get_my_positions(), "Positions_for_graph": New_positions.positions_for_graph(account.pk) }) return jsonify({'error': 'invalid key'})
def testPosition(self): position = Position(ticker_symbol="NYSE:T", number_of_share=200, account_pk=8) position.save() self.assertEqual(position.ticker_symbol, "NYSE:T") self.assertEqual(position.number_of_share, 200) self.assertEqual(position.account_pk, 8) position = Position(pk=1, ticker_symbol="NYSE:ACB", number_of_share=300, account_pk=2) position.save() self.assertEqual(position.ticker_symbol, "NYSE:ACB") self.assertEqual(position.number_of_share, 300) self.assertEqual(position.account_pk, 2) position = Position(pk=4) position.delete() self.assertEqual(position.ticker_symbol, None) position = Position().one_from_pk(pk=1) self.assertEqual(position.ticker_symbol, "NYSE:ACB") self.assertEqual(position.number_of_share, 300) self.assertEqual(position.account_pk, 2) position = Position().all_from_where_clause() self.assertEqual(position[1].ticker_symbol, "NASDAQ:EA") self.assertEqual(position[1].number_of_share, 100) self.assertEqual(position[1].account_pk, 1) position = Position().all_from_where_clause() self.assertEqual(position[2].ticker_symbol, "NYSE:T") self.assertEqual(position[2].number_of_share, 200) self.assertEqual(position[2].account_pk, 8)
def testAllFromAccountId(self): apple = Position(ticker="AAPL", shares=0, account_id=3) orange = Position(ticker="ORNG", shares=100, account_id=3) banana = Position(ticker="BANA", shares=100, account_id=3) apple.save() orange.save() banana.save() account_id = apple.id all_account_data = Position.all_from_account_id(account_id) results = [] print(all_account_data) self.assertEqual( all_account_data, results, "function should spit out all positions for account id where shares >0" ) pass
def testAll(self): apple = Position(ticker="AAPL", shares=100, account_id=3) orange = Position(ticker="ORNG", shares=150, account_id=3) banana = Position(ticker="BANA", shares=100, account_id=3) apple.save() orange.save() banana.save() all_data = Position.all() self.assertEqual(len(all_data), 3, "all data should return 3 rows of data") self.assertEqual(all_data[0].ticker, "AAPL", "all function should return all position data") self.assertEqual(all_data[1].ticker, "ORNG", "all function should return all position data") # self.assertEqual(apple.shares, 100, "save updates should increase share amt by 50") # self.assertEqual(apple.account_id, 3, "save should keep the same account_id if not changed") # self.assertEqual(all_data, result, "....") pass
def testSave(self): position = Position(ticker = "aapl", number_shares = 3, account_pk=1) position.save() self.assertEqual(position.pk, 2)