Ejemplo n.º 1
0
def gen_tree(q, entry):
    u = u_and_d.u(q, entry)
    d = u_and_d.d(q, entry)
    day_0 = Node(0, 0, fetch.close_data(entry)[-5], 1)
    day_1_u = Node(1, 1, day_0.price * u, q)
    day_1_d = Node(2, 1, day_0.price * d, (1 - q))
    day_2_uu = Node(3, 2, day_1_u.price * u, q * q)
    day_2_ud = Node(4, 2, day_1_u.price * d, 2 * q * (1 - q))
    day_2_dd = Node(5, 2, day_1_d.price * d, (1 - q) * (1 - q))
    day_3_uuu = Node(6, 3, day_2_uu.price * u, q * q * q)
    day_3_uud = Node(7, 3, day_2_uu.price * d, 3 * q * q * (1 - q))
    day_3_udd = Node(8, 3, day_2_ud.price * d, 3 * q * (1 - q) * (1 - q))
    day_3_ddd = Node(9, 3, day_2_dd.price * d, (1 - q) * (1 - q) * (1 - q))
    day_4_uuuu = Node(10, 4, day_3_uuu.price * u, q * q * q * q)
    day_4_uuud = Node(11, 4, day_3_uuu.price * d, 4 * q * q * q * (1 - q))
    day_4_uudd = Node(12, 4, day_3_uud.price * d,
                      6 * q * q * (1 - q) * (1 - q))
    day_4_uddd = Node(13, 4, day_3_udd.price * d,
                      4 * q * (1 - q) * (1 - q) * (1 - q))
    day_4_dddd = Node(14, 4, day_3_ddd.price * d,
                      (1 - q) * (1 - q) * (1 - q) * (1 - q))

    nodes = tree(day_0, day_1_u, day_1_d, day_2_uu, day_2_ud, day_2_dd,
                 day_3_uuu, day_3_uud, day_3_udd, day_3_ddd, day_4_uuuu,
                 day_4_uuud, day_4_uudd, day_4_uddd, day_4_dddd)

    return nodes
Ejemplo n.º 2
0
def plot(q, entry, real_life):
    u = u_and_d.u(q, entry, real_life)
    d = u_and_d.d(q, entry, real_life)

    tree_object = n.gen_tree(q, entry, real_life)

    tree = [
        tree_object.n0, tree_object.n1, tree_object.n2, tree_object.n3,
        tree_object.n4, tree_object.n5, tree_object.n6, tree_object.n7,
        tree_object.n8, tree_object.n9, tree_object.n10, tree_object.n11,
        tree_object.n12, tree_object.n13, tree_object.n14
    ]

    fig, ax = plt.subplots()

    day = [i.day for i in tree]
    price = [i.price for i in tree]
    #scale = [1000*abs(i.probability) for i in tree]
    scale = 150
    #colors = [i.probability for i in tree]
    colors = 'royalblue'
    colors_2 = 'grey'
    colors_3 = 'white'
    colors_4 = 'black'
    colors_5 = 'red'
    marker = '.'
    font = 'Tahoma'

    # Add arrows
    for i in tree[0:10]:
        x_tail = i.day
        y_tail = i.price
        x_head = i.day + 1
        y_head_u = i.price * u
        y_head_d = i.price * d
        dx = x_head - x_tail
        #dy_u = y_head_u - y_tail
        #dy_d = y_head_d - y_tail
        arrow_u = mpatches.FancyArrowPatch((x_tail, y_tail),
                                           (x_head, y_head_u),
                                           mutation_scale=15,
                                           zorder=1,
                                           color=colors_2)
        arrow_d = mpatches.FancyArrowPatch((x_tail, y_tail),
                                           (x_head, y_head_d),
                                           mutation_scale=15,
                                           zorder=1,
                                           color=colors_2)
        ax.add_patch(arrow_u)
        ax.add_patch(arrow_d)

    # Plot the chart
    ax.scatter(day, price, s=scale, c=colors, marker=marker, zorder=2)

    # Add rectangles
    width = 0.5
    height = (tree_object.n10.price - tree_object.n14.price) / 15

    for x, y in zip(day, price):
        ax.add_patch(
            mpatches.Rectangle(
                xy=(x - width / 2, y - height / 2 +
                    (tree_object.n1.price - tree_object.n0.price) / 2),
                width=width,
                height=height,
                linewidth=1,
                color=colors))

    # Zip joins x and y coordinates in pairs
    for x, y in zip(day, price):

        label = "{:.2f}".format(y)

        # Label each point with the price
        ax.annotate(
            label,  # this is the text
            (x, y + (tree_object.n1.price - tree_object.n0.price) /
             2),  # this is the point to label
            va='center',
            ha='center',
            c=colors_3,
            fontname=font)  # horizontal alignment can be left, right or center

    # Expected value plot
    real_life_data_day = [0, 1, 2, 3, 4]
    day_0_ev = tree[0].price
    day_1_ev = (sum(tree[i].price * tree[i].probability for i in range(1, 3)))
    day_2_ev = (sum(tree[i].price * tree[i].probability for i in range(3, 6)))
    day_3_ev = (sum(tree[i].price * tree[i].probability for i in range(6, 10)))
    day_4_ev = (sum(tree[i].price * tree[i].probability
                    for i in range(10, 15)))

    ev = [day_0_ev, day_1_ev, day_2_ev, day_3_ev, day_4_ev]
    print(ev)
    x = np.array(real_life_data_day)
    y = np.array(ev)
    m, b = np.polyfit(x, y, 1)
    print(m)
    ax.scatter(real_life_data_day,
               ev,
               s=scale,
               c=colors_5,
               marker="",
               zorder=2)
    ax.plot(x, m * x + b, c=colors_5)
    label2 = str(round(m, 3)) + "(x)+ " + str(round(b, 2))
    print(label2)
    ax.annotate(
        label2,  # this is the text
        (0.5, (tree_object.n14.price)),  # this is the point to label
        va='center',
        ha='center',
        c=colors_5,
        fontname=font)  # horizontal alignment can be left, right or center

    label3 = "q = " + str(q)
    ax.annotate(
        label3,  # this is the text
        (2, tree_object.n14.price),  # this is the point to label
        va='center',
        ha='center',
        c=colors,
        fontname=font)  # horizontal alignment can be left, right or center

    # Real-life data

    real_life_data_price = [real_life[i] for i in range(5, 10)]

    # Plot real-life data as a line plot
    ax.plot(real_life_data_day, real_life_data_price, c=colors_4, zorder=4)

    plt.xticks(np.arange(0, 5, 1))

    stock = entry.upper()
    start_day = datetime.today() - timedelta(days=4)
    d1 = start_day.strftime("%B %d, %Y")
    ax.set_xlabel('Days', fontname=font, weight='bold')
    ax.set_ylabel('Price ($)', fontname=font, weight='bold')
    ax.set_title(stock + ' Predicted Close Price Over Time',
                 fontname=font,
                 weight='bold')

    fig.tight_layout()
    plt.show()
Ejemplo n.º 3
0
import numpy as np
import u_and_d_General as u_and_d
import fetch
from Hull_White_class import Node

u = u_and_d.u()
d = u_and_d.d()

day_0 = Node(0, 0, fetch.close_data()[-5], 1)
day_1_u = Node(1, 1, day_0.price * u, u)
day_1_d = Node(2, 1, day_0.price * d, d)
day_2_uu = Node(3, 2, day_1_u.price * u, u * u)
day_2_ud = Node(4, 2, day_1_u.price * d, u * d)
day_2_dd = Node(5, 2, day_1_d.price * d, d * d)
day_3_uuu = Node(6, 3, day_2_uu.price * u, u * u * u)
day_3_uud = Node(7, 3, day_2_uu.price * d, u * u * d)
day_3_udd = Node(8, 3, day_2_ud.price * d, u * d * d)
day_3_ddd = Node(9, 3, day_2_dd.price * d, d * d * d)
day_4_uuuu = Node(10, 4, day_3_uuu.price * u, u * u * u * u)
day_4_uuud = Node(11, 4, day_3_uuu.price * d, u * u * u * d)
day_4_uudd = Node(12, 4, day_3_uud.price * d, u * u * d * d)
day_4_uddd = Node(13, 4, day_3_udd.price * d, u * d * d * d)
day_4_dddd = Node(14, 4, day_3_ddd.price * d, d * d * d * d)