def test_weekend_is_business_day(self): busdays = [datetime.date(2018, 4, 28)] cal = Calendar(busdays=busdays) next_busday = cal.addbusdays(datetime.date(2018, 4, 27), 1) assert datetime.date(2018, 4, 28) == next_busday next_busday = cal.addbusdays(datetime.date(2018, 4, 27), 2) assert datetime.date(2018, 4, 30) == next_busday prev_busday = cal.addbusdays(datetime.date(2018, 4, 30), -1) assert datetime.date(2018, 4, 28) == prev_busday prev_busday = cal.addbusdays(datetime.date(2018, 4, 30), -2) assert datetime.date(2018, 4, 27) == prev_busday
def main(): """ Main example """ logging.info("--- SETTING UP IDENTIFIERS ---") asset_manager_id = random.randint(1, 2**31 - 1) calendar = Calendar() # This can fail if yesterday was a holiday - need to add a holiday calendar business_date = calendar.addbusdays(date.today(), -2) logging.info("Business Date: %s", business_date) business_date_str = business_date.isoformat() symbols = ['TWTR', 'AAPL', 'RBS.L', 'Z77.SI', '0008.HK'] logging.info("--- PULL MARKET DATA FROM YAHOO FINANCE ---") eod_prices = [] for symbol in symbols: share = Share(symbol=symbol) logging.info("Stock Name: %s", share.get_name()) close = (share.get_historical( start_date=business_date_str, end_date=business_date_str)[0].get('Close')) eod_price = EODPrice(asset_manager_id=asset_manager_id, asset_id=symbol, business_date=business_date, price=Decimal(close)) logging.info("EOD Price: %s", eod_price.price) eod_prices.append(eod_price) logging.info("--- PERSIST PRICES TO AMAAS ---") # Some of these attributes can be derived from the eod_prices - cleanup market_data_interface.persist_eod_prices(asset_manager_id=asset_manager_id, business_date=business_date, eod_prices=eod_prices, update_existing_prices=True)
def trade_period(cls, start, days, holidays=None): """ 计算某个时间x个交易日后的时间,或之前(days为一个负数) :param start: :param days: :return: """ try: holidays = cls.holidays if holidays is None else holidays cal = Calendar(workdays=cls.workdays, holidays=holidays) end = cal.addbusdays(start, days) return end except Exception as e: ExceptionInfo(e) return start
def export_report(request): """ Exporting the monthly attendance report has a PDF format. """ try: if request.method == "GET": cal = Calendar() now = datetime.now() this_year = now.year this_month = now.month date1 = datetime(this_year, this_month, 1) date2 = cal.addbusdays(date1, monthrange(this_year, this_month)[1]) nodw = cal.busdaycount(date1, date2) all_days = range(1, nodw + 1) dict = {} for u in User.objects.all(): c = Attendance.objects.filter(month=now.strftime("%b"), username=u, approved_or_not=True) l = 0 for i in c: l = l + calculate_days(i.from_date, i.to_date) print l dict[u] = nodw - l context = { 'dict': dict, 'this_month': now.strftime("%B"), 'this_year': this_year, 'nodw': nodw, } pdf_filename = '{0}.pdf'.format(now.strftime("%B")) temp_path = os.path.join(settings.PDF_ROOT, pdf_filename) write_pdf('Reports/report.html', context, temp_path) with open(temp_path, 'rb') as fh: response = HttpResponse(fh.read(), content_type="application/pdf") response[ 'Content-Disposition'] = 'inline; filename=' + os.path.basename( temp_path) return response except Exception, e: return HttpResponseRedirect('/error/')
print("Start predicting close prices in the following " + str(timeperiod) + " days.") # Re-train the model with whole dataset, and make a prediction for the last n(timeperiod) days. max_accuracy_model.fit(X, Y) prediction = max_accuracy_model.predict(real_time_test) print("=======================================================") close_prices = real_time_test.close.to_numpy() days = date_series[-timeperiod:].values.astype("datetime64[D]") cal = Calendar() # Stock market does not open during weekends, so we need to add n(timeperiod) business days # representing the future days following_days = [cal.addbusdays(str(day), timeperiod) for day in days] # print out the result for i in range(timeperiod): comparator = '' if prediction[i] == 1: comparator = '>' else: comparator = '<' print("On {date}, {stock}'s close price would be {comparator} {price}" \ .format(date=following_days[i].date(), stock=stock_code, comparator=comparator, price=close_prices[i]) )
from business_calendar import Calendar, MO, TU, WE, TH, FR import datetime date1 = datetime.datetime(2013,1,10) # normal calendar, no holidays cal = Calendar() date2 = cal.addbusdays(date1, 25) print('%s days between %s and %s' % \ (cal.busdaycount(date1, date2), date1, date2)) # don't work on Fridays? no problem! cal = Calendar(workdays=[MO,TU,WE,TH]) date2 = cal.addbusdays(date1, 25) print('%s days between %s and %s' % \ (cal.busdaycount(date1, date2), date1, date2)) # holiday? no problem! cal = Calendar(workdays=[MO,TU,WE,TH], holidays=['2013-01-17']) date2 = cal.addbusdays(date1, 25) print('%s days between %s and %s' % \ (cal.busdaycount(date1, date2), date1, date2))