def get_raw_data(index_name,retry_attempts = 3):   
    if index_name:
        while retry_attempts > 0 :
            try:
                df = pdr.get_data_yahoo(index_name)
                new_df = df.reindex(index=pd.date_range(df.index.min(), 
                                          df.index.max(), 
                                          freq='D')).fillna(method='ffill')
                retry_attempts = 0
                return new_df
            except:
                print("Data pull failed. {} retry attempts remaining".\
                      format(retry_attempts))
                retry_attempts = retry_attempts - 1
    else:
        print("Invalid usage. Parameter index_name is required")
    return None
Пример #2
0
        def pressPlot(x):
            global stockFg, lineC, arrow
            try:
                #Getting stock open price, live price and %netrise
                livP = si.get_live_price(x)  #not very live
                table = si.get_quote_table(x)
                opnP = table['Open']
                pclP = table['Previous Close']

                netPrise = opnP - pclP
                prcntrise = (netPrise / pclP) * 100
                print("Open {0}, Previous Close {1}, Current {2}".format(
                    opnP, pclP, round(livP, 2)))

                if netPrise >= 0:
                    stockFg = 'green'
                    lineC = 'g'
                    arrow = '↑'
                else:
                    stockFg = 'red'
                    lineC = 'r'
                    arrow = '↓'

                #destroys any existing toolbars or labels if they already existed
                #prior to a new plot
                if self.tool == True:
                    self.toolbar.destroy()
                    self.tickerPriceL.destroy()
                    self.tickerRiseL.destroy()
                    self.tickerprcntL.destroy
                    self.tickerTitleL.destroy()

                #Plots ticker and ticker Price Live
                self.tickerPrice = round(livP, 2)
                self.tickerPriceL = tk.Label(
                    stockFrame,
                    text=str(self.tickerPrice) + ' USD',
                    font=("malgun gothic semilight", 15, "bold"),
                    bg='grey',
                    fg='white')
                self.tickerPriceL.place(relx=0.41, rely=0.81, anchor='n')

                self.tickerRiseL = tk.Label(stockFrame,
                                            text=round(netPrise, 2),
                                            font=("malgun gothic semilight",
                                                  10, "bold"),
                                            bg='grey',
                                            fg=stockFg)
                self.tickerRiseL.place(relx=0.05, rely=0.875, anchor='nw')

                self.tickerprcntL = tk.Label(
                    stockFrame,
                    text='(' + str(round(prcntrise, 2)) + '%) ' + arrow,
                    font=("malgun gothic semilight", 10, "bold"),
                    bg='grey',
                    fg=stockFg)
                self.tickerprcntL.place(relx=0.13, rely=0.875, anchor='nw')

                self.tickerTitleL = tk.Label(stockFrame,
                                             text=x.upper(),
                                             font=("malgun gothic semilight",
                                                   25, "bold"),
                                             bg='grey',
                                             fg='white')
                self.tickerTitleL.place(relx=0.05, rely=0.8, anchor='nw')

                #Plotting the graph of given stock (Ticker)
                pt.style.use("ggplot")
                start = dt.datetime(year1, month1,
                                    date1)  #Start date, xi on plot
                end = dt.datetime(year2, month2,
                                  date2)  #End date as today,xf on plot

                #Creates button which opens window for allowing investing
                investB = ttk.Button(stockFrame, text="Invest", command=Invest)
                investB.place(relx=0.68, rely=0.918, anchor='n')

                #Acquires data of given stock (ticker) from
                #Yahoo Finance API
                df = web.get_data_yahoo(str(x), start, end)

                #Plots given data, with identifier "Adj Close" on axes
                df["Adj Close"].plot()

                #Prints title for Stock graph
                print(x.upper(),
                      "stock price from " + str(start) + " to " + str(end))

                #Graph styling on size
                f = Figure(figsize=(5, 4), dpi=100)
                a = f.add_subplot(111)

                #Makes plot visible on select frame
                f.patch.set_facecolor('#121f1f')  #sets AXES colour
                a.patch.set_facecolor('xkcd:grey')  #sets actual PLOT colour
                a.set_title(str(x.upper()) + " stock price from " +
                            str(start) + " \nto " + str(end),
                            color="#%02x%02x%02x" % (255, 250, 250))
                a.set_xlabel('Date')  #sets x axis label to "date"
                a.set_ylabel('Price($)')  #sets y axis label to "price"
                a.plot(df["Adj Close"], lineC)

                #Allocates canvas area to display graph (embeds mtplot graph
                #using FigureCanvasTkAgg function
                canvas = FigureCanvasTkAgg(f, stockFrame)
                canvas.draw()
                canvas.get_tk_widget().place(relx=0.5, rely=0.9, anchor='n')
                canvas._tkcanvas.place(relx=0.5, rely=0.1, anchor="n")

                #Creates toolbar to manipulate stock graph
                self.toolbar = NavigationToolbar2Tk(canvas, self)
                self.toolbar.configure(bg='#2e3131')
                self.toolbar.update()
                self.tool = True

                if self.open == True:
                    self.r.destroy()

                if self.i == True:
                    self.LabelI.destroy()

            #Exception handling in case that invalid ticker is entered
            except web._utils.RemoteDataError:
                print("Invalid ticker, try again - must be NYSE")
                self.LabelI = tk.Label(
                    stockFrame,
                    text="Invalid ticker, try again - must be NYSE",
                    bg="green",
                    fg="red")
                self.LabelI.place(relx=0.5, rely=0.9, anchor="n")
                self.i = True  #True since invalid text has appeared

            except AttributeError:
                print("Invalid ticker, try again - must be NYSE")
                self.LabelI = tk.Label(
                    stockFrame,
                    text="Invalid ticker, try again - must be NYSE",
                    bg="green",
                    fg="red")
                self.LabelI.place(relx=0.5, rely=0.9, anchor="n")
                self.i = True

            except ValueError:
                print("Please input viewing domain")
Пример #3
0
def collect_stock_data(ticker):
    get_price_data(ticker, '15min')
    get_price_data(ticker, '1hour')
    ddf = pdr.get_data_yahoo(ticker)
    ddf.to_csv('data/' + ticker + 'daily.csv')
use_column_width=True)
st.markdown("*[Image Courtesy : {}]*".format("Yahoo Finance"))



## Create a sidebar for user input
st.sidebar.header("User input")

## Get user input
tickers = st.sidebar.text_input("Ticker","AAPL")
start_date = st.sidebar.text_input("From","2015-01-01")
end_date = st.sidebar.text_input("To",str(datetime.date.today()))

## Fetch live data
df = pdr.get_data_yahoo(tickers, 
                        start=pd.to_datetime(start_date),
                        end=pd.to_datetime(end_date))

st.header("Showing results for {}".format(tickers))

st.markdown("Closing Price")
st.line_chart(df["Close"])




## Sentiment analysis
def find_missing(lst): 
    return [i for x, y in zip(lst, lst[1:])  
        for i in range(x + 1, y) if y - x > 1] 
Пример #5
0
import pandas as pd
import pandas_datareader as pdr
import datetime
aapl = pdr.get_data_yahoo(
    'AAPL',
    start=datetime.datetime(2006, 10, 1),
    end=datetime.datetime(2012, 1,
                          1))  # Copied from the article to import the data

# Abhi Uppal | [email protected]
# Github Username: AbhiUppal

#------------------------------
# Head, tail, and describe
#------------------------------

# Return first rows of `aapl`
aapl.head()

# Return last rows of `aapl`
aapl.tail()

# Describe `aapl`
aapl.describe()

#------------------------------
# Index, columns, and subsetting
#------------------------------

# Inspect the index
aapl.index
Пример #6
0
import pandas as pd
#import numpy as np
import pandas_datareader as web
import datetime
import matplotlib.pyplot as plt
start = datetime.datetime(2017,1,1)
end = datetime.datetime.today()
df = web.get_data_yahoo('2454.tw', start, end)

datas = len(df)+3
del df['date']
del df['volume']
del df['high']
del df['low']
del df['open']
#del df['close']
del df['adjclose']

df["ma30"] = round(df["close"].rolling(window = 30, center = False, min_periods=1).mean(), 2)
df["ma60"] = round(df["close"].rolling(window = 60, center = False, min_periods=1).mean(), 2)
df["ma90"] = round(df["close"].rolling(window = 90, center = False, min_periods=1).mean(), 2)


df.plot()
plt.title("2454.tw")
plt.xlabel("Days")
plt.ylabel("Price")
plt.gca().invert_xaxis()
plt.show()
writer = pd.ExcelWriter('stock2454 copy.xlsx')
df.to_excel(writer,'Sheet2')
Пример #7
0
def update_graph(stock, radioval):
    # {"TSLA": "Tesla","AAPL": "Apple","FB": "Facebook","MSFT": "Microsoft",}
    radio = {
        "High": "High  Prices",
        "Low": "Low Prices",
        "Volume": "Market Volume",
    }
    trace1 = []
    trace2 = []
    df = pdr.get_data_yahoo(str(stock),
                            start=datetime(2006, 10, 1),
                            end=datetime.now())
    df.reset_index(inplace=True)
    count = df['Date'].count() + 1
    train_data = df[:][0:int(count * 0.8)]
    test_data = df[:][int(count * 0.8):]
    if (stock == None):
        trace1.append(
            go.Scatter(x=[0],
                       y=[0],
                       mode='markers',
                       opacity=0.7,
                       textposition='bottom center'))
        traces = [trace1]
        data = [val for sublist in traces for val in sublist]
        figure = {
            'data':
            data,
            'layout':
            go.Layout(colorway=['#FF7400', '#FFF400', '#FF0056'],
                      height=600,
                      title=f"{radio[radioval]}",
                      paper_bgcolor='rgba(0,0,0,0)',
                      plot_bgcolor='rgba(0,0,0,0)')
        }
    else:
        trace1.append(
            go.Scatter(x=train_data['Date'],
                       y=train_data[radioval],
                       mode='lines',
                       opacity=0.7,
                       name=f'Training Set',
                       textposition='bottom center'))
        trace2.append(
            go.Scatter(x=test_data['Date'],
                       y=test_data[radioval],
                       mode='lines',
                       opacity=0.6,
                       name=f'Test Set',
                       textposition='bottom center'))
        traces = [trace1, trace2]
        data = [val for sublist in traces for val in sublist]
        figure = {
            'data':
            data,
            'layout':
            go.Layout(colorway=[
                "#5E0DAC", '#FF4F00', '#375CB1', '#FF7400', '#FFF400',
                '#FF0056'
            ],
                      height=600,
                      title=f"{radio[radioval]} Train-Test Sets for {stock}",
                      xaxis={
                          "title": "Date",
                          'rangeselector': {
                              'buttons':
                              list([{
                                  'count': 1,
                                  'label': '1M',
                                  'step': 'month',
                                  'stepmode': 'backward'
                              }, {
                                  'count': 6,
                                  'label': '6M',
                                  'step': 'month',
                                  'stepmode': 'backward'
                              }, {
                                  'step': 'all'
                              }])
                          },
                          'rangeslider': {
                              'visible': True
                          },
                          'type': 'date'
                      },
                      yaxis={"title": "Price (USD)"},
                      paper_bgcolor='rgba(0,0,0,0)',
                      plot_bgcolor='rgba(0,0,0,0)')
        }
    return figure
Пример #8
0
def update_graph(selected_dropdown):
    #    dropdown = {"TSLA": "Tesla","AAPL": "Apple","FB": "Facebook","MSFT": "Microsoft",}
    trace1 = []
    trace2 = []
    for stock in selected_dropdown:
        df = pdr.get_data_yahoo(str(stock),
                                start=datetime(2006, 10, 1),
                                end=datetime.now())
        df.reset_index(inplace=True)
        trace1.append(
            go.Scatter(x=df["Date"],
                       y=df["High"],
                       mode='lines',
                       opacity=0.7,
                       name=f'High {stock}',
                       textposition='bottom center'))
        trace2.append(
            go.Scatter(x=df["Date"],
                       y=df["Low"],
                       mode='lines',
                       opacity=0.6,
                       name=f'Low {stock}',
                       textposition='bottom center'))
    traces = [trace1, trace2]
    data = [val for sublist in traces for val in sublist]
    figure = {
        'data':
        data,
        'layout':
        go.Layout(
            colorway=[
                "#5E0DAC", '#FF4F00', '#375CB1', '#FF7400', '#FFF400',
                '#FF0056'
            ],
            height=600,
            title=
            f"High and Low Prices for {', '.join(str(stock) for stock in selected_dropdown)} Over Time",
            xaxis={
                "title": "Date",
                'rangeselector': {
                    'buttons':
                    list([{
                        'count': 1,
                        'label': '1M',
                        'step': 'month',
                        'stepmode': 'backward'
                    }, {
                        'count': 6,
                        'label': '6M',
                        'step': 'month',
                        'stepmode': 'backward'
                    }, {
                        'step': 'all'
                    }])
                },
                'rangeslider': {
                    'visible': True
                },
                'type': 'date'
            },
            yaxis={"title": "Price (USD)"},
            paper_bgcolor='rgba(0,0,0,0)',
            plot_bgcolor='rgba(0,0,0,0)')
    }
    return figure
Пример #9
0
                first_flag = False
            else:
                json_file.write(',\n')
            json_file.write('[' + str(int(time_stamp * 1000)) + ',' +
                            str(stock_name[indicator_type][x]) + ']')
        x += 1

    json_file.write(']')
    json_file.close()


db = MySQLdb.connect("localhost", "root", "1119", "StockTestDatabase")
cursor = db.cursor()

sql = "SELECT Name FROM Stock;"
result = cursor.execute(sql)
result = cursor.fetchall()
for item in result:
    stock_name = pdr.get_data_yahoo(symbols=str(item[0]),
                                    start=oneyear,
                                    end=now)
    stock_df = Sdf.retype(stock_name)
    #rsi_14
    stock_name['rsi'] = stock_df['rsi_14']
    write_file(str(item[0]), 'rsi')
    #macd
    stock_name['macd'] = stock_df.get('macd')
    write_file(str(item[0]), 'macd')
    #dma
    stock_name['dma'] = stock_df['dma']
    write_file(str(item[0]), 'dma')
Пример #10
0
import pandas as pd
import datetime
import pandas_datareader as pdr
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
# style.use('fivethirtyeight')

start = datetime.datetime(2018, 1, 1)
end = datetime.date.today()

baba = pdr.get_data_yahoo('BABA', start, end)

print(baba.head())

baba['High'].plot()
plt.legend()
plt.show()
Пример #11
0
    b = df_iris.species == specie
    fig.add_trace(go.Scatter3d(x=df_iris["sepal_length"][b], y=df_iris["sepal_width"][b], z=df_iris["petal_width"][b], name=specie, mode="markers"))


fig.show()


# In[ ]:


yf.pdr_override()

symbols = ["AAPL","MSFT"]
stocks = []
for symbol in symbols:
    stocks.append(pdr.get_data_yahoo(symbol, start="2020-01-01", end="2020-05-31"))


# In[ ]:


fig = go.Figure()

for stock,symbol in zip(stocks,symbols):
    fig.add_trace(go.Scatter(x=stock.index, y=stock.Close, name=symbol))

fig.show()


# In[ ]:
import pandas as pd
import pandas_datareader as reader
import datetime as dt

start = dt.datetime(2017, 9, 1)
end = dt.datetime(2017, 10, 1)

aapl = reader.get_data_yahoo('AAPL', start, end)
print(aapl)

f = reader.data.DataReader('F', 'google', start, end)
f = f.ix['2017-09-27']

file = 'Foreign_Equity_Mapping.csv'
p = pd.read_csv(filepath_or_buffer=file, sep=',')
print(p)
def compute_correlation_companies(company_1, company_2, start_year,
                                  start_month, start_day, end_year, end_month,
                                  end_day):
    a = int(start_year)
    b = int(start_month)
    c = int(start_day)
    d = int(end_year)
    e = int(end_month)
    f = int(end_day)

    # Error: same start and end date
    if ((a == d) and (b == e) and (c == f)):
        print("The start and end dates must be different")
        sys.exit()

    # Error: the months exist out of range
    if ((b < 1 or b > 12) or (e < 1 or e > 12)):
        print("Month out of range")
        sys.exit()

    # Error: days are out of range for non-leap years and regular months
    if ((c < 1) or (f < 1) or (c > 31) or (f > 31)
            or ((b == 2 or e == 2) and (c > 28 or f > 28) and
                (a % 4 != 0 or d % 4 != 0))):
        print("Day out of range")
        sys.exit()

    # Error: day out of range for leap year
    if ((b == 2 or e == 2) and (c > 29 or f > 29)
            and (a % 4 == 0 or d % 4 == 0)):
        print("Day out of range")
        sys.exit()

    # Error: handle months with 31 days
    if ((b == 1 or b == 3 or b == 5 or b == 7 or b == 8 or b == 10 or b == 12
         or e == 1 or e == 3 or e == 5 or e == 7 or e == 8 or e == 10
         or e == 12) and (c > 31)):
        print("Day out of range")
        sys.exit()

    # Error: handle months with 30 days
    if ((b == 4 or b == 6 or b == 9 or b == 11 or e == 4 or e == 6 or e == 9
         or e == 11) and (c > 30)):
        print("Day out of range")
        sys.exit()

    if ((a > d) or (a == d and ((b > e) or ((b == e and c > f))))):
        print("Start date must be earlier than end date")
        sys.exit()

    temp_1 = pdr.get_data_yahoo(symbols=company_1,
                                start=datetime(a, b, c),
                                end=datetime(d, e, f))
    temp_2 = pdr.get_data_yahoo(symbols=company_2,
                                start=datetime(a, b, c),
                                end=datetime(d, e, f))
    comp1_list = []
    comp2_list = []
    x = 0
    while (x < len(temp_1)):
        entry = temp_1['Close'][x]
        entry = round(entry, 2)
        comp1_list.append(entry)
        x += 1

    y = 0
    while (y < len(temp_2)):
        entry = temp_2['Close'][y]
        entry = round(entry, 2)
        comp2_list.append(entry)
        y += 1

    comp1_list = pd.Series(comp1_list)
    comp2_list = pd.Series(comp2_list)
    return round(comp1_list.corr(comp2_list), 2)
Пример #14
0
pd.set_option('display.max_columns',None)

# We would like all available data from 01/01/2000 until 12/31/2016.
start_date = '2007-08-01'
end_date = '2018-08-31'

stock_code = '0700.HK'
stock_code = '^HSI'
start = datetime(2016, 1, 1)
end = datetime(2018, 9, 30)

# User pandas_reader.data.DataReader to load the desired data. As simple as that.
# df = data.get_data_yahoo('2800.hk', start_date, end_date)
# the early start day is 2007-12-31
df = data.get_data_yahoo(stock_code, start=start, end=end)

print(len(df.columns))
print(df.columns)

no_of_record = len(df.index)
print(len(df.index))

print(df.head(5))
print(df.tail(5))
# print("--------------------")
# print(df.iloc[0:15]) # from 0 to 2
# print("--------------------")

# close_price_df = df[['Close']].copy()
# print(close_price_df.head(10))
Пример #15
0
import pandas_datareader as pdr
import datetime

msft = pdr.get_data_yahoo('MSFT',
                          start=datetime.datetime(2018, 1, 1),
                          end=datetime.datetime(2018, 8, 1))

print msft
Пример #16
0
import  pandas_datareader as web

price = web.get_data_yahoo('AAPL', '2015-01-01')['Adj Close']

print price
Пример #17
0
# -*- coding:utf-8 -*-

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import pandas_datareader as pdr
import matplotlib.pyplot as plt
from datetime import datetime

start = datetime(2009, 12, 31)
Apple = pdr.get_data_yahoo('AAPL', start=start)
Microsoft = pdr.get_data_yahoo('MSFT', start=start)

# 股价对比
Apple['Adj Close'].plot(legend=True, label='Apple.Inc')
Microsoft['Adj Close'].plot(legend=True, label='Microsoft')
plt.show()

# 交易量对比
Apple['Volume'].plot(legend=True, label='Apple.Inc')
Microsoft['Volume'].plot(legend=True, label='Microsoft')
plt.show()

# 股票价格极差对比
Apple['high-low'] = Apple['High'] - Apple['Low']
Microsoft['high-low'] = Apple['High'] - Apple['Low']
Apple['high-low'].plot(label='Apple.Inc')
Microsoft['high-low'].plot(label='Microsoft')
plt.show()

# daily return(每天变化情况)
Пример #18
0
def update_graph(stock, stock2, radioval):
    # dropdown = {"TSLA": "Tesla", "AAPL": "Apple", "FB": "Facebook", "MSFT": "Microsoft", }
    radio = {
        "High": "High Prices",
        "Low": "Low Prices",
        "Volume": "Market Volume",
    }
    trace1 = []
    if (stock == None) or (stock2 == None):
        trace1.append(
            go.Scatter(x=[0],
                       y=[0],
                       mode='markers',
                       opacity=0.7,
                       textposition='bottom center'))
        traces = [trace1]
        data = [val for sublist in traces for val in sublist]
        figure = {
            'data':
            data,
            'layout':
            go.Layout(colorway=['#FF7400', '#FFF400', '#FF0056'],
                      height=600,
                      title=f"{radio[radioval]}",
                      paper_bgcolor='rgba(0,0,0,0)',
                      plot_bgcolor='rgba(0,0,0,0)')
        }
    else:
        df = pdr.get_data_yahoo(str(stock),
                                start=datetime(2006, 10, 1),
                                end=datetime.now())
        df.reset_index(inplace=True)
        df2 = pdr.get_data_yahoo(str(stock2),
                                 start=datetime(2006, 10, 1),
                                 end=datetime.now())
        df2.reset_index(inplace=True)
        trace1.append(
            go.Scatter(x=df[radioval][-1000:],
                       y=df2[radioval][-1000:],
                       mode='markers',
                       opacity=0.7,
                       textposition='bottom center'))
        traces = [trace1]
        data = [val for sublist in traces for val in sublist]
        figure = {
            'data':
            data,
            'layout':
            go.Layout(
                colorway=['#FF7400', '#FFF400', '#FF0056'],
                height=600,
                title=
                f"{radio[radioval]} of {stock} vs {stock2} Over Time (1000 iterations)",
                xaxis={
                    "title": stock,
                },
                yaxis={"title": stock2},
                paper_bgcolor='rgba(0,0,0,0)',
                plot_bgcolor='rgba(0,0,0,0)')
        }
    return figure
Пример #19
0
        "Clicca sul link qua sotto per attivare una prova gratuita")
    st.sidebar.markdown("https://wp.me/P9LLei-3v")
    titolo = ("^GSPC")

mesi_proiezione = st.number_input("Mesi per la proiezione statistica",
                                  min_value=1,
                                  max_value=240,
                                  value=60)

# # Scarica la serie storica e disegnala

# In[78]:

try:

    df = pdr.get_data_yahoo(titolo, start='1980-1-1')['Close']
    df = df.resample('M').last()
    df = pd.DataFrame(df)
    df['index'] = df.index
    df = df.set_index('index', 1)

except:

    try:
        client = Client(
            'http://ws.bbfinance.net/_bnb_ws/bnb_ws.wsdl')  #4G58NHO2VNH3RJD9
        LOGIN = client.service.Login("*****@*****.**",
                                     "PMuxLBzkFvRib21")
        TOK = LOGIN.LOGIN_RESPONSE.token

        A = client.service.GetHistoryFromDate(TOK, "SP", titolo, '1980-1-1',
Пример #20
0
def update_graph(stock, radioval):
    radio = {
        "High": "High Prices",
        "Low": "Low Prices",
        "Volume": "Market Volume",
    }
    #{"TSLA": "Tesla","AAPL": "Apple","FB": "Facebook","MSFT": "Microsoft",}
    trace1 = []
    trace2 = []
    if (stock == None):
        trace1.append(
            go.Scatter(x=[0],
                       y=[0],
                       mode='markers',
                       opacity=0.7,
                       textposition='bottom center'))
        traces = [trace1]
        data = [val for sublist in traces for val in sublist]
        figure = {
            'data':
            data,
            'layout':
            go.Layout(colorway=['#FF7400', '#FFF400', '#FF0056'],
                      height=600,
                      title=f"{radio[radioval]}",
                      paper_bgcolor='rgba(0,0,0,0)',
                      plot_bgcolor='rgba(0,0,0,0)')
        }
    else:
        df = pdr.get_data_yahoo(str(stock),
                                start=datetime(2006, 10, 1),
                                end=datetime.now())
        df.reset_index(inplace=True)
        count = df['Date'].count() + 1
        train_data = df[:][0:int(count * 0.8)]
        test_data = df[:][int(count * 0.8):]
        train_ar = train_data[radioval].values
        test_ar = test_data[radioval].values
        history = [x for x in train_ar]
        predictions = list()
        for t in range(len(test_ar)):
            global model
            model = ARIMA(history, order=(3, 1, 0))
            model_fit = model.fit(disp=0)
            output = model_fit.forecast()
            yhat = output[0]
            predictions.append(yhat)
            obs = test_ar[t]
            history.append(obs)
        error = mean_squared_error(test_ar, predictions)
        trace1.append(
            go.Scatter(x=test_data['Date'],
                       y=test_data['High'],
                       mode='lines',
                       opacity=0.6,
                       name=f'Actual Series',
                       textposition='bottom center'))
        trace2.append(
            go.Scatter(x=test_data['Date'],
                       y=np.concatenate(predictions).ravel(),
                       mode='lines',
                       opacity=0.7,
                       name=f'Predicted Series (MSE: {error})',
                       textposition='bottom center'))
        traces = [trace1, trace2]
        data = [val for sublist in traces for val in sublist]
        figure = {
            'data':
            data,
            'layout':
            go.Layout(
                colorway=[
                    "#5E0DAC", '#FF4F00', '#375CB1', '#FF7400', '#FFF400',
                    '#FF0056'
                ],
                height=600,
                title=
                f"{radio[radioval]} ARIMA Predictions vs Actual for {stock}",
                xaxis={
                    "title": "Date",
                    'rangeselector': {
                        'buttons':
                        list([{
                            'count': 1,
                            'label': '1M',
                            'step': 'month',
                            'stepmode': 'backward'
                        }, {
                            'count': 6,
                            'label': '6M',
                            'step': 'month',
                            'stepmode': 'backward'
                        }, {
                            'step': 'all'
                        }])
                    },
                    'rangeslider': {
                        'visible': True
                    },
                    'type': 'date'
                },
                yaxis={"title": "Price (USD)"},
                paper_bgcolor='rgba(0,0,0,0)',
                plot_bgcolor='rgba(0,0,0,0)')
        }
    return figure
Пример #21
0
'''
Created on 9 Aug 2020

@author: snake91
'''

import pandas as pd
import pandas_datareader as web
import tensorflow as tf
import numpy as np

data = web.get_data_yahoo(["GOOG"])
data.columns = data.columns.droplevel(level=1)

data["Returns"] = data["Adj Close"] / data["Adj Close"].shift()

lags = 50

arr = np.array(
    [np.array(data["Returns"].shift(i))[lags:] for i in range(1, lags + 1)])
arr = np.where(np.isnan(arr), 0, arr)

x_train = arr[1:, :1100]
Y_train = arr[0, :1100]
Y_train = np.reshape(Y_train, (1, 1100))

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=[x_train.shape[0], x_train.shape[1]]),
    tf.keras.layers.Dense(lags, activation="softmax", name="layer1"),
    tf.keras.layers.Dense(lags, activation="softmax", name="layer2")
])
Пример #22
0
def result():
    if request.method == 'POST':
        print(request.form)
        stocks = request.form['stocks'].split()
        strt_date = datetime.strptime(request.form['start_date'], '%Y-%m-%d')
        window = int(request.form['window'])
        print(stocks)

        # Get data
        start_date = strt_date - timedelta(window+5)
        end_date = datetime.today()
        initial_capital = 1000000

        # stocks = ['MSFT','AAPL','GOOG','AMZN','XOM','HSBC','BRK-B','JPM','BAC','WFC']

        print("Loading data for following stocks: ")
        print(stocks)
        spy = pdr.get_data_yahoo(symbols='SPY', start=start_date, end=end_date)
        stock_data = pdr.get_data_yahoo(symbols=stocks, start=start_date, end=end_date)
        print("Stock data loaded ...")

        # Calculate Benchmark Returns
        spy['Returns'] = spy['Adj Close'].pct_change(1)


        # Calculate returns
        for ticker in stock_data.columns.levels[1]:
            stock_data['Return', ticker] = stock_data['Adj Close', ticker].pct_change(1)
        print("Calculated Returns ...")

        # Calculate Inverse Variance
        for ticker in stock_data.columns.levels[1]:
            stock_data['InvVar', ticker] = stock_data['Return', ticker].rolling(window).apply(lambda x: 1/x.var(), raw=False)

        # Calculate Portfolio weights
        df = stock_data.InvVar
        df = df.div(df.sum(axis=1), axis=0)
        for ticker in stock_data.columns.levels[1]:
            stock_data['Weights', ticker] = df[ticker].round(2)
        del df
        print("Calculated Portfolio Weights ...")

        # Remove NaN values
        stock_data.dropna(inplace=True)


        # # Portfolio Returns
        # 1. Get Change in weights
        # 2. Get number of shares
        # 3. Get Trading costs
        # 4. Calculate Porfolio Value and Cumulative returns.


        Portfolio = stock_data[['Adj Close', 'Weights']]

        stock_val = np.array(Portfolio['Adj Close'])
        stock_weight = np.array(Portfolio['Weights'])
        port_val = np.empty(shape=stock_val.shape[0])
        shares_holding = np.empty(shape=stock_val.shape)
        traded_volume = np.zeros(shape=stock_val.shape)
        traded_value = np.zeros(shape=stock_val.shape)
        trading_cost = np.zeros(shape=stock_val.shape)

        shares_holding[0] = np.round_(stock_weight[0]/stock_val[0]*initial_capital)
        port_val[0] = np.dot(shares_holding[0], stock_val[0])

        for ind in range(1, len(stock_val)):
            shares_holding[ind] = shares_holding[ind-1] + np.round_((stock_weight[ind]-stock_weight[ind-1])/stock_val[ind]*port_val[ind-1])
            traded_volume[ind] = shares_holding[ind] - shares_holding[ind-1]
            traded_value[ind] = traded_volume[ind]*stock_val[ind]
            trading_cost[ind] = 0.01*abs(traded_value[ind])
            port_val[ind] = np.dot(shares_holding[ind], stock_val[ind])
            port_val[ind] = port_val[ind] - traded_value[ind].sum() - trading_cost[ind].sum()

        print("Calculated Portfolio returns ...")

        Portfolio['Value'] = pd.DataFrame(data=port_val, index=Portfolio['Adj Close'].index)
        # Portfolio['Return'] = Portfolio['Value'].pct_change(1)
        Traded_Volume = pd.DataFrame(data=traded_volume, index=Portfolio['Adj Close'].index, columns=Portfolio['Adj Close'].columns)
        # Traded_Value = pd.DataFrame(data=traded_value, index=Portfolio['Adj Close'].index, columns=Portfolio['Adj Close'].columns)
        # Trading_Cost = pd.DataFrame(data=trading_cost)
        # Shares_Holding = pd.DataFrame(data=shares_holding, index=Portfolio['Adj Close'].index, columns=Portfolio['Adj Close'].columns)

        del stock_val
        del stock_weight
        del port_val
        del shares_holding
        del traded_volume
        del traded_value
        del trading_cost

        to_trade = Traded_Volume.iloc[-1].to_dict()

        #Plot traded volume and shares holding

        # ax1 = plt.gca()
        # (Portfolio['Value']/Portfolio['Value'].iloc[0])[start_date:].plot(figsize=(10,6), ax=ax1, title="Portfolio Performance");
        # (spy['Adj Close']/spy['Adj Close'][Portfolio['Value'].index.values[0]])[start_date:].plot(ax=ax1);
        # ax1.legend(['Portfolio','S&P 500'])
        # portfolio_value_url = 'static/img/portfolio_value.png'
        # plt.savefig(portfolio_value_url)

        fig = plt.figure(figsize=(10,6))
        ax = plt.subplot(111)
        ax.plot((Portfolio['Value']/Portfolio['Value'].iloc[0])[start_date:])
        ax.plot((spy['Adj Close']/spy['Adj Close'][Portfolio['Value'].index.values[0]])[start_date:])
        plt.xlabel("Date")
        plt.ylabel("Returns")
        plt.title('Portfolio Value')
        ax.legend(['Portfolio','SP 500'])
        #plt.show()
        portfolio_value_url = 'static/img/portfolio_value.png'
        fig.savefig(portfolio_value_url)

        fig2 = plt.figure(figsize=(10,6))
        ax2 = plt.subplot(111)
        ax2.pie(Portfolio['Weights'].iloc[-1], autopct='%.2f%%', labels=Portfolio['Weights'].columns)
        plt.title('Portfolio Composition')

        portfolio_weights_url = 'static/img/portfolio_weights.png'
        fig2.savefig(portfolio_weights_url)

        # ax2 = plt.gca()
        # Portfolio['Weights'].iloc[-1].plot(kind='pie', autopct='%.2f%%', ax=ax2, title="Portfolio Weights", legend=False);
        # portfolio_weights_url = 'static/img/portfolio_weights.png'
        # plt.savefig(portfolio_weights_url)

        return render_template('results.html', stocks=stocks, start_date=strt_date, to_trade=to_trade,
                                window=window,portfolio_weights_url=portfolio_weights_url,
                                portfolio_value_url=portfolio_value_url)
Пример #23
0
import datetime
from datetime import date, datetime

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, PercentFormatter, FormatStrFormatter
import matplotlib.dates as mdates
from matplotlib.dates import MonthLocator, DateFormatter
from matplotlib import rc, rcParams

#####################################################################################
## VIX racker
## Data
start_data = date(2019, 7, 1)
today = datetime.now()

vix = pdr.get_data_yahoo('^VIX', start_data, today)

#####################################################################################

## Inputs
start = start_data
end = date(2019, 8, 30)
line1_1_color = 'blue'
linewidth = 3
title1 = 'CBOE VIX (July - August 2019)'
title_fontsize = 30
y1_label_text = ''
x1_label_text = ''
y1_label_fontsize = 25
x1_label_fontsize = 25
y1_tick_min = 10
Пример #24
0
 def data(ticker):
     return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as web

df = web.get_data_yahoo("DELTACORP.NS", start="2019-04-01", end="2020-03-31")
#adds reproducability of code and flexibility of time period
"""We can also use the following code in comments if the scraped csv from attached zip is used

df = pd.read_csv('DELTACORP.csv',header=[0])            """

df['Daily_Return'] = df['Close'].pct_change()
#Daily Returns have been stored in a separate column
df['Daily_Return'] = df['Daily_Return'] * 100
#will be relevant as we need to return values in% and not /100

print("Daily Returns %")
print("Maximum:" + str(round(df['Daily_Return'].max(axis=0), 3)))
print("Minimum:" + str(round(df['Daily_Return'].min(axis=0), 3)))
print("Mean:" + str(round(df['Daily_Return'].mean(), 4)))
print("Sample Standard Deviation:" +
      str(round(df['Daily_Return'].std(axis=0), 4)))

df['Daily_Return'].plot(kind='line', title='Line Graph for Daily Returns')
plt.show()

df.drop(df.columns.difference(['Date', 'Close', 'Daily_Return']),
        1,
        inplace=True)
df.to_csv(
    r'D:\000Finance\FOFA ASSIGNMENT 2\85_Shubh_2018A2PS0832H_DELTACORP\2 daily Returns (risk unadjusted) DELTACORP.csv',
Пример #26
0
import streamlit as st
import pandas_datareader as pdr

st.write('''
# 삼성전자 주식 데이터
마감 가격과 거래량을 차트로 보여줍니다!
''')

df = pdr.get_data_yahoo('005930.KS', '2020-01-01', '2020-09-30')

st.line_chart(df.Close)
st.line_chart(df.Volume)
Пример #27
0
# I'm using daily close prices
# SPDR S$p500 ETF (SPY)
SPY = "SPY"

# My two other ETFs are
# 1. Vanguard S$P500 ETF (VOO)
# 2. iShares Core S&P500 ETF (IVV)

etfs_tickers = ["IVV", "SPY", "VOO", "^GSPC"]

# using 2 years of data from January 01, 2018 to December 31, 2019
starttime = datetime.datetime(2018, 1, 1)
endtime = datetime.datetime(2019, 12, 31)

# get only the closing prices
etfs = pdr.get_data_yahoo(etfs_tickers, starttime, endtime)['Close']
etfs.columns = ["iShares", "SPDR", "Vanguard", "S&P500"]

# print out dataset head
print2(etfs.head())

# compute simple reurns
etfs_return = etfs.pct_change().dropna()

# etfs_return.fillna(0, inplace=True)
returns2 = round(etfs_return * 100, 3)
print2(etfs_return, returns2)

# compute active returns
eft_index = etfs_return["S&P500"]
ppp = returns2.sub(returns2.iloc[:, -1], axis=0).drop(['S&P500'], axis=1)
Пример #28
0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader as web
import requests_cache
import datetime

session = requests_cache.CachedSession(cache_name='cache',
                                       backend='sqlite',
                                       expire_after=datetime.timedelta(days=3))

one_day = datetime.timedelta(days=1)
one_week = datetime.timedelta(days=7)

stock = web.get_data_yahoo("VTSAX",
                           start="2009-01-01",
                           end="2019-04-30",
                           session=session)

test_start = datetime.datetime(2009, 1, 5)
test_end = datetime.datetime(2019, 1, 7)

assert test_start.weekday() == 0  # Monday
assert test_end.weekday() == test_start.weekday()


# Strategy: invest on 1st day each month (or whatever next possible day is)
def strategy1(is_valid_day, *args, **kwargs):
    """1st"""
    investments = []
    for i in range(2009, 2019):
        for j in range(1, 13):
Пример #29
0
import pandas_datareader as pdr
import datetime
import quandl

aapl = pdr.get_data_yahoo('AAPL',
                          start=datetime.datetime(2006, 10, 1),
                          end=datetime.datetime(2012, 1, 1))

aapl2 = quandl.get("WIKI/AAPL", start_date="2006-10-01", end_date="2012-01-01")
Пример #30
0
# -*- coding: utf-8 -*-
"""
Created on Sun May 10 19:13:30 2020

@author: Hossein
"""

import pandas as pd
import pandas_datareader as pdr
import matplotlib.pyplot as plt

spy = pdr.get_data_yahoo('spy', "2002-01-01")
spy['Adj Close'].plot()

daily_return = spy['Adj Close'].pct_change()

daily_return1 = (spy['Adj Close'][1:] / spy['Adj Close'][:-1].values) - 1

daily_return1.plot()

plt.figure(figsize=(60, 30))
daily_return.plot()

daily_return.hist(bins=10)

mean = daily_return.mean()
std = daily_return.std()
daily_return.hist(bins=30)
plt.axvline(mean, color='w', Linestyle='dashed', linewidth=2)
plt.axvline(std, color='r', Linestyle='dashed', linewidth=2)
plt.axvline(-std, color='r', Linestyle='dashed', linewidth=2)
def MAIN():

    data = web.get_data_yahoo(USA, start, end)[["Close"]]
    data = data.rename(columns={"Close": "S&P 500"})
    data_2 = web.get_data_yahoo(Canada, start, end)[["Close"]]
    data_2 = data_2.rename(columns={"Close": "TSX 60"})
    data_3 = web.get_data_yahoo(Japan, start, end)[["Close"]]
    data_3 = data_3.rename(columns={"Close": "Nikkei 225"})

    frames = [data, data_2, data_3]
    result = pd.concat(frames, axis=1)
    result.dropna(inplace=True)

    index = pd.DataFrame(index=result.index)

    SP_List = []
    TSX_List = []
    Nikkei_List = []
    seven_list = []

    for i in range(len(result)):
        indexed = (100 / result["S&P 500"][0]) * result["S&P 500"][i]
        SP_List.append(indexed)
        indexed = (100 / result["TSX 60"][0]) * result["TSX 60"][i]
        TSX_List.append(indexed)
        indexed = (100 / result["Nikkei 225"][0]) * result["Nikkei 225"][i]
        Nikkei_List.append(indexed)
        indexed = (100 * ((1 + 0.00026852275)**i))
        seven_list.append(indexed)

    index["S&P 500 index"] = SP_List
    index["TSX index"] = TSX_List
    index["Nikkei 225 index"] = Nikkei_List
    index["7%"] = seven_list

    index_log = pd.DataFrame(index=result.index)

    SP_List_Log = []
    TSX_List_Log = []
    Nikkei_List_Log = []
    seven_list_Log = []

    for i in range(len(result)):
        SP_List_Log.append(math.log(index["S&P 500 index"][i]))
        TSX_List_Log.append(math.log(index["TSX index"][i]))
        Nikkei_List_Log.append(math.log(index["Nikkei 225 index"][i]))
        seven_list_Log.append(math.log(index["7%"][i]))

    index_log["S&P 500"] = SP_List_Log
    index_log["S&P/TSX 60"] = TSX_List_Log
    index_log["Nikkei 225 "] = Nikkei_List_Log
    index_log["Consistent 7% Return"] = seven_list_Log

    my_colors = ['b', 'r', 'g', 'k']
    my_linetype = ["solid", "solid", "solid", "dashed"]
    fig, ax = plt.subplots()
    for col, color, linetype in zip(index_log.columns, my_colors, my_linetype):
        index_log[col].plot(color=color, linestyle=linetype, ax=ax)
    plt.ylabel("Natural Log of Index", labelpad=20)
    plt.xlabel("Years", labelpad=20)
    plt.legend(loc="upper left")
    plt.show()
Пример #32
0
 def get_data_from_yahoo(self, ticker: str, start: str, end: str):
     start = dt.datetime.strptime(start, "%Y-%m-%d")
     end = dt.datetime.strptime(end, "%Y-%m-%d")
     df = pdr.get_data_yahoo(ticker, start=start, end=end)
     df['Ticker'] = ticker
     return df
Пример #33
0
    else:
        price = K * exp(-r * T) * N(-d2) - S * exp(-q * T) * N(-d1)
    return price


def bs_vega(cp_flag, S, K, T, r, v, q=0.0):
    d1 = (log(S / K) + (r + v * v / 2.) * T) / (v * sqrt(T))
    return S * sqrt(T) * n(d1)


app = dash.Dash(__name__)
server = app.server
app.config['suppress_callback_exceptions'] = True

model = ''
df = pdr.get_data_yahoo('ibm', start=datetime(2006, 10, 1), end=datetime.now())
df.reset_index(inplace=True)
# df2 = pd.read_csv("dataset_Facebook.csv",";")
#
# df_ml = df2.copy()
#
# lb_make = LabelEncoder()
# type = df_ml['Type']
# df_ml["Type"] = lb_make.fit_transform(type)
# df_ml = df_ml.fillna(0)
#
# X = df_ml.drop(['like'], axis = 1).values
# Y = df_ml['like'].values
#
# X = StandardScaler().fit_transform(X)
#