Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
64 lines (56 sloc) 2.47 KB
import pynance as pn
import pandas as pd
import os
import pandas_datareader
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import BDay
from pandas.tseries.offsets import CustomBusinessDay
def dates_to_calc(beg_d, end_d, per):
us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())
print(pd.DatetimeIndex(start=beg_d,end=end_d, freq=us_bd))
def get(tickers, start, end):
def data(ticker):
return pandas_datareader.DataReader(ticker, 'google', start, end)
datas = map(data, tickers)
return pd.concat(datas, keys=tickers, names=['Ticker','Date'])
def create_time_series():
name = input("Name of file? \n ")
#name_save = input("Name to save under \n")
name = name + ".csv"
#name_save += ".csv"
df = pd.read_csv(name)
df = df.sort_values("date")
dates = df["date"].unique()
d_len = len(dates)
for i in range(d_len - 1):
s = dates[i].split("/")
beg_d = pd.datetime(int(s[2]),int(s[0]),int(s[1])) -BDay(1)
e = dates[i+1].split("/")
end_d = pd.datetime(int(e[2]),int(e[0]),int(e[1]))- BDay(1)
end_ds = end_d.strftime("%d/%m/%Y")
if beg_d != end_d:
per = "D"
d_df = pd.DataFrame(df[df["date"] == dates[i]])
d_df.columns = ['Date', 'Ticker', 'MC', 'Cluster', 'Weight', 'px_close', 'cusip', 'face']
tickers = d_df["Ticker"]
#print(d_df)
all_data = get(tickers, beg_d, end_d)
all_data = all_data[dates[i]:][['Close']].reset_index()
#all_data["Date"] = pd.datetime(all_data["Date"])
#ea = all_data.join(d_df)
ea = pd.merge(all_data, d_df,how = "right", on = ["Ticker"])
if i == 0:
tot = ea
else:
tot = tot.append(ea)
jt = tot
jtc = pd.concat([jt["Ticker"], jt["Date_x"], jt["Close"], jt["Date_y"], jt["Cluster"],
jt["cusip"], jt["Weight"]], axis = 1 )
jtc["return_close"] = jtc["Close"]/jtc["Close"].shift(1) -1
jtc["return_to_index"]= (jtc["Close"]/jtc["Close"].shift(1) -1)*jtc["Weight"]
jtc.columns = ['Ticker', 'Date_Price', 'Close', 'Date_ReBal', 'Cluster', 'Cusip', "Weight", "Return", "Return_Index"]
jtc = jtc.sort_values(["Date_ReBal", "Date_Price"])
jtc['Date_ReBal'] = pd.to_datetime(jtc['Date_ReBal'], format='%m/%d/%Y')
jtc = jtc.where(jtc["Date_Price"] >= jtc["Date_ReBal"]).dropna()
#tot.to_csv(name_save, sep = ",")
return jtc