def index(request): # variables to give inputs start_year = "2007-01-1" end_year = "2008-01-31" input_price = 500000 time_period = "Q" #set BYearEnd as Buisness year end function BuisnessYearEnd = BYearEnd() # obtain the start year # start_year = raw_input("enter start year")+str("-01-1") #Last buisness day of previous year start_year = BuisnessYearEnd.rollback(start_year) # obtain the end year # end_year = raw_input("enter end year")+str("-01-31") #Last buisness day of current year end_year = BuisnessYearEnd.rollback(end_year) # input_price = input("enter the input price") # time_period = raw_input(" W for weekly, M for monthly, Q for quarterly, A for yearly") number_of_stocks = int(input("enter the number of stocks")) print(start_year.date(), end_year) stock_symbols = stocks(number_of_stocks) # assign weights weights = assign_weights(start_year, end_year, time_period, stock_symbols) weighted_returns(start_year, end_year, input_price, stock_symbols, weights, time_period) return render(request, 'index.html')
class TestBYearEndLagged(Base): _offset = BYearEnd def test_bad_month_fail(self): msg = "Month must go from 1 to 12" with pytest.raises(ValueError, match=msg): BYearEnd(month=13) with pytest.raises(ValueError, match=msg): BYearEnd(month=0) offset_cases = [] offset_cases.append( ( BYearEnd(month=6), { datetime(2008, 1, 1): datetime(2008, 6, 30), datetime(2007, 6, 30): datetime(2008, 6, 30), }, ) ) offset_cases.append( ( BYearEnd(n=-1, month=6), { datetime(2008, 1, 1): datetime(2007, 6, 29), datetime(2007, 6, 30): datetime(2007, 6, 29), }, ) ) @pytest.mark.parametrize("case", offset_cases) def test_offset(self, case): offset, cases = case for base, expected in cases.items(): assert_offset_equal(offset, base, expected) def test_roll(self): offset = BYearEnd(month=6) date = datetime(2009, 11, 30) assert offset.rollforward(date) == datetime(2010, 6, 30) assert offset.rollback(date) == datetime(2009, 6, 30) on_offset_cases = [ (BYearEnd(month=2), datetime(2007, 2, 28), True), (BYearEnd(month=6), datetime(2007, 6, 30), False), ] @pytest.mark.parametrize("case", on_offset_cases) def test_is_on_offset(self, case): offset, dt, expected = case assert_is_on_offset(offset, dt, expected)
def calculate_ytd_perf(self): '''Return Ytd Performance (float)''' date = self.get_computation_date() - BYearEnd() while not cal.is_working_day(date): date = date - timedelta(days=1) return (float(self.get_nav().tail(n=1)) / self.get_nav().loc[date] - 1) * 100
class TestBYearEnd(Base): _offset = BYearEnd offset_cases = [] offset_cases.append((BYearEnd(), { datetime(2008, 1, 1): datetime(2008, 12, 31), datetime(2008, 6, 30): datetime(2008, 12, 31), datetime(2008, 12, 31): datetime(2009, 12, 31), datetime(2005, 12, 30): datetime(2006, 12, 29), datetime(2005, 12, 31): datetime(2006, 12, 29) })) offset_cases.append((BYearEnd(0), { datetime(2008, 1, 1): datetime(2008, 12, 31), datetime(2008, 6, 30): datetime(2008, 12, 31), datetime(2008, 12, 31): datetime(2008, 12, 31), datetime(2005, 12, 31): datetime(2006, 12, 29) })) offset_cases.append((BYearEnd(-1), { datetime(2007, 1, 1): datetime(2006, 12, 29), datetime(2008, 6, 30): datetime(2007, 12, 31), datetime(2008, 12, 31): datetime(2007, 12, 31), datetime(2006, 12, 29): datetime(2005, 12, 30), datetime(2006, 12, 30): datetime(2006, 12, 29), datetime(2007, 1, 1): datetime(2006, 12, 29) })) offset_cases.append((BYearEnd(-2), { datetime(2007, 1, 1): datetime(2005, 12, 30), datetime(2008, 6, 30): datetime(2006, 12, 29), datetime(2008, 12, 31): datetime(2006, 12, 29) })) @pytest.mark.parametrize('case', offset_cases) def test_offset(self, case): offset, cases = case for base, expected in compat.iteritems(cases): assert_offset_equal(offset, base, expected) on_offset_cases = [(BYearEnd(), datetime(2007, 12, 31), True), (BYearEnd(), datetime(2008, 1, 1), False), (BYearEnd(), datetime(2006, 12, 31), False), (BYearEnd(), datetime(2006, 12, 29), True)] @pytest.mark.parametrize('case', on_offset_cases) def test_onOffset(self, case): offset, dt, expected = case assert_onOffset(offset, dt, expected)
class TestBYearEndLagged(Base): _offset = BYearEnd def test_bad_month_fail(self): pytest.raises(Exception, BYearEnd, month=13) pytest.raises(Exception, BYearEnd, month=0) offset_cases = [] offset_cases.append((BYearEnd(month=6), { datetime(2008, 1, 1): datetime(2008, 6, 30), datetime(2007, 6, 30): datetime(2008, 6, 30) })) offset_cases.append((BYearEnd(n=-1, month=6), { datetime(2008, 1, 1): datetime(2007, 6, 29), datetime(2007, 6, 30): datetime(2007, 6, 29) })) @pytest.mark.parametrize('case', offset_cases) def test_offset(self, case): offset, cases = case for base, expected in compat.iteritems(cases): assert_offset_equal(offset, base, expected) def test_roll(self): offset = BYearEnd(month=6) date = datetime(2009, 11, 30) assert offset.rollforward(date) == datetime(2010, 6, 30) assert offset.rollback(date) == datetime(2009, 6, 30) on_offset_cases = [(BYearEnd(month=2), datetime(2007, 2, 28), True), (BYearEnd(month=6), datetime(2007, 6, 30), False)] @pytest.mark.parametrize('case', on_offset_cases) def test_onOffset(self, case): offset, dt, expected = case assert_onOffset(offset, dt, expected)
'AS-JAN' : YearBegin(month=1), 'AS' : YearBegin(month=1), 'AS-FEB' : YearBegin(month=2), 'AS-MAR' : YearBegin(month=3), 'AS-APR' : YearBegin(month=4), 'AS-MAY' : YearBegin(month=5), 'AS-JUN' : YearBegin(month=6), 'AS-JUL' : YearBegin(month=7), 'AS-AUG' : YearBegin(month=8), 'AS-SEP' : YearBegin(month=9), 'AS-OCT' : YearBegin(month=10), 'AS-NOV' : YearBegin(month=11), 'AS-DEC' : YearBegin(month=12), # Annual - Business 'BA-JAN' : BYearEnd(month=1), 'BA-FEB' : BYearEnd(month=2), 'BA-MAR' : BYearEnd(month=3), 'BA-APR' : BYearEnd(month=4), 'BA-MAY' : BYearEnd(month=5), 'BA-JUN' : BYearEnd(month=6), 'BA-JUL' : BYearEnd(month=7), 'BA-AUG' : BYearEnd(month=8), 'BA-SEP' : BYearEnd(month=9), 'BA-OCT' : BYearEnd(month=10), 'BA-NOV' : BYearEnd(month=11), 'BA-DEC' : BYearEnd(month=12), 'BA' : BYearEnd(month=12), # Annual - Business (Start) 'BAS-JAN' : BYearBegin(month=1),
def test_bad_month_fail(self): msg = "Month must go from 1 to 12" with pytest.raises(ValueError, match=msg): BYearEnd(month=13) with pytest.raises(ValueError, match=msg): BYearEnd(month=0)
def test_misspecified(self): msg = "Month must go from 1 to 12" with pytest.raises(ValueError, match=msg): BYearBegin(month=13) with pytest.raises(ValueError, match=msg): BYearEnd(month=13)
def test_roll(self): offset = BYearEnd(month=6) date = datetime(2009, 11, 30) assert offset.rollforward(date) == datetime(2010, 6, 30) assert offset.rollback(date) == datetime(2009, 6, 30)
def returns(ts, calc_type='D', force=False): """ Calculate returns time series of returns for various time windows. :param ts: :py:obj:`TimeSeries`, :py:obj:`pandas.Series`, :py:obj:`pandas.DataFrame` Time series whose returns will be calculated. :param calc_type: {'D', 'W', 'M', '6M', 'Y', '3Y', 'WTD', 'MTD', 'YTD', 'SI'}, optional The time window for return calculation. Default is 'D' (daily returns). :param force: bool, optional Backward-fill missing data. Default is False. :return :py:obj:`pandas.Series`, :py:obj:`pandas.DataFrame` Series or DataFrame of returns. """ if isinstance(ts, TimeSeries): df = ts.ts_values else: df = ts if df.empty: return df first_index = df.first_valid_index() last_index = df.last_valid_index() def array_return(x): return x[-1] / x[0] - 1 def one_month_ago(x): return to_datetime(to_ql_date(x) - ql.Period(1, ql.Months)) def six_months_ago(x): return to_datetime(to_ql_date(x) - ql.Period(6, ql.Months)) calc_type = calc_type.upper() if calc_type == 'D': return df.pct_change() elif calc_type == 'W': df = df.reindex() return df.resample(BDay()).fillna(method='pad').rolling( 6, min_periods=2).apply(array_return) elif calc_type == 'M': one_month_ago = df.index.map(one_month_ago) df_one_month_ago = df.reindex(one_month_ago, method='pad') if force: df_one_month_ago = df_one_month_ago.fillna(df.loc[first_index]) return pd.Series(index=df.index, data=df.values / df_one_month_ago.values) - 1 elif calc_type == '6M': six_months_ago = df.index.map(six_months_ago) df_six_months_ago = df.reindex(six_months_ago, method='pad') if force is True: df_six_months_ago = df_six_months_ago.fillna(df.loc[first_index]) return pd.Series(index=df.index, data=df.values / df_six_months_ago.values) - 1 elif calc_type == 'Y': one_year_ago = df.index - pd.DateOffset(years=1) df_one_year_ago = df.reindex(one_year_ago, method='pad') if force is True: df_one_year_ago = df_one_year_ago.fillna(df.loc[first_index]) return pd.Series(index=df.index, data=df.values / df_one_year_ago.values) - 1 elif calc_type == '3Y': three_years_ago = df.index - pd.dateOffset(years=3) df_three_years_ago = df.reindex(three_years_ago, method='pad') if force: df_three_years_ago = df_three_years_ago.fillna(df.loc[first_index]) return pd.Series(index=df.index, data=df.values / df_three_years_ago.values) - 1 elif calc_type == 'WTD': index = pd.date_range(first_index, last_index, freq=Week(weekday=4)) df_week_end = df.reindex(index, method='pad').reindex(df.index, method='pad') return df / df_week_end - 1 elif calc_type == 'MTD': index = pd.date_range(first_index, last_index, freq=BMonthEnd()) df_month_end = df.reindex(index, method='pad').reindex(df.index, method='pad') return df / df_month_end - 1 elif calc_type == 'YTD': index = pd.date_range(first_index, last_index, freq=BYearEnd()) df_year_end = df.reindex(index, method='pad').reindex(df.index, method='pad') return df / df_year_end - 1 elif calc_type == 'SI': return df / df.loc[first_index] - 1
class TestBYearEnd(Base): _offset: type[YearOffset] = BYearEnd offset_cases = [] offset_cases.append( ( BYearEnd(), { datetime(2008, 1, 1): datetime(2008, 12, 31), datetime(2008, 6, 30): datetime(2008, 12, 31), datetime(2008, 12, 31): datetime(2009, 12, 31), datetime(2005, 12, 30): datetime(2006, 12, 29), datetime(2005, 12, 31): datetime(2006, 12, 29), }, ) ) offset_cases.append( ( BYearEnd(0), { datetime(2008, 1, 1): datetime(2008, 12, 31), datetime(2008, 6, 30): datetime(2008, 12, 31), datetime(2008, 12, 31): datetime(2008, 12, 31), datetime(2005, 12, 31): datetime(2006, 12, 29), }, ) ) offset_cases.append( ( BYearEnd(-1), { datetime(2007, 1, 1): datetime(2006, 12, 29), datetime(2008, 6, 30): datetime(2007, 12, 31), datetime(2008, 12, 31): datetime(2007, 12, 31), datetime(2006, 12, 29): datetime(2005, 12, 30), datetime(2006, 12, 30): datetime(2006, 12, 29), datetime(2007, 1, 1): datetime(2006, 12, 29), }, ) ) offset_cases.append( ( BYearEnd(-2), { datetime(2007, 1, 1): datetime(2005, 12, 30), datetime(2008, 6, 30): datetime(2006, 12, 29), datetime(2008, 12, 31): datetime(2006, 12, 29), }, ) ) @pytest.mark.parametrize("case", offset_cases) def test_offset(self, case): offset, cases = case for base, expected in cases.items(): assert_offset_equal(offset, base, expected) on_offset_cases = [ (BYearEnd(), datetime(2007, 12, 31), True), (BYearEnd(), datetime(2008, 1, 1), False), (BYearEnd(), datetime(2006, 12, 31), False), (BYearEnd(), datetime(2006, 12, 29), True), ] @pytest.mark.parametrize("case", on_offset_cases) def test_is_on_offset(self, case): offset, dt, expected = case assert_is_on_offset(offset, dt, expected)