class TestBMonthBegin(Base): _offset = BMonthBegin def test_offsets_compare_equal(self): # root cause of #456 offset1 = BMonthBegin() offset2 = BMonthBegin() assert not offset1 != offset2 offset_cases = [] offset_cases.append((BMonthBegin(), { datetime(2008, 1, 1): datetime(2008, 2, 1), datetime(2008, 1, 31): datetime(2008, 2, 1), datetime(2006, 12, 29): datetime(2007, 1, 1), datetime(2006, 12, 31): datetime(2007, 1, 1), datetime(2006, 9, 1): datetime(2006, 10, 2), datetime(2007, 1, 1): datetime(2007, 2, 1), datetime(2006, 12, 1): datetime(2007, 1, 1)})) offset_cases.append((BMonthBegin(0), { datetime(2008, 1, 1): datetime(2008, 1, 1), datetime(2006, 10, 2): datetime(2006, 10, 2), datetime(2008, 1, 31): datetime(2008, 2, 1), datetime(2006, 12, 29): datetime(2007, 1, 1), datetime(2006, 12, 31): datetime(2007, 1, 1), datetime(2006, 9, 15): datetime(2006, 10, 2)})) offset_cases.append((BMonthBegin(2), { datetime(2008, 1, 1): datetime(2008, 3, 3), datetime(2008, 1, 15): datetime(2008, 3, 3), datetime(2006, 12, 29): datetime(2007, 2, 1), datetime(2006, 12, 31): datetime(2007, 2, 1), datetime(2007, 1, 1): datetime(2007, 3, 1), datetime(2006, 11, 1): datetime(2007, 1, 1)})) offset_cases.append((BMonthBegin(-1), { datetime(2007, 1, 1): datetime(2006, 12, 1), datetime(2008, 6, 30): datetime(2008, 6, 2), datetime(2008, 6, 1): datetime(2008, 5, 1), datetime(2008, 3, 10): datetime(2008, 3, 3), datetime(2008, 12, 31): datetime(2008, 12, 1), datetime(2006, 12, 29): datetime(2006, 12, 1), datetime(2006, 12, 30): datetime(2006, 12, 1), datetime(2007, 1, 1): datetime(2006, 12, 1)})) @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 = [(BMonthBegin(), datetime(2007, 12, 31), False), (BMonthBegin(), datetime(2008, 1, 1), True), (BMonthBegin(), datetime(2001, 4, 2), True), (BMonthBegin(), datetime(2008, 3, 3), True)] @pytest.mark.parametrize('case', on_offset_cases) def test_onOffset(self, case): offset, dt, expected = case assert_onOffset(offset, dt, expected)
def BackTest_1_Beta(self): d = self.Dates[0] for i in xrange( int( self.Periods_Diff(self.Dates[0], self.Dates[1]) / self.Frequency)): # Compute the date for historical data extract d_f = d - BMonthBegin() * self.Histo_Length # Extract the Benchmark index returns bnch = self.Benchmark.Extract_Index_Returns([d_f, d]) returns = self.Benchmark.Extract_Returns([d_f, d], d - MonthEnd()) assets = self.Benchmark.Extract_Compo(d - MonthEnd()) std_Index = np.std(bnch) beta = np.empty(len(assets)) for i in xrange(len(beta)): # Delete missing value in the estimation beta[i] = bnch.iloc[:, 0].cov(returns.iloc[:, i]) / std_Index self.RB_Weighted_1_risk(beta) bckData = self.Benchmark.Extract_Returns( [d + BDay(), (d + BDay()) + BMonthEnd() * self.Frequency], d - MonthEnd()).loc[:, assets] self.Compute_Performance(bckData) self.Wght_Histo[d] = pd.DataFrame(self.Weights, index=assets) d = (d + BMonthEnd() * self.Frequency) + BDay()
def build_prediction_model(): boe = load_boe_history() shb = load_shb_history() # filter BOE data to match SHB date range shb_min = shb['valid_from'].min() shb_max = shb['valid_from'].max() mask = (boe['Date'] >= shb_min) & (boe['Date'] <= shb_max) boe = boe.loc[mask] # reduce BOE data to start-of-month values boe['fom'] = boe['Date'].apply(lambda x: BMonthBegin().rollback(x)) boe = boe[boe['Date'] == boe['fom']] # add in monthly changes for term in TERMS: boe[f'{term}ydiff'] = boe[f'{term}y'].diff() shb[f'{term}diff'] = shb[term].diff() df = boe.merge(shb, on='period', how='inner') # drop the first row - which will contain NaN df = df.iloc[1:-1] for term in TERMS: X = df[[f'{term}ydiff']] y = df[[f'{term}diff']] # using default 5-split tss = TimeSeriesSplit(n_splits=5).split(X) model = LinearRegression().fit(X, y) scores = cross_val_score(model, X, y, cv=tss) r2 = scores.mean() store(term, model, 'sklearn_LinearRegression_CV', r2)
def _initial_fx_load(self, start_date = '2011-06-01', end_date = '2016-09-01'): self.db_method = 'replace' date = pd.to_datetime(start_date) stop_date = pd.to_datetime(end_date) - BMonthBegin() + BMonthBegin() date_list = [date.strftime('%Y-%m-%d')] while date != stop_date: date = date + BMonthBegin() date_list.append(date.strftime('%Y-%m-%d')) date_list.append('2012-01-03') date_list.append('2013-01-02') date_list.append('2014-01-02') date_list.append('2015-01-02') date_list.append('2016-01-04') self._get_open_fx(date_list) fx_dic = _get_open_fx(date_list) _load_fx_to_db(fx_dic,db_method='replace') return True
def BackTest_MinVar(self): d = self.Dates[0] for i in xrange( int( self.Periods_Diff(self.Dates[0], self.Dates[1]) / self.Frequency)): d_f = d - BMonthBegin() * self.Histo_Length vcov, assets = self.Benchmark.Extract_VCov_Matrix([d_f, d], d - MonthEnd()) self.MinVariance(vcov, 0.001) bckData = self.Benchmark.Extract_Returns( [d + BDay(), (d + BDay()) + BMonthEnd() * self.Frequency], d - MonthEnd()).loc[:, assets] self.Compute_Performance(bckData) self.Wght_Histo[d] = pd.DataFrame(self.Weights, index=assets) d = (d + BMonthEnd() * self.Frequency) + BDay()
def BackTest_1_Sigma(self): d = self.Dates[0] for i in xrange( int( self.Periods_Diff(self.Dates[0], self.Dates[1]) / self.Frequency)): d_f = d - BMonthBegin() * self.Histo_Length Std, assets = self.Benchmark.Extract_Sigma([d_f, d], d - MonthEnd()) self.RB_Weighted_1_risk(Std) bckData = self.Benchmark.Extract_Returns( [d + BDay(), (d + BDay()) + BMonthEnd() * self.Frequency], d - MonthEnd()).loc[:, assets] self.Compute_Performance(bckData) self.Wght_Histo[d] = pd.DataFrame(self.Weights, index=assets) d = (d + BMonthEnd() * self.Frequency) + BDay()
def chart_rate_moves(): boe = load_boe_history() shb = load_shb_history() # filter BOE data to match SHB date range shb_min = shb['valid_from'].min() shb_max = shb['valid_from'].max() mask = (boe['Date'] >= shb_min) & (boe['Date'] <= shb_max) boe = boe.loc[mask] # reduce BOE data to start-of-month values boe['fom'] = boe['Date'].apply(lambda x: BMonthBegin().rollback(x)) boe = boe[boe['Date'] == boe['fom']] # add in monthly changes for rate in TERMS: boe[f'{rate}ydiff'] = boe[f'{rate}y'].diff() shb[f'{rate}diff'] = shb[rate].diff() df = boe.merge(shb, on='period', how='inner') rows = 1 cols = 5 fig, axs = plt.subplots(rows, cols, figsize=(15, 4), squeeze=False, sharey=True) for idx, rate in enumerate(TERMS): row = idx % rows col = idx // rows ax = axs[row][col] residual = df[f'{rate}diff'] - df[f'{rate}ydiff'] # ax.plot(df['Date'], residual, 'r+', label='residual') # ax.plot(boe['Date'], boe[f'{rate}ydiff'], 'g', label='boe diff') # ax.plot(shb['valid_from'], shb[f'{rate}diff'], 'c.', label='shb diff') ax.plot(df[f'{rate}ydiff'], df[f'{rate}diff'], 'b+') ax.set_title(f'{rate} year rate') ax.legend() plt.show()
'D' : Day(), 'B' : BDay(), 'H' : Hour(), 'T' : Minute(), 'S' : Second(), 'L' : Milli(), 'U' : Micro(), None : None, # Monthly - Calendar 'M' : MonthEnd(), 'MS' : MonthBegin(), # Monthly - Business 'BM' : BMonthEnd(), 'BMS' : BMonthBegin(), # Annual - Calendar 'A-JAN' : YearEnd(month=1), 'A-FEB' : YearEnd(month=2), 'A-MAR' : YearEnd(month=3), 'A-APR' : YearEnd(month=4), 'A-MAY' : YearEnd(month=5), 'A-JUN' : YearEnd(month=6), 'A-JUL' : YearEnd(month=7), 'A-AUG' : YearEnd(month=8), 'A-SEP' : YearEnd(month=9), 'A-OCT' : YearEnd(month=10), 'A-NOV' : YearEnd(month=11), 'A-DEC' : YearEnd(month=12), 'A' : YearEnd(month=12),
def test_offsets_compare_equal(self): # root cause of #456 offset1 = BMonthBegin() offset2 = BMonthBegin() assert not offset1 != offset2
def test_quarterly_dont_normalize(): date = datetime(2012, 3, 31, 5, 30) offsets = (QuarterBegin, QuarterEnd, BQuarterEnd, BQuarterBegin) for klass in offsets: result = date + klass() assert (result.time() == date.time()) @pytest.mark.parametrize( 'offset', [MonthBegin(), MonthEnd(), BMonthBegin(), BMonthEnd()]) def test_apply_index(offset): rng = pd.date_range(start='1/1/2000', periods=100000, freq='T') ser = pd.Series(rng) res = rng + offset res_v2 = offset.apply_index(rng) assert (res == res_v2).all() assert res[0] == rng[0] + offset assert res[-1] == rng[-1] + offset res2 = ser + offset # apply_index is only for indexes, not series, so no res2_v2 assert res2.iloc[0] == ser.iloc[0] + offset assert res2.iloc[-1] == ser.iloc[-1] + offset
import pandas as pd from pandas.tseries.offsets import BMonthBegin from datetime import date from datetime import datetime #d = date.today() #offset = BMonthBegin() #offset.rollforward(d) #offset.rollback(d) offset = BMonthBegin() def getFirstDay(d): day = datetime.strptime(str(d), '%Y%m%d').date() fd = offset.rollforward(day) return fd df = pd.read_csv("newticker.csv") df['new'] = df['Month'].apply(lambda x: getFirstDay(x)) df.to_csv("NewFile.csv")
full_df = full_df.dropna() print(full_df) """# Create list of tickers from volume screened df""" volScreenList = list(full_df.columns) print(volScreenList) print(len(volScreenList)) """# Create Prices Data frame from volume screened list and remove tickers with avg price <1""" #Get dates todayD = date.today() offset = BMonthEnd(-1) offsetFirst = BMonthBegin(-13) # Get first mo. first day, last mo. last day lastDayLastMonth = todayD+offset firstDayFirstMonth = todayD+offsetFirst # Append prices to tickers prices_df = pd.DataFrame() for ticker in volScreenList: try: prices_df[ticker] = historical_datas[ticker]['close'].loc[f"{firstDayFirstMonth.year}-{firstDayFirstMonth.month}-{firstDayFirstMonth.day}" : f"{lastDayLastMonth.year}-{lastDayLastMonth.month}-{lastDayLastMonth.day}"] print(f"completed {ticker}") except: print(f"error with {ticker}")