def metrics_calculated(self,link,start_date,exp_end_date,act_end_date, gh): start = dt.date(int(start_date.split("/")[2]), int(start_date.split("/")[0]), int(start_date.split("/")[1])) exp_end = dt.date(int(exp_end_date.split("/")[2]), int(exp_end_date.split("/")[0]), int(exp_end_date.split("/")[1])) today = dt.date.today() project.estimate_percent_complete(link,gh) self.est_perc_compl = project.estimate_percent_complete(link, gh)[0] * 100 self.accuracy = project.estimate_percent_complete(link, gh)[1] * 100 if start <= today and self.act_end_date == "" and self.est_perc_compl != 0: days_elapsed = np.busday_count(start,today) est_total_days = days_elapsed * 100 / self.est_perc_compl est_end = project.estimate_end_date(start,est_total_days) self.est_end_date = est_end.strftime("%m/%d/%Y") self.est_days_remaining = np.busday_count(today,est_end) print "Estimated end date: %s" % self.est_end_date print "Estimated business days remaining: %i" % self.est_days_remaining print "Estimated project completion: %i%%" % self.est_perc_compl print "Accuracy of estimate: %i%%" % self.accuracy else: days_elapsed = 0 print "Business days elapsed to date: %i" % days_elapsed if today > exp_end: self.bus_days_behind = np.busday_count(exp_end,today) else: self.bus_days_behind = 0 print "Business days behind schedule: %i" % self.bus_days_behind
def drawdowns(equity_curve): i = np.argmax(np.maximum.accumulate(equity_curve.values) - equity_curve.values) # end of the period j = np.argmax(equity_curve.values[:i]) # start of period drawdown=abs(100.0*(equity_curve[i]-equity_curve[j])) DT=equity_curve.index.values start_dt=pd.to_datetime(str(DT[j])) MDD_start=start_dt.strftime ("%Y-%m-%d") end_dt=pd.to_datetime(str(DT[i])) MDD_end=end_dt.strftime ("%Y-%m-%d") NOW=pd.to_datetime(str(DT[-1])) NOW=NOW.strftime ("%Y-%m-%d") MDD_duration=np.busday_count(MDD_start, MDD_end) try: UW_dt=equity_curve[i:].loc[equity_curve[i:].values>=equity_curve[j]].index.values[0] UW_dt=pd.to_datetime(str(UW_dt)) UW_dt=UW_dt.strftime ("%Y-%m-%d") UW_duration=np.busday_count(MDD_end, UW_dt) except: UW_dt="0000-00-00" UW_duration=np.busday_count(MDD_end, NOW) return MDD_start, MDD_end, MDD_duration, drawdown, UW_dt, UW_duration
def metrics_actual(self,start_date,exp_end_date,act_end_date): start = dt.date(int(start_date.split("/")[2]), int(start_date.split("/")[0]), int(start_date.split("/")[1])) exp_end = dt.date(int(exp_end_date.split("/")[2]), int(exp_end_date.split("/")[0]), int(exp_end_date.split("/")[1])) act_end = dt.date(int(act_end_date.split("/")[2]), int(act_end_date.split("/")[0]), int(act_end_date.split("/")[1])) self.act_days_elapsed = np.busday_count(start,act_end) if act_end > exp_end: self.bus_days_behind = np.busday_count(exp_end,act_end) else: self.bus_days_behind = 0 print "Actual business days elapsed: %i" % self.act_days_elapsed print "Business days behind schedule: %i" % self.bus_days_behind
def count_working_hours(s): ''' Return the number of working hours between start and end dates assuming 8 hour working days Args: s : a Story object ''' start_date = s.started_date end_date = s.accepted_date if end_date is None or start_date is None: return None time_spent_day1 = dt.datetime(start_date.year, start_date.month, start_date.day, 18, 0, 0) - start_date time_spent_dayn = end_date - dt.datetime(end_date.year, end_date.month, end_date.day, 9, 0, 0) hours_between = np.busday_count(start_date, end_date) - 1 total_time = (time_spent_day1 + dt.timedelta(hours=int(hours_between*8)) + time_spent_dayn) return total_time.total_seconds() / 3600
def get_times(now, then): """Calculate the remaining time.""" total = then - now days = total.days weeks = total.days // 7 bus_days = np.busday_count(now, then) return weeks, days, bus_days, total
def calcExpectedPrices(stock, interest): prices = {} interest /= 100.0 histVolatility = stock.volatility / 100.0 for observedOption in stock.observedOptions: option = stock.observedOptions[observedOption] strike = option.strike expirationDate = option.expirationDate lifetime = numpy.busday_count(date.today(), expirationDate + timedelta64(1, 'D'), '1111100', holidays) lifetime /= 252.0 presentValue = strike * numpy.exp(-interest * lifetime) stHalf = histVolatility * numpy.power(lifetime, 0.5) d1 = (numpy.log(stock.price / strike) + (interest + histVolatility * histVolatility / 2) * lifetime) / \ stHalf d2 = d1 - stHalf delta = scipy.stats.norm.cdf(d1) # print delta norm_d2_PV = scipy.stats.norm.cdf(d2) * presentValue # print norm_d2_PV callExpectedPrice = round(delta * stock.price - norm_d2_PV, 2) if option.type == optionType.PUT: putExpectedPrice = round(callExpectedPrice + presentValue - stock.price, 2) prices[option.symbol] = putExpectedPrice else: prices[option.symbol] = callExpectedPrice return prices
def busday_count_mask_NaT(begindates, enddates, out=None): """ Simple of numpy.busday_count that returns `float` arrays rather than int arrays, and handles `NaT`s by returning `NaN`s where the inputs were `NaT`. Doesn't support custom weekdays or calendars, but probably should in the future. See Also -------- np.busday_count """ if out is None: out = empty(broadcast(begindates, enddates).shape, dtype=float) beginmask = begindates == NaTD endmask = enddates == NaTD out = busday_count( # Temporarily fill in non-NaT values. where(beginmask, _notNaT, begindates), where(endmask, _notNaT, enddates), out=out, ) # Fill in entries where either comparison was NaT with nan in the output. out[beginmask | endmask] = nan return out
def _compute_busday_offsets(announcement_dates): """ Compute expected business day offsets from a DataFrame of announcement dates. """ # Column-vector of dates on which factor `compute` will be called. raw_call_dates = announcement_dates.index.values.astype( 'datetime64[D]' )[:, None] # 2D array of dates containining expected nexg announcement. raw_announce_dates = ( announcement_dates.values.astype('datetime64[D]') ) # Set NaTs to 0 temporarily because busday_count doesn't support NaT. # We fill these entries with NaNs later. whereNaT = raw_announce_dates == NaTD raw_announce_dates[whereNaT] = make_datetime64D(0) # The abs call here makes it so that we can use this function to # compute offsets for both next and previous earnings (previous # earnings offsets come back negative). expected = abs(np.busday_count( raw_call_dates, raw_announce_dates ).astype(float)) expected[whereNaT] = np.nan return pd.DataFrame( data=expected, columns=announcement_dates.columns, index=announcement_dates.index, )
def getfuturebusdayttm(nowdate=None): if not nowdate: nowdate = dt.date.today() expire_date = getfutureexpiredate(nowdate) days = np.busday_count(nowdate, expire_date, holidays=holidays) # holidays = [] # print 'TTM (BD): ', days return days
def busdays(startdate, enddate): if pd.isnull(enddate) == True: delay = np.nan else: start = startdate.date() end = enddate.date() delay = np.busday_count(start, end) return delay
def gap_correction(ts): """ For adjusting volatility based on monthly or weekly data :param ts: ts :return: ts / sqrt(mean-gap in business days) """ meangap = np.mean([np.busday_count(enddates=ts.index[i], begindates=ts.index[i-1]) for i in range(1,ts.size)]) return ts / np.sqrt(meangap)
def workable_day(df): ''' count the number of workable days, exclude weekends, on-hold days, invited days ''' if (df['New Value'] == 'COMPLETE').any(): num_days = np.busday_count(begindates = df['Date Created'].iloc[0].date(), enddates = df[df['New Value'] == 'COMPLETE']['Date'].iloc[0].date()) elif (df['New Value'] == 'CANCELLED').any(): num_days = np.busday_count(begindates = df['Date Created'].iloc[0].date(), enddates = df[df['New Value'] == 'CANCELLED']['Date'].iloc[0].date()) return num_days else: num_days = np.busday_count(begindates = df['Date Created'].iloc[0].date(), enddates = date.today()) if (df['New Value'] == 'ON HOLD').any(): begin = df[df['New Value'] == 'ON HOLD']['Date'].iloc[0].date() try: bus_days = np.busday_count(begindates = begin, enddates = df[df['Old Value'] == 'ON HOLD']['Date'].iloc[0].date()) except IndexError: # sometime project can be changed back to on hold after complete bus_days = np.busday_count(begindates = begin, enddates = date.today()) finally: num_days = num_days - bus_days if (df['New Value'] == 'INVITED').any(): begin = df[df['New Value'] == 'INVITED']['Date'].iloc[0].date() try: bus_days = np.busday_count(begindates = begin, enddates = df[df['Old Value'] == 'INVITED']['Date'].iloc[0].date()) except IndexError: bus_days = np.busday_count(begindates = begin, enddates = date.today()) finally: num_days = num_days - bus_days return num_days
def main(): args = get_parser() today = datetime.date(args.y1, args.m1, args.d1) end_2016 = datetime.date(args.y2, args.m2, args.d2) end_day_included = datetime.date(args.y3, args.m3, args.d3) total = end_2016 - today A = total.days # print(A) businessdays = np.busday_count(today, end_day_included) # print(businessdays) weekend_count = A - businessdays print(weekend_count)
def dif_two_dates(date1, date2, max_diff): try: formatted_d1 = (str(date1)).split(' ')[0] formatted_d2 = (str(date2)).split(' ')[0] if formatted_d2 == 'None': formatted_d2 = (lambda x: strftime("%Y-%m-%d"))('None') formatted_d1 = month_name_to_number(formatted_d1) formatted_d2 = month_name_to_number(formatted_d2) difference = busday_count(formatted_d1, formatted_d2, weekmask="Sun Mon Tue Wed Thu") if difference > max_diff: return True else: return False except: pass
def diff_custom_calendar_timedeltas(start, end, freq): """ Compute the difference between two pd.Timedelta taking into consideration custom frequency, which is used to deal with custom calendars, such as a trading calendar Parameters ---------- start : pd.Timestamp end : pd.Timestamp freq : CustomBusinessDay (see infer_trading_calendar) freq : pd.DataOffset (CustomBusinessDay, Day or BDay) Returns ------- pd.Timedelta end - start """ if not isinstance(freq, (Day, BusinessDay, CustomBusinessDay)): raise ValueError("freq must be Day, BusinessDay or CustomBusinessDay") weekmask = getattr(freq, 'weekmask', None) holidays = getattr(freq, 'holidays', None) if weekmask is None and holidays is None: if isinstance(freq, Day): weekmask = 'Mon Tue Wed Thu Fri Sat Sun' holidays = [] elif isinstance(freq, BusinessDay): weekmask = 'Mon Tue Wed Thu Fri' holidays = [] if weekmask is not None and holidays is not None: # we prefer this method as it is faster actual_days = np.busday_count(np.array(start).astype('datetime64[D]'), np.array(end).astype('datetime64[D]'), weekmask, holidays) else: # default, it is slow actual_days = pd.date_range(start, end, freq=freq).shape[0] - 1 if not freq.onOffset(start): actual_days -= 1 timediff = end - start delta_days = timediff.components.days - actual_days return timediff - pd.Timedelta(days=delta_days)
def get_date_diff_hrs(from_date, to_date): """Returns difference of hours between two dates excluding weekends.""" weekdays = numpy.busday_count(from_date.date(), to_date.date()) from_date_end_time = datetime( year=from_date.year, month=from_date.month, day=from_date.day, hour=23, minute=59, second=59) to_date_start_time = datetime( year=to_date.year, month=to_date.month, day=to_date.day) from_day_seconds = (from_date_end_time - from_date).total_seconds() + 1 to_day_seconds = (to_date - to_date_start_time).total_seconds() total_seconds = from_day_seconds + to_day_seconds date_diff_hrs = (weekdays - 1) * 24 + total_seconds / 3600.0 return date_diff_hrs
def day_order(): date = request.args.get('date') month = request.args.get('month') year = request.args.get('year') day_order = 1 start = dt.date(2016,7,4) end = dt.date(int(year),int(month),int(date)) days = np.busday_count(start, end) day_order+=days for i in range(len(holidays)): if start<holidays[i]<end: day_order-=1 if day_order>5: day_order%=5 if day_order==0: day_order=5 return jsonify({str(end): day_order})
def busDaysQty(endDate, startDate): daysQty = np.busday_count( startDate.date(), endDate.date() ) return daysQty
def time_to_maturity(t0, T, y=252): t0 = pd.to_datetime(t0) T = pd.to_datetime(T) #datetime()은 날짜로 형식바꿈 return (np.busday_count(t0,T)/y) #평일 카운트 np.busday
def formula(individu, period, parameters): # * Tous les calculs sont faits sur le mois * # Les types de contrats gérés contrat_de_travail = individu('contrat_de_travail', period) TypesContratDeTravail = contrat_de_travail.possible_values # [ temps_plein # temps_partiel # forfait_heures_semaines # forfait_heures_mois # forfait_heures_annee # forfait_jours_annee ] contrat_de_travail_debut = individu('contrat_de_travail_debut', period) contrat_de_travail_fin = individu('contrat_de_travail_fin', period) # Volume des heures rémunérées à un forfait heures forfait_heures_remunerees_volume = individu('forfait_heures_remunerees_volume', period) # noqa F841 # Volume des heures rémunérées à forfait jours forfait_jours_remuneres_volume = individu('forfait_jours_remuneres_volume', period) heures_duree_collective_entreprise = individu('heures_duree_collective_entreprise', period) # Volume des heures rémunérées contractuellement (heures/mois, temps partiel) heures_remunerees_volume = individu('heures_remunerees_volume', period) # Volume des heures non rémunérées (convenance personnelle hors contrat/forfait) heures_non_remunerees_volume = individu('heures_non_remunerees_volume', period) # Décompte des jours en début et fin de contrat # http://www.gestiondelapaie.com/flux-paie/?1029-la-bonne-premiere-paye debut_mois = datetime64(period.start.offset('first-of', 'month')) fin_mois = datetime64(period.start.offset('last-of', 'month')) + timedelta64(1, 'D') # busday ignores the last day jours_ouvres_ce_mois = busday_count( debut_mois, fin_mois, weekmask='1111100' ) # jours travaillables sur l'intersection du contrat de travail et du mois en cours jours_ouvres_ce_mois_incomplet = busday_count( max_(contrat_de_travail_debut, debut_mois), min_(contrat_de_travail_fin, fin_mois), weekmask='1111100' ) duree_legale_mensuelle = 35 * 52 / 12 # ~151,67 heures_temps_plein = switch(heures_duree_collective_entreprise, {0: duree_legale_mensuelle, 1: heures_duree_collective_entreprise}) jours_absence = heures_non_remunerees_volume / 7 coefficient_proratisation_temps_partiel = heures_remunerees_volume / heures_temps_plein coefficient_proratisation_forfait_jours = forfait_jours_remuneres_volume / 218 # temps plein coefficient = switch( contrat_de_travail, { # temps plein TypesContratDeTravail.temps_plein: ( (jours_ouvres_ce_mois_incomplet - jours_absence) / jours_ouvres_ce_mois ), # temps partiel # (en l'absence du détail pour chaque jour de la semaine ou chaque semaine du mois) TypesContratDeTravail.temps_partiel: coefficient_proratisation_temps_partiel * ( (jours_ouvres_ce_mois_incomplet * coefficient_proratisation_temps_partiel - jours_absence) / (jours_ouvres_ce_mois * coefficient_proratisation_temps_partiel + 1e-16) ), TypesContratDeTravail.forfait_jours_annee: coefficient_proratisation_forfait_jours * ( (jours_ouvres_ce_mois_incomplet * coefficient_proratisation_forfait_jours - jours_absence) / (jours_ouvres_ce_mois * coefficient_proratisation_forfait_jours + 1e-16) ) } ) # Forfait en heures # coefficient = (contrat_de_travail >= 2) * (contrat_de_travail <= 3) * ( # forfait_heures_remunerees_volume / 45.7 * 52 / 12 # ) + return (jours_ouvres_ce_mois_incomplet > 0) * coefficient
def days(self): start = self.interval_start.strftime('%Y-%m-%d') end = (self.interval_end + timedelta(days=1)).strftime('%Y-%m-%d') return numpy.busday_count( start, end, holidays=self.holidays())
def zcb_value(t_hor, t_end, rd_type, x_thor, *, tau_x=None, facev=1, eta=0.013): """For details, see here. Parameters ---------- t_hor : date t_end : array, shape(k_,) rd_type : string, x_thor : array, shape(j_, d_) if d_>1 or (t_,) for d_=1 tau_x : array, optional, shape(d_,) facev : array, optional, shape(k_,) eta : scalar Returns ------- v : array, shape(j_, k_) if k_>1 or (t_,) for k_=1 """ j_ = x_thor.shape[0] k_ = t_end.shape[0] if isinstance(facev, int): facev = facev * np.ones(k_) # Step 1: Compute times to maturity at the horizon tau_hor = np.array( [np.busday_count(t_hor, t_end[i]) / 252 for i in range(k_)]) # Step 2: Compute the yield to maturity if rd_type == 'y': # Step 2a: Interpolate yields interp = sp.interpolate.interp1d(tau_x.flatten(), x_thor, axis=1, fill_value='extrapolate') y = interp(tau_hor) elif rd_type == 'sr': # Step 2b: Compute yields # interpolate shadow rates interp = sp.interpolate.interp1d(tau_x.flatten(), x_thor, axis=1, fill_value='extrapolate') # Transform shadow rates to yields y = shadowrates_ytm(interp(tau_hor), eta) elif rd_type == 'ns': # Step 2c: Compute yields y = np.zeros((j_, k_)) idx_nonzero = (tau_hor > 0) for j in range(j_): y[j, idx_nonzero] = nelson_siegel_yield(tau_hor[idx_nonzero], x_thor[j]) # Step 3: Compute zero coupon-bonds value v = facev * np.exp(-tau_hor * y) return np.squeeze(v)
def metrics_expected(self,start_date,exp_end_date): start = dt.date(int(start_date.split("/")[2]), int(start_date.split("/")[0]), int(start_date.split("/")[1])) exp_end = dt.date(int(exp_end_date.split("/")[2]), int(exp_end_date.split("/")[0]), int(exp_end_date.split("/")[1])) self.exp_days_elapsed = np.busday_count(start,exp_end) print "Expected business days elapsed: %i" % self.exp_days_elapsed
Buy_Signals = [] Sell_Signals = [] CloseBuy_Signals = [] CloseSell_Signals = [] st = "2015-01-01" end_dt = "2016-02-01" Ticker = "EUR_USD" tf1 = "D" tf2 = "M5" tf3 = "M15" # mc = pClose(Ticker, tf2, st, en) Portfolio = np.zeros((1,5000)) en = FindDateRange(st, 2) Account = 100 while np.busday_count(en, end_dt) > 20: # for p in range(0,15): PP = 0.0 R1 = [0,0,0,0,0] # R2 = [0,0,0,0,0] S1 = [0,0,0,0,0] # S2 = [0,0,0,0,0] Ticker = Sec[5] o = pOpen(Ticker, tf1, st, en) h = pHigh(Ticker, tf1, st, en) l = pLow(Ticker, tf1, st, en) c = pClose(Ticker, tf1, st, en) d = pDate(Ticker, tf1, st, en)
def preprocess_promo(folder, big_table, sql_table, target_value, dataset_name): """[Preprocess the saved dataset downloaded to the server for the promo model] Arguments: folder {[string]} -- [folder containing the dataset (Please put a "/" at the end of the name to indicate a folder)] big_table {[string]} -- [name of the dataset on the server] sql_table {[string]} -- [name of the corresponding table on the datalake] target_value {[string]} -- [name of the column to predict] Raises: Exception: [if there are missing features] Exception: [if there is duplicates in the features] Exception: [if the model cannot be trained properly] Returns: [type] -- [description] """ table_path = folder + big_table df_test = pd.read_csv(table_path) df = df_test df['current_dm_slot_type_code'].value_counts() try: print(len(df[df['ind_out_of_stock'] == 1])) print(len(df[df['ind_out_of_stock'] == 1]) / len(df)) except: print('NO OOS flag') len(df) # ### Load and preprocess sys.path.append(os.getcwd()) reload(utils_v2) df2, errors, liste = read_data(df, sql_table) df = df2 try: len(df.loc[df['active_flag'] == 1]) except: print('NO ACTIVE FLAG') try: print(len(df.loc[df['planned_bp_flag'] == 1])) len(df[df['planned_bp_flag'] == 0]) except: print('NO PLANNED BP FLAG') # + try: len(df.loc[df['bp_flag'] == 1]) except: print('NO BP FLAG') try: len(df.loc[(df['bp_flag'] == 0) | (df['planned_bp_flag'] == 0)]) except: print('NO BP FLAG or PLANNED BP FLAG') len(df.loc[:, 'full_item'].unique()) try: df['ind_out_of_stock_flag'].unique() except: print('NO ind_out_of_stock_flag FLAG') try: len(df.loc[df['assortment_active_flag'] == 1]) except: print('NO assortment_active_flag FLAG') # ### Need to drop lines with no dm end or start date infos # + df.current_dm_psp_start_date = pd.to_datetime(df.current_dm_psp_start_date) df.current_dm_psp_end_date = pd.to_datetime(df.current_dm_psp_end_date) df_droped = df.dropna( subset=['current_dm_psp_start_date', 'current_dm_psp_end_date'], how='any') # - df = df_droped df.loc[df.current_dm_psp_start_date.notnull( ), 'current_dm_busday'] = np.busday_count( df.current_dm_psp_start_date.dropna().values.astype('datetime64[D]'), df.current_dm_psp_end_date.dropna().values.astype('datetime64[D]')) # number weekend in current DM df['curr_psp_days'] = (df.current_dm_psp_end_date - df.current_dm_psp_start_date).dt.days df['current_dm_weekend_days'] = df.curr_psp_days - df.current_dm_busday # ## Adding uplift as feature df['uplift_value'] = df['4w_sales_4w_bef'] * df['uplift'] df['uplift_value'].describe() # ### Preparing data for xgboost identification = [ 'item_id', 'sub_id', # DELETED on 12/07 Sprint4: 95 # 'dm_start_week', 'dm_sales_qty', 'current_dm_theme_id', 'current_dm_theme_en_desc', # DELETED on 12/07 Sprint4: 95 # 'current_theme_start_date', # 'current_theme_end_date', # ADDED back on 12/07 Sprint4: 96 'current_theme_start_date', 'current_theme_end_date', 'current_dm_psp_start_date', 'current_dm_psp_end_date', # DELETED on 12/07 Sprint4: 95 # 'current_dm_msp_end_date', # ADDED on 12/07 Sprint4: 95 'psp_start_week', 'psp_start_month', 'psp_end_week', 'full_item', 'item_store', # DELETED on 12/07 Sprint4: 95 # 'ind_out_of_stock' ] flat_features = [ # DELETED on 12/07 Sprint4: 95 # 'trxn_month', # 'trxn_week', 'current_dm_page_no', 'current_dm_nsp', 'current_dm_psp', # DELETED on 12/07 Sprint4: 95 # 'psp_nsp_ratio', # ADDED on 12/07 Sprint4: 95 'current_dm_psp_nsp_ratio', 'last_year_dm_sales', 'last_year_dm_psp_nsp_ratio', 'last_year_fam_dm_sales_avg', 'last_5dm_sales_avg', 'fam_last_5dm_sales_avg', 'current_dm_weekend_days', 'curr_psp_days', # ADDED Sprint 4 v1: # 'last_year_lunar_sales_qty_1m_avg', # 'last_year_lunar_sales_qty_1m_sum', # 'last_year_lunar_ratio_1m', # 'last_year_lunar_sales_qty_3m_sum', # 'last_year_lunar_sales_qty_3m_avg', # 'last_year_lunar_ratio_3m', # ADDED Sprint 4: vrai exact 'last_year_dm_sales_vrai_exact', 'vrai_exact_or_lunar_1m', 'vrai_exact_or_lunar_3m', 'current_dm_busday', # ADDED Sprint 4: 93. uplift 'uplift_value', 'uplift', '4w_sales_4w_bef', # ADDED Sprint 4: 94. discount promo 'coupon_disc_ratio_avg_max', 'coup_disc_ratio_mech_max', ] # + time_features = [] dummies_names = [ 'store_code', 'item_seasonal_code', 'sub_family_code', 'current_dm_page_strategy_code', # DELETED on 12/07 Sprint4: 95 # 'current_dm_nl', # ADDED on 12/07 Sprint4: 95 'nl', 'current_dm_slot_type_code', # ADDED Sprint 4 v1: 'festival_type', ] # - df[[ 'nl', 'current_dm_psp_nsp_ratio', 'current_dm_slot_type_name', 'family_code', 'psp_start_week', 'psp_start_month', 'psp_end_week', ]].head() # # Check features missing used_cols = dummies_names + time_features + flat_features + identification ok = df.columns[df.columns.isin(used_cols)] not_used = df.columns[~df.columns.isin(used_cols)] not_used.to_list() # + {"active": ""} # type id: 3 types # 01, 02, 04 # 80% is 01, direct discount, dd # 17% 02, next purchase coupon, np # 04, changing coupon, cp # --> we dont have 03, AC advertising coupon in our scope # # type code: 14 different # 7 in our table # CP: customer purchase, 94% # MPM: promotion for carrefour members, 2% # MP: member price, 2% # CC: customer changing coupon, less than 1% # MPCM: member point used for items, 0.3% # MSG: member point, less 0.2 # EX: exchange coupon, less 0.2 # # NDV: Number of distinct value # ndv_coupon_activity_type_id # ndv_coupon_typecode # # - used_cols_df = pd.Series(used_cols) error_do_not_exist = used_cols_df[~used_cols_df.isin(df.columns)] if not (error_do_not_exist.to_list() == []): print(error_do_not_exist) raise Exception('Some features do not exist. Check the names!') if not (len(used_cols) == len(set(used_cols))): print(used_cols_df[used_cols_df.duplicated()]) raise Exception('Duplicated features!!!') def create_dummies(dummies_names, df): for i in dummies_names: df = pd.concat([df, pd.get_dummies(df[i], prefix=i + "_")], axis=1) return df df = create_dummies(dummies_names, df) dummies_features = [] for i in df.columns: if any([i.startswith(s + '__') for s in dummies_names]): dummies_features.append(i) dummies_names features = dummies_features + flat_features + time_features sample = df.loc[:50, features] sample.columns[sample.columns.duplicated()] if not (sample.columns[sample.columns.duplicated()].to_list() == []): print(sample.columns[sample.columns.duplicated()]) raise Exception('Some features are duplicated. Check the names!') #### Save features now = datetime.datetime.now() str(now) names_features = [ folder + 'features/dummies_features_' + str(now) + '.csv', folder + 'features/flat_features_' + str(now) + '.csv', folder + 'features/time_features_' + str(now) + '.csv', folder + 'features/identification_' + str(now) + '.csv' ] try: os.mkdir(folder + 'features') print("Directory created ") pd.Series(dummies_features).to_csv(names_features[0], index=False, header=False) pd.Series(flat_features).to_csv(names_features[1], index=False, header=False) pd.Series(time_features).to_csv(names_features[2], index=False, header=False) pd.Series(identification).to_csv(names_features[3], index=False, header=False) except FileExistsError: print("Directory already exists") # + csv = [] for file in glob.glob(folder + "features/*.csv"): csv.append(file) # - iden = [s for s in csv if 'identification' in s][0] flat = [s for s in csv if 'flat' in s][0] dumm = [s for s in csv if 'dummies' in s][0] dummies_features = pd.read_csv(dumm, squeeze=True).tolist() flat_features = pd.read_csv(flat, squeeze=True).tolist() #time_features = pd.read_csv(names_features[2], squeeze=True).tolist() identification = pd.read_csv(iden, squeeze=True).tolist() try: df['current_theme_start_date'] = pd.to_datetime( df['current_theme_start_date']) except: print('REPLACE THEME START BY PSP START') df['current_theme_start_date'] = pd.to_datetime( df['current_dm_psp_start_date']) df['week_end_date'] = df['current_theme_start_date'] try: df['planned_bp_flag'] except: print('FILLED planned_bp_flag with 0') df['planned_bp_flag'] = 0 try: df['active_flag'] except: print('FILLED active_flag with 0') df['active_flag'] = 1 # + try: df['out_stock_flag'] except: try: df['out_stock_flag'] = df['ind_out_of_stock'] print('FILLED out_stock_flag with ind_out_of_stock') except: print('FILLED out_stock_flag with 0') df['out_stock_flag'] = 0 # - with open(f'{folder}calendar.pkl', 'rb') as input_file: calendar = pickle.load(input_file) calendar['week_end_date'] = calendar['date_value'] df = df.merge(calendar[['week_end_date', 'week_key']], how='left') # ## Filter weeks with negative sales df[df['dm_sales_qty'] < 0].head() df_no_neg = df[(df['dm_sales_qty'] >= 0) | (df['dm_sales_qty'].isna())] df_no_neg['dm_sales_qty'].describe() with open(folder + dataset_name + '.pkl', 'wb') as output_file: pickle.dump(df_no_neg, output_file)
# from functools import partial from american_option_pricing import american_option import density_utilities as du import prediction_ensemble_py as pe import warnings warnings.simplefilter(action='ignore', category=FutureWarning) """ ####################################################################################### Import Data ####################################################################################### """ data = pd.read_excel('etfs_sept_11.xlsx', index_col=None) current_date = date(2020, 9, 4) expiry_date = date(2020, 9, 11) days_to_expiry = np.busday_count(current_date, expiry_date) - 1 min_p_profit = 35 forecast_dens = False save_results = True save_plots = True Strategies = [ "Bearish Call Modified Butterfly", "Bullish Put Modified Butterfly" ] """ ####################################################################################### Get Risk Free Date ####################################################################################### """
def get_trades(self, aut_stp_pct, sell_pct, sec_sell, hld_days): def reset_trading_var(): self.ent_trggerd, self.in_trade = False, False self.ent_dt, self.sell_trggerd = None, False self.brk_tke_hg, self.aut_stp, self.ent_p = 0, 0, 0 self.stp_loss, self.pre_ent_stp, self.hgst_brk_res_p = 0, 0, 0 self.hld_days_lft = 0 def add_trades(exit_p, use_prev_dt=False): self.rsult_p = n_decim(exit_p - self.ent_p) self.rsult_pct = n_decim((exit_p - self.ent_p) / self.ent_p * 100) self.arr_rsult_p = np.append(self.arr_rsult_p, self.rsult_p) self.arr_rsult_pct = np.append(self.arr_rsult_pct, self.rsult_pct) if not use_prev_dt: exit_dt = sdt else: exit_dt = prev_day_dt lst_trades.append((self.rsult_p, self.rsult_pct, self.ent_dt, n_decim(self.ent_p), exit_dt, n_decim(exit_p))) def pct_change(p1, p2): return 1 - ((p1 - p2) / p1) def qualify_trigger(initial=True): lwr_res_stop, lwr_res_p = 0, 0 for res_stats in sorted(self.dict_res_stats[prd_count]): (res_p, _, res_stop, res_take) = res_stats if res_p > cl: if cl > self.pre_ent_stp and res_p - cl > cl - lwr_res_p: self.pre_ent_stp = lwr_res_stop if initial: self.ent_trggerd = True self.hld_days_lft = hld_days elif not initial: reset_trading_var() break else: lwr_res_stop = res_stop lwr_res_p = res_p aut_stp_pct = num_to_pct(aut_stp_pct) sell_pct = num_to_pct(sell_pct) sec_sell = num_to_pct(sec_sell) self.hld_days = hld_days self.dict_trades = {} dict_triggers_copy = copy.deepcopy(self.dict_triggers) self.arr_rsult_p, self.arr_rsult_pct = np.array([]), np.array([]) for period in self.lst_prd: lst_trades = [] prev_day_dt, prev_day_cl = None, 0 (prd_count, prd_start, prd_end, prd_type) = period_info(period) reset_trading_var() six_months = datetime.timedelta(180) pre_period_start = dt_to_str(str_to_dt(prd_start) - six_months) for day in self.ohlc_table[pre_period_start:prd_end].itertuples(): (dt, op, hg, lw, cl) = day sdt = dt_to_str(dt) enter_new_trade = not self.in_trade and self.ent_trggerd if enter_new_trade and dt >= str_to_dt(prd_start): if self.hld_days_lft == 0: if op > self.pre_ent_stp: self.ent_dt = sdt self.ent_p = op self.in_trade = True self.aut_stp = n_decim(op * aut_stp_pct) else: reset_trading_var() else: if cl > op and cl > prev_day_cl: qualify_trigger(initial=False) self.hld_days_lft -= 1 else: reset_trading_var() else: self.ent_trggerd = False self.pre_ent_stp = 0 if self.sell_trggerd: add_trades(op) reset_trading_var() if self.in_trade and sdt in self.lst_earnings_dt: if np.busday_count(self.ent_dt, sdt) > 4: add_trades(prev_day_cl, use_prev_dt=True) reset_trading_var() if self.in_trade: higher_res_p, higher_res_take = 0, 0 for (res_p, *_) in self.dict_res_stats[prd_count]: if cl > res_p: if res_p > self.hgst_brk_res_p: self.hgst_brk_res_p = res_p self.brk_tke_hg = 0 break for res_stats in self.dict_res_stats[prd_count]: (res_p, _, res_stop, res_take) = res_stats same_zone = res_p == self.hgst_brk_res_p if res_stop < op and op < res_p and same_zone: if hg > higher_res_take and pct_change(hg, cl) < sell_pct or\ hg > higher_res_p and pct_change(hg, cl) < sec_sell: if cl < higher_res_p: self.sell_trggerd = True self.stp_loss = res_stop break elif res_p < op: if hg > higher_res_p: if cl < higher_res_p: if pct_change(hg, cl) < sec_sell: self.sell_trggerd = True self.brk_tke_hg = 0 if hg > higher_res_take: if cl < higher_res_p: if hg > self.brk_tke_hg: self.brk_tke_hg = hg if pct_change(hg, cl) < sell_pct: self.sell_trggerd = True self.brk_tke_hg = 0 if self.brk_tke_hg: if pct_change(self.brk_tke_hg, cl) < sell_pct: if cl < higher_res_p: self.sell_trggerd = True self.brk_tke_hg = 0 self.stp_loss = res_stop break higher_res_p = res_p higher_res_take = res_take if self.stp_loss > self.aut_stp: if lw < self.stp_loss: if op > self.stp_loss: add_trades(self.stp_loss) else: add_trades(op) reset_trading_var() else: if self.in_trade and lw < self.aut_stp: if op >= self.aut_stp: add_trades(self.aut_stp) else: add_trades(op) reset_trading_var() prev_day_dt = sdt prev_day_cl = cl for trigger in dict_triggers_copy[prd_count].copy(): (trg_dt, trg_p, trg_start_dt, trg_end_dt) = trigger if cl > trg_p and dt > trg_start_dt or dt > trg_end_dt: dict_triggers_copy[prd_count].remove(trigger) if self.in_trade or self.ent_trggerd: continue if cl > trg_p and dt == trg_end_dt: qualify_trigger() previous_day_loss = False breakout_day = True after_prd_end = str_to_dt(prd_end) + six_months if self.in_trade: for day in self.ohlc_table[prd_end:after_prd_end].itertuples(): (dt, op, hg, lw, cl) = day hgst_stp = n_decim(self.dict_res_stats[prd_count][0][2]) self.stp_loss = hgst_stp if cl < self.stp_loss and not breakout_day: add_trades(self.stp_loss) break if previous_day_loss: if cl < op: add_trades(cl) break if cl > op: previous_day_loss = False if cl < op: previous_day_loss = True breakout_day = False self.dict_trades[prd_count] = lst_trades
def get_triggers(self, days_apart, inv_days, hld_bef, hld_aft, hg_diff): def lst_trg_add(): return dt_to_str(pot_dt1), pot_p1, pot_dt2, pot_inv_dt1 self.dict_triggers = {} inv_dt = datetime.timedelta(inv_days) six_months = datetime.timedelta(180) hg_diff = hg_diff / 100 total_hld = hld_bef + hld_aft + 1 peak_i = hld_bef for period in self.lst_prd: prev_days_cl, prev_days_dt, lst_pot_trg, lst_trg = [], [], [], [] (prd_count, prd_start, prd_end, prd_type) = period_info(period) pre_period_start = dt_to_str(str_to_dt(prd_start) - six_months) selected_period = self.ohlc_table[pre_period_start:prd_end] for day in selected_period.itertuples(): (dt, op, hg, lw, cl) = day if len(prev_days_cl) > total_hld: del prev_days_cl[0] del prev_days_dt[0] if hg_lw_finder(peak_i, prev_days_cl): hg_dt = prev_days_dt[peak_i] hg_cl = prev_days_cl[peak_i] lst_pot_trg.append([hg_dt, hg_cl, hg_dt + inv_dt]) prev_days_cl.append(cl) prev_days_dt.append(dt) for pot_trigger in lst_pot_trg: (_, pot_trg_p, pot_inv_dt) = pot_trigger if cl > pot_trg_p and dt < pot_inv_dt: pot_trigger[2] = dt for index, pot_trg1 in enumerate(lst_pot_trg.copy()): (pot_dt1, pot_p1, pot_inv_dt1) = pot_trg1 for pot_trg2 in lst_pot_trg[index+1:].copy(): (pot_dt2, pot_p2, _) = pot_trg2 trg_pct_diff = (pot_p1 - pot_p2) / pot_p1 if pot_inv_dt1 >= pot_dt2 and np.busday_count(dt_to_str( pot_dt1), dt_to_str(pot_dt2)) > days_apart and\ trg_pct_diff <= hg_diff and trg_pct_diff >= 0: if len(lst_trg) > 0: added_to_list = False for index2, (trg_dt, *_) in enumerate(lst_trg): if pot_dt1 < str_to_dt(trg_dt): pot_dt1 = dt_to_str(pot_dt1) lst_trg.insert(index2, lst_trg_add()) added_to_list = True break if not added_to_list: lst_trg.append(lst_trg_add()) else: lst_trg.append(lst_trg_add()) break self.dict_triggers[prd_count] = sorted(lst_trg)
def max_dd_duration(cumulative): i = (maximum.accumulate(cumulative) - cumulative).idxmax() j = cumulative[:i].idxmax() s = to_datetime(j).strftime('%Y-%m-%d') e = to_datetime(i).strftime('%Y-%m-%d') return busday_count(s, e)
# Establish the SLA for each order, based on the origin to destination route df['sla'] = [sla[i] for i in df['origin_destination']] df['2nd_deliver_attempt'].fillna(0, inplace=True) # Convert epoch time to datetime format start_date = pd.to_datetime(df['pick'] + timezone, unit='s').dt.date first_date = pd.to_datetime(df['1st_deliver_attempt'] + timezone, unit='s').dt.date second_date = pd.to_datetime(df['2nd_deliver_attempt'] + timezone, unit='s').dt.date # Number of days from when the parcel was picked up to 1st delivery attempt df['num_days_first'] = np.busday_count(start_date, first_date, weekmask=workdays, holidays=public_holidays) # Number of days between the 1st and 2nd delivery attempt df['num_days_second'] = np.busday_count(first_date, second_date, weekmask=workdays, holidays=public_holidays) df['is_late'] = (df['num_days_first'] > df['sla']) | (df['num_days_second'] > sla_second_attempt) df['is_late'] = df['is_late'].astype(int) results = df[['orderid', 'is_late']] results.to_csv('results.csv', index=False)
import logging
def texp(self, dt=today): return np.busday_count(p.Timestamp(dt).asm8.astype('<M8[D]'), self.asm)
import datetime import numpy as np import time from datetime import timedelta d1 = datetime.date.today() d2 = datetime.date(2016,8,5) d3 = datetime.date(2016,01,20) print "Today is " + str(d1) print "Go live is " + str(d2) print str((d2-d1).days -1) + ' absolute days that are left before go-live' days = np.busday_count(d1,d2) -1 print "Days without weekends " + str(days) + " !!!! " #time_used_in_current_day = datetime.strftime("%H:%M:%S") #time_to_go_home = datetime.strptime("18:30:00") #print time_to_go_home #print time_used_in_current_day #d1 = datetime.strptime(str(time_to_go_home), "%Y-%m-%d %H:%M:%S") #d2 = datetime.strptime(str(time_used_in_current_day), "%Y-%m-%d %H:%M:%S") #print(d1 - d2)
def get_weekends(number_of_days, from_date, to_date): import numpy as np start = getdate(from_date) end = getdate(to_date) return number_of_days - np.busday_count(start, end) - 1
PackedOutput=PackedFilter[[ 'Order number', 'Order created : OBOI0001', 'DATE Order OBOI0001', 'Order packed: OBOI1001', 'Date Order OBOI1001', 'Lost order ', ]] PackedOutput['DATE Order OBOI0001']=pd.to_datetime(PackedOutput['DATE Order OBOI0001'],coerce=True).fillna(1000) PackedOutput['Date Order OBOI1001']=pd.to_datetime(PackedOutput['Date Order OBOI1001'],coerce=True).fillna(1000) A = [c.date() for c in PackedOutput['DATE Order OBOI0001']] B = [d.date() for d in PackedOutput['Date Order OBOI1001']] PackedOutput['PickPackTime (Days)'] = np.busday_count(A, B) PackedSorted=PackedOutput.sort(['PickPackTime (Days)'],ascending=False) # 3) Orders with Address Error OBOCOO9 AddressError=TimeOutput[(TimeOutput['Address error OBOCOO9']=='Yes')] AddressErrorOutput=AddressError[[ 'Order number', 'Order created : OBOI0001', 'DATE Order OBOI0001', 'Order packed: OBOI1001', 'Date Order OBOI1001', 'Address error OBOCOO9', 'Date Address error OBOCOO9'
def test_stock_report_views(self): ''' test_stock_report_views ''' client = Client(enforce_csrf_checks=True) stock_list = [] enddate = timezone.now().date() - timedelta(days=1) startdate = enddate - timedelta(days=30) duration = int(np.busday_count(startdate, enddate + timedelta(days=1))) # set first, second stock to failing stock_list.append( Stock.objects.create( title='foo_title', code='foo_code', sector='foo_sector', tradeVolume=1, score=30, )) stock_list.append( Stock.objects.create( title='foo_title', code='foo_code', sector='foo_sector', tradeVolume=1, score=70, )) # Create 100 stocks for i in range(100): stock_list.append( Stock.objects.create( title='foo_title', code='foo_code', sector='foo_sector', tradeVolume=1, score=50, )) # Create news objects for i in range(100): News.objects.create( stock=stock_list[i], date='2020-12-07', title='foo_title', press='foo_press', link='foo_link', ) testdate = timezone.now().date() - timedelta(days=2) # set first, second stock to failing StockHistory.objects.create( stock=stock_list[0], date=testdate, tradeVolume=1, endPrice=100, ) StockHistory.objects.create( stock=stock_list[1], date=testdate, tradeVolume=1, endPrice=100, ) # Create stockhistory objects for i in range(2, 102): for _ in range(duration): StockHistory.objects.create( stock=stock_list[i], date=testdate, tradeVolume=1, endPrice=100, ) # stock_top100_stockinfo response = client.get('/api/stocks/report/up/stockinfo/') self.assertEqual(response.status_code, 200) response = client.get('/api/users/token/') csrftoken = response.cookies['csrftoken'].value response = client.delete('/api/stocks/report/up/stockinfo/', HTTP_X_CSRFTOKEN=csrftoken) self.assertEqual(response.status_code, 405) # stock_top100_news response = client.get('/api/stocks/report/up/news/') self.assertEqual(response.status_code, 200) response = client.get('/api/users/token/') csrftoken = response.cookies['csrftoken'].value response = client.delete('/api/stocks/report/up/news/', HTTP_X_CSRFTOKEN=csrftoken) self.assertEqual(response.status_code, 405) # stock_top100_stockhistory response = client.get('/api/stocks/report/up/stockhistory/') self.assertEqual(response.status_code, 200) response = client.get('/api/users/token/') csrftoken = response.cookies['csrftoken'].value response = client.delete('/api/stocks/report/up/stockhistory/', HTTP_X_CSRFTOKEN=csrftoken) self.assertEqual(response.status_code, 405) # stock_bottom100_stockinfo response = client.get('/api/stocks/report/down/stockinfo/') self.assertEqual(response.status_code, 200) response = client.get('/api/users/token/') csrftoken = response.cookies['csrftoken'].value response = client.delete('/api/stocks/report/down/stockinfo/', HTTP_X_CSRFTOKEN=csrftoken) self.assertEqual(response.status_code, 405) # stock_bottom100_news response = client.get('/api/stocks/report/down/news/') self.assertEqual(response.status_code, 200) response = client.get('/api/users/token/') csrftoken = response.cookies['csrftoken'].value response = client.delete('/api/stocks/report/down/news/', HTTP_X_CSRFTOKEN=csrftoken) self.assertEqual(response.status_code, 405) # stock_bottom100_stockhistory response = client.get('/api/stocks/report/down/stockhistory/') self.assertEqual(response.status_code, 200) response = client.get('/api/users/token/') csrftoken = response.cookies['csrftoken'].value response = client.delete('/api/stocks/report/down/stockhistory/', HTTP_X_CSRFTOKEN=csrftoken) self.assertEqual(response.status_code, 405)
def getoneyearday(nowdate=None): if not nowdate: nowdate = dt.date.today() oneyearbizdays = np.busday_count(nowdate, dt.date(nowdate.year+1, nowdate.month, nowdate.day), holidays=holidays) # print 'OneYearDay: ', oneyearbizdays return oneyearbizdays
def formula(individu, period, parameters): # * Tous les calculs sont faits sur le mois * # Les types de contrats gérés contrat_de_travail = individu('contrat_de_travail', period) TypesContratDeTravail = contrat_de_travail.possible_values # [ temps_plein # temps_partiel # forfait_heures_semaines # forfait_heures_mois # forfait_heures_annee # forfait_jours_annee ] contrat_de_travail_debut = individu('contrat_de_travail_debut', period) contrat_de_travail_fin = individu('contrat_de_travail_fin', period) # Volume des heures rémunérées à un forfait heures forfait_heures_remunerees_volume = individu( 'forfait_heures_remunerees_volume', period) # noqa F841 # Volume des heures rémunérées à forfait jours forfait_jours_remuneres_volume = individu( 'forfait_jours_remuneres_volume', period) heures_duree_collective_entreprise = individu( 'heures_duree_collective_entreprise', period) # Volume des heures rémunérées contractuellement (heures/mois, temps partiel) heures_remunerees_volume = individu('heures_remunerees_volume', period) # Volume des heures non rémunérées (convenance personnelle hors contrat/forfait) heures_non_remunerees_volume = individu('heures_non_remunerees_volume', period) # Décompte des jours en début et fin de contrat # http://www.gestiondelapaie.com/flux-paie/?1029-la-bonne-premiere-paye debut_mois = datetime64(period.start.offset('first-of', 'month')) fin_mois = datetime64(period.start.offset( 'last-of', 'month')) + timedelta64( 1, 'D') # busday ignores the last day jours_ouvres_ce_mois = busday_count(debut_mois, fin_mois, weekmask='1111100') # jours travaillables sur l'intersection du contrat de travail et du mois en cours jours_ouvres_ce_mois_incomplet = busday_count( max_(contrat_de_travail_debut, debut_mois), min_(contrat_de_travail_fin, fin_mois), weekmask='1111100') duree_legale_mensuelle = 35 * 52 / 12 # ~151,67 heures_temps_plein = switch(heures_duree_collective_entreprise, { 0: duree_legale_mensuelle, 1: heures_duree_collective_entreprise }) jours_absence = heures_non_remunerees_volume / 7 coefficient_proratisation_temps_partiel = heures_remunerees_volume / heures_temps_plein coefficient_proratisation_forfait_jours = forfait_jours_remuneres_volume / 218 # temps plein coefficient = switch( contrat_de_travail, { # temps plein TypesContratDeTravail.temps_plein: ((jours_ouvres_ce_mois_incomplet - jours_absence) / jours_ouvres_ce_mois), # temps partiel # (en l'absence du détail pour chaque jour de la semaine ou chaque semaine du mois) TypesContratDeTravail.temps_partiel: coefficient_proratisation_temps_partiel * ((jours_ouvres_ce_mois_incomplet * coefficient_proratisation_temps_partiel - jours_absence) / (jours_ouvres_ce_mois * coefficient_proratisation_temps_partiel + 1e-16)), TypesContratDeTravail.forfait_jours_annee: coefficient_proratisation_forfait_jours * ((jours_ouvres_ce_mois_incomplet * coefficient_proratisation_forfait_jours - jours_absence) / (jours_ouvres_ce_mois * coefficient_proratisation_forfait_jours + 1e-16)) }) # Forfait en heures # coefficient = (contrat_de_travail >= 2) * (contrat_de_travail <= 3) * ( # forfait_heures_remunerees_volume / 45.7 * 52 / 12 # ) + return (jours_ouvres_ce_mois_incomplet > 0) * coefficient
def busday_duration(date_a: datetime, date_b: datetime = None, interval="hours") -> int: """ Returns a duration as specified by variable interval. Only includes business days. Functions, except totalDuration, returns[quotient, remainder] Args: date_a: First date date_a (Optional): Second date Returns: The duration between the two dates in the interval indicated (or hours if none is given) """ if date_b == None: date_b = datetime.now(date_a.tzinfo) full_duration = date_b - date_a # pylint: disable=no-member bus_days = np.busday_count( date_a.date(), date_b.date()).item() duration = full_duration if full_duration.days == 2 and bus_days == 1: duration = full_duration - timedelta(days=(2)) elif full_duration.days > bus_days: duration = full_duration - \ timedelta(days=(full_duration.days - bus_days)) duration_in_s = duration.total_seconds() def years(): return divmod(duration_in_s, 31556926) # Seconds in a year=31556926. def days(seconds=None): # Seconds in a day = 86400 return divmod(seconds if seconds != None else duration_in_s, 86400) def hours(seconds=None): # Seconds in an hour = 3600 return divmod(seconds if seconds != None else duration_in_s, 3600) def minutes(seconds=None): # Seconds in a minute = 60 return divmod(seconds if seconds != None else duration_in_s, 60) def seconds(seconds=None): if seconds != None: return divmod(seconds, 1) return duration_in_s def totalDuration(): y = years() d = days(y[1]) # Use remainder to calculate next variable h = hours(d[1]) m = minutes(h[1]) s = seconds(m[1]) return "Time between dates: {} years, {} days, {} hours, {} minutes and {} seconds".format(int(y[0]), int(d[0]), int(h[0]), int(m[0]), int(s[0])) return { 'years': int(years()[0]), 'days': int(days()[0]), 'hours': int(hours()[0]), 'minutes': int(minutes()[0]), 'seconds': int(seconds()), 'default': totalDuration() }[interval]
def fetch_historical_quotes(self, universe): logging.info(f"Fetching historical quotes for universe {universe}.") # Instanciate necessary objects universe_db = UniversesDatabase() tws = Tws() contracts_db = ContractsDatabase() quotes_db = QuotesDatabase() quotes_status_db = QuotesStatusDatabase() tools = Tools() # Get config constants REDOWNLOAD_DAYS = int(get_config_value('quotes', 'redownload_days')) # Get universe members contract_ids = universe_db.get_universe_members(universe=universe) # Setup progress bar manager = enlighten.get_manager() pbar = manager.counter(total=len(contract_ids), desc="Contracts", unit="contracts") exiter = GracefulExiter() tws.connect() logging.info(f"Connnected to TWS.") try: for contract_id in contract_ids: # Abort, don't process further contracts if exiter.exit() or tws.has_error(): logging.info("Aborting historical quotes fetching.") break # Get contracts data filters = {'contract_id': contract_id} columns = ['broker_symbol', 'exchange', 'currency'] contract = contracts_db.get_contracts( filters=filters, return_columns=columns)[0] quotes_status = quotes_status_db.get_quotes_status(contract_id) logging.info( f"Preparing to fetch hiostorical quotes for {contract['broker_symbol']} on {contract['exchange']}" ) # Calculate length of requested data if quotes_status is None: duration_str = "15 Y" quotes_from = date.today() fifteen_years = timedelta(days=5479) quotes_from -= fifteen_years quotes_till = date.today().strftime('%Y-%m-%d') elif quotes_status['status_code'] == 1: start_date = (quotes_status['daily_quotes_requested_till']) end_date = date.today().strftime('%Y-%m-%d') ndays = np.busday_count(start_date, end_date) if ndays <= REDOWNLOAD_DAYS: logging.info( f"Existing data is only {ndays} days old. Contract aborted." ) pbar.update() continue if ndays > 360: logging.info( f"Existing data is already {ndays} days old. Contract aborted." ) pbar.update() continue ndays += 6 duration_str = str(ndays) + ' D' quotes_from = quotes_status['daily_quotes_requested_from'] quotes_till = end_date else: logging.info("Contract already has error status. Skipped.") pbar.update() continue # Request quotes from tws quotes = tws.download_historical_quotes( contract_id=contract_id, symbol=contract['broker_symbol'], exchange=tools.encode_exchange_ib(contract['exchange']), currency=contract['currency'], duration=duration_str) if quotes is not None: # Inserting quotes into database logging.info(f"Storing {len(quotes)} quotes to database.") quotes_db.insert_quotes(quotes=quotes) # Write finished info to contracts database quotes_status_db.insert_quotes_status( contract_id=contract_id, status_code=1, status_text="Successful", daily_quotes_requested_from=quotes_from, daily_quotes_requested_till=quotes_till) else: # Write error info to contracts database error_code, error_text = tws.get_contract_error() quotes_status_db.insert_quotes_status( contract_id=contract_id, status_code=error_code, status_text=error_text, daily_quotes_requested_from=None, daily_quotes_requested_till=None) pbar.update() logging.info( f"Finished fetching historical quotes for universe {universe}." ) finally: tws.disconnect() logging.info(f"Disconnnected from TWS.")
attackrate = {k:[] for k in grp_dict.keys()} count = {k:0 for k in grp_dict.keys()} exposed = {k:0 for k in grp_dict.keys()} inperson_days = {k:0 for k in grp_dict.keys()} possible_days = {k:0 for k in grp_dict.keys()} for sid,stats in sim.school_stats.items(): if stats['type'] not in ['es', 'ms', 'hs']: continue inf = stats['infectious'] inf_at_sch = stats['infectious_stay_at_school'] # stats['infectious_arrive_at_school'] stats['infectious_stay_at_school'] in_person = stats['in_person'] exp = stats['newly_exposed'] num_school_days = stats['num_school_days'] possible_school_days = np.busday_count(first_date, last_date) n_exp = {} for grp in groups: n_exp[grp] = np.sum(exp[grp]) for gkey, grps in grp_dict.items(): in_person_days = scheduled_person_days = num_exposed = num_people = 0 for grp in grps: in_person_days += np.sum(in_person[grp]) scheduled_person_days += num_school_days * stats['num'][grp] num_exposed += n_exp[grp] num_people += stats['num'][grp] perc_inperson_days_lost[gkey].append( 100*(scheduled_person_days - in_person_days)/scheduled_person_days if scheduled_person_days > 0 else 100 ) attackrate[gkey].append( 100 * num_exposed / num_people)
def _moved(self, from_date, to_date, bday): return np.busday_count( np.datetime64(from_date.date()), np.datetime64(to_date.date()), busdaycal=bday.calendar, )
# Get all the labels for the board label_list = board.get_labels() # Pull all the cards from the board using the basic Card object card_list = board.all_cards() # Initialise the list of the CardPlus objects which is a suclass of the base Card object that stores more data card_plus_list = [] # Convert Card objects to CardPlus objects while adding extra data todays_date = date.today() for card in card_list: # Number of working days since card last updated last_updated_date = card.date_last_activity business_days_between = numpy.busday_count(last_updated_date.date(), todays_date) card.business_days_since_update = business_days_between # Organisation and registers card.organisation = "None" card.registers = [] card.location_picker = False for card_label_id in card.idLabels: for board_label in label_list: if ((card_label_id == board_label.id) and (board_label.color == "green")): card.organisation = board_label.name break elif ((card_label_id == board_label.id) and (board_label.color == "black")): card.registers.append(board_label.name) break elif ((card_label_id == board_label.id) and (board_label.color == "sky")):
]).T Synthetic.columns = ['=', 'Synthetic', '+', 'Equivalent'] ### Grabbing the current risk free rate. 3 Month Treasury Bill i-Rate rf = pd.read_html( 'https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield', header=0)[1] rf = rf.iloc[len(rf) - 1, 3] ### Adding time to expiry to all_stocks_df. This adds the number of business days # until the option expires and the number of actual days until it expires BDays_to_expiry = pd.Series() Days_to_expiry = pd.Series() for i in all_stocks_df.index.get_level_values(2): BDays_to_expiry = BDays_to_expiry.append( pd.Series(np.busday_count(str(dt.datetime.today().date()), str(i)))) Days_to_expiry = Days_to_expiry.append( pd.Series( np.busday_count(str(dt.datetime.today().date()), str(i), weekmask=[1, 1, 1, 1, 1, 1, 1]))) all_stocks_df = all_stocks_df.assign(BDays_to_expiry=BDays_to_expiry.values) all_stocks_df = all_stocks_df.assign(Days_to_expiry=Days_to_expiry.values) ### All the stock pricing data. This uses Alpha Vantage to grab stock information. # The key comes with a free account and it's limited to 5 requests # per minute, which is why the loop has to sleep. It grabs the price every hour # and the price from everyday. ts = TimeSeries(key='SSMOWFN5YVIU7J3K', output_format='pandas') Pricing_intraday = {}
df = pd.DataFrame(index=pd.date_range('2014-01-01', '2019-12-31', freq='D')) df['Date'] = df.index df["YearMonthDay"] = (df.Date.dt.strftime('%Y%m%d')).astype(int) df['FirstDateOfMonth'] = df.Date.dt.to_period("m").dt.start_time df['LastDateOfMonth'] = df.Date.dt.to_period("m").dt.end_time df['NextDay'] = pd.DatetimeIndex(df.LastDateOfMonth) + pd.DateOffset(days=1) df.loc[df['YearMonthDay'].isin(reddays), 'RedDays'] = 1 df.loc[df['YearMonthDay'].isin(halfdays), 'HalfDays'] = 0.5 df.update(df[['RedDays', 'HalfDays']].fillna(0)) df['RedDaysSum'] = df.groupby(['FirstDateOfMonth' ])['RedDays'].transform(lambda x: x.sum()) df['HalfDaysSum'] = df.groupby(['FirstDateOfMonth' ])['HalfDays'].transform(lambda x: x.sum()) df['NonWorkingDays'] = df.RedDaysSum + +df.HalfDaysSum df["NetWorkingDays"] = np.busday_count( df.FirstDateOfMonth.values.astype('datetime64[D]'), df.NextDay.values.astype('datetime64[D]')) df['WorkingDays'] = df.NetWorkingDays - df.NonWorkingDays df['Utalizationrate'] = 0.92 df['Workingdays_utilized'] = df.WorkingDays * df.Utalizationrate df.drop_duplicates(['FirstDateOfMonth'], keep='first', inplace=True) df = df[keepcols] df.to_sql(name=str(table), con=sqlite_db, index=False, if_exists='replace') '''========================================================================''' table = 'crm' pd.io.sql.execute('DROP table IF EXISTS ' + str(table), sqlite_db) for df in pd.read_csv(Main_Path + '\\Indata\\crm.csv', sep=';', encoding=encoding,
def run(self): # ESP_MSVP stopped at Actualización 116 (25.05.2020) start = 116 stop = (date.today() - date(2020, 5, 25)).days + 117 if stop > 153: # Weekdays only from Actualización 153 (01.07.2020) stop = np.busday_count(date(2020, 7, 1), date.today()) + 154 if self.sliding_window_days: start = max(start, stop - self.sliding_window_days) for actualizacion in range(start, stop): parsed = self.fetch(actualizacion) time.sleep(5) # crawl delay if parsed is None: continue content = unicodedata.normalize('NFKC', parsed['content']) fecha = datetime.strptime(get_fecha(content), '%d.%m.%Y').strftime('%Y-%m-%d') if actualizacion < 234: # Actualización 116-233 (25.05.2020 to 21.10.2020) tabs = get_ccaa_tables(content, ['Tabla 1. Casos', 'Tabla 2. Casos']) if 'Acrobat Distiller' in parsed['metadata'][ 'producer']: # fragile tabs[0] = [[col for col in row if col != ''] for row in tabs[0]] tabs[1] = [[col for col in row if col != ''] for row in tabs[1]] df1 = pd.DataFrame([row[0:2] for row in tabs[0]], columns=['ccaa', 'confirmed']) df2 = pd.DataFrame([[row[i] for i in (0, 1, 3, 5)] for row in tabs[1]], columns=[ 'ccaa', 'hospitalised', 'hospitalised_icu', 'dead' ]) data = df1.merge(df2, on='ccaa') else: # From Actualización 234 (22.10.2020) onwards, hospitalised and hospitalised_icu # became non-cumulative so we skipped them if actualizacion == 234: # Actualización 234 (22.10.2020) created a separate table for deaths tabs = get_ccaa_tables( content, ['Tabla 1. Casos', 'Tabla 4. Casos']) elif actualizacion == 266: # Actualización 266 (07.12.2020) had no hospitalisation data tabs = get_ccaa_tables( content, ['Tabla 1. Casos', 'Tabla 3. Casos']) elif actualizacion >= 235 and actualizacion <= 359: # Actualización 235 to 359 (23.10.2020 to 22.04.2021) moved Table 4 to Table 5 tabs = get_ccaa_tables( content, ['Tabla 1. Casos', 'Tabla 5. Casos']) else: # Actualización 360+ (23.04.2021 to present) moved Table 5 back to Table 4 tabs = get_ccaa_tables( content, ['Tabla 1. Casos', 'Tabla 4. Casos']) if 'Acrobat Distiller' in parsed['metadata'][ 'producer']: # fragile tabs[0] = [[col for col in row if col != ''] for row in tabs[0]] tabs[1] = [[col for col in row if col != ''] for row in tabs[1]] df1 = pd.DataFrame([row[0:2] for row in tabs[0]], columns=['ccaa', 'confirmed']) df2 = pd.DataFrame([row[0:2] for row in tabs[1]], columns=['ccaa', 'dead']) data = df1.merge(df2, on='ccaa') for index, record in data.iterrows(): # ccaa,confirmed,hospitalised,hospitalised_icu,dead ccaa = record[0] confirmed = int(record[1]) if pd.notna(record[1]) else None if actualizacion < 234: hospitalised = int(record[2]) if pd.notna( record[2]) else None hospitalised_icu = int(record[3]) if pd.notna( record[3]) else None dead = int(record[4]) if pd.notna(record[4]) else None else: hospitalised = None hospitalised_icu = None dead = int(record[2]) if pd.notna(record[2]) else None success, adm_area_1, adm_area_2, adm_area_3, gid = self.adm_translator.tr( country_code='ESP', input_adm_area_1=ccaa, input_adm_area_2=None, input_adm_area_3=None, return_original_if_failure=True) upsert_obj = { 'source': self.SOURCE, 'date': fecha, 'country': 'Spain', 'countrycode': 'ESP', 'adm_area_1': adm_area_1, 'adm_area_2': adm_area_2, 'adm_area_3': adm_area_3, 'confirmed': confirmed, 'dead': dead, 'hospitalised': hospitalised, 'hospitalised_icu': hospitalised_icu, 'gid': gid } self.upsert_data(**upsert_obj)
import numpy as np np.busday_count('2019-01-12', '2019-12-31') def calculo_salario(salarioneto, dias): bruto = salarioneto / dias return bruto salarioneto = input("cual es el salario mensual :") fecha1 = input("cual es la fecha inicial del mes en el formato aaaa-mm-dd :") fecha2 = input("cual es la fecha final del mes en el formato aaaa-mm-dd :") dias = np.busday_count(fecha1, fecha2) + 1 # diasquincena = input ("cuantos dias tuvo la quincena :") semana = calculo_salario(int(salarioneto), int(dias)) print(semana)
def Working_days(start_date, date_string): if (type(start_date) != unicode): start_date = u'2000-10-27 00:00:00' arr = date_string.split() arr.remove(u'Week') brr = arr[0].split('-') start = brr[0] end = brr[1] d1 = start.split('/') d2 = end.split('/') month = int(d2[0]) day = int(d2[1]) year = int(d2[2]) if month == 1 and day == 31: month = 2 day = 1 if month == 2 and day == 29: month = 3 day = 1 if month == 2 and day == 28 and year % 4 == 0: month = 2 day = 29 if month == 3 and day == 31: month = 4 day = 1 if month == 5 and day == 31: month = 6 day = 1 if month == 7 and day == 31: month = 8 day = 1 if month == 8 and day == 31: month = 9 day = 1 if month == 10 and day == 31: month = 11 day = 1 if month == 12 and day == 31: month = 1 day = 1 year = year + 1 if month == 4 and day == 30: month = 5 day = 1 if month == 6 and day == 30: month = 7 day = 1 if month == 9 and day == 30: month = 10 day = 1 if month == 11 and day == 30: month = 12 day = 1 d2[0] = str(month) d2[1] = str(day) d2[2] = str(year) if (len(d1[0]) == 1): d1[0] = u'0' + d1[0] if (len(d1[1]) == 1): d1[1] = u'0' + d1[1] if (len(d2[0]) == 1): d2[0] = u'0' + d2[0] if (len(d2[1]) == 1): d2[1] = u'0' + d2[1] begin = d1[2] + '-' + d1[0] + '-' + d1[1] finish = d2[2] + '-' + d2[0] + '-' + d2[1] a = start_date.split() a.remove(a[1]) a = a[0].split('-') start_month = a[1] start_day = a[2] start_year = a[0] if (len(a[0]) == 1): a[0] = u'0' + a[0] if (len(a[1]) == 1): a[1] = u'0' + a[1] Rstart = a[0] + '-' + a[1] + '-' + a[2] count1 = np.busday_count(Rstart, begin) count2 = np.busday_count(Rstart, finish) if (count1 >= 0): return np.busday_count(begin, finish) else: return max(0, np.busday_count(Rstart, finish))
@author: Administrator """ import pandas as pd import numpy as np from scipy.interpolate import interp1d import matplotlib filaname = r'C:\Users\Administrator\PycharmProjects\backtester\data\spx_vols.txt' df = pd.read_csv(filaname, parse_dates=['Date', 'Maturity']) # dtype={'Date': np.datetime64, 'Maturity': np.datetime64}) df['Maturity'] = pd.to_datetime(df['Maturity'], format='%Y%m%d') #HACK: why doens't parse dates already do this? df['DaysToMaturity'] = (df['Maturity'] - df['Date']).dt.days #TODO: check whether using day instead of business day to interpolate is correct? df['DateDiff'] = np.busday_count( [d.date() for d in df['Date']], [d.date() for d in df['Maturity']], ) def groupbyInterpFunc(data): return interp1d(data['DaysToMaturity'], data['Volatility'], bounds_error=False, fill_value='extrapolate', kind='cubic') dfGroupedByInterp = pd.DataFrame(df.groupby(['Date', 'Spot']).apply(groupbyInterpFunc)) interpFuncDict = dfGroupedByInterp.to_dict('index') daysToMaturity = np.array(range(1,92)) interpDict = {k: v[0](daysToMaturity) for k,v in interpFuncDict.items()} listOfDfs = [pd.DataFrame({'Date': k[0], 'Spot': k[1], 'DaysToMaturity':daysToMaturity, 'Volatility':v}) for k,v in interpDict.items()] dfInterpolatedVols = pd.concat(listOfDfs) dfInterpolatedVols['Maturity'] = dfInterpolatedVols['Date'] + pd.to_timedelta(dfInterpolatedVols['DaysToMaturity'], unit='d') dfInterpolatedVols['Symbol'] = 'SPY' import pandas as pd
Math = 'http://examplezoomlink.com' Design = 'http://examplezoomlink.com' PHE = 'http://examplezoomlink.com' Music = 'http://examplezoomlink.com' IBC = 'http://examplezoomlink.com' ManageBac = 'http://*.managebac.com/' #REPLACE THIS LINK WITH YOUR SCHOOL'S MANAGEBAC LINK Classroom = 'https://classroom.google.com/' Drive = 'https://drive.google.com/' # Add all your holidays here holidays = [] # Assuming 2020-09-14 is Day 1 start_date = '2020-09-11' #Set this to this the first day of your cycle today = np.busday_count(start_date, np.datetime64('today'), holidays=holidays) % 8 + 1 #Set 8 now = datetime.now() t_string = now.strftime("%H:%M") time = re.sub(":","",str(t_string)) time = int(time) print(time) day = datetime.today().weekday() print(day) if day != 2: #NON-WEDNESDAY SCHEDULES Our school has a differently timed schedules on Wednesdays 'Teacher Development'. if 0 < time < 925: #8:00 - 9:25 classes = { 1: 'Humanities', #1st class day 1 2: 'French', #1st class day 2 3: 'Independent', #1st class day 3 4: 'Music', #etc... 5: 'English',
def main(): beginningDate = input(beginningDateString) endingDate = input(endingDateString) difference = np.busday_count(beginningDate, endingDate, weekmask= workingDaysString) print("Excluding weekend days,and NOT counting last day, the difference in working days is :") cprint(str(difference) + " working days", 'green' )
def cout_reel_periode(self, donnees_periode): ''' Calcul le cout reel sur une periode precise compte tenu des heures de presence reelles. donnees_periode (asm.donnees_presence) ''' d = donnees_periode # Coherence des donnees fournies n_jours_prevus = np.busday_count(d.debut, d.fin, weekmask=self.jours_semaine) if n_jours_prevus != len(d.n_heures_jour): err = 'donnees_presence: Il y a {} jours travailles (du {} au {} avec {}j/sem ), et {} horaires fournis.' err = err.format(n_jours_prevus, d.debut, d.fin, self.n_jours_s, len(d.n_heures_jour)) raise NameError(err) if n_jours_prevus != len(d.n_repas_jour): err = 'donnees_presence: Il y a {} jours travailles (du {} au {} avec {}j/sem ), et {} info repas.' err = err.format(n_jours_prevus, d.debut, d.fin, self.n_jours_s, len(d.n_repas_jour)) raise NameError(err) # Analyse des jours de presence un par un garde, entretien, repas = 0, 0, 0 heures_semaine = [[0, False] for i in range((d.N_semaines_completes() + d.N_semaines_incompletes()))] for ijour, (Nheures, AvecRepas) in enumerate( zip(d.n_heures_jour, d.n_repas_jour)): # Decompte des heures par semaine (heures complementaires/supplementaires) isemaine, est_complete = d.indice_semaine_entiere_du_jour( ijour, self.jours_semaine) heures_semaine[isemaine][0] += Nheures heures_semaine[isemaine][1] = est_complete # Une journee prevue non effectuee (ou plus courte) est due dans son entier garde += self.taux_h * self.n_heures_j # Une journee plus longue conduit a un cout supplementaire if Nheures > self.n_heures_j: garde += self.taux_h * (Nheures - self.n_heures_j) # Frais d'entretiens dus uniquement pour les jours de presence if Nheures > 0: entretien += self.frais_entretien_journalier(Nheures) # Repas if AvecRepas: repas += self.frais_repas # Affiche les heures supplementaires for i, (n, complete) in enumerate(heures_semaine): if complete: ncomp = int(n > self.n_heures_j * sum(self.jours_semaine)) * ( n - self.n_heures_j * sum(self.jours_semaine)) nsupp = int(n > 45) * (n - 45) print('semaine {} (complete): {} heures -> {} comp et {} supp'. format(i, n, ncomp, nsupp)) return garde + entretien + repas
import numpy as np # Number of weekdays in January 2011 np.busday_count('2011-01', '2011-02') # Number of weekdays in 2011 np.busday_count('2011', '2012') # Number of Saturdays in 2011 np.busday_count('2011', '2012', weekmask='Sat')
def N_jours_ouvres(self): return np.busday_count(self.debut, self.fin) - self.jours_feries
def get_number_days(dfstart, dfend): # get the number of days not including the weekends A = [d.date() for d in dfstart] B = [d.date() for d in dfend] return np.busday_count(A, B) + 1
def get_current_weekdays_year(date): ''' Retorna la cantidad de dias habiles hasta el anio pasado. ''' return np.busday_count(dt.date(date.year, 1, 1), date) + 2
def days_between(date_from, date_to, calendar): dt = date_to - date_from days = dt.days if calendar else np.busday_count(date_from, date_to) return days + 1
import os from datetime import datetime from numpy import busday_count from flask import Flask, render_template, url_for # Functions ==================================================================== def total_spent(per_day: int = 300_000, cola_year: int = (1412 * 12), strike_start: str = '2020-02-10', today: str = datetime.today().strftime('%Y-%m-%d'), holidays: int = 1, total_grads: int = 1977): days = busday_count(strike_start, today) - holidays total = days * per_day millions = f'{(total / 1e6):.1f}' cola_years = int(round(total / cola_year)) percent_grads = int(round(cola_years / total_grads * 100)) return millions, cola_years, total_grads, percent_grads def create_app(test_config=None): """The application factory function This function creates and configures the Flask application object. For more on application factories, see the Flask documentation/tutorial: http://flask.pocoo.org/docs/1.0/tutorial/factory/
def generateSignal(self, predictModel, featureSelectionFunction, minTrainDays=1800, trainMode='extention', recordModels=True): modelRecord = {} outputPrediction = pd.Series() for predictStartDate, predictEndDate in zip(self.changeHandDates, self.changeHandDates[1:]): # check if we have enough data print('start predict from {} to {}'.format(predictStartDate, predictEndDate)) trainDataDays = np.busday_count( np.datetime64(self.startDate), np.datetime64(predictStartDate.date())) if trainDataDays < minTrainDays: print('We only have {} trainDataDays'.format(trainDataDays)) continue # split the traing and testing set if trainMode == 'extention': trainStartDate = self.startDate elif trainMode == 'rolling': trainStartDate = predictStartDate - pd.Timedelta(minTrainDays, unit='B') X_train, y_train = self.rawXs[ trainStartDate:predictStartDate], self.rawYs[ trainStartDate:predictStartDate] print('train shape (X, y):{}'.format(X_train.shape, y_train.shape)) X_test, y_test = self.rawXs[ predictStartDate:predictEndDate], self.rawYs[ predictStartDate:predictEndDate] print('test shape (X, y):{}'.format(X_test.shape, y_test.shape)) y_predictSeries, model = self.generateOnePeriodSignal(X_train, y_train, X_test, y_test,\ featureSelectionFunction, predictModel) # concat outputs outputPrediction = pd.concat([outputPrediction, y_predictSeries]) # tqdm.write("precision:{}".format(metrics.precision_score(y_true, y_pred))) # tqdm.write("recall:{}".format(metrics.recall_score(y_true, y_pred))) # tqdm.write("f1:{}\n".format(metrics.f1_score(y_true, y_pred))) if recordModels: performance = {} performance.update({ 'precision': metrics.precision_score(y_test, y_predictSeries), 'recall': metrics.recall_score(y_test, y_predictSeries), 'f1_score': metrics.f1_score(y_test, y_predictSeries) }) modelRecord.update({ str(predictStartDate.date()): { 'trainStartDate': trainStartDate, 'predictStartDate': predictStartDate, 'predictEndDate': predictEndDate, 'model': model, 'performance': performance } }) if recordModels: return (outputPrediction, modelRecord) else: return (outputPrediction)
#print(z) sigma = np.std(z) * 252**0.5 #print() print("sigma", sigma) #problem 4 M = 387 hs = 1 ht = 1 / 252 r = 0.05 / 100 K = 385 start = dt.date(2021, 3, 3) end = dt.date(2021, 9, 30) days = np.busday_count(start, end) tend = days / 252 price = np.zeros((M + 1, days + 1)) s = np.array(range(M + 1)) ct = s - K for i in range(len(ct)): if (ct[i] < 0): ct[i] = 0 price[:, days] = ct price = np.matrix(price) a = 1 - (sigma**2) * (s**2) * ht / (hs**2) - r * ht