Ejemplo n.º 1
0
def getStockQuotes(symbols, source, startDate, endDate):
    quotes = pd.DataFrame()

    for symbol in symbols:
        quotes[symbol] = pdr.get_data_fred(symbol,
                                           data_source=source,
                                           start=startDate,
                                           end=endDate)['Adj Close']

    return quotes
Ejemplo n.º 2
0
def multi_ticker_download(ticker, start, end):
    ticker = ticker.upper()
    if '_FRED_' in ticker:
        ticker = ticker.replace('_FRED_', '')
        data = pdr.get_data_fred(ticker, start, end)
        final = pd.DataFrame(index=pd.date_range(start, end))
        final.index.name = ticker
        final['Date'] = final.index
        final['Close'] = data
        final['Close'] = final['Close'].ffill()
        return final
    data = pdr.DataReader(ticker,
                          data_source='iex',
                          start=start,
                          end=end,
                          retry_count=3,
                          pause=1)
    return data
    def load(self,
             start_date='01-01-2000',
             end_date='2020-10-30',
             to_months=True):
        rates: dict = {}

        for i in self.FRED_Codes:
            rates[i] = pdr.get_data_fred(i, start_date, end_date)

        df = rates[self.FRED_Codes[0]]
        for i in self.FRED_Codes[1:]:
            df = pd.merge(df, rates[i], left_index=True, right_index=True)

        if to_months:
            df = df.resample("m").mean()
            df = df.sort_values("DATE", ascending=False)
            df.index = sorted(df.index.to_period("M"), reverse=True)

        return df
##############################################################################

#Random Forests to Predict 12 months ahead Price to Free Cash Flow

##############################################################################
#%%

#############Creating the Macroeconomic feature set#########################

start = datetime.datetime(1996, 1, 1)
start_quandl = "1996-01-01" #create date to be used with Quandl function
end = datetime.datetime(2019, 9, 30)
end_quandl = "2019-09-30"

CPI = pdr.get_data_fred('CPIAUCSL', start, end)
CPI.columns = ['Headline CPI']
CPI['CPI YoY Change'] = CPI.pct_change(12)
#create 1 month lag since we won't know CPI data until about 1 month after data point
CPI['CPI YoY Change Lag'] = CPI['CPI YoY Change'].shift(-1)

Core_CPI = pdr.get_data_fred('CPILFESL', start, end)
Core_CPI.columns = ['Core CPI']
Core_CPI['Core CPI YoY Change'] = Core_CPI.pct_change(12)
#create 1 month lag since we won't know CPI data until about 1 month after data point
Core_CPI['Core CPI YoY Change Lag'] = Core_CPI['Core CPI YoY Change'].shift(-1)

WTI_Oil = pdr.get_data_fred('DCOILWTICO', start, end)
WTI_Oil.columns = ['WTI Oil']

Gold = pdr.get_data_fred('GOLDAMGBD228NLBM', start, end)
    )

#create set and list of all tickers
myset_ticker = set(tickers_df1.ticker)
list_tickers = list(myset_ticker)

#filtered USA fundamental  and equity price data
USA_fundamentals = fundamental_data[fundamental_data['ticker'].isin(list_tickers)]
USA_equity_prices = equity_prices[equity_prices['ticker'].isin(list_tickers)]

#############Creating the Macroeconomic feature set#########################
#%%
start = datetime.datetime(1996, 1, 1)
end = datetime.datetime(2020, 5, 31)

ECON_Policy_Uncertainty_Index = pdr.get_data_fred('USEPUINDXD', start, end)
ECON_Policy_Uncertainty_Index.columns = ['Economic Policy Uncertainty Index']

SAHM_Rule = pdr.get_data_fred('SAHMCURRENT', start, end)
SAHM_Rule.columns = ['Sahm Rule']
#data comes out 1 month after the fact
SAHM_Rule['Sahm Rule (Lag)'] = SAHM_Rule['Sahm Rule'].shift(1)

Yale_Shiller_US_Confidence = quandl.get(["YALE/US_CONF_INDEX_VAL_INDIV", 
                                         "YALE/US_CONF_INDEX_VAL_INST"])
Yale_Shiller_US_Confidence = Yale_Shiller_US_Confidence[Yale_Shiller_US_Confidence.columns[::2]]
#data comes out two months after the fact
Yale_Shiller_US_Confidence = Yale_Shiller_US_Confidence.shift(2)

Yale_Shiller_US_Confidence.columns = ['US Shiller Valuation Index Indiv (Lag)',
                                       'US Shiller Valuation Index Inst (Lag)']
Ejemplo n.º 6
0
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as pdr
import seaborn as sns
from statsmodels.tsa.ar_model import AutoReg, ar_select_order
from statsmodels.tsa.api import acf, pacf, graphics

sns.set_style('darkgrid')
pd.plotting.register_matplotlib_converters()
# Default figure size
sns.mpl.rc('figure', figsize=(16, 6))
'''
The first set of examples uses the month-over-month growth rate in U.S. Housing starts that has not been seasonally adjusted. 
The seasonality is evident by the regular pattern of peaks and troughs. We set the frequency for the time series to “MS” (month-start) to avoid warnings when using AutoReg.
'''
data = pdr.get_data_fred('HOUSTNSA', '1959-01-01', '2019-06-01')
housing = data.HOUSTNSA.pct_change().dropna()
# Scale by 100 to get percentages
housing = 100 * housing.asfreq('MS')
fig, ax = plt.subplots()
ax = housing.plot(ax=ax)
'''
We can start with an AR(3). While this is not a good model for this data, it demonstrates the basic use of the API.
'''
mod = AutoReg(housing, 3, old_names=False)
res = mod.fit()
#print(res.summary())
'''
AutoReg supports the same covariance estimators as OLS. Below, we use cov_type="HC0", which is White’s covariance estimator. 
While the parameter estimates are the same, all of the quantities that depend on the standard error change.
'''
Ejemplo n.º 7
0
@author: jeremylhour
"""
import numpy as np
import pandas as pd
import pandas_datareader as pdr
import statsmodels.api as sm
import matplotlib.pyplot as plt

# ------------------------
# DOWNLOAD FRED DATA
# ------------------------

start = '1979-01-01'
end = '2020-12-01'

industrialProduction = pdr.get_data_fred('IPMAN', start=start, end=end)
income = pdr.get_data_fred('W875RX1', start=start, end=end)
sales = pdr.get_data_fred('CMRMTSPL', start=start, end=end)
emp = pdr.get_data_fred('PAYEMS', start=start, end=end)

df = pd.concat((industrialProduction, income, sales, emp), axis=1)
df.columns = ['indprod', 'income', 'sales', 'emp']
df.index.freq = df.index.inferred_freq

# ------------------------
# PLOT SERIES AND STANDARDIZE
# ------------------------
df.loc[:, 'indprod':'emp'].plot(subplots=True, layout=(2, 2), figsize=(15, 6))

# Create log-differenced series and standardize
for col in df.columns:
Ejemplo n.º 8
0
#---- ch05/pandas-barh/plot
ax = frame.plot.barh(stacked=True)
ax.invert_yaxis()
plt.show()


#---- ch05/pandas-datareader
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as pdr


#---- ch05/pandas-fred-gdp
gdp = pdr.get_data_fred(['GDPPOT', 'GDPC1'], 
                        start='1960-01-01', end='2020-04-01')
gdp


#---- ch05/pandas-fred-gdp/plot
gdp[['GDPPOT', 'GDPC1']].plot()
plt.show()


#---- ch05/pandas-fred-gdp-gap
gdp['gap'] = gdp['GDPC1'] - gdp['GDPPOT']
gdp['rgap'] = gdp['gap'] / gdp['GDPPOT']
gdp


#---- ch05/pandas-fred-price
Ejemplo n.º 9
0
print(len(df4.DATE))
print(len(set(df4.DATE)))
df4= df4.set_index('DATE') 

df_rt2 = df4[~df4.index.duplicated()]

df_rt2.to_csv(cwd+"/FINAL/REET_FINAL.csv",index=True,index_label="DATE")
df_rt2.to_csv(cwd+"/BASE/REET_BASE.csv",index=True,index_label="DATE")


##################################DTWEXBGS###########################################################################


start_date = date.today()-datetime.timedelta(100)
end_date = date.today()
fx = pdr.get_data_fred("DTWEXBGS", start_date, end_date)

df=pd.DataFrame(fx)
df.columns=['DTWEXBGS']
print(df)
df = df.resample('D').asfreq()
df= df.interpolate(method='linear', axis=0).ffill().bfill()
df.to_csv(cwd+"/FINAL/IGNORE/DTWEXBGS_FRED_FINAL.csv",index=True,header=True,index_label="DATE")

x3 = pd.read_csv(cwd+"/BASE/DTWEXBGS_BASE.csv",parse_dates=["DATE"],dayfirst=True,index_col="DATE")
x4 = pd.read_csv(cwd+"/FINAL/IGNORE/DTWEXBGS_FRED_FINAL.csv",parse_dates=["DATE"], dayfirst=True,index_col="DATE")

ust_final_hope_2=pd.concat([x3,x4]).reset_index() #.drop_duplicates(keep="first")
df4=pd.DataFrame(ust_final_hope_2)
df4 = df4.dropna()
print(len(df4.DATE))
Ejemplo n.º 10
0
import pandas_datareader as pdr
import datetime as dt
from dateutil.relativedelta import *
import statsmodels.api as sm
import plotly.express as px
from plotly.offline import plot

#initializations

#pull giant historical list of data from Fred
start = dt.datetime(1970, 1, 1)
end = dt.date.today()
macro_data = pdr.get_data_fred([
    'USAGDPDEFQISMEI', 'NA000334Q', 'ND000334Q', 'GDPC1', 'CPIAUCSL', 'PCEC96',
    'PCECC96', 'RSAFS', 'RRSFS', 'DSPIC96', 'RSAOMV', 'PSAVERT', 'PAYEMS',
    'PRSCQ', 'CES0500000017', 'ICSA', 'CCSA', 'UMCSENT', 'MICH',
    'CSCICP03USM665S', 'M1', 'M2', 'INDPRO', 'DGORDER', 'NEWORDER', 'BUSINV',
    'TLNRESCONS', 'TLRESCONS', 'NETEXP', 'IMPGSC1', 'EXPGSC1', 'BOPGSTB',
    'DPCERD3Q086SBEA', 'CPILFESL', 'DCOILWTICO'
], start, end)

############### Begin Analysis ###############
#convert nominal quarterly data to real using implicit deflator, calculate a trailing four quarter average
#macro_data.loc[:,'realGDP'] = .1*(macro_data.loc[:,'NA000334Q']/macro_data.loc[:,'USAGDPDEFQISMEI'])
macro_data.loc[:, 'realGDP'] = macro_data.loc[:, 'GDPC1']
gdp_data = macro_data.loc[:, 'realGDP'].dropna()
#gdp_data_trailing = gdp_data.rolling(window=4).sum()

#initialize series long name dictionary and dictionary of how many data points are required to annualize
#divisor = {}
series_name = {}
points_required = {}
Ejemplo n.º 11
0
    for name in inv.index:
        data_sources[name] = ("yahoo", name, "raw")

sd = datetime(2007, 1, 1)
ed = datetime(2021, 4, 15)

features_dataframe = pd.DataFrame(
    index=pd.date_range(start=sd, end=ed, freq='D'))
features_dataframe.index.name = "Date"
for name in sorted(data_sources.keys()):
    src, symbol, kind = data_sources[name]
    if (src == "yahoo"):
        series = (pdr.get_data_yahoo(symbols=symbol, start=sd,
                                     end=ed)["Adj Close"]).rename(name)
    elif (src == "fred"):
        series = (pdr.get_data_fred(symbols=symbol, start=sd,
                                    end=ed))[symbol].rename(name)

    if kind == "rate":
        features_dataframe[name] = series
    elif kind == "raw":
        for period in (5, 20):
            extended_name = f"{name}_std_{period}"
            features_dataframe[extended_name] = series.dropna().rolling(
                period).std()
            extended_name = f"{name}_skew_{period}"
            features_dataframe[extended_name] = series.dropna().rolling(
                period).skew()
            extended_name = f"{name}_kurt_{period}"
            features_dataframe[extended_name] = series.dropna().rolling(
                period).kurt()
    else:
Ejemplo n.º 12
0
# This cell sets the plotting style, registers pandas date converters for
# matplotlib, and sets the default figure size.

sns.set_style("darkgrid")
pd.plotting.register_matplotlib_converters()
# Default figure size
sns.mpl.rc("figure", figsize=(16, 6))
sns.mpl.rc("font", size=14)

# The first set of examples uses the month-over-month growth rate in U.S.
# Housing starts that has not been seasonally adjusted. The seasonality is
# evident by the regular pattern of peaks and troughs. We set the frequency
# for the time series to "MS" (month-start) to avoid warnings when using
# `AutoReg`.

data = pdr.get_data_fred("HOUSTNSA", "1959-01-01", "2019-06-01")
housing = data.HOUSTNSA.pct_change().dropna()
# Scale by 100 to get percentages
housing = 100 * housing.asfreq("MS")
fig, ax = plt.subplots()
ax = housing.plot(ax=ax)

# We can start with an AR(3).  While this is not a good model for this
# data, it demonstrates the basic use of the API.

mod = AutoReg(housing, 3, old_names=False)
res = mod.fit()
print(res.summary())

# `AutoReg` supports the same covariance estimators as `OLS`.  Below, we
# use `cov_type="HC0"`, which is White's covariance estimator. While the
import pandas_datareader as pdr
print(pdr.get_data_fred('GS10'))
Ejemplo n.º 14
0
import pandas as pd
import numpy as np

import pandas_datareader as pdr

pdr.get_data_fred('GS10')

# test 1.0
start_ = '2017-04-22'
end_ = '2018-04-22'

df = pdr.data.DataReader(name='AMZN',
                         data_source='yahoo',
                         start=start_,
                         end=end_)
print(df)
Ejemplo n.º 15
0
def risk_budget(account_value):
    return [
        vol_target * account_value, vol_target * account_value * np.sqrt(252)
    ]


#function definitions
def annual_return_equities():
    return annual_return


#initializations
sns.set()

#Get current risk free rate
risk_free = pdr.get_data_fred(['USD3MTD156N'], dt.datetime(2020, 1, 1),
                              dt.date.today())
r = (risk_free.loc[:, 'USD3MTD156N'].values)[-1]
target_sharpe = 2

#pull Investment Grade spreads, total return data from FRED
start = dt.datetime(1996, 1, 1)
end = dt.date.today()
IG_ICE_data = pdr.get_data_fred(
    ['BAMLC0A0CM', 'BAMLC0A4CBBB', 'BAMLC8A0C15PY', 'BAMLC0A0CMEY'], start,
    end)

fig = plt.figure(figsize=[8, 8])
plt.title("ICE BofA IG Spread Histogram, 1996-")
sns.distplot(IG_ICE_data.loc[:, 'BAMLC0A0CM'],
             bins=50,
             label='IG',
Ejemplo n.º 16
0
import pandas as pd
import numpy as np
#import quandl
import seaborn as sns
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt

#initializations
sns.set()

#pull Investment Grade spreads, total return data from FRED
start = dt.datetime(1996, 1, 1)
end = dt.date.today()
IG_ICE_data = pdr.get_data_fred(['BAMLC0A0CM', 'BAMLC0A4CBBB', 'BAMLC0A0CMEY'],
                                start, end)

fig = plt.figure(figsize=[8, 8])
plt.title("ICE BofA IG Spread Histogram, 1996-")
sns.distplot(IG_ICE_data.loc[:, 'BAMLC0A0CM'], bins=50, label='IG')
sns.distplot(IG_ICE_data.loc[:, 'BAMLC0A4CBBB'], bins=50, label='BBB')
plt.xlabel('IG (blue), BBB (orange)')
IG_ICE_quantile = IG_ICE_data.loc[:, 'BAMLC0A0CM'].rank(pct=True)[-1]
BBB_ICE_quantile = IG_ICE_data.loc[:, 'BAMLC0A4CBBB'].rank(pct=True)[-1]

print("ICE/BofA IG spreads of " +
      str(100 * IG_ICE_data.loc[:, 'BAMLC0A0CM'][-1]) + "bp are higher than " +
      '{:.1%}'.format(IG_ICE_quantile) + " of history (1919-current)")
print("ICE/BofA BBB spreads of " +
      str(100 * IG_ICE_data.loc[:, 'BAMLC0A4CBBB'][-1]) +
      "bp are higher than " + '{:.1%}'.format(BBB_ICE_quantile) +
Ejemplo n.º 17
0
## 인터넷 상의 CSV 파일 입력
## 웹상에는 다양한 데이터 파일이 CSV 파일 형태로 제공된다.
## read_csv 명령 사용시 파일 패스 대신 URL을 지정하면
## Pandas가 직접 해당 파일을 다운로드하여 읽어들인다.
df = pd.read_csv("https://raw.githubusercontent.com/datascienceschool/docker_rpython/master/data/titanic.csv")

pd.set_option("display.max_rows", 20)  # 앞뒤로 모두 20행만 보여준다.
df

# 만약 앞이나 뒤의 특정 갯수만 보고 싶다면 head 메서드나 tail 메서드를 이용
df.head()
df.tail(2)


## 인터넷 상의 데이터 베이스 자료 입력
## pandas_datareader 패키지의 DataReader 을 사용하면
## 일부 인터넷 사이트의 자료를 바로 pandas로 읽어들일 수 있다.
import datetime
dt_start = datetime.datetime(2015, 1, 1)
dt_end = "2016, 6, 30"

# data_source 인수로 데이터를 읽어올 웹 사이트를 지정
import pandas_datareader as pdr

gdp = pdr.get_data_fred('GDP', dt_start, dt_end)
gdp.tail()

# 데이터 코드에 리스트를 넣으면 여러개의 데이터를 동시에 가져온다.
inflation = pdr.get_data_fred(["CPIAUCSL", "CPILFESL"], dt_start, dt_end)
inflation.tail()
Ejemplo n.º 18
0
def mode_trend():
    input_code_num = str(input('''
Would you like to view
(1) Absolute trend for one currency
(2) Relative trend for two currencies

Sample input : 1 or 2
'''))
    if input_code_num == '2':
        input_code = str(input('''
________________________________________
Currently Supported and Respective Code:
Brazilian Real : BRL
Canadaian Dollar : CAD
Chinese Yuan : CNY
Denish Krone : DKK
Hong Kong Dollar : HKD
Indian Rupee : INR
Japanese Yen : JPY
South Korean Won : KRW
Malaysian Ringgit : MYR
Mexican Peso : MXN
Norwegian Krone: NOK
Swedish Krona : SEK
South African Rand: ZAR
Singapore Dollar : SGD
Sri Lankan Rupee: LKR
Swiss Franc : CHF
New Taiwan Dollar : TWD
Thai Baht : THB
Australian Dollar : AUD
Euro : EUR
New Zealand Dollar : NZD
British Pound : GBP
Venezuelan Bolivar : VEF
________________________________________
Please enter two currency codes, seperate them with comma:

Sample input : CNY,SGD
'''))
        input_code = input_code.upper()
        temp_code = input_code.split(',',2)

        input_start = str(input('please enter start date (eg. 2019-1-1):'))
        start = input_to_date(input_start)
        input_end = str(input('please enter end date (eg. 2019-1-1):'))
        end = input_to_date(input_end)

        real_code0 = input_to_code(temp_code[0])
        real_code1 = input_to_code(temp_code[1])
        df = data.get_data_fred([real_code0,real_code1],start,end)
        df['float0'] = [float(x) for x in df[real_code0]]
        df['float1'] = [float(x) for x in df[real_code1]]
        df['ratio'] = df['float0']/df['float1']
        df['ratio'].plot.line(figsize = (15,8))
        plt.show()

    else:
        input_code = str(input('''
________________________________________
Currently Supported and Respective Code:
Brazilian Real : BRL
Canadaian Dollar : CAD
Chinese Yuan : CNY
Denish Krone : DKK
Hong Kong Dollar : HKD
Indian Rupee : INR
Japanese Yen : JPY
South Korean Won : KRW
Malaysian Ringgit : MYR
Mexican Peso : MXN
Norwegian Krone: NOK
Swedish Krona : SEK
South African Rand: ZAR
Singapore Dollar : SGD
Sri Lankan Rupee: LKR
Swiss Franc : CHF
New Taiwan Dollar : TWD
Thai Baht : THB
Australian Dollar : AUD
Euro : EUR
New Zealand Dollar : NZD
British Pound : GBP
Venezuelan Bolivar : VEF
________________________________________

Please enter money code to view currency trend:
(sample input : SGD/CNY/JPY)
'''))

        input_code = input_code.upper()
        code = input_to_code(input_code)
        input_start = str(input('please enter start date (eg. 2019-1-1):'))
        start = input_to_date(input_start)
        input_end = str(input('please enter end date (eg. 2019-1-1):'))
        end = input_to_date(input_end)

        df = data.get_data_fred(code,start,end)
        name = input_code + ' Currency Trend'
        df = df.rename(columns={code:name})
        df.plot.line(figsize = (15,8))
        plt.show()

    next = next_request()
    return next
Ejemplo n.º 19
0
    ax = fig.add_subplot(1, 1, 1)

    # 折れ線グラフをセット
    ax.plot(voo.index, voo['Close'], label=symbol)
    ax.grid(axis='y', color='gray', ls=':')
    ax.legend()

    # 折れ線グラフを表示
    fig.show()
    fig.savefig('voo.png')


##################################################
# メイン
##################################################
if __name__ == '__main__':

    #  matplotlibの日本語フォント設定処理
    plt_font_init()

    # 米国国債10年を5年分取得
    gs10 = pdr.get_data_fred('GS10')
    #print(gs10)

    # ダウ平均をSqooqで取得
    f = web.DataReader('^DJI', 'stooq')
    #print(f.head())

    # 米国株取得
    get_us_stock('VOO')
Ejemplo n.º 20
0
def mode_calculate():

    flag = False

    while flag == False:
        input_code = str(input('''
________________________________________
Currently Supported and Respective Code:

Brazilian Real : BRL
Canadaian Dollar : CAD
Chinese Yuan : CNY
Denish Krone : DKK
Hong Kong Dollar : HKD
Indian Rupee : INR
Japanese Yen : JPY
South Korean Won : KRW
Malaysian Ringgit : MYR
Mexican Peso : MXN
Norwegian Krone: NOK
Swedish Krona : SEK
South African Rand: ZAR
Singapore Dollar : SGD
Sri Lankan Rupee: LKR
Swiss Franc : CHF
New Taiwan Dollar : TWD
Thai Baht : THB
Australian Dollar : AUD
Euro : EUR
New Zealand Dollar : NZD
British Pound : GBP
Venezuelan Bolivar : VEF
________________________________________

Would you like to exchange money from ___ ?
(sample input : SGD/CNY/JPY)
'''))
        input_code = input_code.upper()
        in_code = input_to_code(input_code)
        output_code= str(input('''
________________________________________
Currently Supported and Respective Code:
Brazilian Real : BRL
Canadaian Dollar : CAD
Chinese Yuan : CNY
Denish Krone : DKK
Hong Kong Dollar : HKD
Indian Rupee : INR
Japanese Yen : JPY
South Korean Won : KRW
Malaysian Ringgit : MYR
Mexican Peso : MXN
Norwegian Krone: NOK
Swedish Krona : SEK
South African Rand: ZAR
Singapore Dollar : SGD
Sri Lankan Rupee: LKR
Swiss Franc : CHF
New Taiwan Dollar : TWD
Thai Baht : THB
Australian Dollar : AUD
Euro : EUR
New Zealand Dollar : NZD
British Pound : GBP
Venezuelan Bolivar : VEF
________________________________________

To ___?
(sample input : SGD/CNY/JPY)
'''))
        output_code = output_code.upper()
        out_code = input_to_code(output_code)

        input_amount = float(input('\nHow much '+input_code+' would you like to exchange?\n'))
        input_date = str(input('''
Exchange date requested (sample input : 2019-1-1):
(kindly skip this portion if the date requested is today)
''' ))

        if input_date =='':
            url = 'https://api.exchangerate-api.com/v4/latest/USD'
            response = requests.get(url)
            result = response.json()
            get_dates = result.get('rates')
            in_currency = float(get_dates.get(input_code))
            out_currency = float(get_dates.get(output_code))
            input_date = ' today'

        else:
            end = input_to_date(input_date)
            df = data.get_data_fred([in_code,out_code],end,end)
            df = df.reset_index()
            check = df.loc[df['DATE']==input_date]
            if check.shape[0]==0 or check.isnull().values.any():
                print('Sorry. The rate on the date requested is unavailable.')
                continue
            else:
                in_currency = float(df.loc[df['DATE']== input_date][in_code])
                out_currency = float(df.loc[df['DATE']== input_date][out_code])
                input_date = ' on ' + input_date

        rate = 1/in_currency*out_currency
        rate = float("{0:.2f}".format(rate))
        out_amount = input_amount*rate
        out_amount = float("{0:.2f}".format(out_amount))

        print('''
------------------------|RESULT|------------------------
''')
        print(str(input_amount) + ' ' + input_code  + ' can be exchanged into ' +str(out_amount)+ ' '+output_code + input_date)
        print('The exchange rate is ' + str(rate))

        print('''
--------------------------------------------------------
''')

        flag = True

    next = next_request()

    return next
Ejemplo n.º 21
0
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as pdr
import seaborn as sns
from statsmodels.tsa.ar_model import AutoReg, ar_select_order
from statsmodels.tsa.api import acf, pacf, graphics
import numpy as np

data = pdr.get_data_fred('INDPRO', '1959-01-01', '2019-06-01')
ind_prod = data.INDPRO.pct_change(12).dropna().asfreq('MS')
_, ax = plt.subplots(figsize=(16, 9))
ind_prod.plot(ax=ax)

sel = ar_select_order(ind_prod, 13, 'bic', old_names=False)
res = sel.model.fit()
print(res.summary())

sel = ar_select_order(ind_prod, 13, 'bic', glob=True, old_names=False)
sel.ar_lags
res_glob = sel.model.fit()
print(res.summary())

ind_prod.shape

fig = res_glob.plot_predict(start=714, end=732)

res_ar5 = AutoReg(ind_prod, 5, old_names=False).fit()
predictions = pd.DataFrame({
    "AR(5)":
    res_ar5.predict(start=714, end=726),
    "AR(13)":
Ejemplo n.º 22
0
# pip install pandas-datareader
import openpyxl
import datetime as dt
import pandas_datareader as web

workbook = openpyxl.load_workbook('example.xlsx')
sheet = workbook.active

for row in range(5, 10):
    date = sheet[f'C{row}'].value
    today = dt.datetime.now()
    age = today.year - date.year - ((today.month, today.day) <
                                    (date.month, date.day))
    sheet[f'D{row}'] = age

    usd_to_eur = web.get_data_fred('DEXUSEU').iloc[-1]['DEXUSEU']
    sheet[f'E{row}'] = sheet[f'E{row}'].value * usd_to_eur
    sheet[f'E{row}'].number_format = "#,##0.00$"

workbook.save('example.xlsx')
Ejemplo n.º 23
0
#
# One reason that this kind of approach can be useful is that the CPI is
# released earlier in the month than the PCE. One the CPI is released,
# therefore, we can update our dynamic factor model with that additional
# datapoint, and obtain an improved forecast for that month's PCE release. A
# more involved version of this kind of analysis is available in Knotek and
# Zaman (2017).

# We start by downloading the core CPI and PCE price index data from
# [FRED](https://fred.stlouisfed.org/), converting them to annualized
# monthly inflation rates, removing two outliers, and de-meaning each series
# (the dynamic factor model does not

import pandas_datareader as pdr

levels = pdr.get_data_fred(['PCEPILFE', 'CPILFESL'], start='1999',
                           end='2019').to_period('M')
infl = np.log(levels).diff().iloc[1:] * 1200
infl.columns = ['PCE', 'CPI']

# Remove two outliers and de-mean the series
infl['PCE'].loc['2001-09':'2001-10'] = np.nan

# To show how this works, we'll imagine that it is April 14, 2017, which
# is the data of the March 2017 CPI release. So that we can show the effect
# of multiple updates at once, we'll assume that we haven't updated our data
# since the end of January, so that:
#
# - Our **previous dataset** will consist of all values for the PCE and
# CPI through January 2017
# - Our **updated dataset** will additionally incorporate the CPI for
# February and March 2017 and the PCE data for February 2017. But it will
Ejemplo n.º 24
0
for ii in range(1, simLen + 1):
    weights = np.random.normal(0, 1, len(tickers))
    weights /= weights.sum()
    if np.any(weights >= 2) or np.any(weights <= -2): continue
    port_var = np.matmul(np.matmul(weights, vcov), np.transpose(weights))
    port_std = np.append(port_std, port_var**0.5)
    port_ret = np.append(port_ret, np.matmul(weights, ret_mean_an))
    if (ii % 100 == 0 and ii != simLen + 1):
        b = ('Finished with iteration ' + str(ii) + ' of ' +
             str(len(range(1, simLen + 1))))
        sys.stdout.write('\r' + b)
    if (ii == simLen):
        sys.stdout.write('\r' + '-' * 30 + ' Done! ' + '-' * 30 + '\n')

## Identify mean-variance portfolio through simulation ##
risk_free = web.get_data_fred('TB3MS', start=end, end=end) / 100
s_p = (port_ret - risk_free['TB3MS'][0]) / port_std
s_p_m_ind = np.argmax(s_p)
min_var_ind = np.argmin(port_std)

### Identify GMVP by constrained optimization ###
rf = risk_free.values[0].tolist()
rf = rf[0]

# Initial guesses
x0 = np.ones(len(weights))
x0 /= x0.sum()

# Run optimization routine, find GMVP
b = (-3, 3)
bnds = (b, ) * len(weights)
import pandas_datareader as pdr
from datetime import datetime
start = datetime(2020, 3, 25)
end = datetime(2020, 3, 31)

#iex data source
df = web.DataReader('F', 'iex', start, end)

#https://fred.stlouisfed.org/
#federal reserve bank of St Louis
#many different indices, follow menu and get codes from url.
#https://fred.stlouisfed.org/categories/32255

#https://fred.stlouisfed.org/series/GS10
#10-Year Treasury Constant Maturity Rate (GS10)
df = pdr.get_data_fred('GS10', start, end)
#pandas_datareader.get_data_fred
df = pdr.get_data_fred('BAMLHYH0A0HYM2TRIV', start, end)
#https://fred.stlouisfed.org/series/BAMLHYH0A0HYM2TRIV
df = pdr.get_data_fred('SP500', start, end)

#https://pandas-datareader.readthedocs.io/en/latest/remote_data.html#econdb
df = pdr.DataReader('ticker=RGDPUS', 'econdb')
#https://www.econdb.com/tree/
df = pdr.DataReader('ticker=RBA_C04', 'econdb')#empty result
df = pdr.DataReader('ticker=RBA_C04', 'econdb', start, end)#empty result



RGDPUS
import numpy as np
from pandas_datareader import get_data_fred

fred = ["GDPC1", "GDPDEF", "CNP16OV", "FEDFUNDS", 'GDPPOT', 'USRECQ']

data = (get_data_fred(fred, '1965', '2007Q4').resample('Q').mean().to_period())

data['LNSindex'] = data['CNP16OV'] / data['CNP16OV']['1992Q3']
data['output'] = np.log(data['GDPC1'] / data['LNSindex']) * 100
data['inflation'] = np.log(data['GDPDEF'] / data['GDPDEF'].shift(1)) * 400
data['interest rate'] = data['FEDFUNDS']

data['ygr'] = data['output'].diff()
data['xgap'] = np.log(data.GDPC1 / data.GDPPOT)
data['USRECQ'][data.USRECQ == 0] = np.nan

obs = ['ygr', 'inflation', 'interest rate']
np.savetxt('data/longsample.txt', data[obs]['1966':'2007'])
np.savetxt('data/post83sample.txt', data[obs]['1984':'2007'])
np.savetxt('data/post91sample.txt', data[obs]['1992':'2007'])
np.savetxt('data/xgap.txt', data['xgap']['1966':'2007'])
np.savetxt('data/usrec.txt', data['USRECQ']['1966':'2007'])