def data_filler(self): # In case we lack some data values # We can just fill them with interpolation # We do this at csv level self.dailyData start_date = self.dailyData.index[0].strftime("%Y-%m-%d") #strptime("%Y-%m-%d") end_date = dt.datetime(self.dailyData.index[-1].strftime("%Y-%m-%d")) print (start_date) # We have to get all working days between these 2 dates and check if # they exist, if they dont, we fill them # Create days of bussines busday_list = [] next_busday = np.busday_offset(start_date, 1, roll='forward') busday_list.append(next_busday) while (next_busday < end_date): # While we havent finished # print next_busday, end_date next_busday = np.busday_offset(next_busday, 1, roll='forward') busday_list.append(next_busday) Ndays_list = len(busday_list) # Number of days that there should be Ndays_DDBB = len(self.dailyData.index.tolist()) print (Ndays_list, Ndays_DDBB) ## TODO # for i in range (Ndays_list): # print "e" print (start_date, end_date)
def workday(self, d, offset=0): """Mimics the workday function in Excel""" d = self._simple_cast(d) if self.adj is None and isinstance(offset, int): if offset >= 0: adj = 'preceding' else: adj = 'following' nd = busday_offset(d, offsets=offset, busdaycal=self.buscore, roll=adj) elif self.adj is None and (isinstance(offset, ndarray) or isinstance(offset, Series)): if all(offset >= 0): adj = 'preceding' elif all(offset < 0): adj = 'following' else: raise NotImplementedError('If offset is an array like ' 'structure, then all values must ' 'have the same sign') nd = busday_offset(d, offsets=offset, busdaycal=self.buscore, roll=adj) else: nd = busday_offset(d, offsets=offset, busdaycal=self.buscore, roll=self.adj) return to_datetime(nd)
def data_filler(self): # In case we lack some data values # We can just fill them with interpolation # We do this at csv level self.dailyData start_date = self.dailyData.index[0].strftime( "%Y-%m-%d") #strptime("%Y-%m-%d") end_date = dt.datetime(self.dailyData.index[-1].strftime("%Y-%m-%d")) print start_date # We have to get all working days between these 2 dates and check if # they exist, if they dont, we fill them # Create days of bussines busday_list = [] next_busday = np.busday_offset(start_date, 1, roll='forward') busday_list.append(next_busday) while (next_busday < end_date): # While we havent finished # print next_busday, end_date next_busday = np.busday_offset(next_busday, 1, roll='forward') busday_list.append(next_busday) Ndays_list = len(busday_list) # Number of days that there should be Ndays_DDBB = len(self.dailyData.index.tolist()) print Ndays_list, Ndays_DDBB ## TODO # for i in range (Ndays_list): # print "e" print start_date, end_date
def movedatebymonth(your_date, no_of_month=1, business_day='No Adjustment', holidays=[]): bdc = np.busdaycalendar(weekmask='1111100', holidays=holidays) start_m = dt64(your_date, 'M') day = _datediff(dt64(start_m, 'D'), dt64(your_date, 'D') ) end_m = nb_movedatebymonth(start_m, td64(no_of_month,'M')) next_m = end_m + 1 days_in_end_m = _datediff(dt64(end_m, 'D'), dt64(next_m, 'D')) if days_in_end_m > day: enddate = dt64(end_m, 'D') + day else: enddate = dt64(end_m, 'D') + days_in_end_m - 1 if business_day == 'No Adjustment' or business_day is None: return enddate elif business_day == 'Following': return np.busday_offset(enddate, 0, roll='forward', busdaycal=bdc) elif business_day == 'Preceeding': return np.busday_offset(enddate, 0, roll='backward', busdaycal=bdc) elif business_day == 'Modified Following': new_date = np.busday_offset(enddate, 0, roll='forward', busdaycal=bdc) if dt64(new_date, 'M') > end_m: return np.busday_offset(enddate, 0, roll='backward', busdaycal=bdc) else: return new_date
def expiry(dt): sat_fri_date = np.datetime64('2015-02') exp = np.busday_offset(dt, 2, roll='forward', weekmask='Fri') if np.datetime64(dt) < sat_fri_date: exp = np.busday_offset(exp, 1, weekmask='1111111') else: exp = move_busdays(str(exp), 0) return exp
def atteindre(self, isin): # path is the filepath to a text file if os.path.exists(XLSX_PATH): self._path = os.path.join(XLSX_PATH, isin + '.xlsx') self._panel = pd.read_excel(self._path, index_col='Date', usecols=[ 'High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close', 'Date' ]) if not self._panel.empty: try: self._last_day = self._panel.iloc[-1:].index[0].split( ' ')[0] self._first_day = self._panel.iloc[0:].index[0].split( ' ')[0] self._next_day = np.busday_offset(np.datetime64( self._last_day), offsets=1, roll='forward') a_jour = pd.to_datetime(str( self._last_day)) == pd.to_datetime(DATE_DU_JOUR) le_jour_avant = pd.to_datetime(str( self._next_day)) < pd.to_datetime(DATE_DU_JOUR) avant_l_heure_de_fin_seance_du_jour = pd.to_datetime( DATE_DU_JOUR + datetime.now().time().strftime(' %H:%M:%S') ) < pd.to_datetime(DATE_DU_JOUR + ' 17:00:00') self._doit_etre_maj = (le_jour_avant) | ( (not le_jour_avant) & (not avant_l_heure_de_fin_seance_du_jour) & (not a_jour)) except: self._last_day = self._panel.iloc[-1:].index[0].date( ).strftime('%Y-%m-%d') self._first_day = self._panel.iloc[0:].index[0] self._next_day = np.busday_offset(np.datetime64( self._last_day), offsets=1, roll='forward') a_jour = pd.to_datetime(str( self._last_day)) == pd.to_datetime(DATE_DU_JOUR) le_jour_avant = pd.to_datetime(str( self._next_day)) < pd.to_datetime(DATE_DU_JOUR) avant_l_heure_de_fin_seance_du_jour = pd.to_datetime( DATE_DU_JOUR + datetime.now().time().strftime(' %H:%M:%S') ) < pd.to_datetime(DATE_DU_JOUR + ' 17:00:00') self._doit_etre_maj = (le_jour_avant) | ( (not le_jour_avant) & (not avant_l_heure_de_fin_seance_du_jour) & (not a_jour)) print('prochaine MAJ', self._next_day) else: raise FileNotFoundError('Le fichier ', isin, 'est introuvable')
def biz_days_offset(date, count): date1 = np.busday_offset(date, 0, roll='forward', holidays=US_HOLIDAY_LIST) if not np.is_busday(date, holidays=US_HOLIDAY_LIST): print('\nWARNING: the date "' + str(date) + '" is not a business day; ' \ + 'using the first business day immediately following that date,' \ + 'namely, "' + str(date1) + '"\n') biz_date = np.busday_offset(date1, count, holidays=US_HOLIDAY_LIST) # print('inside utils_general we have value: ' + str(biz_date)) return str(biz_date)
async def add_working_days(start_date: date, increment: int): end_date_without_holidays = busday_offset(start_date, increment, roll="forward").item() co_holidays = holidays.CO() holidays_range = co_holidays[start_date:end_date_without_holidays] end_date = busday_offset(start_date, increment, holidays=holidays_range, roll="forward") return end_date.item()
def get_plot_prediction(symbol): predictions = get_stock_prediction(symbol) if (len(predictions) > 0): # temp_df = stock_market_live_data[stock_market_live_data['symbol']==symbol] temp_df = GetLiveStockData(symbol, size=50) actuals = list(temp_df.tail(1).values[0])[0:ROLLING_PERIOD] # actuals = list(temp_df[FEATURES].values[0]) # transform log price to price of past data actuals = list(np.exp(actuals)) days_before = temp_df['last_market_date'].values[-1] days_before_list = [] for d in range(ROLLING_PERIOD): days_before_list.append( str( np.busday_offset(np.datetime64(days_before, 'D'), -d, roll='backward'))) days_before_list.sort() fig, ax = plt.subplots(figsize=(9, 3)) plt.plot(days_before_list, actuals, color='teal', linewidth=2.5) for d in range(1, PREDICT_OUT_PERIOD + 1): days_before_list.append( str( np.busday_offset(np.datetime64(days_before, 'D'), d, roll='forward'))) actuals.append(np.exp(predictions[-1])) plt.suptitle('Forecast for ' + str(temp_df['prediction_date'].values[-1])[0:10] + ' $' + str(np.round(np.exp(predictions[-1]), 2))) ax.plot(days_before_list, actuals, color='teal', linestyle='dashed') ax.grid() plt.xticks(days_before_list, days_before_list, fontsize=7) # ax.set_xticklabels(days_before_list, rotation = 35, ha="right") fig.autofmt_xdate() img = io.BytesIO() plt.savefig(img, format='png') img.seek(0) plot_bit_to_text = base64.b64encode(img.getvalue()).decode() chart_plot = Markup( '<img style="padding:0px; width: 80%; height: 500px" src="data:image/png;base64,{}">' .format(plot_bit_to_text)) return (chart_plot)
def get_interest_rate(self): """ If the interest rate on dates t_0 and t_1 are f(t_0) and f(t_1), respectively. The linear interpolation of interest rate on date t is: f(t) = f(t_0) * ((t_1 - t)/(t_1 - t_0)) + f(t_1) * ((t - t_0)/(t_1 - t_0)) """ rate_keys = list(self.rates_dict.keys()) # For exp dates that expire within 1 month (a.k.a. no lower bound) rate_keys.append(0) t0 = np.max([n for n in rate_keys if n <= self.years_to_exp]) t1 = np.min([n for n in rate_keys if n > self.years_to_exp]) if pd.isna([t0, t1]).any(): raise Exception(f"Unable to interpolate interest rate! Lower bound: {t0} Upper bound: {t1}") # Housekeeping interest_rate = 0 for t in [t0, t1]: if t != 0: # Interest rate DataFrame of time period chosen df = self.rates_dict[t] dates = list(df["date"]) # If unable to find rate for data date, interpolate if self.date not in dates: # Get dates before and after data date date_0 = self.date date_1 = self.date while date_0 not in dates: date_0 = pd.to_datetime(np.busday_offset(date_0, -1)).date() while date_1 not in dates: date_1 = pd.to_datetime(np.busday_offset(date_1, 1)).date() rate_t = (float(df[df["date"] == date_0]["continuous rate"]) + float(df[df["date"] == date_1]["continuous rate"])) / 2 else: rate_t = float(df[df["date"] == self.date]["continuous rate"]) # If lower bound is 0 else: rate_t = 0 # Add component contribution to total if t == t0: interest_rate = interest_rate + rate_t * ((t1 - self.years_to_exp) / (t1 - t0)) elif t == t1: interest_rate = interest_rate + rate_t * ((self.years_to_exp - t0) / (t1 - t0)) return interest_rate
def get_plot_prediction(symbol): predictions = get_stock_prediction(symbol) if (len(predictions) > 0): temp_df = stock_market_live_data[stock_market_live_data['symbol'] == symbol] actuals = list(temp_df[FEATURES].values[0]) # transform log price to price of past data actuals = list(np.exp(actuals)) days_before = temp_df['last_market_date'].values[-1] days_before_list = [] for d in range(ROLLING_PERIOD): days_before_list.append( (np.busday_offset(np.datetime64(days_before, 'D'), -d, roll='backward'))) days_before_list = sorted(days_before_list) fig, ax = plt.subplots(figsize=(5, 5)) ax.plot(days_before_list, actuals, color='green', linewidth=4) for d in range(1, PREDICT_OUT_PERIOD + 1): days_before_list.append( (np.busday_offset(np.datetime64(days_before, 'D'), d, roll='forward'))) actuals.append(np.exp(predictions[0])) days_before_list = sorted(days_before_list) plt.suptitle('Forecast for ' + str(temp_df['date'].values[-1])[0:10] + ': $' + str(np.round(np.exp(predictions[0]), 2))) ax.plot(days_before_list, actuals, color='blue', linestyle='dashed') ax.grid() plt.xticks(days_before_list, days_before_list, fontsize=7) ax.set_xticklabels(days_before_list, rotation=35, ha="right") img = io.BytesIO() plt.savefig(img, format='png') img.seek(0) plot_bit_to_text = base64.b64encode(img.getvalue()).decode() chart_plot = Markup( '<img style="padding:1px; border:1px solid #021a40; width: 300px; height: 400px" src="data:image/png;base64,{}">' .format(plot_bit_to_text)) return (chart_plot)
def adjustdates(months, dates, business_day, holidays=[]): bdc = np.busdaycalendar(weekmask='1111100', holidays=holidays) datalen = len(dates) newdates = [] for i in range(datalen): month = months[i] dmonth = dt64(dates[i], 'M') if dmonth == month: if business_day == 'Following': dates[i] = np.busday_offset(dates[i], 0, roll='forward', busdaycal=bdc) elif business_day == 'Preceeding': dates[i] = np.busday_offset(dates[i], 0, roll='backward', busdaycal=bdc) elif business_day == 'Modified Following': new_date = np.busday_offset(dates[i], 0, roll='forward', busdaycal=bdc) if dt64(new_date, 'M') > month: dates[i] = np.busday_offset(dates[i], 0, roll='backward', busdaycal=bdc) elif dmonth > month: #print(month,dmonth) nextmonth = month + td64(1, 'M') nextmonthdate = dt64(nextmonth, 'D') days = _datediff(dt64(month, 'D'), nextmonthdate) dates[i] = dt64(month, 'D') + days - 1 if business_day == 'Following': dates[i] = np.busday_offset(dates[i], 0, roll='forward', busdaycal=bdc) elif business_day == 'Preceeding': dates[i] = np.busday_offset(dates[i], 0, roll='backward', busdaycal=bdc) elif business_day == 'Modified Following': new_date = np.busday_offset(dates[i], 0, roll='forward', busdaycal=bdc) if dt64(new_date, 'M') > month: dates[i] = np.busday_offset(dates[i], 0, roll='backward', busdaycal=bdc)
def tenor_to_maturity(start_date, tenors, convention='Actual/365 Fixed', business_day='No Adjustment', holidays=[]): bdc = np.busdaycalendar(weekmask='1111100', holidays=holidays) if isinstance(tenors, list): result = [] for tenor in tenors: mat_date = None if 'w' in tenor or 'W' in tenor: tenor_len = [float(number) for number in re.findall(r'-?\d+\.?\d*', tenor)] if len(tenor_len) == 0: mat_date = None result.append(mat_date) else: number = tenor_len[0] number = np.int32(number) mat_date = np.datetime64(start_date, 'D') + 7 * number mat_date = np.busday_offset(mat_date, 0, roll='forward', busdaycal=bdc) result.append(mat_date) elif 'm' in tenor or 'M' in tenor: mat_date = mat_tenor_by_month(start_date, tenor, convention=convention, business_day=business_day, holidays=holidays) result.append(mat_date) elif 'y' in tenor or 'Y' in tenor: mat_date = mat_tenor_by_month(start_date, tenor, convention=convention, business_day=business_day, holidays=holidays, multiplier=12) result.append(mat_date) elif tenor == 'O/N': mat_date = np.busday_offset(start_date, 1, roll='forward', busdaycal=bdc) result.append(mat_date) return result else: result = tenor_to_maturity(start_date, [tenors], convention=convention, business_day=business_day, holidays=holidays) return result[0]
def gen_path(symbol, start, end=None, vol=0, T=1, N=800, n=100): print('Number of time steps: {}'.format(N)) print('Number of simulations: {}'.format(n)) S, beta, _ = soup.get_quote(symbol) rf = rate.get_rate() if vol == 0: start = np.busday_offset(td, -252 * 2, roll="modifiedpreceding").astype(datetime) print("volatility: std of the stock returns for the past two years") start = (start.year, start.month, start.day) stock_hist, returns, std = gq.get_quotes(symbol, start_date=start, end_date=None) print('daily std: {}'.format(std)) std = std * np.sqrt(252) mean_price, t, results = wiener.get_path(S0=S, vol=std, rf=rf, T=T, N=N, n=n) plt.title('Average path') plt.plot(t, mean_price[1:]) plt.show() return mean_price, t, rf, std, results
def _next_face_to_face_date(self) -> Optional[date]: """This method returns the next face-to-face contact date. It returns None if no future face-to-face contact is required.""" # TODO(#5768): Eventually delete or move this method to our calculate pipeline. if self.etl_client.most_recent_face_to_face_date is None: return np.busday_offset( self.etl_client.supervision_start_date, NEW_SUPERVISION_CONTACT_DEADLINE_BUSINESS_DAYS, roll="forward", ).astype(date) case_type = StateSupervisionCaseType(self.etl_client.case_type) supervision_level = StateSupervisionLevel( self.etl_client.supervision_level) if (case_type not in SUPERVISION_CONTACT_FREQUENCY_REQUIREMENTS or supervision_level not in SUPERVISION_CONTACT_FREQUENCY_REQUIREMENTS[case_type]): logging.warning( "Could not find requirements for case type %s, supervision level %s", self.etl_client.case_type, self.etl_client.supervision_level, ) return None face_to_face_requirements = SUPERVISION_CONTACT_FREQUENCY_REQUIREMENTS[ case_type][supervision_level] if face_to_face_requirements[0] == 0: return None return self.etl_client.most_recent_face_to_face_date + timedelta(days=( face_to_face_requirements[1] // face_to_face_requirements[0]))
def _compute_rolled_dates_table(self, roll_convention): """Computes and caches rolled dates table.""" already_computed = self._table_cache.rolled_dates.get( roll_convention, None) if already_computed is not None: return already_computed roll_convention_np = _to_np_roll_convention(roll_convention) holidays_arg = self._holidays_np if holidays_arg is None: holidays_arg = [] # np.busday_offset doesn't accept None adjusted_np = np.busday_offset(dates=self._dates_np, offsets=0, roll=roll_convention_np, weekmask=1 - self._weekend_mask, holidays=holidays_arg) rolled_date_table = adjusted_np.astype(np.int32) + _ORDINAL_OF_1_1_1970 # To make tensor caching safe, lift the ops out of the current scope using # tf.init_scope(). This allows e.g. to cache these tensors in one # tf.function and reuse them in another tf.function. with tf.init_scope(): rolled_date_table = tf.convert_to_tensor(rolled_date_table, name="rolled_date_table") self._table_cache.rolled_dates[roll_convention] = rolled_date_table return rolled_date_table
def _apply_business_days_logic(self, holidays: List[date], offset: int = None, roll: str = 'preceding'): if offset is not None: offset_to_use = offset else: offset_to_use = self.number if self.number else 0 return to_datetime(busday_offset(self.result, offset_to_use, roll, holidays=holidays, weekmask=self.week_mask)).date()
def apply(self, other): if isinstance(other, datetime): dtype = type(other) elif isinstance(other, np.datetime64): dtype = other.dtype elif isinstance(other, (timedelta, Tick)): return BDay(self.n, offset=self.offset + other, normalize=self.normalize) else: raise TypeError('Only know how to combine trading day with ' 'datetime, datetime64 or timedelta!') dt64 = self._to_dt64(other) day64 = dt64.astype('datetime64[D]') time = dt64 - day64 if self.n<=0: roll = 'forward' else: roll = 'backward' result = np.busday_offset(day64, self.n, roll=roll, busdaycal=self.busdaycalendar) if not self.normalize: result = result + time result = result.astype(dtype) if self.offset: result = result + self.offset return result
def apply(self, other): if self.n <= 0: roll = "forward" else: roll = "backward" if isinstance(other, datetime): date_in = other np_dt = np.datetime64(date_in.date()) np_incr_dt = np.busday_offset(np_dt, self.n, roll=roll, busdaycal=self.calendar) dt_date = np_incr_dt.astype(datetime) result = datetime.combine(dt_date, date_in.time()) if self.offset: result = result + self.offset return result elif isinstance(other, (timedelta, Tick)): return BDay(self.n, offset=self.offset + other, normalize=self.normalize) else: raise ApplyTypeError("Only know how to combine trading day with " "datetime, datetime64 or timedelta.")
def GetMonday(firstDay, way='forward'): firstEntry = firstDay.astype('M8[D]') beforeMonday = np.busday_offset(firstEntry, 0, way, [1,0,0,0,0,0,0]) if abs(firstEntry-beforeMonday) == np.timedelta64(7, 'D'): return firstEntry.astype('M8[s]') else: return beforeMonday.astype('M8[s]')
def PCA_analysis(dataframe_return, days_diff): """ This function receives a pandas dataframe that contains the returns for all the tickers and perform the PCA analysis on the return data from (days_diff) back to one day before. It returns the explained ratio and singular values stored as a dictionary with time as index """ # Initialize the time_index that performs PCA analysis time_index = [] # Initialize the dictionary that contains the explained variance ratio and # singular values of the PCA ETF_integration = dict() # obtain a four weeks time slots for time in dataframe_return.index: # obtain the last time_index last_time = pd.Timestamp( np.busday_offset(time, -days_diff, roll='forward')) # make sure the last time index is not out of the range of the data if last_time >= dataframe_return.index[0]: # retrieve the index between these two time points index_between = (dataframe_return.index >= last_time) & (dataframe_return.index <= time) # store the data in between tmp_data = dataframe_return[index_between] # Now we perform the PCA analysis pca = PCA() pca.fit(tmp_data) # obtain the explained ratio and singular values ratio, singularValue = pca.explained_variance_ratio_, pca.singular_values_ time_index.append(time) ETF_integration[time] = (ratio, singularValue) return time_index, ETF_integration
def select_option(self, Max_list, Number_of_holidays): delta = [] result = [] for j in range(0, len(Max_list)): delta.append(Max_list[j][0].weekday()) for i in range(0, len(delta)): #print("Option :", i + 1) option_result = [] for j in range(i, delta[i] + 1): d = {} start_date = Max_list[i][0] - datetime.timedelta(days=j) End_date = np.busday_offset(start_date, Number_of_holidays + Max_list[i][1]) start_date64 = np.datetime64(start_date) if start_date.weekday() == 0: d["Start Date"] = str(start_date - datetime.timedelta(days=2)) d["End Date"] = str(End_date) d["Total days"] = str((End_date - start_date64) + 2) else: d["Start Date"] = str(start_date) d["End Date"] = str(End_date) d["Total days"] = str(End_date - start_date64) option_result.append(d) result.append(option_result) return result
def bday_gen(current_date=datetime(2017, 1, 1, 9)): while True: current_date = np.busday_offset(current_date, offsets=1, roll='forward') yield pd.Timestamp(current_date).to_pydatetime().date()
def get_datetime_by_days_since_start(self, class_days, add_holidays=False): """ The date `class days` from the start of the semester Arguments: class_days {int} -- number of days since start Keyword Arguments: add_holidays {bool} -- [description] (default: {False}) Returns: {datetime} -- [description] """ excluded_days = self.excluded_days() # The next day of class excluding holidays/weekends, -1 because first day counts as 1, not zero. d = numpy.busday_offset(self.first_day, class_days - 1, roll='forward', holidays=excluded_days).astype(date) # Might want to include the holidays (if class day is Friday, then work done on weekend/holidays won't show up # till Monday. For chart, want to include those days # if (add_holidays): # next_date = numpy.busday_offset(self.first_day, class_days + 1, roll='backward', holidays=excluded_days) # # next_date = workday(self.first_day, class_days + 1, excluded_days) # num_holidays_to_add = next_date - d - timedelta(days=1) # If more than one day difference # d += num_holidays_to_add # convert from date to datetime dt = datetime.combine(d, datetime.max.time()) # make timezone aware return timezone.make_aware(dt, timezone.get_default_timezone())
def get_first_monday(self, year): date = np.busday_offset("%s-05" % year, 0, roll="forward", holidays=["%s-05-01" % year], weekmask="Mon") return date.tolist().strftime("%d/%m/%Y")
def get_first_monday(self, year): start = year + '-05' days = np.busday_offset(start, 0, roll='forward', weekmask='Mon') days = days.astype(datetime) days_format = days.strftime('%d/%m/%Y') return days_format
def get_current_data(self, gm=0): rf = rate.get_rate() start = np.busday_offset(td, -252 * self.std_horiz, roll="modifiedpreceding").astype(datetime) start = (start.year, start.month, start.day) stock_hist, returns, std = quandl.get_quotes(self.symbol, start_date=start, end_date=None) std = std * np.sqrt(252) if gm == 1: x = (pd.Series(returns) - pd.Series(returns).mean()) w = 1 / (1 + x**2)**2 returns = returns * w std = np.std(returns) * np.sqrt(252) print("Summary of past returns for " + self.symbol) print(pd.Series(returns).describe()) print('std: {}'.format(std)) print('stock price: {}'.format(stock_hist[-1])) self.std = std self.S0 = stock_hist[-1] #self.beta = beta self.rf = rf pass
def get_fwd_rates(d1, d2, fwd_curve, cal, roll_conv): # Get historical fixings historical_dates = d1[d1 <= fwd_curve.curve_date] historical_fixings = [] historical_fixing_dates = [] for i,d in enumerate(historical_dates): if d in fwd_curve.historical_fixings.keys(): historical_fixings.append(fwd_curve.historical_fixings[d]/100.0) historical_fixing_dates.append(d) else: j = 1 while j < 10: d = DatetimeIndex([d]) d = pd.Timestamp(np.busday_offset(dates=d.astype(str), offsets=j, roll=roll_conv, busdaycal=cal)[0]) if d in fwd_curve.historical_fixings.keys(): historical_fixings.append(fwd_curve.historical_fixings[d]/100.0) historical_fixing_dates.append(d) break j += 1 # Calculate future fixings d2 = d2[d1 > fwd_curve.curve_date] d1 = d1[d1 > fwd_curve.curve_date] t = pd.Series(sch.year_fraction(d1,d2,fwd_curve.day_count_basis)) future_fixings = list((1 / t) * (fwd_curve.get_dcf(d1) / fwd_curve.get_dcf(d2) - 1)) return pd.Series(historical_fixing_dates + d1.to_list()), pd.Series(historical_fixings + future_fixings)
def get_next_face_to_face_date(client: ETLClient) -> Optional[date]: """Calculates the next face-to-face contact date. It returns None if no future face-to-face contact is required.""" # TODO(#5769): Eventually move this calculation to our calculate pipeline. # In the meantime, we're hard-coding the relation to US_ID as a quick stop gap if client.state_code != "US_ID": raise CaseTriageInvalidStateException(client.state_code) case_type = StateSupervisionCaseType(client.case_type) supervision_level = StateSupervisionLevel(client.supervision_level) if (case_type not in SUPERVISION_CONTACT_FREQUENCY_REQUIREMENTS or supervision_level not in SUPERVISION_CONTACT_FREQUENCY_REQUIREMENTS[case_type]): logging.warning( "Could not find requirements for case type %s, supervision level %s", client.case_type, client.supervision_level, ) return None if client.most_recent_face_to_face_date is None: return np.busday_offset( client.supervision_start_date, NEW_SUPERVISION_CONTACT_DEADLINE_BUSINESS_DAYS, roll="forward", ).astype(date) face_to_face_requirements = SUPERVISION_CONTACT_FREQUENCY_REQUIREMENTS[ case_type][supervision_level] if face_to_face_requirements[0] == 0: return None return client.most_recent_face_to_face_date + timedelta( days=(face_to_face_requirements[1] // face_to_face_requirements[0]))
def business_day_offset(dates: DateOrDates, offsets: Union[int, Iterable[int]], roll: str = 'raise', calendars: Union[str, Tuple[str, ...]] = (), week_mask: Optional[str] = None) -> DateOrDates: """ Apply offsets to the dates and move to the nearest business date :param dates: The input date or dates :param offsets: The number of days by which to adjust the dates :param roll: Which direction to roll, in order to get to the nearest business date :param calendars: Calendars to use for holidays :param week_mask: Which days are considered weekends (defaults to Saturday and Sunday) :return: A date (if dates is a single date) or tuple of dates, adjusted by the offsets **Examples** >>> import datetime as dt >>> prev_bus_date = business_day_offset(dt.date.today(), -1, roll='forward') """ calendar = GsCalendar.get(calendars) res = np.busday_offset( dates, offsets, roll, busdaycal=calendar.business_day_calendar(week_mask)).astype(dt.date) return tuple(res) if isinstance(res, np.ndarray) else res
def offset(self, dates, offsets=0, end=None, roll='preceding'): """Return valid business date with optional offset or roll treatment Parameters ---------- dates: int or array_like of int Input dates (YYYYMMDD int format) offsets: int, optional Number of business days to offset end: int, optional End of offset range (default None to only return single offset) roll: {'raise', 'forward', 'following', 'backward', 'preceding'} How to treat dates that are not a valid day (default 'preceding') """ if end: # return all dates within window [offsets, end] around {date} if is_list_like(dates): return self._map(self.offset, dates, offsets, end, roll=roll) return np.array([ self.offset(dates, offsets=i, roll=roll) for i in np.arange(offsets, end + 1) ]).T try: return int( pd.to_datetime( np.busday_offset( dates=np.array([self(dates)], dtype='datetime64[D]'), offsets=offsets, roll=roll, busdaycal=self.busdaycalendar_)).strftime('%Y%m%d')[0]) except: #return [self.offset(d, offsets, roll=roll) for d in dates] return self._map(self.offset, dates, offsets, end=end, roll=roll)
def apply(self, other): if isinstance(other, datetime): dtype = type(other) elif isinstance(other, np.datetime64): dtype = other.dtype elif isinstance(other, (timedelta, Tick)): return BDay(self.n, offset=self.offset + other, normalize=self.normalize) else: raise TypeError("Only know how to combine trading day with " "datetime, datetime64 or timedelta!") dt64 = self._to_dt64(other) day64 = dt64.astype("datetime64[D]") time = dt64 - day64 if self.n <= 0: roll = "forward" else: roll = "backward" result = np.busday_offset(day64, self.n, roll=roll, busdaycal=self.busdaycalendar) if not self.normalize: result = result + time result = result.astype(dtype) if self.offset: result = result + self.offset return result
def getNBusDateFromNdays(ndays, skip=False): if ndays == 0: dtnow = '{:%Y-%m-%d}'.format(datetime.now()) dt = np.busday_offset(dtnow, 0, roll='backward') if not xnys.is_session(dt): return getNBusDateFromNdays(ndays + 1, skip) else: now = '{:%Y-%m-%d}'.format(datetime.now()) dtnow = np.busday_offset(now, 0, roll='backward') dt = np.busday_offset(dtnow, -ndays, roll='backward') if not xnys.is_session(dt): if skip: dt = str('SKIP') else: return getNBusDateFromNdays(ndays + 1, skip) #print('ndays {:d} bdate {}'.format(ndays,dt)) return str(dt)
def getLastTrDay(endD): from datetime import datetime from datetime import date import numpy as np import pandas as pds refDate = datetime.date(datetime.utcnow()) refHour = datetime.utcnow().hour lstTDR = endD if endD >= refDate: if np.is_busday(refDate): if refHour > 6: lstTDR = np.busday_offset(refDate, -1, roll='backward') else: lstTDR = np.busday_offset(endD, -2, roll='backward') else: if refHour > 6: lstTDR = np.busday_offset(refDate, 0, roll='backward') else: lstTDR = np.busday_offset(endD, -2, roll='backward') buff = pds.to_datetime(lstTDR) return buff
def next_business_day(day): ''' Roll the clock forward one business day. See the numpy example: >>> # First business day after a date ... np.busday_offset('2011-03-20', 1, roll='backward') numpy.datetime64('2011-03-21','D') ''' d = np.busday_offset(day, 1, roll='backward') return d
def test_getExpectedPricePut_ObservedList(self): petr4 = Stock('Petrobrás', 'PETR4') petr4.price = 8.9 petr4.volatility = 70.6 # For automatic test purposes we need to calculate the expiration date # from today to get the same result every time. businessDays = 19 expirationDate = busday_offset(datetime.date.today(), businessDays, 'backward', '1111100', blackscholes.holidays) petr4.observeOption('PETRR76', 7.6, expirationDate) expectedPrices = blackscholes.calcExpectedPrices(petr4, 14.25) self.assertEqual(0.17, expectedPrices['PETRR76'])
def test_getExpectedPriceCall_ObservedList(self): itub4 = Stock('Itaú', 'ITUB4') itub4.price = 30.08 itub4.volatility = 49.25 # For automatic test purposes we need to calculate the expiration date # from today to get the same result every time. businessDays = 19 expirationDate = busday_offset(datetime.date.today(), businessDays, 'backward', '1111100', blackscholes.holidays) itub4.observeOption('ITUBF8', 33.81, expirationDate) expectedPrices = blackscholes.calcExpectedPrices(itub4, 14.25) self.assertEqual(0.57, expectedPrices['ITUBF8'])
def util_nBday(effective, dstring, adjust): eff_date = np.array([effective], dtype='datetime64[D]') end_date = useful.date_offset(eff_date, dstring) end_date = np.busday_offset(end_date, [0], roll=useful.adj[adjust], busdaycal=useful.hcal['US']) end_date = end_date[0] end_month = np.datetime64(end_date, 'M') end_year = np.datetime64(end_date, 'Y') ndays = (end_date - end_month)/np.timedelta64(1, 'D') ndays = int(ndays+1) nmonths = (end_month - end_year)/np.timedelta64(1, 'M') nmonths = int(nmonths) + 1 nyears = (end_year - np.datetime64('0000'))/np.timedelta64(1, 'Y') nyears = int(nyears) final_date = datetime.date(nyears, nmonths, ndays) return final_date
import pandas_datareader.data as web import datetime import numpy as np start_date = datetime.datetime.today().strftime('%Y-%m-%d') start_date1=datetime.datetime.strptime("2016-06-06",'%Y-%m-%d') print start_date print np.busday_offset(start_date,0,roll='backward') #If weekend, find the last closest Friday print np.busday_offset(start_date1,0,roll='backward') #If weekday keeps being weekday a = web.DataReader("F","yahoo","2016-06-03") print type(a) print a print " " print a["Adj Close"][0]
def prev_business_day(day): ''' Roll the clock backward one business day. ''' return np.busday_offset(day, -1, roll='backward')
""" Mother's day ============ In the USA and Canada, Mother's Day is the second Sunday of May. Use NumPy's datetime64 data type and datetime64 utilities to compute the date of Mother's Day for the current year. Note: NumPy datetime64 values can be created from a string with the format YYYY-MM-DD HH:MM:SS.sss where everything after the year designation is optional. Bonus ~~~~~ Extract the current year programmatically using the datetime module from the standard library. """ from numpy import busday_offset, datetime64 from datetime import datetime year_str = "2014" # Bonus: # To automatically extract the current year: year_str = str(datetime.now().year) date_str = year_str + "-05" may = datetime64(date_str) second_sunday = busday_offset(may, 1, roll="forward", weekmask="Sun") print("Second sunday of May this year is {}".format(second_sunday))
#Code to query each entry one by one, get the price from Yahoo Finance API and then update FTPORTFOLIO with current price and does the delta calculation import sqlite3 import pandas_datareader.data as web import datetime import numpy as np import decimal #Since we have weekday data only, what this does is to give us the last closest week day volume # Stuff to update: Get non-trading days due to holidays start_date = datetime.datetime.today().strftime('%Y-%m-%d') start_date=np.busday_offset(start_date,0,roll='backward') print "Welcome to your Portfolio Rebalancing Calculator. Today's date is %s" %start_date #print start_date #print start_date - 1 cash=raw_input("Input total amount of USD that you will be investing this period if any : >>>") try: int(cash) print "Ok you used an integer good job!" except ValueError: try: float(cash) print "Floating values? What are you doing mate? Buying penny stocks?" except ValueError: print "Cash input must be numeric" #Get USD/CAD rate for the day try: er=web.DataReader("CAD=X",'yahoo',start_date)["Close"][0] except: print "Can't find the USD/CAD exchange rate of :" + start_date er=web.DataReader("CAD=X",'yahoo',start_date-1)["Close"][0]