Exemplo n.º 1
0
def beam_search(root, k, game_length):  # k is the beam size
    current_q = [root]  # we will iterate through this to create child
    next_q = []  # we will que children nodes here
    df = data.tail(game_length + 1).copy()
    count = 0
    result = []
    for level in range(game_length):
        stockObject = p.Stock('AAPL', df.iloc[level, 0])

        #  stockObject = p.Stock('AAPL', df.iloc[game_length,0]) --> this is funny bug

        # print("level:",game_length)

        # print("open price on given day:",df.iloc[level,0])

        while current_q:
            node = current_q.pop()

            for act in ['b', 's', 'h']:  # buy sell hold
                shares = int(root.five_percent / float(df.iloc[level][0]))
                if doable(act, stockObject, node, shares):
                    count += 1
                    # print("action and shares",act,shares)
                    child = perform_action(act, node.player, stockObject,
                                           shares)
                    node.children.append(child)
                    next_q.append(child)

        if level != game_length - 1:
            current_q = take_beams(k, next_q, level)

        if level == game_length - 1:

            final_result = take_beams(1, next_q, level)
            # TotalShares = sum(playerObj.portfolio.values())
            TotalCash = final_result[0].player.cash + \
                        (final_result[0].player.portfolio['AAPL'] * df.iloc[game_length, 0])

            print("Cash", final_result[0].player.cash)
            print("Portfolio", final_result[0].player.portfolio)
            print("Last day Open Price", df.iloc[game_length, 0])
            print("Total Cash Value", TotalCash)
            print("Total Nodes Processed", count)
            if TotalCash > root.starting_cash:
                print(
                    f"Wow! our AI made profit of:{TotalCash - root.starting_cash}\n"
                )
            elif TotalCash < root.starting_cash:
                print(
                    f"Oh No! our AI lost :{root.starting_cash - TotalCash}\n")
            else:
                print(f"No profit/ loss\n")

            # return take_beams(1, next_q, level)  # in the end of the game we will take the best node
            result.append((TotalCash - root.starting_cash))
            result.append(count)
            return result
        next_q = []
Exemplo n.º 2
0
def basic_AI(root, k, game_length):  # k is the beam size
    current_q = [root]  # we will iterate through this to create child
    next_q = []  # we will que children nodes here
    df = data.tail(game_length+1).copy()
    count = 0
    for level in range(game_length):
        stockObject = p.Stock('AAPL', df.iloc[level,0])
        while current_q:
            node = current_q.pop()
            for act in ['b', 's', 'h']:  # buy sell hold
                shares = int(root.five_percent/float(df.iloc[level][0]))
                if doable(act, stockObject, node, shares):
                    count+=1
                    # new_node_obj = Node(action = act, player = p.Player(name = 'bob', money = copy.copy(node.player.cash), portf = copy.copy(node.player.portfolio)))
                    child = perform_action(act, node.player, stockObject, shares)
                    node.children.append(child)
                    next_q.append(child)

        if level != game_length-1:
            current_q = random_take_beam(k, next_q, level)
            
        if level == game_length-1:
            final_result = random_take_beam(1, next_q, level)

            # TotalShares = sum(playerObj.portfolio.values())
            TotalCash = final_result[0].player.cash + \
                        (final_result[0].player.portfolio['AAPL'] * df.iloc[game_length, 0])

            print("Cash", final_result[0].player.cash)
            print("Portfolio", final_result[0].player.portfolio)
            print("Last day Open Price", df.iloc[game_length, 0])
            print("Total Cash Value", TotalCash)
            print("Total No of nodes processed:",count)
            if TotalCash > root.starting_cash:
                print(f"Wow! baseline AI made profit of:{TotalCash - root.starting_cash}\n")
            elif TotalCash < root.starting_cash:
                print(f"Oh No! baseline AI lost :{root.starting_cash - TotalCash}\n")
            else:
                print(f"No profit/  loss\n")

            return (TotalCash - root.starting_cash)  # in the end of the game we will take one node randomly
        next_q = []
Exemplo n.º 3
0
                f"Player your Shares : {playerObj.portfolio} your Cash: {playerObj.cash}\n"
            )

            print(
                f"Day {turns+1} apple stock: Open-->{float(df.iloc[turns][0])} \n"
            )
            print(f"High-->{float(df.iloc[turns][1])} \n")
            print(f"Low-->{float(df.iloc[turns][2])} \n")
            print(f"Close-->{float(df.iloc[turns][3])} \n")
            print(f"Volume-->{float(df.iloc[turns][4])} \n")

            print("4 things you can do")
            print("1. Buy(B/b)  2. Sell(S/s) 3. Hold(H/h) 4. Exit(E/e) \n")
            choice = input("what you want to do?!!!!\n")
            #str1 = 'Apple'+str(turns)
            stockObject = p.Stock('Apple', df.iloc[turns, 0])

            if choice == 'S' or choice == 's':
                #stck = input("which stock you want to sell?")
                n_shares_sold = int(
                    input("How many shares you want to sell--> "))
                if n_shares_sold > playerObj.portfolio['Apple']:
                    print(
                        f"Sorry you dont own that many stocks, Try again!!! \n"
                    )
                    continue
                playerObj.sell(stockObject, int(n_shares_sold), 'Apple')

            elif choice == 'B' or choice == 'b':
                n_shares_bought = int(
                    input("How many shares you want to buy--> "))
Exemplo n.º 4
0
def beam_search(root, k, game_length):  # k is the beam size
    current_q = [root]  # we will iterate through this to create child
    next_q = []  # we will que children nodes here
    df = data.tail(game_length + 1).copy()
    df_nn = data_nn.tail(game_length + 1).copy()
    # b, h, s = data_nn['b'], data_nn['h'], data_nn['s']
    # hot = np.column_stack((b, h, s))
    # row_nn = hot[-level - 1]
    # nn_action = ''
    count = 0
    y_targs = []
    for level in range(game_length):
        stockObject = p.Stock('AAPL', df.iloc[level, 0])

        while current_q:
            node = current_q.pop()

            for act in ['b', 's', 'h']:  # buy sell hold
                # try:
                shares = int(root.five_percent / float(df.iloc[level][0]))
                # except ValueError:

                if doable(act, stockObject, node, shares):
                    count += 1
                    new_node_obj = Node()
                    child = perform_action(act, node.player, stockObject,
                                           shares)
                    node.children.append(child)
                    next_q.append(child)

        if level != game_length - 1:
            current_q = take_beams(k, next_q, level)
            print(current_q)
            y_targs.append(current_q[0].action)
        if level == game_length - 1:

            final_result = take_beams(1, next_q, level)
            y_targs.append(final_result[0].action)
            print(final_result)

            # TotalShares = sum(playerObj.portfolio.values())
            TotalCash = final_result[0].player.cash + \
                        (final_result[0].player.portfolio['AAPL'] * df.iloc[game_length, 0])

            print("Cash", final_result[0].player.cash)
            print("Portfolio", final_result[0].player.portfolio)
            print("Last day Open Price", df.iloc[game_length, 0])
            print("Total Cash Value", TotalCash)
            print("Total no of nodes Processed:", count)

            if TotalCash > root.starting_cash:
                print(
                    f" Wow! our AI made profit of:{TotalCash - root.starting_cash}\n"
                )
            elif TotalCash < root.starting_cash:
                print(
                    f"Oh No! our AI lost :{root.starting_cash - TotalCash}\n")
            else:
                print(f"No profit/ loss\n")

            # return take_beams(1, next_q, level)  # in the end of the game we will take the best node
            # return (TotalCash - root.starting_cash)
        next_q = []
    return y_targs