Exemplo 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
Exemplo n.º 2
0
def error(q, entry):
    # Predicted nodes
    tree_object = n.gen_tree(q, entry)

    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
    ]

    # Real-life data
    real_life_data_day = [0, 1, 2, 3, 4]
    real_life_data_price = [(fetch.close_data(entry)[i - 5]) for i in range(5)]

    # Day 1 error
    day_1 = tree[1:3]
    error_day_1 = min(
        [abs(real_life_data_price[1] - day_1[i].price)
         for i in range(2)]) / real_life_data_price[1]
    #print(error_day_1)

    # Day 2 error
    day_2 = tree[3:6]
    error_day_2 = min(
        [abs(real_life_data_price[2] - day_2[i].price)
         for i in range(3)]) / real_life_data_price[2]
    #print(error_day_2)

    # Day 3 error
    day_3 = tree[6:10]
    error_day_3 = min(
        [abs(real_life_data_price[3] - day_3[i].price)
         for i in range(4)]) / real_life_data_price[3]
    #print(error_day_3)

    # Day 4 error
    day_4 = tree[10:15]
    error_day_4 = min(
        [abs(real_life_data_price[4] - day_4[i].price)
         for i in range(5)]) / real_life_data_price[4]
    #print(error_day_4)

    # Error set

    errors = [error_day_1, error_day_2, error_day_3, error_day_4]
    total_error = sum(errors)
    #print(total_error)

    return total_error
Exemplo n.º 3
0
import fetch
import pandas as pd

renewable = ["run", "fslr", "cwen", "nee", "tsla", "plug"]
fossil = ["xom", "cvx", "btu", "arch", "cop"]

for stock in renewable:
    fetch.close_data(stock).to_csv(stock + '.csv')

for stock in fossil:
    fetch.close_data(stock).to_csv(stock + '.csv')
Exemplo n.º 4
0
import fetch

close_data = fetch.close_data()

ratios = []
for index in range(len(close_data)-1):
    ratios.append(close_data[index+1]/close_data[index])

def stock_price_ratios():
    return ratios
Exemplo n.º 5
0
def stock_price_ratios(entry):
    ratios = []
    close_data = fetch.close_data(entry)
    for index in range(len(close_data) - 1):
        ratios.append(close_data[index + 1] / close_data[index])
    return ratios
Exemplo n.º 6
0
import matplotlib.pyplot as plt
import numpy as np
import u_and_d_Hull_White as u_and_d
import fetch
from Hull_White_class import Node
from datetime import datetime, timedelta
import matplotlib.patches as mpatches

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, 0.5)
day_1_d = Node(2, 1, day_0.price * d, 0.5)
day_2_uu = Node(3, 2, day_1_u.price * u, 0.25)
day_2_ud = Node(4, 2, day_1_u.price * d, 0.5)
day_2_dd = Node(5, 2, day_1_d.price * d, 0.25)
day_3_uuu = Node(6, 3, day_2_uu.price * u, 0.125)
day_3_uud = Node(7, 3, day_2_uu.price * d, 0.375)
day_3_udd = Node(8, 3, day_2_ud.price * d, 0.375)
day_3_ddd = Node(9, 3, day_2_dd.price * d, 0.125)
day_4_uuuu = Node(10, 4, day_3_uuu.price * u, 0.0625)
day_4_uuud = Node(11, 4, day_3_uuu.price * d, 0.25)
day_4_uudd = Node(12, 4, day_3_uud.price * d, 0.375)
day_4_uddd = Node(13, 4, day_3_udd.price * d, 0.25)
day_4_dddd = Node(14, 4, day_3_ddd.price * d, 0.0625)

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]

fig,ax = plt.subplots()
Exemplo n.º 7
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)
Exemplo n.º 8
0
def plot(q, entry):
    u = u_and_d.u(q, entry)
    d = u_and_d.d(q, entry)

    
    tree_object = n.gen_tree(q, entry)

    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=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
                    (2,(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


    # Real-life data

    real_life_data_price = [(fetch.close_data(entry)[i-5]) for i in range(5)]

    # 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 = fetch.stock(entry)
    start_day = datetime.today() - timedelta(days=4)
    d1 = start_day.strftime("%B %d, %Y")
    ax.set_xlabel('Days past ' + d1, 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()
Exemplo n.º 9
0
import fetch
import pandas

fetch.close_data("plug").to_csv('stock.csv')
Exemplo n.º 10
0
    label = "{:.2f}".format(y)

    # Label each point with the price
    ax.annotate(label, # this is the text
                 (x,y+(n.day_1_u.price-n.day_0.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



# Real-life data
real_life_data_day = [0,1,2,3,4]
real_life_data_price = [(fetch.close_data()[i-5]) for i in range(5)]

# 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 = fetch.stock()
start_day = datetime.today() - timedelta(days=4)
d1 = start_day.strftime("%B %d, %Y")
ax.set_xlabel('Days past ' + d1, 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()