Ejemplo n.º 1
0
    def __init__(self, stock_id, start_date = None, end_date = None):
        self.df = None
        self.sid = stock_id
        self.start = start_date
        self.end = end_date
        
        try:
            self.df = pda.get_data_yahoo(stock_id + u'.TW', start_date, end_date)
        except:
            pass
        
        if self.df is None:
            try:
                self.df = pda.get_data_yahoo(stock_id + u'.TWO', start_date, end_date)
            except:
                pass
            
        if self.df is None:
            try:
                self.df = pda.get_data_yahoo(stock_id, start_date, end_date)
            except:
                print("KEY_ERROR: Wrong stock id.")
                raise

        self.df = self.df[self.df.Volume != 0]
Ejemplo n.º 2
0
def GetStockQuote( *args ):
    '''
        Read the stock quote by givn stock symbol, and the time frame.
    '''
    try:
        if len(args) == 1:  #default 60 days
            symbol = args[0]
            quote = web.get_data_yahoo(symbol,sixty_days.strftime("%m/%d/%Y"), time.strftime("%m/%d/%Y"))
        elif  len(args) == 2:   #can select days to today
            symbol = args[0]
            startdate = datetime.date.today() - datetime.timedelta(days=args[1]) #Need enough days for MACD calculation
            quote = web.get_data_yahoo(symbol,startdate.strftime("%m/%d/%Y"), time.strftime("%m/%d/%Y"))         
        elif len(args) == 3:  # symbol, start date, end date        
            symbol = args[0]
            startdate = datetime.date.today() - datetime.timedelta(days=args[1]) #Need enough days for MACD calculation
            enddate = datetime.date.today() - datetime.timedelta(days=args[2])            
            quote = web.get_data_yahoo(symbol,startdate.strftime("%m/%d/%Y"), enddate.strftime("%m/%d/%Y"))             
        
        print("Last day close is " , quote.Close[-1])
        
        return quote;    
    except Exception  as identifier:
        symbol = args[0]
        print("Cannot get the quote for ", symbol, "Error Message" , identifier)
        return "NoQuote"
Ejemplo n.º 3
0
 def plot_prices(self, start_date, end_date):
     """
     """
     shares = get_data_yahoo(self.share_id, start=start_date,
                             end=end_date)
     shares.Close.plot(label=self.share_id)
     plt.legend()
Ejemplo n.º 4
0
 def get(self, ticker, start = None, end = None):
     if start is None:
         start = datetime.date(2010, 01, 01)
     if end is None:
         end = datetime.date.today()
     self.ticker = ticker
     return pd_data.get_data_yahoo(ticker + ".AX", start, end)
Ejemplo n.º 5
0
def collect_new():
    today = datetime.date.today()
    with open(banknum,'r') as f:
        for i in f.read().splitlines():
            tickers = i + '.HK'
            filename = i + '.csv'
            data2 = pdr.get_data_yahoo(tickers, start="2006-01-01", end=today)
            data2.sort_index(ascending=False).to_csv(filename)
Ejemplo n.º 6
0
def load_ticker(ticker):
    fname='data_yahoo/'+ticker+'.csv'
    if os.path.exists(fname):
        df = read_csv(fname)
    else:
        df= data.get_data_yahoo(ticker,'1/1/2001','12/1/2017')    
        df.to_csv(fname)
    return df
Ejemplo n.º 7
0
def GetSecurityData(security, startDate, endDate):
    # This function grabs data from yahoo and is delayed by 15 minutes
    # security - security to read in
    # startDate/endDate in format of startDate = datetime.datetime(2006, 01, 15)

    securityData = data.get_data_yahoo(security, startDate, endDate)

    return securityData
Ejemplo n.º 8
0
    def retrieve_prices(equity_tickers=None,
                        start_date=None,
                        end_date=dt.datetime.today()):

        data = pdata.get_data_yahoo(symbols=equity_tickers,
                                    start=start_date,
                                    end=end_date)

        return data
Ejemplo n.º 9
0
    def up_down_ratio(self):
        bars = data.get_data_yahoo(self.symbol, datetime.datetime(2015,1,1), datetime.datetime(2016,1,1))

        a = bars['Open']-bars['Close']
        n4 = a.mean()
        a = a.sub(n4)        
        n1 = a.abs().mean()
        n2 = a[a>=0].mean()
        n3 = a[a<0].abs().mean()
        return n1, n2, n3, n4
Ejemplo n.º 10
0
    def test_get_data_interval(self):
        # daily interval data
        pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='d')
        self.assertEqual(len(pan), 252)

        # weekly interval data
        pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='w')
        self.assertEqual(len(pan), 53)

        # montly interval data
        pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='m')
        self.assertEqual(len(pan), 12)

        # dividend data
        pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='v')
        self.assertEqual(len(pan), 4)

        # test fail on invalid interval
        self.assertRaises(ValueError, web.get_data_yahoo, 'XOM', interval='NOT VALID')
Ejemplo n.º 11
0
    def test_get_date_ret_index(self):
        pan = web.get_data_yahoo(['GE', 'INTC', 'IBM'], '1977', '1987',
                                 ret_index=True)
        self.assertTrue(hasattr(pan, 'Ret_Index'))
        if hasattr(pan, 'Ret_Index') and hasattr(pan.Ret_Index, 'INTC'):
            tstamp = pan.Ret_Index.INTC.first_valid_index()
            result = pan.Ret_Index.ix[tstamp]['INTC']
            self.assertEqual(result, 1.0)

        # sanity checking
        assert np.issubdtype(pan.values.dtype, np.floating)
Ejemplo n.º 12
0
Archivo: data.py Proyecto: ran404/ffn
def yf(ticker, field, start=None, end=None, mrefresh=False):
    if field is None:
        field = 'Adj Close'

    tmp = pdata.get_data_yahoo(ticker, start=start, end=end)

    if tmp is None:
        raise ValueError('failed to retrieve data for %s:%s' % (ticker, field))

    if field:
        return tmp[field]
    else:
        return tmp
Ejemplo n.º 13
0
def get_price(reload_data, ticker_list, start, end):
    f_loc = '/Users/Potzenhotz/python/finance/data/'
    f_title = 'stocks_data' + str(start) + '.csv'
    f_name = f_loc + f_title
    if reload_data:
        write_data = True
        read_data = False
    else:
        write_data = False
        read_data = True
    #-----------------------------------------------------------------------
    #Load Finance Data
    #-----------------------------------------------------------------------
    if reload_data:
        all_data = {}
        i=-1
        for ticker in ticker_list:
            i+=1
            print(i,ticker)
            try:
                all_data[ticker] = web.get_data_yahoo(ticker, start, end )
            except:
                print ("Cant find ", ticker)
                pass
        price = pd.DataFrame({tic: data['Adj Close']
                            for tic, data in all_data.items()})
         
    #-----------------------------------------------------------------------
    #Write Finance Data
    #-----------------------------------------------------------------------
    if write_data:
        price.to_csv(f_name)               
        #Reread data to get right format
        #while loading Date is index after reading Date will be column
        price = pd.read_csv(f_name)
    
    
    #-----------------------------------------------------------------------
    #Read Finance Data
    #-----------------------------------------------------------------------
    if read_data:
        #check if end date is current date
        if (end.date() != dt.datetime.today().date()):
            print('#####')
            print('Caution data is out of date: %s' % (end))
            print('#####')
        price = pd.read_csv(f_name)



    return price
Ejemplo n.º 14
0
    def get_stock_values(self):
        '''
        extracts the stock data from yahoo and calculates the mean for:
        -last week
        -6 months ago
        -12 month ago
        '''
        try:
            if self.country == 'ger':
                self.stock_yahoo = self.stock + '.DE'
            elif self.country == 'us':
                self.stock_yahoo = self.stock
            elif self.country == 'uk':
                self.stock_yahoo = self.stock + '.L'

            end = dt.date.today()
            yesterday = dt.date.today()
            last_week = dt.date.today() - dt.timedelta(14)
            six_months = dt.date.today() + relativedelta(months=-6)
            six_months_start = six_months-dt.timedelta(7)
            twelve_months = dt.date.today() + relativedelta(months=-12)
            twelve_months_start = twelve_months-dt.timedelta(7)

            self.stock_week = web.get_data_yahoo(self.stock_yahoo, last_week, end )
            self.stock_6_months_ago = web.get_data_yahoo(self.stock_yahoo,six_months_start, six_months )
            self.stock_12_months_ago = web.get_data_yahoo(self.stock_yahoo, twelve_months_start, twelve_months)
            
            self.stock_avg_week = self.stock_week['Adj Close'].mean() 
            self.stock_avg_6_months = self.stock_6_months_ago['Adj Close'].mean() 
            self.stock_avg_12_months = self.stock_12_months_ago['Adj Close'].mean() 

        except:
            if self.__print_debug: print('Problem with yahoo stock extract')
            e_1 = sys.exc_info()[0]
            e_2 = sys.exc_info()[1]
            if self.__print_debug: print( "Error: %s with %s" % (e_1, e_2) )
Ejemplo n.º 15
0
    def test_get_data_multiple_symbols_two_dates(self):
        pan = web.get_data_yahoo(['GE', 'MSFT', 'INTC'], 'JAN-01-12',
                                 'JAN-31-12')
        result = pan.Close.ix['01-18-12']
        self.assertEqual(len(result), 3)

        # sanity checking
        assert np.issubdtype(result.dtype, np.floating)

        expected = np.array([[18.99,  28.4, 25.18],
                             [18.58, 28.31, 25.13],
                             [19.03, 28.16, 25.52],
                             [18.81, 28.82, 25.87]])
        result = pan.Open.ix['Jan-15-12':'Jan-20-12']
        self.assertEqual(expected.shape, result.shape)
Ejemplo n.º 16
0
def query_stock(stock, start='2011.01.01', end='today'):
    stock+=".TW"
    st = datetime.date(datetime.strptime(start, '%Y.%m.%d'))

    if end == 'today':
        en = datetime.date(datetime.today())
    else:
        en = datetime.date(datetime.strptime(end, '%Y.%m.%d'))
    df = data.get_data_yahoo(stock, st, en)
    fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
    SMA20=moving_average(df.Close.values, 20)
    add_line = Scatter(
        x=df.index[-len(SMA20):],
        y=SMA20,
        name= '20SMA',
        line=Line(color='rgb(200, 200, 250)')
        )
    fig['data'].extend([add_line])
    plotly.offline.plot(fig, validate=False)
Ejemplo n.º 17
0
    def calculate_investment(self):
        share_details = get_data_yahoo(self.share_id, start=self.start_date)
        share_details['Shares Owned'] = Series(np.zeros(len(share_details)),
                                               index=share_details.index)
        share_details['Investment'] = Series(np.zeros(len(share_details)),
                                             index=share_details.index)

        for idx, investment in enumerate(self.monthly_investments):
            start_date, end_date = self.monthly_investment_dates[idx]
            add_monthly(share_details, start_date, end_date, investment)

        for idx, investment in enumerate(self.investments):
            add_shares(share_details, self.investment_dates[idx], investment)

        share_details['Value'] = Series(share_details['Shares Owned'] *
                                        share_details.Close / 100,
                                        index=share_details.index)

        self.share_details = share_details
Ejemplo n.º 18
0
 def load_data(self):
     '''Download the data using panda'''
     self.data_exist = False
     self.check_data_exist()
     self.all_data = {}
     if self.data_exist == False:
         i=-1
         for ticker in self.stock_list:
             i+=1
             if self.__print_info: print ('Info: ',i, ticker)
             try:
                 self.all_data[ticker] = web.get_data_yahoo(ticker, self.start_date, self.end_date )
             except:
                 if self.__print_info: print('Cant find: ', ticker)
                 pass
         self.raw_df = pd.DataFrame({tic: data['Adj Close'] for tic, data in self.all_data.items()})
         self.write_raw_df()
     else:
         self.read_raw_df()
Ejemplo n.º 19
0
def update_index(sid, n_year):
    end_date = datetime.datetime.now().strftime('%Y-%m-%d')
    # n 年的資料
    start_date = (datetime.datetime.now()- datetime.timedelta(days=n_year*365)).strftime('%Y-%m-%d')
    SID = sid
    out_file = "測試資料/台股/{0}d.csv".format(SID)
    

    def true_val(current_date, current_val, dividend_dataframe):
        return current_val  # 不還原權值, adj close 就是還原值了
#        ret = current_val
#        for idx, dividend_date in enumerate(dividend_dataframe.index.values):
#            if dividend_date <= current_date:
#                ret = ret + dividend_dataframe.iloc[idx][0]
#        return ret

    # 分紅資料
 #   dividend_dataframe = pdr.get_data_yahoo(SID, start=start_date, end=end_date,actions='only')
    dividend_dataframe = ""
    # print(dividend_dataframe)
    time.sleep(1)

    # 日K 資料
    data = pdr.get_data_yahoo(SID, start=start_date, end=end_date)

    for idx, current_date  in enumerate(data.index.values):
        o = true_val(current_date, data.iloc[idx][0], dividend_dataframe)
        h = true_val(current_date, data.iloc[idx][1], dividend_dataframe)
        l = true_val(current_date, data.iloc[idx][2], dividend_dataframe)
        c = true_val(current_date, data.iloc[idx][3], dividend_dataframe)
        data.at[current_date,"Open"]=o
        data.at[current_date,"High"]=h
        data.at[current_date,"Low"]=l
        data.at[current_date,"Close"]=c

    print(len(data.index.values))
    data.to_csv(out_file, sep=',', encoding='utf-8')


    # download Panel
    #= pdr.get_data_yahoo(["SPY", "IWM"], start="2017-01-01", end="2017-04-30")
    #print(data3)
    time.sleep(3)
Ejemplo n.º 20
0
def graph_date(stock):
    stock_price_url = 'https://hk.finance.yahoo.com/chart/TSLA#eyJtdWx0aUNvbG9yTGluZSI6ZmFsc2UsImJvbGxpbmdlclVwcGVyQ29sb3IiOiIjZTIwMDgxIiwiYm9sbGluZ2VyTG93ZXJDb2xvciI6IiM5NTUyZmYiLCJtZmlMaW5lQ29sb3IiOiIjNDVlM2ZmIiwibWFjZERpdmVyZ2VuY2VDb2xvciI6IiNmZjdiMTIiLCJtYWNkTWFjZENvbG9yIjoiIzc4N2Q4MiIsIm1hY2RTaWduYWxDb2xvciI6IiMwMDAwMDAiLCJyc2lMaW5lQ29sb3IiOiIjZmZiNzAwIiwic3RvY2hLTGluZUNvbG9yIjoiI2ZmYjcwMCIsInN0b2NoRExpbmVDb2xvciI6IiM0NWUzZmYiLCJyYW5nZSI6IjF5In0%3D'
    souce_code = urllib.request.urlopen(stock_price_url).read().decode()
    # stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'

    #print(souce_code)
    stock_data = []
    split_source = souce_code.split('\n')

    #print(souce_code.encode("GBK", 'ignore'))
    for line in split_source:
        split_line = line.split(',')
        if len(split_line) == 6:
            if 'values' not in line:
                stock_data.append(line)
                #print(line.encode("GBK", 'ignore'))

    print(web.get_data_yahoo('AAPL','1/1/2014','20/8/2015'))

    #date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d', 'utf-8')})
        # %Y = full year. 2015
        # %y = partial year 15
        # %m = number month
        # %d = number day
        # %H = hours
        # %M = minutes
        # %S = seconds
        # 12-06-2014
        # %m-%d-%Y

    #plt.plot_data(date, closep, '-', label='Price')

    plt.xlabel('date')
    plt.ylabel('price')

    plt.title('data from web')
    plt.legend()
    plt.show()
Ejemplo n.º 21
0
    def _getDatas(self):
        combIndex = None
        for s in self.symbolList:
            self.symbolData[s] = web.get_data_yahoo(s,
                                                    start=self.startDate.strftime("%Y%m%d"),
                                                    end=self.endDate.strftime("%Y%m%d"),
                                                    adjust_price=True).sort_index()
            del self.symbolData[s]['Adj_Ratio']
            self.symbolData[s].columns = ['open', 'high', 'low', 'close', 'volume']

            if combIndex is None:
                combIndex = self.symbolData[s].index
            else:
                combIndex = combIndex.union(self.symbolData[s].index)

            self.latestSymbolData[s] = []
            self.symbolData[s] = transfromDFtoDict(self.symbolData[s])

        self.dateIndex = combIndex
        self.start = 0
        for i, s in enumerate(self.symbolList):
            if s not in self.symbolData:
                del self.symbolList[i]
Ejemplo n.º 22
0
def test_returns_verifiable(ticker):
    # Test that the quotes line up with data off of Yahoo
    now = datetime.now()
    prior_day = now - TradingDay(1)

    quotes = Tradeking1mTimesales.retrieve_data(prior_day, ticker)

    yahoo_ohlc = list(map(tuple, get_data_yahoo(ticker, prior_day, prior_day)[
        ['Open', 'High', 'Low', 'Close']
    ].values))[0]

    open = high = close = 0
    low = 999999
    for index, quote in enumerate(quotes['quotes']):
        if index == 0:
            open = quote['opn']
        if index == len(quotes['quotes']) - 1:
            close = quote['last']
        high = max(high, quote['hi'])
        low = min(low, quote['lo'])

    tradeking_ohlc = (open, high, low, close)

    assert_allclose(tradeking_ohlc, yahoo_ohlc, rtol=1e-3)
Ejemplo n.º 23
0
def get_symbol_from_yahoo(symbol, start=None, end=None):
    """Wrapper for pandas.io.data.get_data_yahoo().
    Retrieves prices for symbol from yahoo and computes returns
    based on adjusted closing prices.

    Parameters
    ----------
    symbol : str
        Symbol name to load, e.g. 'SPY'
    start : pandas.Timestamp compatible, optional
        Start date of time period to retrieve
    end : pandas.Timestamp compatible, optional
        End date of time period to retrieve

    Returns
    -------
    pandas.DataFrame
        Returns of symbol in requested period.
    """
    px = web.get_data_yahoo(symbol, start=start, end=end)
    rets = px[['Adj Close']].pct_change().dropna()
    rets.index = rets.index.tz_localize("UTC")
    rets.columns = [symbol]
    return rets
Ejemplo n.º 24
0
import pandas_datareader.data as web
from pandas.io.data import _yahoo_codes

a=web.get_quote_yahoo("MSFT")
b=_yahoo_codes.update({'MarketCap' : 'j1'})
print(a)
print(b)

a=web.get_quote_yahoo("GEN.CO")
b=_yahoo_codes.update({'52WeekLow' : 'j'})
print(a)
print(b)

print("stop")

aapl = web.get_data_yahoo("GEN.CO",'1/1/2016', '5/20/2016')
#appl = web.get_data_google("GEN",'1/1/2016', '5/20/2016')
#print(aapl.Close) # get all close price

#closeprice = ta.SMA(numpy.array(aapl.Close)) # Avoid Argument 'real' has incorrect type (expected numpy.ndarray, got Series) error. 
sma = ta.SMA(numpy.array(aapl.Close))
rsi = ta.RSI(numpy.array(aapl.Close))

macddiv, macdsignal, macdhist = ta.MACD(numpy.array(aapl.Close), fastperiod=12, slowperiod=26, signalperiod=9)

cross = macdsignal - macddiv

bollinger = 


Ejemplo n.º 25
0
 def test_get_data_single_symbol(self):
     #single symbol
     #http://finance.yahoo.com/q/hp?s=GOOG&a=09&b=08&c=2010&d=09&e=10&f=2010&g=d
     # just test that we succeed
     web.get_data_yahoo('GOOG')
Ejemplo n.º 26
0
factors = DataFrame({'f1': fac1, 'f2': fac2, 'f3': fac3}, index=ticker_subset)

print(factors.corrwith(port))
print(pd.ols(y=port, x=factors).beta)


def beta_exposure(chunk, factors=None):
    return pd.ols(y=chunk, x=factors).beta


by_ind = port.groupby(industries)
exposures = by_ind.apply(beta_exposure, factors=factors)
print(exposures.unstack())

data = web.get_data_yahoo('SPY', '2006-01-01','2012-07-27')


px = data['Adj Close']
returns = px.pct_change()


def to_index(rets):
    index = (1 + rets).cumprod()
    first_loc = max(index.notnull().index.get_loc(index.notnull().argmax()) - 1, 0)
    index.values[first_loc] = 1
    return index


def trend_signal(rets, lookback, lag):
    signal = Series.rolling(rets, lookback, min_periods=lookback - 5).std()
Ejemplo n.º 27
0
def my_form_post():
    text = request.form.get('stock')
    if("none" in text):
        my_form_post()
    y = Share(text)
    t = str(datetime.now())
    date = t[:10]
    price = float(y.get_price())
    change = float(y.get_change())
    book = float(y.get_book_value())
    p_b = 1

    if(book != 0):
        p_b = price/book

    short = float(y.get_short_ratio())
    vol = float(y.get_avg_daily_volume())
    capital = (y.get_market_cap())
    result = 0
    show = ""

    if(change == 0 and short == 0):
        result = 0
    elif(short < 2):
        result = 1
    elif(short > 3 and vol >1):
        result = -1
    elif(change < 0 and vol > 1):
        result = -1
    elif(change > 0 and vol < 1):
        if(p_b > 1):
            result = 1
        elif(p_b < 1):
            result = -1

    up = 1

    if(change < 0):
        up = -1
    show = "\nCurrent price of the stock is : " + str(price)

    if(up >= 0):
        show = show + "\nStocks are up by " + str(change)
    else:
        show = show + "\nStocks are down by : " + str(change)
        
    if(capital is not None):
        show = show + "\nMarket Capitalisation of the current stock is " + capital

    if(result == -1):
        show = show + "\n\nAccording to our analysis, the stocks are vulnerable to fall.\nSo it's not advised to invest in them."
    elif(result == 0):
        show = show + "\n\nAccording to our analysis, the stocks seem to be neutral.\nSo, nothing can be said regarding investing in them."
    else:
        show = show + "\n\nAccording to our analysis, the stocks seem to be going up.\nSo, it's the right time to invest in them."

    data1 = web.get_data_yahoo(str(text), start = '2015-01-01', end = date)[['Close']]
    data1.plot(subplots = True, figsize = (30, 12))
    plt.title(text,fontsize=30)
    plt.figtext(0.50, -0.001, show,verticalalignment='bottom', horizontalalignment='center',color='green', fontsize=15)
    plt.legend(loc = 'best')
    plt.savefig("C:/Python34/financial hackathon/static/p1.png")
    return send_from_directory("static","p1.png")
Ejemplo n.º 28
0
Archivo: api.py Proyecto: ASH1998/SuZu
st.table(df.iloc[0:10])

stocknames = df['NAME']
stocknames = ['Select Stock : '] + list(stocknames)
selection = st.selectbox("Select Intended Stock : ", stocknames)

if selection != 'Select Stock : ':
    # st.write('You selected:', format_text(option, 0, len(option)))
    st.write('Selected File Name:', selection)
    res = df[df['NAME'] == selection]
    st.dataframe(res)

    with st.spinner("Fetching Data..."):
        data = pdr.get_data_yahoo(str(res['YCODE'].iloc[0]),
                                  start="2017-01-01",
                                  end="2021-04-17")

        st.dataframe(data.tail(10))

        st.line_chart(data['Open'])
        # st.line_chart(data['High'])

# with st.spinner(text='In progress'):
#     time.sleep(5)
#     st.success('Done')

# st.balloons()

arr = np.random.normal(1, 1, size=100)
Ejemplo n.º 29
0
def _get_data_yahoo(names, start, end):
    return pdr.get_data_yahoo(names, start=start, end=end)
Ejemplo n.º 30
0
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
import yfinance as yf
from utilities import (get_betas, get_capm, portfolio_expected_returns,
                       multi_portfolio_sigma, portfolio_sigma,
                       generate_random_weights, calc_holding_returns, mvo)
from portfolio import portfolio, benchmarks

yf.pdr_override()

holdings = pd.DataFrame(portfolio).fillna(0.0)
holdings['min_weight'] = 0.0
holdings['max_weight'] = 1.0

print('---- Downloading Ticker Data ----')
holdings_th = pdr.get_data_yahoo(
    holdings['symbol'].tolist())['Adj Close'][holdings['symbol'].tolist()]
holdings_dr = holdings_th.pct_change()[-756:]
holdings_cov = holdings_dr.cov()

benchmarks = pd.DataFrame(benchmarks).set_index('symbol')
benchmarks_th = pdr.get_data_yahoo(
    benchmarks.index.tolist())['Adj Close'][benchmarks.index.tolist()]
benchmarks_dr = benchmarks_th.pct_change()[-756:]

#%%
print('---- Calculating Expected Returns ----')
holdings = calc_holding_returns(holdings, benchmarks, holdings_dr,
                                benchmarks_dr)

print('---- Generating 10000 random portfolios ----')
w = generate_random_weights(10000, len(holdings))
Ejemplo n.º 31
0
    return priceChange 

def calc(companyname):
    # 获取公司2017年股票数据
    start_date = '2017-01-01'
    end_date = '2018-01-01'
    companyDf = data.get_data_yahoo(gafataDict[companyname],start_date,end_date)
    # 累计涨幅
    closeCol = companyDf['Close']
    companyChange = change(closeCol)
    return companyChange

#获取阿里巴巴2017年的股票数据
start_date = '2017-01-01'
end_date = '2018-01-01'
babaDf=data.get_data_yahoo(gafataDict['阿里'],start_date, end_date)
#累计涨幅
closeCol=babaDf['Close']
babaChange=change(closeCol)

#谷歌
googDf=data.get_data_yahoo(gafataDict['谷歌'],start_date, end_date)
closeCol=googDf['Close']
googChange=change(closeCol)


#亚马逊
amazDf=data.get_data_yahoo(gafataDict['亚马逊'],start_date, end_date)
closeCol=amazDf['Close']
amazChange=change(closeCol)
Ejemplo n.º 32
0
import datetime
import fix_yahoo_finance as yf
from pandas_datareader import data as pdr

if __name__ == '__main__':
    # Date Ranges for SP 500 and for all tickers
    # Modify these date ranges each week.
    # The below will pull back stock prices from the start date until end date specified.
    start_sp = datetime.datetime(1992, 1, 1)
    end_sp = datetime.datetime(2018, 3, 9)
    # This variable is used for YTD performance.

    yf.pdr_override()  # <== that's all it takes :-)
    sp500 = pdr.get_data_yahoo('^GSPC', start_sp, end_sp)

    sp500.to_csv('sp500.csv')

    print(sp500.head())
Ejemplo n.º 33
0
from pandas_datareader import data as pdr

yf.pdr_override()

stock = input("enter a stock ticker symbol: ")
print(stock)

startyear = 2010
startmonth = 1
startday = 1

startdate = dt.datetime(startyear, startmonth, startday)

now = dt.datetime.now()
while stock != "quit":
    df = pdr.get_data_yahoo(stock, startdate, now)
    df.drop(df[df["Volume"] < 1000].index, inplace=True)
    dfmonth = df.groupby(pd.Grouper(freq="M"))["High"].max()
    glDate = 0
    lastGLV = 0
    currentDate = ""
    currentGLV = 0
    for index, value in dfmonth.items():
        if (value > currentGLV):
            currentGLV = value
            currentDate = index
            counter = 0
        if (value < currentGLV):
            counter = counter + 1
            if (counter == 3 and ((index.month != now.month) or
                                  (index.year != now.year))):
Ejemplo n.º 34
0
import pandas_datareader.data as pdr
import yfinance as yf
import datetime as dt
import pandas as pd
import yahoo_fin.stock_info as si

yf.pdr_override()

tickers = si.tickers_sp500()
tickers = [item.replace('.', '-') for item in tickers]

num_of_years = 10
start = dt.date.today() - dt.timedelta(days=int(365.25 * num_of_years))

for ticker in tickers:
    try:
        df = pdr.get_data_yahoo(ticker, start)
        df.to_csv(
            f'/Users/shashank/Documents/Code/Python/Outputs/S&P500/{ticker}.csv'
        )
        print(f'{ticker} is done')
    except:
        continue
Ejemplo n.º 35
0
def plotTsS2(start_date, end_date):

    plt.close()
    yf.pdr_override()

    data = pdr.get_data_yahoo("BTC-USD", start_date, end_date)
    btc = pd.DataFrame(data)

    btc = btc[['Adj Close']]
    btc.columns = ['Close']
    btc.reset_index(level=0, inplace=True)

    b = pd.DataFrame()

    for x in btc:
        b['price'] = btc['Close']
        b['sma'] = btc['Close'].rolling(window=20).mean()
        b['std'] = btc['Close'].rolling(window=20).std()

        b['bolU'] = b['sma'] + (2 * b['std'])  #Calculating Upper Bound
        b['bolD'] = b['sma'] - (2 * b['std'])  #Calculating Lower Bound
        #Convert Bollinger Bands to %b - bollinger column
        b['bollinger'] = (b['price'] - b['bolD']) / (b['bolU'] - b['bolD'])

    bb1 = b[['price', 'bolU', 'bolD', 'bollinger']]
    bb1.columns = ['Price', 'Upper Band', 'Lower Band', 'Bollinger']
    bb1.fillna(0, inplace=True)

    # In[ ]:

    # In[9]:

    RSI = pd.DataFrame(index=btc.index)

    RSI['price'] = btc['Close']

    RSI['val'] = None
    RSI['up'] = 0  #When there is no up  value 0
    RSI['down'] = 0  #When there is no down   value 0

    size = RSI.shape[0]
    dp = 14

    for x in range(size):
        if x == 0:
            continue  #first day will continue
        #calculating the ups , when closing price is higher in day x than x -1
        if RSI['price'].iloc[x] > RSI['price'].iloc[x - 1]:  #
            RSI['up'].iloc[x] = RSI['price'].iloc[x] - RSI['price'].iloc[x - 1]
        else:
            #calculating the downs days , when closing price is lower in day x than x -1
            RSI['down'].iloc[x] = RSI['price'].iloc[x -
                                                    1] - RSI['price'].iloc[x]

        if x >= dp:
            avgUp = RSI['up'][x - dp:x].sum(
            ) / dp  #calculates avg up of last dp days
            avgDown = RSI['down'][x - dp:x].sum(
            ) / dp  #calculates avg down of last dp days
            rs = avgUp / avgDown  #calculation of RS
            RSI['val'].iloc[x] = 100 - 100 / (1 + rs)

    signals = pd.DataFrame(index=btc.index)  #copy index for BTC

    signals['price'] = btc['Close']
    signals['id'] = 0.0

    signals['RSI'] = RSI['val']
    signals['RSI'].fillna(0, inplace=True)

    signals['bollinger'] = bb1['Bollinger']

    signals['id'] = [np.nan for i in signals.index]

    # only  verifications for days after DPth (period of RSI) day
    signals['id'][dp:].loc[((signals['RSI'] < 30) &
                            (signals['bollinger'] < 0))] = 1
    signals['id'][dp:].loc[((signals['RSI'] > 70) &
                            (signals['bollinger'] > 1))] = 0
    signals['id'].ffill(inplace=True)  #fill empty values with 0
    signals['id'].fillna(0, inplace=True)

    signals['buySell'] = signals['id'].diff()
    signals['buySell'].fillna(0, inplace=True)

    fig, (ax) = plt.subplots(1, 1)

    ax.plot(signals['price'])
    ax.set_xlabel('Date')
    ax.set_ylabel('Value in $')
    day = signals.loc[signals.buySell == 1.0].index
    day2 = signals.loc[signals.buySell == -1.0].index

    #x cordinates, index of day    --> y cordinate,price of day index
    ax.scatter(
        x=day,
        y=signals.loc[day, 'price'],
        marker='^',
        color='green',
    )
    ax.scatter(x=day2, y=signals.loc[day2, 'price'], marker='v', color='red')

    plt.show()
Ejemplo n.º 36
0
# Variables
tickers = si.tickers_sp500()
tickers = [item.replace(".", "-")
           for item in tickers]  # Yahoo Finance uses dashes instead of dots
index_name = '^GSPC'  # S&P 500
start_date = datetime.datetime.now() - datetime.timedelta(days=365)
end_date = datetime.date.today()
exportList = pd.DataFrame(columns=[
    'Stock', "RS_Rating", "50 Day MA", "150 Day Ma", "200 Day MA",
    "52 Week Low", "52 week High"
])
returns_multiples = []

# Index Returns
index_df = pdr.get_data_yahoo(index_name, start_date, end_date)
index_df['Percent Change'] = index_df['Adj Close'].pct_change()
index_return = (index_df['Percent Change'] + 1).cumprod()[-1]

# Find top 30% performing stocks (relative to the S&P 500)
for ticker in tickers:
    # Download historical data as CSV for each stock (makes the process faster)
    df = pdr.get_data_yahoo(ticker, start_date, end_date)
    df.to_csv(f'{ticker}.csv')

    # Calculating returns relative to the market (returns multiple)
    df['Percent Change'] = df['Adj Close'].pct_change()
    stock_return = (df['Percent Change'] + 1).cumprod()[-1]

    returns_multiple = round((stock_return / index_return), 2)
    returns_multiples.extend([returns_multiple])
Ejemplo n.º 37
0
 def test_get_data_multiple_symbols(self):
     # just test that we succeed
     sl = ['AAPL', 'AMZN', 'GOOG']
     web.get_data_yahoo(sl, '2012')
Ejemplo n.º 38
0
 def getStockData(self, tickers, st, ed):
     """ Get data from yahoo Finance"""
     data = pdr.get_data_yahoo(tickers, start = st, end = ed)[['High','Low',\
                              'Open','Adj Close','Volume']]
     data.columns = ['high', 'low', 'open', 'close', 'volume']
     return data
Ejemplo n.º 39
0
import numpy as np

# For Visualization
import matplotlib.pyplot as plt
import seaborn as sns
# For data retrival
from pandas_datareader import data, wb
from datetime import datetime
sns.set_style('whitegrid')
# Let us consider the stocks for various tech gaints
tech_list = ['AAPL', 'GOOG', 'MSFT', 'AMZN', 'C']
end = datetime(2020, 6, 1)
start = datetime(end.year - 1, end.month, end.day)

for stock in tech_list:
    globals()[stock] = data.get_data_yahoo(stock, start, end)

# AAPL['Adj Close'].plot(legend = True,figsize=(10,4))
AAPL['Volume'].plot(legend=True, figsize=(10, 4))

# Finding moving averages for fav AAPL stock

ma_day = [10, 20, 50]
for ma in ma_day:
    column_name = "MA for days" + str(ma)
    AAPL[column_name] = AAPL['Adj Close'].rolling(ma).mean()
# To understand the flowing line and trading averages
AAPL[['Adj Close', 'MA for days10', 'MA for days20',
      'MA for days50']].plot(subplots=False, figsize=(10, 4))

# To check for daily returns
Ejemplo n.º 40
0
4. When de MACD line cuts the signal line from below = bullish period, cuts the signal from above = bearish period

Cons: Many false positives (especially during sideways markets). It's a lagging indicator.

- People use it in conjunction with another indicator.
'''

# Import necesary libraries
import pandas_datareader.data as pdr
import datetime
import matplotlib.pyplot as plt

# Download historical data for required stocks
ticker = "MSFT"
ohlcv = pdr.get_data_yahoo(ticker,
                           datetime.date.today() - datetime.timedelta(1825),
                           datetime.date.today())


def MACD(DF, a, b, c):
    """function to calculate MACD
       typical values a = 12; b =26, c =9"""
    df = DF.copy(
    )  #Copy because i don't wanna make any change in the original DF
    df["MA_Fast"] = df["Adj Close"].ewm(span=a, min_periods=a).mean()
    df["MA_Slow"] = df["Adj Close"].ewm(span=b, min_periods=b).mean()
    df["MACD"] = df["MA_Fast"] - df["MA_Slow"]
    df["Signal"] = df["MACD"].ewm(span=c, min_periods=c).mean()
    df.dropna(inplace=True)  #Delete the NaN rows
    return df
import pandas as pd
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()
 
dow = pdr.get_data_yahoo('^DJI', '2000-01-04')
kospi = pdr.get_data_yahoo('^KS11', '2000-01-04')

df = pd.DataFrame({'DOW' dow['Close'], 'KOSPI' kospi['Close']})
df = df.fillna(method='bfill')
df = df.fillna(method='ffill')

import matplotlib.pyplot as plt
plt.figure(figsize=(7, 7))
plt.scatter(df['DOW'], df['KOSPI'], marker='.')
plt.xlabel('Dow Jones Industrial Average')
plt.ylabel('KOSPI')
plt.show()
Ejemplo n.º 42
0
def stockHistory(symbol, startDate, endDate, code):
    stock = pdr.get_data_yahoo(symbol, start=startDate, end=endDate)

    # Assign `Adj Close` to `daily_close`
    daily_close = stock[['Adj Close']]

    # Daily returns
    daily_pct_change = daily_close.pct_change()

    # Replace NA values with 0
    daily_pct_change.fillna(0, inplace=True)

    stock['pctChange'] = stock[['Adj Close']].pct_change()

    # stock['logDelta'] = np.log(daily_close.pct_change()+1)

    stock.fillna(0, inplace=True)

    stock['delta'] = "Test"
    stock['EMA12'] = 0.0
    stock['EMA26'] = 0.0
    stock['MACD'] = 0.0
    stock['MACD Signal'] = 0.0
    stock['MACD Trend'] = 0.0
    stock['RSI'] = 0.0
    stock['RSI Trend'] = 0.0
    stock['Stochastic'] = 0.0
    stock['Stochastic SMA'] = 0.0

    probabilities = [0, 0, 0]
    transitions = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    transitionsDict = {}
    for key in [('Up', 'Up'), ('Up', 'Neutral'), ('Up', 'Down')]:
        transitionsDict[key] = 0
    for key in [('Neutral', 'Up'), ('Neutral', 'Neutral'),
                ('Neutral', 'Down')]:
        transitionsDict[key] = 0
    for key in [('Down', 'Up'), ('Down', 'Neutral'), ('Down', 'Down')]:
        transitionsDict[key] = 0

    sums = 0
    classes = []

    for i in range(0, len(stock.index)):
        # test.append(stock.iloc[i]['pctChange'])
        result = convert(stock.iloc[i]['pctChange'], probabilities)
        stock['delta'][i] = result[0]
        classes.append(result[2])
        if i > 0:
            transitionsDict[(result[0], stock['delta'][i - 1])] += 1
            if stock['delta'][i - 1] is "Up":
                transitions[result[2]][0] += 1
                sums += 1
            elif stock['delta'][i - 1] is "Neutral":
                transitions[result[2]][1] += 1
                sums += 1
            elif stock['delta'][i - 1] is "Down":
                transitions[result[2]][2] += 1
                sums += 1

        probabilities = result[1]

    probabilitiesPct = []
    summed = probabilities[0] + probabilities[1] + probabilities[2]
    probabilitiesPct = [
        probabilities[0] / summed, probabilities[1] / summed,
        probabilities[2] / summed
    ]

    print(transitions)
    stock = ema(stock, 12, 'EMA')
    stock = ema(stock, 26, 'EMA')
    stock = macd(stock)
    stock = ema(stock, 9, 'MACD')
    stock = rsi(stock, 14)
    stock = stochastic(stock, 14)

    # print(stock)
    stock.to_csv(symbol + code + ".csv")
    return (stock, classes, transitions)
Ejemplo n.º 43
0
def plotPorts2(start_date, end_date):
    plt.close()
    yf.pdr_override()

    data = pdr.get_data_yahoo("BTC-USD", start_date, end_date)
    btc = pd.DataFrame(data)

    btc = btc[['Adj Close']]
    btc.columns = ['Close']
    btc.reset_index(level=0, inplace=True)

    # In[8]:

    b = pd.DataFrame()

    for x in btc:
        b['price'] = btc['Close']
        b['sma'] = btc['Close'].rolling(window=20).mean()
        b['std'] = btc['Close'].rolling(window=20).std()

        b['bolU'] = b['sma'] + (2 * b['std'])  #Calculating Upper Bound
        b['bolD'] = b['sma'] - (2 * b['std'])  #Calculating Lower Bound
        #Convert Bollinger Bands to %b - bollinger column
        b['bollinger'] = (b['price'] - b['bolD']) / (b['bolU'] - b['bolD'])

    bb1 = b[['price', 'bolU', 'bolD', 'bollinger']]
    bb1.columns = ['Price', 'Upper Band', 'Lower Band', 'Bollinger']
    bb1.fillna(0, inplace=True)

    # In[ ]:

    # In[9]:

    RSI = pd.DataFrame(index=btc.index)

    RSI['price'] = btc['Close']

    RSI['val'] = None
    RSI['up'] = 0  #When there is no up  value 0
    RSI['down'] = 0  #When there is no down   value 0

    size = RSI.shape[0]
    dp = 14

    for x in range(size):
        if x == 0:
            continue  #first day will continue
        #calculating the ups , when closing price is higher in day x than x -1
        if RSI['price'].iloc[x] > RSI['price'].iloc[x - 1]:  #
            RSI['up'].iloc[x] = RSI['price'].iloc[x] - RSI['price'].iloc[x - 1]
        else:
            #calculating the downs days , when closing price is lower in day x than x -1
            RSI['down'].iloc[x] = RSI['price'].iloc[x -
                                                    1] - RSI['price'].iloc[x]

        if x >= dp:
            avgUp = RSI['up'][x - dp:x].sum(
            ) / dp  #calculates avg up of last dp days
            avgDown = RSI['down'][x - dp:x].sum(
            ) / dp  #calculates avg down of last dp days
            rs = avgUp / avgDown  #calculation of RS
            RSI['val'].iloc[x] = 100 - 100 / (1 + rs)

    signals = pd.DataFrame(index=btc.index)  #copy index for BTC

    signals['price'] = btc['Close']
    signals['id'] = 0.0

    signals['RSI'] = RSI['val']
    signals['RSI'].fillna(0, inplace=True)

    signals['bollinger'] = bb1['Bollinger']

    signals['id'] = [np.nan for i in signals.index]

    # only  verifications for days after DPth (period of RSI) day
    signals['id'][dp:].loc[((signals['RSI'] < 30) &
                            (signals['bollinger'] < 0))] = 1
    signals['id'][dp:].loc[((signals['RSI'] > 70) &
                            (signals['bollinger'] > 1))] = 0
    signals['id'].ffill(inplace=True)  #fill empty values with 0
    signals['id'].fillna(0, inplace=True)

    signals['buySell'] = signals['id'].diff()
    signals['buySell'].fillna(0, inplace=True)

    ###################################################################################
    # Code taken from Willems, K., 2019. (Tutorial) Python For Finance: Algorithmic Trading

    initInvestment = 100000
    stocksOwned = pd.DataFrame(index=signals.index).fillna(0.0)

    noCur = 10  #No of currency to be purchased

    stocksOwned['BTC'] = noCur * signals['id']

    portfolio = pd.DataFrame(index=signals.index)
    portfolio['Holdings'] = stocksOwned['BTC'].multiply(btc['Close'], axis=0)

    buySell = stocksOwned['BTC'].diff()

    portfolio['cash'] = initInvestment - (buySell.multiply(btc['Close'],
                                                           axis=0)).cumsum()

    portfolio['total'] = portfolio['cash'] + portfolio['Holdings']

    portfolio['cash'][0] = initInvestment
    portfolio['total'][0] = initInvestment

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

    # In[50]:

    fig, (ax) = plt.subplots(1, 1, sharex=True)

    ax.plot(portfolio.index, portfolio['total'], label='Price')
    ax.set_xlabel('Date')
    ax.set_ylabel('Value of portfolio in USD')
    day = signals.loc[signals.buySell == 1.0].index
    day2 = signals.loc[signals.buySell == -1.0].index

    ax.scatter(x=day, y=portfolio.loc[day, 'total'], color='green')
    ax.scatter(x=day2, y=portfolio.loc[day2, 'total'], color='red')

    plt.show()
Ejemplo n.º 44
0
    "PG", "TRV", "UTX", "UNH", "VZ", "V", "WMT", "DIS"
]  # Constituents of dow jones as of 2014-02-01 (except dow chemicals because Yahoo didn't have all the data)

ohlc_mon = {}  # directory with ohlc value for each stock
attempt = 0  # initializing passthrough variable
drop = [
]  # initializing list to store tickers whose close price was successfully extracted
while len(tickers) != 0 and attempt <= 5:
    tickers = [
        j for j in tickers if j not in drop
    ]  # removing stocks whose data has been extracted from the ticker list
    for i in range(len(tickers)):
        try:
            ohlc_mon[tickers[i]] = pdr.get_data_yahoo(tickers[i],
                                                      datetime.date.today() -
                                                      datetime.timedelta(2419),
                                                      datetime.date.today(),
                                                      interval='m')
            ohlc_mon[tickers[i]].dropna(inplace=True)
            drop.append(tickers[i])
        except:
            print(tickers[i], " :failed to fetch data...retrying")
            continue
    attempt += 1

tickers = ohlc_mon.keys(
)  # redefine tickers variable after removing any tickers with corrupted data

################################Backtesting####################################

# calculating monthly return for each stock and consolidating return info by stock in a separate dataframe
Ejemplo n.º 45
0
def test_yfinance():
    for symbol in ['MSFT', 'IWO', 'VFINX','BTC-USD']:
        print(">>", symbol, end=' ... ')
        data = pdr.get_data_yahoo(symbol,start='2020-09-25', end='2020-10-02')
        print(data)
Ejemplo n.º 46
0
def download_historical(ticker: str, period: str, interval: str):
    data = pdr.get_data_yahoo(ticker, period=period, interval=interval)
    return data
Ejemplo n.º 47
0
 def data(ticker):
     return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
Ejemplo n.º 48
0
def get_history(symbols, start, end, data_path, visible=False):

    from pandas_datareader import data as y_data
    """ to get Yahoo data from saved csv files. If the file does not exist for the symbol, 
    data is read from Yahoo finance and the csv saved.
    symbols: symbol list
    start, end : datetime start/end dates
    data_path : datapath for csv files - use double \\ and terminate path with \\
    """

    symbols_ls = list(symbols)
    for ticker in symbols:
        print(ticker, )
        try:
            #see if csv data available
            data = pd.read_csv(data_path + ticker + '.csv',
                               index_col='Date',
                               parse_dates=True)
        except:
            #if no csv data, create an empty dataframe
            data = pd.DataFrame(data=None, index=[start])

        #check if there is data for the start-end data range

        if start.toordinal() < data.index[0].toordinal() \
                             or end.toordinal() > data.index[-1].toordinal():

            if visible:
                print('Refresh data.. ', )
            try:
                new_data = y_data.get_data_yahoo(ticker, start, end)

                if new_data.empty == False:
                    if data.empty == False:
                        try:
                            ticker_data = data.append(new_data).groupby(
                                level=0, by=['rownum']).last()
                        except:
                            print('Merge failed.. ')
                    else:
                        ticker_data = new_data
                    try:
                        ticker_data.to_csv(data_path + ticker + '.csv')
                        if visible:
                            print(' UPDATED.. ')
                    except:
                        print('Save failed.. ')
                else:
                    if visible:
                        print('No new data.. ')
            except:
                print('Download failed.. ')
                # remove symbol from list
                symbols_ls.remove(ticker)
        else:
            if visible:
                print('OK.. ')
        pass

    pdata = pd.Panel(dict((symbols_ls[i], pd.read_csv(data_path + symbols_ls[i] + '.csv',\
                     index_col='Date', parse_dates=True).sort_index(ascending=True)) for i in range(len(symbols_ls))) )

    return pdata.iloc[:, start:end, :]
Ejemplo n.º 49
0
 def test_get_data_adjust_price(self):
     goog = web.get_data_yahoo('GOOG')
     goog_adj = web.get_data_yahoo('GOOG', adjust_price=True)
     self.assertTrue('Adj Close' not in goog_adj.columns)
     self.assertTrue((goog['Open']*goog_adj['Adj_Ratio']).equals(goog_adj['Open']))
    print('The RMS error is {}'.format(rms))

    return {'loss': rms, 'status': STATUS_OK, 'model': model}


#'main' program
if __name__ == '__main__':

    #data_df = pd.read_csv('GOOG_2015-04-01_2020-03-31.csv', index_col='Date', parse_dates=True)
    #using pandas_datareader library to get the data from Yahoo-Finance
    start_date = datetime(2015, 4, 1)
    end_date = datetime(2020, 3, 31)
    ticker = 'GOOG'

    data_df = pdr.get_data_yahoo(tickers=ticker,
                                 start=start_date,
                                 end=end_date)

    #In our model, we will try to predict the future close price of a stock using only the past
    #close prices of that particular stock. So let's a create a new dataframe with only the
    #'Date' and 'Close' price columns
    new_data = pd.DataFrame(index=range(0, len(data_df)),
                            columns=['Date', 'Close'])
    for i in range(0, len(data_df)):
        new_data['Date'][i] = data_df.index[i]
        new_data['Close'][i] = data_df['Close'][i]

    #setting 'Date' column as index and dropping the original column
    new_data.index = new_data.Date
    new_data.drop('Date', axis=1, inplace=True)
Ejemplo n.º 51
0
def GetStockQuote(symbol) :
    quote = web.get_data_yahoo(symbol,thirty_days.strftime("%m/%d/%Y"), time.strftime("%m/%d/%Y"))
    #quote = web.get_data_yahoo(symbol,thirty_days.strftime("%m/%d/%Y"), twenty_days.strftime("%m/%d/%Y"))
    return quote
Ejemplo n.º 52
0
import pandas as pd 
# to correct is_list_like method which as change from pandas.core.common to pandas.api.types
pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader import data as pdr 
import fix_yahoo_finance as yf 
yf.pdr_override()

# download dataframe
start_date  = '2007-01-01'
end_date 	= '2018-07-26'

firstreits 	= 'AW9U.SI'
silverlake 	= '5CP.SI'
perennial  	= 'P22.SI'
oue 		= 'LJ3.SI'

mystocklist = [firstreits, silverlake, perennial, oue]



for stock in mystocklist:
	try:
		print('Reading stock code {0}'.format(stock))
		sdata = pdr.get_data_yahoo(stock, start_date, end_date)		
		sdata.to_csv(stock + '.csv')
	except Exception as e:
		sdata.to_csv(stock + '.csv')
		print("{0} encounter problem {1}".format(stock, e.args))
Ejemplo n.º 53
0
os.chdir('/home/vesis/Documents/Python/Portfolio_analysis')
init_notebook_mode(connected=True)

# Define the date variables
# Make a function considering different opening dates and the time difference(!!!)
start_omxh = datetime.datetime(2011, 12, 29)
end_omxh = datetime.datetime(2020, 12, 29)
end_of_last_year = datetime.datetime(2019, 12, 30)
start_stocks = datetime.datetime(2011, 12, 29)
end_stocks = datetime.datetime(2020, 12, 29)

# DATA PULL
# Pull OMXH25 data from Yahoo! Finance
# Form a subset containing only daily closing values
# Define last year's closing value for YTD calculation
omxh25 = pdr.get_data_yahoo('^OMXH25', start_omxh, end_omxh)
omxh25_adj_close = omxh25[['Adj Close']].reset_index()
omxh25_adj_close_start = omxh25_adj_close[omxh25_adj_close['Date'] ==
                                          end_of_last_year]

# PORTFOLIO DATA PULL #
# Read in the analyzed portfolio and pull corresponding data from Yahoo! Finance
portfolio_df = pd.read_excel('Stocks_v3.xls')
tickers = portfolio_df['Ticker'].unique()


# Function for pulling the ticker data
def get(tickers, startdate, enddate):
    def data(ticker):
        return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
Ejemplo n.º 54
0
cf.go_offline()

# Defining today's Date
from datetime import date
today = date.today()

from pandas_datareader import data as pdr

import yfinance as yf
yf.pdr_override()  # <== that's all it takes :-)

import PortfolioLab as pl

# %%
# download dataframe using pandas_datareader
data = pdr.get_data_yahoo("^GSPC", start="1970-12-21",
                          end="2021-04-30")[['Close']]

# %%
data.columns = ['S&P 500']

# %%
data['DD'] = pl.compute_drawdowns(data)
# %%
data['<10'] = data['DD'].apply(lambda x: 1 if x < -10 else 0)

# %%
data['Shade'] = True
# %%
data
# %%
for i in range(len(data) - 1):
# print title of web app
st.title("Stock Market Analysis and Prediction")
st.markdown(
    "> Stock Market Analysis and Prediction is the project on technical analysis, visualization and prediction using data provided by Yahoo Finance."
)
st.markdown(
    "> It is web app which predicts the future value of company stock or other financial instrument traded on an exchange."
)

# Create a text element and let the reader know the data is loading.
data_load_state = st.text('Loading data...')

# Load data from yahoo finance.
start = dt.date(2010, 1, 1)
end = dt.date.today()
data = pdr.get_data_yahoo("GOOG", start, end)

#fill nan vale with next value within columns
data.fillna(method="ffill", inplace=True)

# Notify the reader that the data was successfully loaded.
data_load_state.text('Loading data...done!')

# create checkbox
if st.checkbox('Show raw data'):
    st.subheader('Raw data')
    st.write(data)

# show the description of data
st.subheader('Detail description about Datasets:-')
descrb = data.describe()
Ejemplo n.º 56
0
mean_prediction_error_array = []

for s in epoch:
    epoch = s
    from numpy.random import seed
    seed(0)
    # from tensorflow import set_random_seed
    # set_random_seed(1)

    # Split Method 1 is 70% train / 20% test / 10% predict
    ################################################################################################################

    ################################################# Get Stock Data ###############################################

    yf.pdr_override()  # <== that's all it takes :-)
    df = pdr.get_data_yahoo(ticker, period='5d', interval='1m')

    # if Load_Saved_Ticker_data == False:
    #     df = web.DataReader(ticker, 'yahoo', start, end)
    #     df.to_csv("C:/Projects/Stock Tools/Stock Data/{ticker}.csv".format(ticker=ticker))
    # if Load_Saved_Ticker_data == True:
    #     df = pd.read_csv("C:/Projects/Stock Tools/Stock Data/{ticker}.csv".format(ticker=ticker))
    df = df['Adj Close'].values
    df = df.reshape(-1, 1)
    print('Stock data Loaded')

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

    ################################################# Training / Test ##############################################
    if split_method == 1:
        dataset_train = np.array(df[:int(df.shape[0] * 0.8)])
Ejemplo n.º 57
0
#e6-3-3: 上網路抓取股價資料畫高低圖
import pandas as pd
from pandas_datareader import data as web
import fix_yahoo_finance
#下載資料起始日與股票代號
start = "2017-01-01"
end = "2017-04-30"
df = web.get_data_yahoo('2317.tw', start, end)
#日股價資料寫入excel檔
writer = pd.ExcelWriter('2317.xlsx')
df.to_excel(writer, '2317')
workbook = writer.book
worksheet = writer.sheets['2317']
#畫高低圖
chart = workbook.add_chart({'type': 'stock'})
chart.add_series({
    'name': '=2317!$B$1',
    'categories': '=2317!$A$2:$A$14',
    'values': '=2317!$B$2:$B$14'
})
chart.add_series({
    'name': '=2317!$C$1',
    'categories': '=2317!$A$2:$A$14',
    'values': '=2317!$C$2:$C$14'
})
chart.add_series({
    'name': '=2317!$D$1',
    'categories': '=2317!$A$2:$A$14',
    'values': '=2317!$D$2:$D$14'
})
chart.set_title({'name': 'High-Low-Close'})
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like #is_list_like is moved to pandas.api.types
import pandas_datareader.data as pdr  #.data allows you to extract information from Yahoo
import datetime as dt

ticker = 'AMZN'
start_date = dt.date.today() - dt.timedelta(365)
end_date = dt.date.today()

#extract daily data (smallest granuality)
data = pdr.get_data_yahoo(ticker, start_date, end_date)
print(data) #extracts daily data - open, high, adj colse and volume

#get monthly data
data_monthly = pdr.get_data_yahoo(ticker, start_date, end_date, interval = 'm')
print(data_monthly) #returns monthly data



Ejemplo n.º 59
0
## 최대손실 낙폭

# MDD(Maximum Drawdown, 최대손실 낙폭)은 특정 기간에 발생한 최고점에서 최저점까지의 가장 큰
# 손실을 의미한다. 퀀트 투자에서 수익률을 높이는 것보다 MDD를 낮추는 것이 더 중요하다.
# 특정 기간 동안 최대한 얼마의 손실이 일어날 수 있는지를 나타낸다.

# MDD = (최저점 - 최고점) / 최저점

from pandas_datareader import data as pdr
import yfinance as yf

yf.pdr_override()
import matplotlib.pyplot as plt

# KOSPI 지수의 심볼은 ^KS11 이다.
kospi = pdr.get_data_yahoo('^KS11', '2004-01-04')

# 산정 기간에 해당하는 window 값은 1년 동안 개장일을 252일로 설정한다.
window = 252
# KOSPI 종가 컬럼에서 1년(거래일 기준) 기간 단위로 최고치 peak를 구한다.
peak = kospi['Adj Close'].rolling(window, min_periods=1).max()

# drawdown 은 최고치(peak) 대비 현재 KOSPI 종가가 얼마나 하락 했는지 구한다.
drawdown = kospi['Adj Close'] / peak - 1.0

# drawdown 에서 1년 기간 단위로 최저치 min_dd 를 구한다.
# 마이너스값이기 때문에 최저치가 바로 최대 손실 낙폭이 된다.

### rolling -> 앞에 있는 숫자 만큼씩 묶어서 계산(7일선, 30일선 등과 같은 걸 계산할 때)
max_dd = drawdown.rolling(window, min_periods=1).min()
Ejemplo n.º 60
0
end_date = "2020-02-14"
date_now = datetime.strftime(now, "%Y%m%d")

# for multiple stock tickers from csv

if ticker1 == "":
    read_csv = pd.read_csv('./db/0.etf_db_metadata.csv')
    tickers = read_csv['Symbol']
    tickers = pd.Series(tickers)
    tickers.drop_duplicates(inplace=True)
    tickers.reset_index(drop=True, inplace=True)
    tickers.sort_values(ascending=True, inplace=True)
    print(tickers)
    for i in tqdm(range(len(tickers))):
        ticker2 = tickers[i].replace('.', '-')
        data = pdr.get_data_yahoo(ticker2, start=start_date, end=end_date)
        data = data[['Open', 'High', 'Low', 'Close', 'Volume']]
        data = pd.DataFrame(data)
        data = round(data, 2)
        savename = './db/' + ticker2 + '.csv'
        data.to_csv(savename)

# get one simply

else:
    data = pdr.get_data_yahoo(ticker1, start=start_date, end=end_date)
    data = data[['Open', 'High', 'Low', 'Close', 'Volume']]
    data = pd.DataFrame(data)
    data = round(data, 2)
    savename = './db/' + ticker1 + '.csv'
    data.to_csv(savename)