def _get_all_historical_data(self, yahoo_ticker):
        """Scrapes the Yahoo Finance website for a given Yahoo Ticker and returns all the
        available data entries in a list of dictionaries

        Parameters
        ----------
        yahoo_ticker : str
            Yahoo ticker to be embedded in the URL to scrap

        Returns
        -------
        data : list of dicts
            option data retrieved from the Yahoo Finance site
        """
        import yahoofinancials

        yahoo_cursor = yahoofinancials.YahooFinancials(yahoo_ticker)

        # download one day just to have access to the metadata
        today = dt.datetime.today().strftime("%Y-%m-%d")
        yesterday = (dt.datetime.today() - dt.timedelta(1)).strftime("%Y-%m-%d")
        tomorrow = (dt.datetime.today() + dt.timedelta(1)).strftime("%Y-%m-%d")

        one_day_data = yahoo_cursor.get_historical_price_data(yesterday, today, 'daily')
        first_trade_date = one_day_data[yahoo_ticker]['firstTradeDate']['formatted_date']

        # now we can download since the beginning
        data = yahoo_cursor.get_historical_price_data(first_trade_date, tomorrow, 'daily')

        return data[yahoo_ticker]
Exemple #2
0
def rebalance():
    contribution_amount = input('How much are you contributing? ')

    allocations = db.session.query(models.allocation.Allocation).all()
    assets = []
    for alloc in allocations:
        for asset in alloc.assets:
            assets.append(asset)

    fin = yahoofinancials.YahooFinancials([asset.id for asset in assets])
    current_price_map = fin.get_current_price()
    current_price_map['SPAXX**'] = 1

    asset_map = {}
    protfolio_value = 0.0

    for asset in assets:
        asset_map[asset.id] = {
            'shares': asset.shares,
            'current_price': current_price_map[asset.id],
            'current_value': asset.shares * current_price_map[asset.id],
            'target_allocation': 0
        }

        protfolio_value += asset_map[asset.id]['current_value']

    table = prettytable.PrettyTable()
    table.field_names = [
        'TICKER', 'BUY AMOUNT', 'CURRENT_ALLOCATION', 'CURRENT_VALUE',
        'TARGET_ALLOCATION', 'TARGET_VALUE', 'ALLOCATION_NAME'
    ]
    rows = []
    for alloc in allocations:
        target_value = (protfolio_value +
                        float(contribution_amount)) * (alloc.target / 100)

        active_asset = None
        current_value = 0.0
        for asset in alloc.assets:
            current_value += asset_map[asset.id]['current_value']
            if asset.is_active:
                active_asset = asset

        current_allocation = round((current_value / protfolio_value) * 100, 2)
        buy_amount = round(target_value - current_value, 2)

        rows.append([
            active_asset.id, buy_amount, current_allocation,
            round(current_value, 2), alloc.target,
            round(target_value, 2), alloc.name
        ])

    rows.sort(key=lambda x: x[0])
    for row in rows:
        table.add_row(row)

    print(table)
    print('\n')
Exemple #3
0
def show_allocation():
    # TODO replace with orm
    # funds = db_cursor.execute('SELECT ticker, shares, target_allocation FROM funds').fetchall()

    funds = db.session.query(models.allocation.Asset).all()
    tickers = [fund.id for fund in funds]

    fin = yahoofinancials.YahooFinancials(tickers)
    current_price_map = fin.get_current_price()
    current_price_map['SPAXX**'] = 1

    fund_dict = {}
    protfolio_value = 0.0
    for fund in funds:
        fund_dict[fund.id] = {
            'shares': fund.shares,
            'current_price': current_price_map[fund.id],
        }

        try:
            fund_dict[fund.id][
                'current_value'] = fund.shares * current_price_map[fund.id]
        except:
            fund_dict[fund.id]['current_value'] = fund.shares

        protfolio_value += fund_dict[fund.id]['current_value']

    allocations = db.session.query(models.allocation.Allocation).all()

    table = prettytable.PrettyTable()
    table.field_names = [
        'NAME', 'CURRENT_ALLOCATION', 'TARGET_ALLOCATION', 'ACTIVE FUND'
    ]
    for alloc in allocations:
        current_allocation = 0.0
        active_asset = None
        for asset in alloc.assets:
            current_allocation += round(
                (fund_dict[asset.id]['current_value'] / protfolio_value) * 100,
                2)
            if asset.is_active:
                active_asset = asset

        table.add_row(
            [alloc.name, current_allocation, alloc.target, active_asset.name])

    print(table)
    print('\n')
Exemple #4
0
def get_company_financial(symbol='NaN',
                          frequency='annual',
                          statement_type=['balance', 'income', 'cash']):

    if symbol == "NaN":
        return 'please input symbol'

    df = pd.DataFrame()
    try:
        stk = yahoofinancials.YahooFinancials(symbol)
    except TypeError:
        return 'please input symbol'

    subject_list = list(
        stk.get_financial_stmts(frequency, statement_type).keys())

    for subject in subject_list:
        globals()['df_' + subject] = pd.DataFrame()
        try:
            data = stk.get_financial_stmts(frequency,
                                           statement_type)[subject][symbol]
        except KeyError:
            return 'please input symbol'

        for num in range(len(data)):
            date_col = list(data[num].keys())[0]
            data[num][date_col]['date'] = date_col
            globals()['df_' + subject] = globals()['df_' + subject].append(
                pd.DataFrame([data[num][date_col]]), ignore_index=True)

    if frequency != 'quarterly':
        if len(statement_type) == 3:
            df = pd.concat([
                df_balanceSheetHistory, df_cashflowStatementHistory,
                df_incomeStatementHistory
            ],
                           axis=1)
        elif len(statement_type) == 2:
            if 'balance' not in statement_type:
                df = pd.concat(
                    [df_cashflowStatementHistory, df_incomeStatementHistory],
                    axis=1)
            elif 'income' not in statement_type:
                df = pd.concat(
                    [df_balanceSheetHistory, df_cashflowStatementHistory],
                    axis=1)
            else:
                df = pd.concat(
                    [df_balanceSheetHistory, df_incomeStatementHistory],
                    axis=1)
        else:
            if 'balance' in statement_type:
                df = df_balanceSheetHistory
            elif 'income' in statement_type:
                df = df_incomeStatementHistory
            else:
                df = df_cashflowStatementHistory
    else:
        if len(statement_type) == 3:
            df = pd.concat([
                df_balanceSheetHistoryQuarterly,
                df_cashflowStatementHistoryQuarterly,
                df_incomeStatementHistoryQuarterly
            ],
                           axis=1)
        elif len(statement_type) == 2:
            if 'balance' not in statement_type:
                df = pd.concat([
                    df_cashflowStatementHistoryQuarterly,
                    df_incomeStatementHistoryQuarterly
                ],
                               axis=1)
            elif 'income' not in statement_type:
                df = pd.concat([
                    df_balanceSheetHistoryQuarterly,
                    df_cashflowStatementHistoryQuarterly
                ],
                               axis=1)
            else:
                df = pd.concat([
                    df_balanceSheetHistoryQuarterly,
                    df_incomeStatementHistoryQuarterly
                ],
                               axis=1)
        else:
            if 'balance' in statement_type:
                df = df_balanceSheetHistoryQuarterly
            elif 'income' in statement_type:
                df = df_incomeStatementHistoryQuarterly
            else:
                df = df_cashflowStatementHistoryQuarterly

    return df
@author: kv83821
"""
import pandas as pd
import yfinance as yf
import yahoofinancials
from datetime import datetime, timedelta

date_last_train = (datetime.today() - timedelta(weeks=26)).strftime('%Y-%m-%d')
date_begin_train = (datetime.today() -
                    timedelta(weeks=520)).strftime('%Y-%m-%d')

date_last_test = datetime.today().strftime('%Y-%m-%d')
date_begin_test = (datetime.today() - timedelta(weeks=26)).strftime('%Y-%m-%d')

yahoo_financials = yahoofinancials.YahooFinancials('INFY.NS')

data_train = yahoo_financials.get_historical_price_data(
    start_date=date_begin_train,
    end_date=date_last_train,
    time_interval='daily')
data_test = yahoo_financials.get_historical_price_data(
    start_date=date_begin_test, end_date=date_last_test, time_interval='daily')

train_df = pd.DataFrame(data_train['INFY.NS']['prices'])
train_df = train_df.drop('date', axis=1).set_index('formatted_date')
train_df = train_df.rename(columns={'open': 'Open'})

test_df = pd.DataFrame(data_test['INFY.NS']['prices'])
test_df = test_df.drop('date', axis=1).set_index('formatted_date')
test_df = test_df.rename(columns={'open': 'Open'})
Exemple #6
0
for i in range(1, len(tables_data)):
    a1 = tables_data[i].select('td')[0].getText().replace("\n","")
    a2 = tables_data[i].select('td')[1].getText().replace("\n","")
    
    stocks = stocks.append(pd.DataFrame([[a1, a2]], columns = col_names))

stocks = stocks.reset_index(drop = True)
stocks.head(5)


# See links:
# https://pypi.org/project/yahoofinancials/
# https://matplotlib.org/3.1.1/gallery/widgets/slider_demo.html

yahoo_financials_stocks = yf.YahooFinancials(stocks['Ticker'])

# 1 Read and get prices:
weekly_stock_prices = yahoo_financials_stocks.get_historical_price_data('2015-01-01', '2019-11-01', 'weekly')

## A. Some exploration:
weekly_stock_prices['AAPL'].keys()
pd.DataFrame(weekly_stock_prices['AAPL']['prices'])

## B. Make up the panel:
my_stocks = list(weekly_stock_prices.keys())
df_prices = pd.DataFrame(columns = ['date', 'ticker', 'formatted_date', 'adjclose', 'volume'])

for tick in my_stocks:
    # tick = my_stocks[0]
    try: 
Exemple #7
0
import yahoofinancials as yf
import pickle
from src.valuation.valuation_fcff import *
from src.valuation.back_up.valuation_input_list import *

#%%
ticker = 'TSN'
yahoo_financials = yf.YahooFinancials(ticker)

read_from_yahoo = False

if read_from_yahoo:
    ## Get financial statements
    #%%
    # all_statement_data_qt =  yahoo_financials.get_financial_stmts('quarterly', ['income', 'cash', 'balance'])
    # all_statement_data_qt
    #%%
    all_statement_data_yr = yahoo_financials.get_financial_stmts(
        'annual', ['income', 'cash', 'balance'])
    with open('./data/20210207_TSN_all_statement_yr.pickle', 'wb') as handle:
        pickle.dump(all_statement_data_yr,
                    handle,
                    protocol=pickle.HIGHEST_PROTOCOL)

else:
    with open('./data/20210207_TSN_all_statement_yr.pickle', 'rb') as handle:
        all_statement_data_yr = pickle.load(handle)

balance_sheet = all_statement_data_yr['balanceSheetHistory']['TSN']
current_balance_sheet = balance_sheet[0]
current_date = [*current_balance_sheet.keys()][0]
Exemple #8
0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
import yahoofinancials as yahoof
import datetime as date

#Using yahoofinancials
today = date.datetime.today()

# Get the data for the stock Tesla by specifying the stock ticker, start date, and end date
yahoo_financials = yahoof.YahooFinancials('TSLA')
data = yahoo_financials.get_historical_price_data(start_date='2015-01-01', 
                                                  end_date='2020-01-01', 
                                                  time_interval='daily')

# Create pandas datframe and format
tsla_df = pd.DataFrame(data['TSLA']['prices'])
tsla_df = tsla_df.drop('date', axis=1).set_index('formatted_date')
tsla_df_close = tsla_df['close']

# Print test
print(tsla_df.head())
print(tsla_df.shape)
print(tsla_df_close)

# Plot the close price
tsla_df_close.plot()
plt.show()