def balance(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk retrieve_bal = Account(pk=pk) msg = "Your current balance = {}".format(retrieve_bal.get_account().balance) return jsonify({'message':msg})
def buy(key, ticker, shares): if Account.api_authenticate(key) == None: msg = "Invalid login credentials, pls retry" else: #pk = Account.login(name, password).pk pk = Account.api_authenticate(key).pk buy_txn = Account(pk=pk) msg = buy_txn.buy(ticker, shares) return jsonify({'message': msg})
def sell(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: if not request.json or 'ticker' not in request.json or 'volume' not in request.json: return jsonify({"error": "bad request"}), 400 pk = Account.api_authenticate(api_key).pk sell_txn = Account(pk=pk) msg = sell_txn.sell(request.json['ticker'], request.json['volume']) return jsonify({'message':msg})
def positions(api_key, ticker): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk user_position = Account(pk=pk) position = user_position.get_position_for(ticker) valuation = Position() getval = valuation.current_value(ticker, position.shares) msg = "Ticker Symbol: {}, Shares: {}, Valuation: ${}".format(position.ticker, position.shares, getval) return jsonify({'message':msg})
def deposit(key, amount): if Account.api_authenticate(key) == None: msg = "Invalid login credentials, pls retry" else: #pk = Account.login(name, password).pk pk = Account.api_authenticate(key).pk account_deposit = Account(pk=pk) new_bal = account_deposit.deposit(float(amount)) account_deposit.save() msg = "New Balance = {}".format(new_bal) return jsonify({'message': msg})
def deposit(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: if not request.json or 'amount' not in request.json: return jsonify({"error": "bad request"}), 400 pk = Account.api_authenticate(api_key).pk account_deposit = Account(pk=pk) new_bal = account_deposit.deposit(float(request.json['amount'])) account_deposit.save() msg = "New Balance = {}".format(new_bal) return jsonify({'message':msg})
def allpositions(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk user_positions = Account(pk=pk) positions = user_positions.get_positions() msg = {'positions':[]} for position in positions: valuation = Position() getval = valuation.current_value(position.ticker, position.shares) msg['positions'].append("Ticker Symbol: {}, Shares: {}, Valuation: ${}".format(position.ticker, position.shares, getval)) return jsonify({'message':msg})
def alltrades(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk user_trades = Account(pk=pk) trades = user_trades.get_trades() msg = {'trades':[]} for trade in trades: msg['trades'].append("Date/Time: {}, Ticker Symbol: {}, No. of Shares: {}, Price per Share: {}". \ format((datetime.fromtimestamp(trade.time) - \ timedelta(hours=2)).strftime('%Y-%m-%d %H:%M:%S'), \ trade.ticker, trade.volume, trade.price)) return jsonify({'message':msg})
def main_loop(): while True: choice = user_menu() if choice is None: # incorrect selection bad_input('Please select option') elif choice == '4': # exit goodbye() break elif choice == '1': login_input = login_username_pw() verified_account = Account.login(login_input) if verified_account: db_password_hash = verified_account[2] password_verify = checkpw(login_input[1].encode(), db_password_hash) if password_verify: account = Account(account_id = verified_account[0]) account.username = verified_account[1] account.balance = int(verified_account[3]) while True: choice = main_menu() if choice is None: bad_input('Please select option') elif choice == '1': # view account balance display_balance(account.balance) elif choice == '2': # deposit funds account.balance += deposit() Account.update_balance(account) elif choice == '3': # buy stock buy_stock = trade_stock() account.buy(buy_stock[0], buy_stock[1]) elif choice == '4': #sell stock buy_stock = trade_stock() account.sell(buy_stock[0], buy_stock[1]) elif choice == '5': # view positions display_positions(Account.get_positions(account)) elif choice == '6': # view trades display_trade_history(Account.get_trades(account)) elif choice == '7': # lookup price of stock ticker = view.lookup_ticker() print("Ticker: {} is currently: ${}".format(ticker, get_price(ticker))) elif choice == '8': # See API Keys display_api(Account.retrieve_api_key(account)) elif choice == '9': # logout goodbye() else: bad_input('Retry') else: bad_input('Incorrect password') else: bad_input('Incorrect username') elif choice == '2': # login with api user_api_key = login_api_key() # print(user_api_key) user = Account.api_authenticate(user_api_key) display_user_from_api_key(user) elif choice == '3': # create new account account_details[0] = "mark" account_details[1] = "1234" account_details[2] = 1000 account_details = create_account() account = Account() account.username = account_details[0] account.password_hash = account_details[1] account.balance = account_details[2] account.api_key = account.generate_api_key() Account.save(account)
def get_members(api_key,managerID): acc = Account.api_authenticate(api_key) if not acc: return jsonify(UNAUTHORIZED), 401 membersList = util.get_assigned_members(managerID) return jsonify({'members': membersList})