Esempio n. 1
0
def get_last_trading_day_quotes(symbol):
    today = datetime.today()
    i = 1
    yesterday = today - timedelta(i)

    df = dr(symbol, 'yahoo', yesterday, today)
    while df.empty:
        i = i + 1
        yesterday = today - timedelta(i)
        df = dr(symbol, 'yahoo', yesterday, today)

    return df
Esempio n. 2
0
 def __init__(self, ticker, json):
     df = dr(ticker, 'yahoo', dt.datetime(1980, 01, 01))
     pnl = pd.Series(np.zeros(len(df)), index=df.index)
     for i in xrange(1, len(pnl)): 
         pnl[pnl.index[i]] = df['Adj Close'][df.index[i]] - df['Adj Close'][df.index[i - 1]]
     df['PnL'] = pnl
     price_list = []
     pnl_list = []
     
     if json:
         newline = '\n'
     else:
         newline = '\\n'
     
     for date, row in df.T.iteritems():
         price_list.append(date.strftime('%Y-%m-%d') + ',' + str(row['Adj Close']) + newline)
         pnl_list.append(date.strftime('%Y-%m-%d') + ',' + str(row['PnL']) + newline)
     price_csv = 'Date,Price' + newline + ''.join(price_list) 
     pnl_csv = 'Date,PnL' + newline + ''.join(pnl_list)
     
     # cache values using object attributes
     self.ticker = ticker
     self.dataframe = df
     self.price_csv = price_csv
     self.pnl_csv = pnl_csv
     self.json = json
import numpy as np
import matplotlib.pyplot as plt
import pandas as pan
from pandas.io.data import DataReader as dr
import datetime
import hp_filter as hp
import Bandpass_Filter as bpf
from prettytable import PrettyTable as pt

# CPI: CPIAUCSL
# GDP: GDPC1
# consumption: PCECC96
# investment: GCEC96

gdp = np.asarray(
    dr('GDPC1', 'fred', start = datetime.datetime(1947,1,1))['VALUE'])
cpi = np.asarray(
    dr('CPIAUCSL', 'fred', start = datetime.datetime(1947,1,1))['VALUE'])
cons = np.asarray(
    dr('PCECC96', 'fred', start = datetime.datetime(1947,1,1))['VALUE'])
inv = np.asarray(
    dr('GCEC96', 'fred', start = datetime.datetime(1947,1,1))['VALUE'])

mask = np.arange(1,cpi.size, 3)
cpi = cpi[mask]

def problem_5():
    """
    This function uses pyplot.psd to plot the spectrum for each of the time
    series generated above.
Esempio n. 4
0
import DSGE
import MonteCarloSimulation as mcs
import NumDeriv as nd
import SolveSS as sols
import UhligSolve as lig
import pandas as pan
from pandas.io.data import DataReader as dr
import datetime

##-------------------- Step 1. Get data and filter it.------------------------##
# We will use Wage data: ECIWAG
# Also business labor: PRS84006173
# and Consumption of fixed capital: COFC

start_date = datetime.datetime(2000, 1, 1)
wage_Data= np.asarray(dr('ECIWAG', 'fred', start = start_date)['VALUE'])
labor_Data = np.asarray(dr('PRS84006173', 'fred', start = start_date)['VALUE'])
consump_Data = np.asarray(dr('COFC', 'fred', start = start_date)['VALUE'])

filt_wage = hp.hp_filter(wage_Data)[1]
filt_labor = hp.hp_filter(labor_Data)[1]
filt_consump = hp.hp_filter(consump_Data)[1]

moments = np.array([np.mean(filt_consump), np.mean(filt_labor),
                    np.mean(filt_wage)   ,np.var(filt_consump)])

##------------------------Step 2. Find steady state --------------------------##
def get_current_ss(beta):
    """
    This calls SolveSS.py to get the steady state for the particular parameter
    vector beta.
Esempio n. 5
0
__author__ = 'Davidws'

import pandas as pd
from pandas.io.data import DataReader as dr
from datetime import datetime
import numpy as np
from numpy.linalg import inv

symbols = ['ORCL', 'MSFT', 'AAPL']  #Define the tickers we want in our project
start_date = '2010-01-01'  # Give the start of the date range
d = {}  # Create an empty dictionary to hold them
for ticker in symbols:
    d[ticker] = dr(
        ticker, 'yahoo', start_date
    )  # Iterate over the tickers and get the stock data into our dictionary
pan = pd.Panel(
    d)  # Create a Panel of the Data - this will allow us to Melt / Cast
close = pan.minor_xs(
    'Adj Close'
)  # Cast on the Adj Close - We could use any of the available columns here
# Careful about the tickers - some stocks are not this old and wont have data then we need to determine what to do about the NAN as we calculate the covariance.

count_tickers = symbols.__len__()  # Get the Number of Stock Tickers we want

exp_return = close.mean(0)  # Calculate the E(R) for the stocks
sd_stock = close.std  # Calculate the Std.Dev for the stocks
cov_stock = close.cov()  # Calculate the Covariance Matrix for the stocks
# Uncomment below if you want to write this to a file - add your own path
# path = ('C:/...)
# close.to_csv(path + 'close.csv', sep=',')
inv_cov = inv(
Esempio n. 6
0
__author__ = 'Davidws'

import pandas as pd
from pandas.io.data import DataReader as dr
from datetime import datetime
import numpy as np
from numpy.linalg import inv

symbols = ['ORCL','MSFT','AAPL'] #Define the tickers we want in our project
start_date = '2010-01-01' # Give the start of the date range
d = {} # Create an empty dictionary to hold them
for ticker in symbols:
    d[ticker] = dr(ticker, 'yahoo',start_date) # Iterate over the tickers and get the stock data into our dictionary
pan = pd.Panel(d) # Create a Panel of the Data - this will allow us to Melt / Cast
close = pan.minor_xs('Adj Close') # Cast on the Adj Close - We could use any of the available columns here
# Careful about the tickers - some stocks are not this old and wont have data then we need to determine what to do about the NAN as we calculate the covariance.

count_tickers = symbols.__len__() # Get the Number of Stock Tickers we want

exp_return = close.mean(0) # Calculate the E(R) for the stocks
sd_stock = close.std # Calculate the Std.Dev for the stocks
cov_stock = close.cov() # Calculate the Covariance Matrix for the stocks
# Uncomment below if you want to write this to a file - add your own path
# path = ('C:/...)
# close.to_csv(path + 'close.csv', sep=',')
inv_cov = inv(cov_stock) # Calculates the inverse of the covariance matrix - This may be going too far for our solution so far.