Beispiel #1
0
def update_price_data(path,
                      ticker,
                      _start_date='2009-10-01',
                      _end_date=str(datetime.datetime.now())[:10]):
    file = path + ticker + '.h5'
    start_date_DT = datetime.datetime.strptime(
        pd.read_hdf(file, 'start_date').iloc[-1], '%Y-%m-%d')
    end_date_DT = datetime.datetime.strptime(
        pd.read_hdf(file, 'end_date').iloc[-1], '%Y-%m-%d')
    _start_date_DT = datetime.datetime.strptime(_start_date, '%Y-%m-%d')
    _end_date_DT = datetime.datetime.strptime(_end_date, '%Y-%m-%d')

    retrieved_at = pd.read_hdf(file, 'retrieved_at')
    updated_at = str(datetime.datetime.now())[:10]
    sort_by_date_ascending = pd.read_hdf(file,
                                         'sort_by_date_ascending').iloc[-1]
    freq = pd.read_hdf(file, 'freq').iloc[-1]

    df = pd.read_hdf(file, 'df')

    if _start_date_DT < start_date_DT:
        yf = YahooFinancials(ticker)
        END = str(start_date_DT - datetime.timedelta(days=1))[:10]
        price_data = clean_stock_data(
            yf.get_historical_stock_data(_start_date, END,
                                         freq)[ticker]['prices'])
        pd_price_data = pd.DataFrame(price_data)
        print("The downloaded price data has shape" + str(pd_price_data.shape))
        df = df.append(pd_price_data, ignore_index=True)

    if _end_date_DT > end_date_DT:
        yf = YahooFinancials(ticker)
        START = str(end_date_DT + datetime.timedelta(days=1))[:10]
        price_data = clean_stock_data(
            yf.get_historical_stock_data(START, _end_date,
                                         freq)[ticker]['prices'])
        pd_price_data = pd.DataFrame(price_data)
        print("The downloaded price data has shape" + str(pd_price_data.shape))
        df = df.append(pd_price_data, ignore_index=True)

    if sort_by_date_ascending:
        df.sort_values('date', ascending=True, inplace=True)
        df.reset_index(inplace=True, drop=True)

    df.to_hdf(file, key='df', mode='w')
    pd.Series(df['formatted_date'].iloc[0]).to_hdf(file, key='start_date')
    pd.Series(df['formatted_date'].iloc[-1]).to_hdf(file, key='end_date')
    pd.Series(freq).to_hdf(file, key='freq')
    pd.Series(sort_by_date_ascending).to_hdf(file,
                                             key='sort_by_date_ascending')
    pd.Series(retrieved_at).to_hdf(file, key='retrieved_at')
    pd.Series(updated_at).to_hdf(file, key='updated_at')

    updated_at = str(datetime.datetime.now())[:10]
    pd.Series(updated_at).to_hdf(file, key='updated_at')
    print(str(ticker) + "'s" + " price data is updated")
Beispiel #2
0
def getStockInfoList(ticker_symbol_list):
    stock = YahooFinancials(ticker_symbol_list)
    stock_info = stock.get_stock_price_data()
    return_info = dict()
    result = dict()

    weeks_ago = (datetime.datetime.today() -
                 datetime.timedelta(days=14)).strftime('%Y-%m-%d')
    tomorrow = (datetime.datetime.today() +
                datetime.timedelta(days=1)).strftime('%Y-%m-%d')
    stock_price = stock.get_historical_stock_data(weeks_ago, tomorrow, "daily")

    for i in range(len(ticker_symbol_list)):
        fullname = stock_info[ticker_symbol_list[i]]['longName']
        return_info['symbol'] = ticker_symbol_list[i]
        return_info['company'] = fullname
        return_info['quantity'] = 0
        for j in range(5):
            return_info.setdefault('dates', []).append(stock_price[
                ticker_symbol_list[i]]['prices'][j]['formatted_date'])
            return_info.setdefault('prices', []).append(
                stock_price[ticker_symbol_list[i]]['prices'][j]['close'])
        result.setdefault('stocks', []).append(return_info)
        return_info = {}

    return result
Beispiel #3
0
def download_yahoo(ticker: str,
                   base_dir: str = default_data_dir) -> pd.DataFrame:
    try:
        yf = YahooFinancials(ticker)
        data = yf.get_historical_stock_data(dt_str(start_date),
                                            dt_str(end_date), 'daily')
    except Exception as err:
        print(f'Unable to read data for {ticker}: {err}')
        return pd.DataFrame({})

    if data.get(ticker) is None or data[ticker].get('prices') is None or \
            data[ticker].get('timeZone') is None or len(data[ticker]['prices']) == 0:
        print(f'Yahoo: no data for {ticker}')
        return pd.DataFrame({})

    prices = {}
    for rec in sorted(data[ticker]['prices'], key=lambda r: r['date']):
        if rec.get('type') is None:
            date = datetime.strptime(rec['formatted_date'], '%Y-%m-%d')
            dic_with_prices(prices, ticker, date, rec['open'], rec['high'],
                            rec['low'], rec['close'], rec['volume'])

    frame = pd.DataFrame.from_dict(
        prices,
        orient='index',
        columns=['Open', 'High', 'Low', 'Close', 'Volume'])
    save_csv(base_dir, ticker, frame, 'yahoo')
Beispiel #4
0
def download_price_data(path, ticker, freq, start_date, end_date,
                        sort_by_date_ascending):
    yf = YahooFinancials(ticker)
    price_data = clean_stock_data(
        yf.get_historical_stock_data(start_date, end_date,
                                     freq)[ticker]['prices'])
    pd_price_data = pd.DataFrame(price_data)
    print("The downloaded price data has shape" + str(pd_price_data.shape))

    retrieved_at = updated_at = str(datetime.datetime.now())[:10]

    if sort_by_date_ascending:
        pd_price_data.sort_values('date', ascending=True, inplace=True)
        pd_price_data.reset_index(inplace=True, drop=True)

    file = path + ticker + '.h5'
    pd_price_data.to_hdf(file, key='df', mode='w')
    print(str(ticker) + "'s" + " price data is saved.")

    pd.Series(ticker).to_hdf(file, key='ticker')
    pd.Series(freq).to_hdf(file, key='freq')
    pd.Series(pd_price_data['formatted_date'].iloc[0]).to_hdf(file,
                                                              key='start_date')
    pd.Series(pd_price_data['formatted_date'].iloc[-1]).to_hdf(file,
                                                               key='end_date')
    pd.Series(sort_by_date_ascending).to_hdf(file,
                                             key='sort_by_date_ascending')
    pd.Series(start_date).to_hdf(file, key='start_date')
    pd.Series(end_date).to_hdf(file, key='end_date')
    pd.Series(retrieved_at).to_hdf(file, key='retrieved_at')
    pd.Series(updated_at).to_hdf(file, key='updated_at')
end_date = (datetime.date.today()).strftime('%Y-%m-%d')
print(end_date)
new_tickers = all_tickers
attempt = 0
drop_list = []
while len(new_tickers) != 0 and attempt <= 5:
    print("-----------------")
    print("attempt number ", attempt)
    print("-----------------")
    new_tickers = [j for j in new_tickers if j not in drop_list]
    for i in range(len(new_tickers)):
        try:
            new_financial = YahooFinancials(new_tickers[i])
            print(
                type(
                    new_financial.get_historical_stock_data(
                        beg_date, end_date, "daily")))
            new_json = new_financial.get_historical_stock_data(
                beg_date, end_date, "daily")
            new_json_field = new_json[new_tickers[i]]['prices']
            new_dataframe = pd.DataFrame(new_json_field)[[
                "formatted_date", "adjclose"
            ]]
            new_dataframe.set_index("formatted_date", inplace=True)
            first_dataframe = new_dataframe[~new_dataframe.index.duplicated(
                keep='first')]
            all_dataframe[new_tickers[i]] = first_dataframe["adjclose"]
            drop_list.append(new_tickers[i])
        except:
            print(new_tickers[i], " :failed to fetch data...retrying")
            continue
    attempt += 1
Beispiel #6
0
'''
ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)

stock_info={
        'marketCap': 0 ,
        'price_c':0,
        'i_rate':0,
        'long_debt':0,
        'short_debt':0,
        'ROE':0,
        'num_shares':0,
        }


stock_summary = yahoo_financials.get_stock_summary_data()

stock_info['num_shares']=stock_summary[ticker]['marketCap']/stock_summary[ticker]['previousClose']
stock_info['price_c']=172.23
stock_info['marketCap']=stock_info['price_c']*stock_info['num_shares']



balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance')
income_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'income')
all_statement_data_qt =  yahoo_financials.get_financial_stmts('quarterly', ['income', 'cash', 'balance'])
earnings_data = yahoo_financials.get_stock_earnings_data()
net_income = yahoo_financials.get_net_income()
historical_stock_prices = yahoo_financials.get_historical_stock_data('2018-01-01', '2018-04-20', 'daily')
Beispiel #7
0
all_tickers = ["AAPL", "MSFT", "CSCO", "AMZN", "INTC"]

# extracting stock data (historical close price) for the stocks identified
close_prices = pd.DataFrame()
end_date = (datetime.date.today()).strftime('%Y-%m-%d')
beg_date = (datetime.date.today() -
            datetime.timedelta(1825)).strftime('%Y-%m-%d')
cp_tickers = all_tickers
attempt = 0
drop = []
while len(cp_tickers) != 0 and attempt <= 5:
    print("-----------------")
    print("attempt number ", attempt)
    print("-----------------")
    cp_tickers = [j for j in cp_tickers if j not in drop]
    for i in range(len(cp_tickers)):
        try:
            yahoo_financials = YahooFinancials(cp_tickers[i])
            json_obj = yahoo_financials.get_historical_stock_data(
                beg_date, end_date, "daily")
            ohlv = json_obj[cp_tickers[i]]['prices']
            temp = pd.DataFrame(ohlv)[["formatted_date", "adjclose"]]
            temp.set_index("formatted_date", inplace=True)
            temp2 = temp[~temp.index.duplicated(keep='first')]
            close_prices[cp_tickers[i]] = temp2["adjclose"]
            drop.append(cp_tickers[i])
        except:
            print(cp_tickers[i], " :failed to fetch data...retrying")
            continue
    attempt += 1
Beispiel #8
0
    new_list = []
    for rec in stock_data_list:
        if 'type' not in rec.keys():
            new_list.append(rec)
    return new_list


# Construct yahoo financials objects for data extraction
aapl_financials = YahooFinancials(ticker)
mfst_financials = YahooFinancials(ticker2)
intl_financials = YahooFinancials(ticker3)
index_financials = YahooFinancials(index)

# Clean returned stock history data and remove dividend events from price history
daily_aapl_data = clean_stock_data(
    aapl_financials.get_historical_stock_data(start_date, end_date,
                                              freq)[ticker]['prices'])
daily_msft_data = clean_stock_data(
    mfst_financials.get_historical_stock_data(start_date, end_date,
                                              freq)[ticker2]['prices'])
daily_intl_data = clean_stock_data(
    intl_financials.get_historical_stock_data(start_date, end_date,
                                              freq)[ticker3]['prices'])
daily_index_data = index_financials.get_historical_stock_data(
    start_date, end_date, freq)[index]['prices']
stock_hist_data_list = [{
    'NDX': daily_index_data
}, {
    'AAPL': daily_aapl_data
}, {
    'MSFT': daily_msft_data
}, {
Beispiel #9
0
from yahoofinancials import YahooFinancials
import yahoofinancials as yf
yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials)
#
# tech_stocks = ['AAPL', 'MSFT', 'INTC']
# yahoo_financials_tech = YahooFinancials(tech_stocks)
# tech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash')
# for s in tech_cash_flow_data_an["cashflowStatementHistory"]:
#     for date in tech_cash_flow_data_an["cashflowStatementHistory"][s][0]:
#         print(s, date, tech_cash_flow_data_an["cashflowStatementHistory"][s][0][date])
yahoo_financials.get_historical_stock_data("2017-09-10", "2017-10-10",
                                           "monthly")
    new_list = []
    for rec in stock_data_list:
        if 'type' not in rec.keys():
            new_list.append(rec)
    return new_list


# Construct yahoo financials objects for data extraction
aapl_financials = YahooFinancials(ticker)
mfst_financials = YahooFinancials(ticker2)
intl_financials = YahooFinancials(ticker3)
index_financials = YahooFinancials(index)

# Clean returned stock history data and remove dividend events from price history
daily_aapl_data = clean_stock_data(
    aapl_financials.get_historical_stock_data(start_date, end_date,
                                              freq)[ticker]['prices'])
daily_msft_data = clean_stock_data(
    mfst_financials.get_historical_stock_data(start_date, end_date,
                                              freq)[ticker2]['prices'])
daily_intl_data = clean_stock_data(
    intl_financials.get_historical_stock_data(start_date, end_date,
                                              freq)[ticker3]['prices'])
daily_index_data = index_financials.get_historical_stock_data(
    start_date, end_date, freq)[index]['prices']
stock_hist_data_list = [{
    'NDX': daily_index_data
}, {
    'AAPL': daily_aapl_data
}, {
    'MSFT': daily_msft_data
}, {
#extracts data through webscraping and not an API
from yahoofinancials import YahooFinancials  #https://github.com/JECSand/yahoofinancials

ticker = 'AMZN'
yahoo_financials = YahooFinancials(ticker)  #new object
historic_stock_prices = yahoo_financials.get_historical_stock_data(
    '2018-01-26', '2019-01-26', 'daily')

print(historic_stock_prices)