def test_route_get_ticker_price(self): ticker = 'tsla' price = util.lookup_price('tsla') response = requests.get( "http://127.0.0.1:5000/api/get_ticker_price/{}".format(ticker)) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'ticker price is ': price})
def sell(self, ticker, amount): """ make a sale! raise KeyError for a non-existent Position and ValueError for insufficient shares. will create a new Trade object, modify a Position, and alter the self.balance. returns nothing.""" ticker = ticker.lower() amount = int(amount) try: ticker_price = util.lookup_price(ticker) position = self.get_position_for(ticker) if position.values['shares'] < amount: raise ValueError else: position.values['shares'] -= amount position.save() transaction = t.Trade(buy_sell='Sell', username=self.values['username'], ticker=ticker, price=ticker_price, shares=amount, time=time()) transaction.save() self.values['balance'] += (ticker_price * amount) self.update_row() except: raise KeyError
def buy(self, ticker, amount): """ make a purchase! raise KeyError for a nonexistent stock and ValueError for insufficient funds. will create a new Trade and modify a Position and alters the user's balance. returns nothing """ ticker = ticker.lower() amount = int(amount) try: ticker_price = util.lookup_price(ticker) if (ticker_price * amount) > self.values['balance']: raise ValueError else: self.values['balance'] = (self.values['balance'] - ticker_price * amount) self.save() transaction = t.Trade(buy_sell='Buy', username=self.values['username'], ticker=ticker, price=ticker_price, shares=amount, time=time()) transaction.save() position = self.get_position_for(ticker) # position.values['shares'] = (position.values['shares'] + amount) position.save() except: raise KeyError
def profit_loss(self, ticker): buy_mkt_value = self.buy_market_value(ticker) sell_mkt_value = self.sell_market_value(ticker) net_mkt_value = sell_mkt_value - buy_mkt_value buy_mkt_volume = self.buy_trade_volume(ticker) sell_mkt_volume = self.sell_trade_volume(ticker) net_mkt_volume = buy_mkt_volume - sell_mkt_volume if net_mkt_volume > 0: return (net_mkt_volume * util.lookup_price(ticker)) + net_mkt_value else: return net_mkt_value
def test_sell(self): user = Account.one_from_pk(1) user_balance_before = user.values['balance'] user.sell('tsla', 1) user_balance_after = (user_balance_before + util.lookup_price('tsla')) self.assertEqual( user.values['balance'], user_balance_after, 'account balance increased for ticker_price * amount ') transaction = user.trades_for('TSLA') self.assertIsInstance(transaction[2], Trade, 'trades_for returns Trade objects') self.assertEqual(transaction[2].values['buy_sell'], 'Sell') self.assertEqual(transaction[2].values['shares'], 1) self.assertEqual(transaction[2].values['username'], 'sami') account_position = user.get_position_for('TSLA') self.assertIsInstance(account_position, Position, 'get_position_for returns Position object') self.assertEqual(account_position.values['shares'], 4) self.assertEqual(account_position.values['username'], 'sami')
def test_buy(self): user = Account.one_from_pk(1) user_balance_before = user.values['balance'] user.buy('AAPL', 1) user_balance_after = (user_balance_before - util.lookup_price('aapl')) self.assertEqual( user.values['balance'], user_balance_after, 'buy if account balance > ticker_price * shares, account balance deducted' ) transaction = user.trades_for('aapl') self.assertIsInstance(transaction[0], Trade, 'trades_for returns Trade objects') self.assertEqual(transaction[0].values['buy_sell'], 'Buy') self.assertEqual(transaction[0].values['shares'], 1) self.assertEqual(transaction[0].values['username'], 'sami') account_position = user.get_position_for('aapl') self.assertIsInstance(account_position, Position, 'get_position_for returns Position object') self.assertEqual(account_position.values['shares'], 1) self.assertEqual(account_position.values['username'], 'sami')
def get_ticker_price(ticker): ticker_price = lookup_price(ticker) return jsonify({'price': ticker_price})
def start(): selection = 0 while selection != 3: selection = v.mainmenu() if selection == 1: #Create Account user_name, password, first_name, last_name = v.create_account() #fix with try and except new_account = a.Account(username = user_name, balance = 10000, first = first_name, last = last_name) new_account.set_password(password) new_account.generate_api_key() v.create_acc_success(new_account.values['username']) elif selection == 2: #Log In user_name, pword_hash = v.login_credentials() account = a.Account() activated = account.login(username = user_name, password = pword_hash) if account != None: login_selection = 0 while login_selection != 7: login_selection = v.account_options(activated.values['username']) if login_selection == 1: #Check Balance v.show_balance(activated.values['balance']) elif login_selection == 2:#check all stocks in portfolio v.show_portfolio(activated.get_positions()) elif login_selection == 3:#check trade history v.show_all_trades(activated.get_trades()) #<-- change elif login_selection == 4:#buy stocks # v.buy_stock(activated) ticker = v.choose_stock() try: ticker_price = util.lookup_price(ticker) except: v.invalid_ticker() raise KeyError amount = v.num_of_shares(ticker, ticker_price) amount = int(amount) balance_before = activated.values['balance'] total = (amount * ticker_price) if balance_before < ticker_price * amount: v.insufficient_funds() else: confirmation = v.confirm(ticker, amount, total) if confirmation == 'Y' or confirmation == 'y': activated.buy(ticker, amount) if (balance_before - total) == activated.values['balance']: v.purchase_success() elif login_selection == 5:#sell stocks ticker = v.choose_stock() try: ticker_price = util.lookup_price(ticker) except: v.invalid_ticker() raise KeyError amount = v.num_of_shares(ticker, ticker_price) amount = int(amount) balance_before = activated.values['balance'] total = (amount * ticker_price) if activated.get_position_for(ticker)['shares'] == 0: #look for ticker v.insufficient_shares() #Ticker not found. else: confirmation = v.confirm(ticker, amount, total) if confirmation == 'Y' or confirmation == 'y': activated.sell(ticker, amount) if (balance_before + total) == activated.values['balance']: v.sale_success() elif login_selection == 6: #VIEW API KEY v.view_api_key(activated.values['api_key']) else: print("INVALID CREDENTIALS/LOGIN ERROR")###remove/change later