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 pay_employees(self, today_date): date_format = "%Y-%m-%d" for emp in self.employeesList: payMet = emp.payDate.split("-") w = datetime.strptime(today_date, date_format) - datetime.strptime( emp.last_pay_date, date_format) worked_days = int(w.days) value = 0 if payMet[0] == "weekly": hours = emp.workHours if worked_days >= (7 * int(payMet[1])) and payMet[2] == str( datetime.strptime(today_date, date_format).strftime("%A")).lower(): if isinstance(emp, Hourly): bonus_hours = hours - (worked_days * 8) aux = hours + 0.5 * bonus_hours value = aux * float(emp.hour_value) if isinstance(emp, Comissioned): if payMet[1] == 1: div = 4 else: div = 2 value = float(emp.wage) / div + emp.comission elif isinstance(emp, Salaried): if payMet[1] == 1: div = 4 else: div = 2 value = emp.wage / div if value > 0: self.payday_employee_method(emp_id=emp.id, value=value, today_date=today_date) elif payMet[0] == "monthly": if isinstance(emp, Hourly): bonus_hours = hours - (worked_days * 8) value = emp.hour_value * (hours + 0.5 * bonus_hours) if isinstance(emp, Comissioned): value = float(emp.wage) + emp.comission if isinstance(emp, Salaried): value = emp.wage if str( datetime.strptime( today_date, date_format).strftime("%d")).lower() == payMet[1]: self.payday_employee_method(emp.id, value, today_date) elif payMet[1] == "$": offset = BMonthEnd() payDay = datetime.strptime(today_date, date_format) d = offset.rollforward(payDay) if payDay.strftime("%d") == d.strftime("%d"): self.payday_employee_method(emp.id, value, today_date) elif str( datetime.strptime( today_date, date_format).strftime("%d")).lower() == payMet[1]: self.payday_employee_method(emp.id, value, today_date) print(self.payment_register) print("\n")
def create_csv_cuotas_cobradas_file( sql_select_csv_fn, output_csv_fn, columns_name_to_filter, db_engine, active_wallet_result, cartera_activa_table_name="cartera_activa"): msg = f"Reading {sql_select_csv_fn} ..." try: logger.info(msg) sql_file_csv = open( os.path.join(settings.MYSQL_SQL_DIR, sql_select_csv_fn)) logger.info(f"{msg} Done!") d = date.today() offset = BMonthEnd() date2 = str(offset.rollforward(d) - BDay(2))[:-9] date1 = str(offset.rollback(d) - BDay(1))[:-9] data = sql_file_csv.read().replace('$1', date1).replace('$2', date2) msg = f"Executing pandas.read_sql for {sql_select_csv_fn} ..." logger.info(msg) result = pandas.read_sql(sql=text( data.format(cartera_activa_table_name)), con=db_engine) logger.info(f"{msg} Done!") pattern = re.compile('[^a-zA-Z0-9\-._: \.\(\)\/]') msg = f"Removing characters '{pattern}' by '_' for {sql_select_csv_fn} data ..." logger.info(msg) for columns_name in columns_name_to_filter: result[columns_name] = result[columns_name].str.replace( pattern, '_') logger.info(f"{msg} Done!") fn = f'{output_csv_fn}_{datetime.datetime.now().strftime("%m-%d-%y-%H%M%S")}.zip' msg = f"Writing csv file {fn}... " logger.info(msg) result_csv = result.to_csv(header=False, decimal=',', index=False, encoding='utf-8-sig') zipbuff = io.BytesIO() with zipfile.ZipFile(zipbuff, "a", zipfile.ZIP_DEFLATED, False) as zip_file: zip_file.writestr(f"results_{output_csv_fn}.csv", result_csv.encode('utf-8-sig')) result = None result_csv = None gc.collect() active_wallet_result.save(fn, ContentFile(zipbuff.getvalue())) logger.info(f"{msg} Done!") except Exception as error: msg = f"{msg} Fail!: {error}" raise Exception(msg)
def BackTest_EqualWeight(self): d = self.Dates[0] for i in xrange( int( self.Periods_Diff(self.Dates[0], self.Dates[1]) / self.Frequency)): assets = self.Benchmark.Extract_Compo(d - MonthEnd()) self.Equal_Weighted(len(assets)) 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_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 test_asfreq(self): ts = Series( [0.0, 1.0, 2.0], index=DatetimeIndex( [ datetime(2009, 10, 30), datetime(2009, 11, 30), datetime(2009, 12, 31), ], freq="BM", ), ) daily_ts = ts.asfreq("B") monthly_ts = daily_ts.asfreq("BM") tm.assert_series_equal(monthly_ts, ts) daily_ts = ts.asfreq("B", method="pad") monthly_ts = daily_ts.asfreq("BM") tm.assert_series_equal(monthly_ts, ts) daily_ts = ts.asfreq(BDay()) monthly_ts = daily_ts.asfreq(BMonthEnd()) tm.assert_series_equal(monthly_ts, ts) result = ts[:0].asfreq("M") assert len(result) == 0 assert result is not ts daily_ts = ts.asfreq("D", fill_value=-1) result = daily_ts.value_counts().sort_index() expected = Series([60, 1, 1, 1], index=[-1.0, 2.0, 1.0, 0.0]).sort_index() tm.assert_series_equal(result, expected)
def option_vol(self, time, option_type, S, K, C0, r=0.0015, d=0.0424): start = time.date() end = BMonthEnd().rollforward(start).date() t = (end - start).days / 365.0 # We need a starting guess for the implied volatility. We chose 0.5 # arbitrarily. vol = 0.5 epsilon = 1.0 # Define variable to check stopping conditions abstol = 1e-4 # Stop calculation when abs(epsilon) < this number i = 0 # Variable to count number of iterations max_iter = 1e3 # Max number of iterations before aborting while epsilon > abstol: # if-statement to avoid getting stuck in an infinite loop. if i > max_iter: break i = i + 1 orig = vol d1, d2 = self.d(vol, S, K, r, t) if option_type == 'CALL': function_value = self.call_price(vol, S, K, r, t, d, d1, d2) - C0 else: function_value = self.put_price(vol, S, K, r, t, d, d1, d2) - C0 vega = S * norm.pdf(d1) * sqrt(t) vol = -function_value / vega + vol epsilon = abs(function_value) return vol
def calculate_mtd_perf(self): '''Return mtd Performance (float)''' date = self.get_computation_date() - BMonthEnd() 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
def test_outer_join(self, freq): # should just behave as union start, end = datetime(2009, 1, 1), datetime(2010, 1, 1) rng = date_range(start=start, end=end, freq=freq) # overlapping left = rng[:10] right = rng[5:10] the_join = left.join(right, how="outer") assert isinstance(the_join, DatetimeIndex) # non-overlapping, gap in middle left = rng[:5] right = rng[10:] the_join = left.join(right, how="outer") assert isinstance(the_join, DatetimeIndex) assert the_join.freq is None # non-overlapping, no gap left = rng[:5] right = rng[5:10] the_join = left.join(right, how="outer") assert isinstance(the_join, DatetimeIndex) # overlapping, but different offset other = date_range(start, end, freq=BMonthEnd()) the_join = rng.join(other, how="outer") assert isinstance(the_join, DatetimeIndex) assert the_join.freq is None
def test_outer_join(self): # should just behave as union # overlapping left = self.rng[:10] right = self.rng[5:10] the_join = left.join(right, how="outer") assert isinstance(the_join, DatetimeIndex) # non-overlapping, gap in middle left = self.rng[:5] right = self.rng[10:] the_join = left.join(right, how="outer") assert isinstance(the_join, DatetimeIndex) assert the_join.freq is None # non-overlapping, no gap left = self.rng[:5] right = self.rng[5:10] the_join = left.join(right, how="outer") assert isinstance(the_join, DatetimeIndex) # overlapping, but different offset rng = date_range(START, END, freq=BMonthEnd()) the_join = self.rng.join(rng, how="outer") assert isinstance(the_join, DatetimeIndex) assert the_join.freq is None
def test_asfreq(self): ts = Series([0., 1., 2.], index=[ datetime(2009, 10, 30), datetime(2009, 11, 30), datetime(2009, 12, 31) ]) daily_ts = ts.asfreq('B') monthly_ts = daily_ts.asfreq('BM') tm.assert_series_equal(monthly_ts, ts) daily_ts = ts.asfreq('B', method='pad') monthly_ts = daily_ts.asfreq('BM') tm.assert_series_equal(monthly_ts, ts) daily_ts = ts.asfreq(BDay()) monthly_ts = daily_ts.asfreq(BMonthEnd()) tm.assert_series_equal(monthly_ts, ts) result = ts[:0].asfreq('M') assert len(result) == 0 assert result is not ts daily_ts = ts.asfreq('D', fill_value=-1) result = daily_ts.value_counts().sort_index() expected = Series([60, 1, 1, 1], index=[-1.0, 2.0, 1.0, 0.0]).sort_index() tm.assert_series_equal(result, expected)
def test_union(self, sort): # overlapping left = self.rng[:10] right = self.rng[5:10] the_union = left.union(right, sort=sort) assert isinstance(the_union, DatetimeIndex) # non-overlapping, gap in middle left = self.rng[:5] right = self.rng[10:] the_union = left.union(right, sort) assert isinstance(the_union, Index) # non-overlapping, no gap left = self.rng[:5] right = self.rng[5:10] the_union = left.union(right, sort=sort) assert isinstance(the_union, DatetimeIndex) # order does not matter if sort is None: tm.assert_index_equal(right.union(left, sort=sort), the_union) # overlapping, but different offset rng = date_range(START, END, freq=BMonthEnd()) the_union = self.rng.union(rng, sort=sort) assert isinstance(the_union, DatetimeIndex)
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 test_get_offset_name(self): assert BDay().freqstr == "B" assert BDay(2).freqstr == "2B" assert BMonthEnd().freqstr == "BM" assert Week(weekday=0).freqstr == "W-MON" assert Week(weekday=1).freqstr == "W-TUE" assert Week(weekday=2).freqstr == "W-WED" assert Week(weekday=3).freqstr == "W-THU" assert Week(weekday=4).freqstr == "W-FRI" assert LastWeekOfMonth(weekday=WeekDay.SUN).freqstr == "LWOM-SUN"
def digest_stocks_features(stock_lst, start_index, finish_index): for stock in stock_lst[start_index:finish_index + 1]: print("working on {}".format(stock)) stock_data_df = stocks_data_dict[stock] all_signals = dict_of_stocks_fp[dict_of_stocks_fp.symbol == stock] # TODO add counter and progress bar or percentage ( curr / len of signals) # TODO per stock per all stocks curr_stock_signals_features = pd.DataFrame() for signal_index, row in enumerate(all_signals.iterrows()): curr_signal_start_date = pd.to_datetime(row[1].start_date) signal_label = row[1].false_positive curr_signal_month_data = stock_data_df["adj_close"][stock_data_df.index <= curr_signal_start_date].resample( "BM").last().interpolate() curr_signal_month_data = curr_signal_month_data[ curr_signal_month_data.index >= curr_signal_start_date - BMonthEnd(100)] curr_signal_week_data = stock_data_df["adj_close"][stock_data_df.index <= curr_signal_start_date].resample( "W").last().interpolate() curr_signal_week_data = curr_signal_week_data[ curr_signal_week_data.index >= curr_signal_start_date - Week(100)] curr_signal_day_data = stock_data_df["adj_close"][stock_data_df.index <= curr_signal_start_date].resample( "B").last().interpolate() curr_signal_day_data = curr_signal_day_data[ curr_signal_day_data.index >= curr_signal_start_date - BDay(100)] curr_signal_only, curr_signal_features_month = do_features(curr_signal_month_data, curr_signal_start_date, all_signals, stock, signal_index, signal_label ) curr_signal_only, curr_signal_features_week = do_features(curr_signal_week_data, curr_signal_start_date, all_signals, stock, signal_index, signal_label ) curr_signal_only, curr_signal_features_day = do_features(curr_signal_day_data, curr_signal_start_date, all_signals, stock, signal_index, signal_label ) timestamps_features_month[str(stock) + "_" + str(signal_index)] = curr_signal_features_month timestamps_features_week[str(stock) + "_" + str(signal_index)] = curr_signal_features_week timestamps_features_day[str(stock) + "_" + str(signal_index)] = curr_signal_features_day # adding to df of current stock curr_stock_signals_features = pd.concat([curr_stock_signals_features, curr_signal_only]) # disregard the date index # still saving it for train/test separations (e.g. learn until 2010 and test afterwards) curr_stock_signals_features = curr_stock_signals_features.reset_index() with lock: # add to the list of all signals of all stocks print("finished {}".format(stock)) all_stocks_signals_features[stock] = curr_stock_signals_features print("finished stocks features")
def test_get_offset(): with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): _get_offset("gibberish") with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): _get_offset("QS-JAN-B") pairs = [ ("B", BDay()), ("b", BDay()), ("bm", BMonthEnd()), ("Bm", BMonthEnd()), ("W-MON", Week(weekday=0)), ("W-TUE", Week(weekday=1)), ("W-WED", Week(weekday=2)), ("W-THU", Week(weekday=3)), ("W-FRI", Week(weekday=4)), ] for name, expected in pairs: offset = _get_offset(name) assert offset == expected, ( f"Expected {repr(name)} to yield {repr(expected)} " f"(actual: {repr(offset)})")
def test_shift(self): shifted = self.rng.shift(5) assert shifted[0] == self.rng[5] assert shifted.freq == self.rng.freq shifted = self.rng.shift(-5) assert shifted[5] == self.rng[0] assert shifted.freq == self.rng.freq shifted = self.rng.shift(0) assert shifted[0] == self.rng[0] assert shifted.freq == self.rng.freq rng = date_range(START, END, freq=BMonthEnd()) shifted = rng.shift(1, freq=BDay()) assert shifted[0] == rng[0] + BDay()
def test_shift(self): shifted = self.rng.shift(5) self.assertEqual(shifted[0], self.rng[5]) self.assertEqual(shifted.offset, self.rng.offset) shifted = self.rng.shift(-5) self.assertEqual(shifted[5], self.rng[0]) self.assertEqual(shifted.offset, self.rng.offset) shifted = self.rng.shift(0) self.assertEqual(shifted[0], self.rng[0]) self.assertEqual(shifted.offset, self.rng.offset) rng = date_range(START, END, freq=BMonthEnd()) shifted = rng.shift(1, freq=BDay()) self.assertEqual(shifted[0], rng[0] + BDay())
def option_data(date, code): url = 'https://www.hkex.com.hk/eng/stat/dmstat/dayrpt/dqe{}.htm'.format( date) resp = requests.get(url) soup = BeautifulSoup(resp.text, 'lxml') table = soup.find('a', {'name': code}).text table = re.split(r'\s{2,}', table)[23:] call_list = table[:table.index('TOTAL CALL')] put_list = table[table.index('TOTAL CALL') + 4:table.index('TOTAL PUT')] my_list = [] for row in range(int(len(call_list) / 11)): i = row * 11 l = call_list[i:i + 11] my_list.append(l) for row in range(int(len(put_list) / 11)): i = row * 11 l = put_list[i:i + 11] my_list.append(l) headers = [ 'Expiry', 'Contract', 'Opening Price', 'Daily High', 'Daily Low', 'Settlement Price', 'Chainge in Settlement Price', 'IV', 'Volume', 'OI', 'Change in OI' ] df = pd.DataFrame(my_list, columns=headers) # Data Procesing df['Contract Type'] = df['Contract'].str[-1] df['Strike'] = df['Contract'].str[:-2].astype(float) df['Expiry Year'] = ('20' + df['Expiry'].str[-2:]).astype(int) df['Expiry Month'] = df['Expiry'].str[:3].apply( lambda x: datetime.datetime.strptime(x, "%b").month) func = lambda x: BMonthEnd().rollforward( datetime.datetime(x['Expiry Year'], x['Expiry Month'], 1)) df['Expiry Date'] = df.apply(func, axis=1) df['T'] = (df['Expiry Date'] - datetime.datetime.today()).astype('timedelta64[D]') return df
def test_shift(self): shifted = self.rng.shift(5) self.assertEqual(shifted[0], self.rng[5]) self.assertEqual(shifted.offset, self.rng.offset) shifted = self.rng.shift(-5) self.assertEqual(shifted[5], self.rng[0]) self.assertEqual(shifted.offset, self.rng.offset) shifted = self.rng.shift(0) self.assertEqual(shifted[0], self.rng[0]) self.assertEqual(shifted.offset, self.rng.offset) with tm.assert_produces_warning(com.PerformanceWarning): rng = date_range(START, END, freq=BMonthEnd()) shifted = rng.shift(1, freq=CDay()) self.assertEqual(shifted[0], rng[0] + CDay())
def test_asfreq(self): ts = Series([0., 1., 2.], index=[datetime(2009, 10, 30), datetime( 2009, 11, 30), datetime(2009, 12, 31)]) daily_ts = ts.asfreq('B') monthly_ts = daily_ts.asfreq('BM') self.assert_series_equal(monthly_ts, ts) daily_ts = ts.asfreq('B', method='pad') monthly_ts = daily_ts.asfreq('BM') self.assert_series_equal(monthly_ts, ts) daily_ts = ts.asfreq(BDay()) monthly_ts = daily_ts.asfreq(BMonthEnd()) self.assert_series_equal(monthly_ts, ts) result = ts[:0].asfreq('M') self.assertEqual(len(result), 0) self.assertIsNot(result, ts)
def test_shift(self): shifted = self.rng.shift(5) assert shifted[0] == self.rng[5] assert shifted.freq == self.rng.freq shifted = self.rng.shift(-5) assert shifted[5] == self.rng[0] assert shifted.freq == self.rng.freq shifted = self.rng.shift(0) assert shifted[0] == self.rng[0] assert shifted.freq == self.rng.freq with warnings.catch_warnings(record=True): warnings.simplefilter("ignore", pd.errors.PerformanceWarning) rng = date_range(START, END, freq=BMonthEnd()) shifted = rng.shift(1, freq=CDay()) assert shifted[0] == rng[0] + CDay()
def test_shift(self): shifted = self.rng.shift(5) assert shifted[0] == self.rng[5] assert shifted.offset == self.rng.offset shifted = self.rng.shift(-5) assert shifted[5] == self.rng[0] assert shifted.offset == self.rng.offset shifted = self.rng.shift(0) assert shifted[0] == self.rng[0] assert shifted.offset == self.rng.offset # PerformanceWarning with warnings.catch_warnings(record=True): rng = date_range(START, END, freq=BMonthEnd()) shifted = rng.shift(1, freq=CDay()) assert shifted[0] == rng[0] + CDay()
def get_last_trading_day_of_month_for_exchange(year, month, exchange): " eg. 2013, 04, CME" if type(month) == str: if month.startswith('0'): month = month.lstrip('0') # covers cases for str/int input where year is provided only as XX or YY if type(year) == int or type(year) is float: if len(str(year)) == 2: if year >= 90: year = year + 1900 else: year = year + 2000 else: year = year else: assert type(year) == str if len(year) == 2: if int(year) >= 90: year = '19' + year else: year = '20' + year else: year = year trading_calendar = get_calendar(exchange) try: last_trading_day = trading_calendar.closes[ trading_calendar.closes.apply(lambda x: (x.year == float( year) and x.month == float(month)))].iloc[-1].date() # todo: corner case where desired month, year combination is too far in the future! except: year = str(year) month = str(month) last_trading_day = BMonthEnd().rollback(year + '-' + month).date() print('trading day is too far into the future for CME calendar') return last_trading_day
_offset_map = { '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),
def test_offsets_compare_equal(self): # root cause of #456 offset1 = BMonthEnd() offset2 = BMonthEnd() assert not offset1 != offset2
def test_normalize(self): dt = datetime(2007, 1, 1, 3) result = dt + BMonthEnd(normalize=True) expected = dt.replace(hour=0) + BMonthEnd() assert result == expected
class TestBMonthEnd(Base): _offset = BMonthEnd def test_normalize(self): dt = datetime(2007, 1, 1, 3) result = dt + BMonthEnd(normalize=True) expected = dt.replace(hour=0) + BMonthEnd() assert result == expected def test_offsets_compare_equal(self): # root cause of #456 offset1 = BMonthEnd() offset2 = BMonthEnd() assert not offset1 != offset2 offset_cases = [] offset_cases.append((BMonthEnd(), { datetime(2008, 1, 1): datetime(2008, 1, 31), datetime(2008, 1, 31): datetime(2008, 2, 29), datetime(2006, 12, 29): datetime(2007, 1, 31), datetime(2006, 12, 31): datetime(2007, 1, 31), datetime(2007, 1, 1): datetime(2007, 1, 31), datetime(2006, 12, 1): datetime(2006, 12, 29)})) offset_cases.append((BMonthEnd(0), { datetime(2008, 1, 1): datetime(2008, 1, 31), datetime(2008, 1, 31): datetime(2008, 1, 31), datetime(2006, 12, 29): datetime(2006, 12, 29), datetime(2006, 12, 31): datetime(2007, 1, 31), datetime(2007, 1, 1): datetime(2007, 1, 31)})) offset_cases.append((BMonthEnd(2), { datetime(2008, 1, 1): datetime(2008, 2, 29), datetime(2008, 1, 31): datetime(2008, 3, 31), datetime(2006, 12, 29): datetime(2007, 2, 28), datetime(2006, 12, 31): datetime(2007, 2, 28), datetime(2007, 1, 1): datetime(2007, 2, 28), datetime(2006, 11, 1): datetime(2006, 12, 29)})) offset_cases.append((BMonthEnd(-1), { datetime(2007, 1, 1): datetime(2006, 12, 29), datetime(2008, 6, 30): datetime(2008, 5, 30), datetime(2008, 12, 31): datetime(2008, 11, 28), datetime(2006, 12, 29): datetime(2006, 11, 30), datetime(2006, 12, 30): datetime(2006, 12, 29), datetime(2007, 1, 1): 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 = [(BMonthEnd(), datetime(2007, 12, 31), True), (BMonthEnd(), datetime(2008, 1, 1), False)] @pytest.mark.parametrize('case', on_offset_cases) def test_onOffset(self, case): offset, dt, expected = case assert_onOffset(offset, dt, expected)
#print (portfolio[key]) #print ("--------------------") # for date in portfolio[key].index: for date in portfolio[key].index: # #print("date",date) history = { 'name':'year', 'value':'', 'app_name':'Fintech', 'app_value':'', 'competitor_name':'S&P 500', 'competitor_value':'' } d = str(date.date().strftime('%b')) last_bday = BMonthEnd().rollforward(date) if (last_bday == date and d == 'Dec') or today == date: if round(portfolio[key][date], 2) < min_value: min_value = round(portfolio[key][date], 2) if round(benchmark[key][date], 2) < min_value: min_value = round(benchmark[key][date], 2) if round(portfolio[key][date], 2) > max_value: max_value = round(portfolio[key][date], 2) if round(benchmark[key][date], 2) > max_value: max_value = round(benchmark[key][date], 2) #if date.replace(day = monthrange(date.year, date.month)[1]) == date: day = str(date.date().strftime('%Y')) value = {'x_axis' : day, 'y_axis' : round(portfolio[key][date], 2)} value_benchmark = {'x_axis' : day, 'y_axis' : round(benchmark[key][date], 2)}
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