def positions_sub_menu(pk):
    retrieve_bal = Account(pk=pk)
    views.generic_msg("Your current balance = {}".format(
        retrieve_bal.get_account().balance))
    while True:
        position_choice = views.position_menu()
        if position_choice is None:  #Bad input
            views.generic_msg(
                "Please enter a number that corresponds to a stated option")
        elif position_choice == 3:  #Exit
            break
        elif position_choice == 1:  #Retrieve and display a given position
            ticker = views.get_input("Please enter a Ticker Symbol")
            user_position = Account(pk=pk)
            position = user_position.get_position_for(ticker)
            valuation = Position()
            getval = valuation.current_value(ticker, position.shares)
            views.show_positions(position, getval)
        elif position_choice == 2:  #Retrieve and display all positions
            user_positions = Account(pk=pk)
            positions = user_positions.get_positions()
            for position in positions:
                valuation = Position()
                getval = valuation.current_value(position.ticker,
                                                 position.shares)
                views.show_positions(position, getval)
Esempio n. 2
0
def positions():
    positions = Account.get_positions(session.get('pk'))
    if positions:
        pass
    else:
        positions = "No positions available in this account"
    return render_template("positions.html", message=positions)
Esempio n. 3
0
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 logged_in_homepage(account):

    while True:
        print(account.username, account.balance)
        selection = view.logged_in_screen(account.username, account.balance)

        if selection not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
            view.improper_selection()
            time.sleep(3)

        if selection == '1':
            Account.get_positions(account)

        elif selection == '2':
            deposit = view.deposit_funds()
            if not deposit.isdigit() or int(deposit) < 1:
                view.improper_balance()
                time.sleep(3)
            else:
                account.balance = int(account.balance) + int(deposit)
                account.save()
                continue

        elif selection == '3':
            ticker_request = view.request_ticker_symbol()
            ticker_response = get_price(ticker_request)
            if type(ticker_response) == list:
                view.return_ticker_symbol_price(ticker_response)
                time.sleep(2)
            else:
                view.improper_ticker()
                time.sleep(3)

        elif selection == '4':
            ticker = view.request_ticker_symbol()
            current_price = get_price(ticker)
            share_amount = view.get_shares(current_price)
            if share_amount:
                try:
                    Account.buy(account, ticker, share_amount, current_price)
                except ValueError:
                    view.insufficient_funds()
            else:
                view.improper_ticker()
                time.sleep(3)

        # elif selection == '5':
        #   Account.get_positions(account)
        #   # time.sleep(3)
        #   ticker_to_sell = view.sell_shares()
        #   has_stock = get_price(ticker_to_sell)
        #   print(has_stock)
        #   amount = view.sell_shares_amount()
        #   current_price = get_price(ticker_to_sell)
        #   if has_stock:
        #       Account.sell(account, ticker_to_sell, amount, current_price)
        #   else:
        #       view.improper_ticker()
        #       time.sleep(3)

        elif selection == '5':
            Account.get_positions(account)
            ticker = view.request_ticker_symbol()
            shares = int(view.sell_shares())

            try:
                Account.sell(account, ticker, shares)
            except ValueError:
                view.wups()
                time.sleep(3)
            # view.clearscreen()

        elif selection == '6':
            selection = view.select_trade_option(account.username)

            if len(selection) != 1 or selection.lower() not in ['a', 'b', 'c']:
                view.improper_selection()
                time.sleep(3)
            elif selection.lower() == 'a':
                account_trades = Account.get_trades(account)
                for trade in account_trades:
                    view.show_trades(account.username, trade)
                    time.sleep(1)
            elif selection.lower() == 'b':
                ticker_symbol = view.request_ticker_symbol()
                account_trades_by_ticker = Account.trades_for(
                    account, ticker_symbol)

                if account_trades_by_ticker:
                    for trade in account_trades_by_ticker:
                        view.show_trades(account.username, trade)
                        time.sleep(3)

                else:
                    view.improper_ticker()
                    time.sleep(3)
            elif selection.lower() == 'c':
                continue
        elif selection == '7':
            view.goodbye()
            welcome_homepage()
            return
        elif selection == '8':
            view.goodbye()
            return
        elif selection == '9':
            top_traded(
                'https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,fb&types=quote,news,chart&range=1m&last=5'
            )
            time.sleep(3)
Esempio n. 5
0
def logged_in_homepage(account):
    
    while True:
        selection = view.logged_in_screen(account.username, account.balance)
        
        if selection not in ["1", "2", "3", "4", "5", "6", "7", "8"]:
            view.improper_selection()
            time.sleep(3)
        
        if selection == "1":
            view.account_positions(account.username)
            Account.get_positions(account)
            time.sleep(3)
        elif selection == "2":
            deposit = view.deposit_funds()
            if not deposit.isdigit() or int(deposit) < 1:
                view.improper_balance()
                time.sleep(3)
            else:
                account.balance = int(account.balance) +  int(deposit)
                account.save()
                continue
        elif selection == "3":
            ticker_request = view.request_ticker_symbol()
            ticker_response = get_price(ticker_request)
            if type(ticker_response) == list:
                view.return_ticker_symbol_price(ticker_response)
                time.sleep(3)
            else:
                view.improper_ticker()
                time.sleep(3)
        elif selection == "4":
            ticker = view.request_ticker_symbol()
            ticker_price = get_price(ticker)
            if ticker_price == False:
                view.improper_ticker()
                time.sleep(3)
                continue
                 
            purchase_amount = view.get_shares(ticker_price)
            if not purchase_amount.isdigit():
                view.improper_money()
                time.sleep(3)
                continue

            total_cost = int(purchase_amount) * int(ticker_price[1])
            
            if total_cost < int(account.balance):
                Account.buy(account, ticker, purchase_amount, ticker_price[1], total_cost)
            elif int(total_cost) > int(account.balance):
                view.not_enough_money()
                time.sleep(3)

        elif selection == "5":
            Account.get_positions(account)
            time.sleep(3)
            ticker_to_sell = view.sell_shares()
            has_stock = get_price(ticker_to_sell)
            position = account.get_position_for(ticker_to_sell)
            amount = view.sell_shares_amount()

            if has_stock and position.shares >= int(amount):
                Account.sell(account, ticker_to_sell, amount)
            elif not has_stock:
                view.improper_ticker()
                time.sleep(3)
            else:
                view.not_enough_shares()
        elif selection == "6":
            selection = view.select_trade_option(account.username)
            
            if len(selection) != 1 or selection.lower() not in ["a", "b", "c"]:
                view.improper_selection()
                time.sleep(3)
            elif selection.lower() == "a":
                account_trades = Account.get_trades(account)
                if not account_trades:
                    view.no_trades(account.username)
                    time.sleep(3)
                for trade in account_trades:
                    view.show_trades(account.username, trade)
                    time.sleep(3)
            elif selection.lower() == "b":
                ticker_symbol = view.request_ticker_symbol()
                account_trades_by_ticker = Account.trades_for(account, ticker_symbol)
               
                if get_price(ticker_symbol) == False:
                    view.improper_ticker()
                    time.sleep(3)
                elif not account_trades_by_ticker:
                    view.no_trades(account.username)
                    time.sleep(3)              
                else:
                    for trade in account_trades_by_ticker:
                        view.show_trades(account.username, trade)
                        time.sleep(3)
            elif selection.lower() == "c":
                continue
        elif selection == "7":
            view.goodbye()
            welcome_homepage()
            return
        elif selection == "8":
            view.goodbye()
            return
Esempio n. 6
0
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)
Esempio n. 7
0
def main():
    while True:
        selection = views.welcome_menu()
        if selection is None:
            #Bad input
            views.generic_msg(
                "Please enter a number that corresponds to a stated option")
        elif selection == 3:
            #Exit
            views.generic_msg("Bye: Thanks for using Terminal Trader")
            break
        elif selection == 1:
            #Create account
            new_username = views.get_input("Username")
            new_password = views.get_input("Password")
            new_account = Account(username=new_username)
            new_account.set_password(new_password)
            new_account.save()
        elif selection == 2:
            #Login
            login_username = views.get_input("Username")
            login_password = views.get_input("Password")
            if not Account.login(login_username, login_password):
                views.generic_msg(
                    "Failed to find an account for the given Username/Password, pls retry"
                )
            else:
                #Retrieve account balance & pk for re-use
                balance = Account()
                current_bal, pk = balance.get_bal(login_username,
                                                  login_password)
                while True:
                    choice = views.main_menu()
                    if choice is None:
                        #Bad input
                        views.generic_msg(
                            "Please enter a number that corresponds to a stated option"
                        )
                    elif choice == 7:
                        #Exit
                        break
                    elif choice == 1:  #View Balance & Positions
                        views.generic_msg(
                            "Your current balance = {}".format(current_bal))
                        while True:
                            position_choice = views.position_menu()
                            if position_choice is None:
                                #Bad input
                                views.generic_msg(
                                    "Please enter a number that corresponds to a stated option"
                                )
                            elif position_choice == 3:
                                #Exit
                                break
                            elif position_choice == 1:
                                #Retrieve and display a given position, taking in a ticker symbol
                                ticker = views.get_input(
                                    "Please enter a Ticker Symbol")
                                user_position = Account(pk=pk)
                                position = user_position.get_position_for(
                                    ticker)
                                views.show_positions(position)
                            elif position_choice == 2:
                                #Retrieve and display all positions
                                user_positions = Account(pk=pk)
                                positions = user_positions.get_positions()
                                for position in positions:
                                    views.show_positions(position)
                    elif choice == 2:  #Deposit money
                        deposit_amount = float(
                            views.get_input("Deposit Amount"))
                        account_deposit = Account()
                        new_bal = account_deposit.deposit(
                            login_username, login_password, deposit_amount)
                        account_deposit.save()
                        views.generic_msg("New Balance = {}".format(new_bal))
                    elif choice == 3:  #Look up stock price
                        ticker = views.get_input(
                            "Please enter a Ticker Symbol")
                        print(util.get_price(ticker))
                    elif choice == 4:  #Buy stock
                        ticker = views.get_input(
                            "Please enter a Ticker Symbol")
                        shares_buy = views.get_input(
                            "Please enter the number of shares to buy")

                    elif choice == 5:  #Sell stock
                        ticker = views.get_input(
                            "Please enter a Ticker Symbol")
                        shares_sell = views.get_input(
                            "Please enter the number of shares to sell")
                    elif choice == 6:  #View Trade History
                        while True:
                            trade_choice = views.trades_menu()
                            if trade_choice is None:
                                #Bad input
                                views.generic_msg(
                                    "Please enter a number that corresponds to a stated option"
                                )
                            elif trade_choice == 3:
                                #Exit
                                break
                            elif trade_choice == 1:
                                #Retrieve and display trades re a give ticker symbol
                                ticker = views.get_input(
                                    "Please enter a Ticker Symbol")
                                user_trade = Account(pk=pk)
                                trades = user_trade.get_trades_for(ticker)
                                views.show_trades(trades)
                            elif trade_choice == 2:
                                #Retrieve and display all trades
                                user_trades = Account(pk=pk)
                                trades = user_trades.get_trades()
                                for trade in trades:
                                    views.show_trades(trade)
Esempio n. 8
0
def logged_in_homepage(account):

    while True:
        selection = view.logged_in_screen(account.username, account.balance)

        if selection not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
            view.improper_selection()
            time.sleep(3)

        if selection == "1":
            Account.get_positions(account)

        elif selection == "2":
            deposit = view.deposit_funds()
            if not deposit.isdigit() or int(deposit) < 1:
                view.improper_balance()
                time.sleep(3)
            else:
                account.balance = int(account.balance) + int(deposit)
                account.save()
                continue
        elif selection == "3":
            ticker_request = view.request_ticker_symbol()
            ticker_response = get_price(ticker_request)
            if type(ticker_response) == list:
                view.return_ticker_symbol_price(ticker_response)
                time.sleep(3)
            else:
                view.improper_ticker()
                time.sleep(3)
        elif selection == "4":
            ticker = view.request_ticker_symbol()
            current_price = get_price(ticker)
            share_amount = view.get_shares(current_price)
            if share_amount:
                try:
                    Account.buy(account, ticker, share_amount, current_price)
                except ValueError:
                    view.insufficient_funds()
            else:
                view.improper_ticker()
                time.sleep(3)

        # elif selection == "5":
        #     Account.get_positions(account)
        #     # time.sleep(3)
        #     ticker_to_sell = view.sell_shares()
        #     has_stock = get_price(ticker_to_sell)
        #     print(has_stock)
        #     amount = view.sell_shares_amount()
        #     current_price = get_price(ticker_to_sell)
        #     if has_stock:
        #         Account.sell(account, ticker_to_sell, amount, current_price)
        #     else:
        #         view.improper_ticker()
        #         time.sleep(3)

        elif selection == "5":
            Account.get_positions(account)
            ticker = view.request_ticker_symbol()
            shares = int(view.sell_shares())

            try:
                Account.sell(account, ticker, shares)
            except ValueError:
                view.wups()
                time.sleep(3)
                # view.clearscreen()

        elif selection == "6":
            selection = view.select_trade_option(account.username)

            if len(selection) != 1 or selection.lower() not in ["a", "b", "c"]:
                view.improper_selection()
                time.sleep(3)
            elif selection.lower() == "a":
                account_trades = Account.get_trades(account)
                for trade in account_trades:
                    view.show_trades(account.username, trade)
                    time.sleep(1)
            elif selection.lower() == "b":
                ticker_symbol = view.request_ticker_symbol()
                account_trades_by_ticker = Account.trades_for(
                    account, ticker_symbol)

                if account_trades_by_ticker:
                    for trade in account_trades_by_ticker:
                        view.show_trades(account.username, trade)
                        time.sleep(3)

                else:
                    view.improper_ticker()
                    time.sleep(3)
            elif selection.lower() == "c":
                continue
        elif selection == "7":
            view.goodbye()
            welcome_homepage()
            return
        elif selection == "8":
            view.goodbye()
            return
        elif selection == "9":
            top_traded(
                "https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,fb&types=quote,news,chart&range=1m&last=5"
            )
            time.sleep(3)