def test_all_holidays(self): au = sum( holidays.AU(years=[1957, 2012, 2015], prov=p) for p in holidays.AU.PROVINCES) holidays_found = sum((au.get_list(key) for key in au), []) all_holidays = [ "New Year's Day", "Australia Day", "Adelaide Cup", "Canberra Day", "Good Friday", "Easter Saturday", "Easter Sunday", "Easter Monday", "Anzac Day", "Queen's Birthday", "Western Australia Day", "Family & Community Day", "Labour Day", "Eight Hours Day", "May Day", "Picnic Day", "Melbourne Cup", "Christmas Day", "Proclamation Day", "Boxing Day", ] for holiday in all_holidays: self.assertIn(holiday, holidays_found, holiday)
def init_zone_demand_factors(model, zone, timestamp): dt = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') not_holiday = dt.date() not in (holidays.AU( prov=cemo.const.ZONE_DEMAND_PCT.get(zone).get('prov'))) is_peak = 'off peak' if dt.weekday() < 5 and 8 <= dt.hour < 20 and not_holiday: is_peak = 'peak' return cemo.const.ZONE_DEMAND_PCT.get(zone).get(is_peak)
def bsm_call(cls, ticker, expiry, strike, rf=0.0225): '''Calculates option price using the black-scholes model Arguments: ticker {str} -- Underlying ticker in yahoo finance format expiry {str} -- Option expiry date strike {float} -- Strike price Keyword Arguments: rf {float} -- Short-term risk-free rate (default: {0.0225}) Returns: val {float} -- Option valuation from BSM ''' # Compute variables s0 = yf.Ticker(ticker).history(period='1d').Close.sum() vol = cls.historical_vol(ticker) print(s0, vol) expiry = datehandler.to_iso(expiry).date() start = datetime.datetime.today().date() if start.weekday() in [5, 6]: start = start - datetime.timedelta( days=start.weekday()) + datetime.timedelta(days=4) nsw_holidays = holidays.AU(prov='NSW') years = np.busday_count( start, expiry, holidays=nsw_holidays[start:expiry]) / 252 print(years) d1 = (np.log(s0 / strike) + years * (rf + (vol**2) / 2)) / (vol * np.sqrt(years)) d2 = d1 - (vol * np.sqrt(years)) n1 = norm.cdf(d1) n2 = norm.cdf(d2) print((np.log(s0 / strike) + years * (rf + (vol**2) / 2))) print(f'd1 = {d1} | d2 = {d2}') print(f'n1 = {n1} | n2 = {n2}') # Compute value of call option val = s0 * n1 - strike * np.exp(-rf * years) * n2 return val
def load(country, region, observed, expand, years): # Erases existing holiday cache and makes a new one... global dates if country == "US": dates = holidays.US(state=region, observed=observed, expand=expand, years=years) elif country == "CA": dates = holidays.CA(prov=region, observed=observed, expand=expand, years=years) elif country == "MX": dates = holidays.MX(observed=observed, expand=expand, years=years) elif country == "NZ": dates = holidays.NZ(prov=region, observed=observed, expand=expand, years=years) elif country == "AU": dates = holidays.AU(prov=region, observed=observed, expand=expand, years=years) elif country == "AT": dates = holidays.AT(prov=region, observed=observed, expand=expand, years=years) elif country == "DE": dates = holidays.DE(prov=region, observed=observed, expand=expand, years=years) else: print "UNKNOWN COUNTRY ", country
def find_good_epics(): spreads_and_epics = [] i_count = 0 pick_from_epics = [] full_hol_list = [] ################################################################### tz = pytz.timezone('Europe/Berlin') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) ger_today = str(str("GER_" + str(todays_date))) print("Europe/Berlin :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('Europe/London') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) gb_today = str(str("GB_" + str(todays_date))) print("Europe/London :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('America/New_York') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) us_today = str(str("US_" + str(todays_date))) print("America/New_York :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('Australia/Sydney') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) aus_today = str(str("AUS_" + str(todays_date))) print("Australia/Sydney :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('Asia/Tokyo') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) jp_today = str(str("JP_" + str(todays_date))) print("Asia/Tokyo :- Today's Date is ..." + str(todays_date)) ################################################################### b_ger_hol = False b_uk_hol = False b_us_hol = False b_aus_hol = False b_jp_hol = False for date, name in sorted(holidays.DE(years=YEAR_var).items()): full_hol_list.append(str("GER_" + str(date))) for date, name in sorted(holidays.UK(years=YEAR_var).items()): full_hol_list.append(str("GB_" + str(date))) for date, name in sorted(holidays.US(years=YEAR_var).items()): full_hol_list.append(str("US_" + str(date))) for date, name in sorted(holidays.AU(years=YEAR_var).items()): full_hol_list.append(str("AUS_" + str(date))) for date, name in sorted(holidays.JP(years=YEAR_var).items()): full_hol_list.append(str("JP_" + str(date))) full_hol_list = sorted(full_hol_list) for d in full_hol_list: #print (d) if str(d) == ger_today: b_ger_hol = True if str(d) == gb_today: b_uk_hol = True if str(d) == us_today: b_us_hol = True if str(d) == aus_today: b_aus_hol = True if str(d) == jp_today: b_jp_hol = True for epic_id in main_epic_ids: tmp_lst = [] base_url = REAL_OR_NO_REAL + '/markets/' + epic_id auth_r = requests.get(base_url, headers=authenticated_headers) d = json.loads(auth_r.text) try: i_count = i_count + 1 if epic_id.find('MXN') != -1: #print("!!DEBUG!!...skipping, FOUND MXN in..." + str(epic_id)) time.sleep(1) elif epic_id.find('SEK') != -1: #print("!!DEBUG!!...skipping, FOUND SEK in..." + str(epic_id)) time.sleep(1) elif epic_id.find('NOK') != -1: #print("!!DEBUG!!...skipping, FOUND NOK in..." + str(epic_id)) time.sleep(1) elif epic_id.find('CNH') != -1: #print("!!DEBUG!!...skipping, FOUND CNH in..." + str(epic_id)) time.sleep(1) else: b_TRADE_OK = True if b_TRADE_OK: current_bid = d['snapshot']['bid'] ask_price = d['snapshot']['offer'] spread = float(current_bid) - float(ask_price) if float(spread) >= -1.51: # tmp_lst.append(epic_id) # spreads_and_epics.append(tmp_lst) pick_from_epics.append(epic_id) # print ("bid : " + str(current_bid)) # print ("ask : " + str(ask_price)) # print ("-------------------------") # print ("spread : " + str(spread)) # print ("-------------------------") print( "!!DEBUG!!...FOUND GOOD EPIC {} spread {}...{}/{}". format(epic_id, spread, i_count, len(main_epic_ids))) time.sleep(1) else: print( "!!DEBUG!!...skipping, NO GOOD EPIC {} spread {} ....Checking next epic spreads...{}/{}" .format(epic_id, spread, i_count, len(main_epic_ids))) time.sleep(1) continue else: print( "!!DEBUG!!...skipping, NOT CURRENTLY TRADEABLE EPIC {} ....Checking next epic spreads...{}/{}" .format(epic_id, i_count, len(main_epic_ids))) except Exception as e: print(e) pass return (pick_from_epics)
import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.pyplot import figure import holidays from datetime import date import sys from datetime import date import pdb au_holidays = holidays.AU(years=[ 2016, 2017, 2018, 2019 ]) # this gets us a dictionary of dates for holidays in Australia AUHOLIDAYS = list(au_holidays.keys()) def lookatData(): print(rawGSD.head(10)) print(rawGSD.describe()) rawGSD.hist(figsize=(25, 20)) plt.show() def getTop5Categories(): ''' This function gets us the most sold categories from our grocery store dataset ''' vals = rawGSD.groupby(['CATEGORY']).sum().sort_values(['PROFIT'], ascending=False) top5Categories = vals.reset_index().CATEGORY.values[
def judge_local_holiday(self, df): country = df['geoNetwork_country'] date = df['visitId'].apply(lambda x: x.date()) judge_holiday = \ np.where(country.isin( ['United States','India','Canada','Germany', 'Japan','France','Mexico','Australia', 'Spain','Netherlands','Italy','Ireland', 'Sweden','Argentina','Colombia','Belgium', 'Switzerland','Czechia','Colombia','Belgium', 'New Zealand','South Africa','South Africa']),\ np.where((country=='United States')& (date.isin(holidays.US())),1, np.where((country=='India')& (date.isin(holidays.India())),1, np.where((country=='Canada')& (date.isin(holidays.CA())),1, np.where((country=='Germany')& (date.isin(holidays.DE())),1,\ np.where((country=='Japan')& (date.isin(holidays.JP())),1, np.where((country=='France')& (date.isin(holidays.FRA())),1, np.where((country=='Mexico')& (date.isin(holidays.MX())),1, np.where((country=='Australia')& (date.isin(holidays.AU())),1,\ np.where((country=='Spain')& (date.isin(holidays.ES())),1, np.where((country=='Netherlands')& (date.isin(holidays.NL())),1, np.where((country=='Italy')& (date.isin(holidays.IT())),1, np.where((country=='Ireland')& (date.isin(holidays.IE())),1,\ np.where((country=='Sweden')& (date.isin(holidays.SE())),1, np.where((country=='Argentina')& (date.isin(holidays.AR())),1, np.where((country=='Colombia')& (date.isin(holidays.CO())),1, np.where((country=='Belgium')& (date.isin(holidays.BE())),1,\ np.where((country=='Switzerland')& (date.isin(holidays.CH())),1, np.where((country=='Czechia')& (date.isin(holidays.CZ())),1, np.where((country=='Denmark')& (date.isin(holidays.DK())),1, np.where((country=='Austria')& (date.isin(holidays.AT())),1,\ np.where((country=='Hungary')& (date.isin(holidays.HU())),1, np.where((country=='Portugal')& (date.isin(holidays.PT())),1, np.where((country=='Norway')& (date.isin(holidays.NO())),1, np.where((country=='Portugal')& (date.isin(holidays.PT())),1,\ np.where((country=='New Zealand')& (date.isin(holidays.NZ())),1, np.where((country=='South Africa')& (date.isin(holidays.ZA())),1, np.where((country=='South Africa')& (date.isin(holidays.ZA())),1,\ 0))))))))))))))))))))))))))),np.nan).astype(int) return judge_holiday
soldDateList=data1[5].split("/") datetime_object = datetime.strptime(data1[5] , '%d/%m/%Y') ### Checking if its a weekend ################################ day=(calendar.day_name[datetime_object.weekday()]) timeofWeek='' if(day=="Sunday" or day=="Saturday"): timeofWeek="WkEnd" else: timeofWeek="WkDay" ### Checking if its a public holiday ################################ aus_holidays = holidays.AU(prov = 'VIC') # or holidays.US(), or holidays.CountryHoliday('US') dayType="" if date(int(soldDateList[2]), int(soldDateList[1]), int(soldDateList[0])) in aus_holidays: dayType="HliDy" else: dayType="WrkngDy" if len(soldDateList[1])<2: soldDateList[1]="0"+soldDateList[1] if len(soldDateList[0])<2: soldDateList[0]="0"+soldDateList[0] data[6]=soldDateList[2]+soldDateList[1]+soldDateList[0] data[7]=timeofWeek data[8]=shortDay(day) data[9]=dayType
except: pass settings["country_last_updated"] = now settings["country_last"] = country_last settings.flush() return country_last country_holidays = { "CA": holidays.CA(), "CO": holidays.CO(), "MX": holidays.MX(), "US": holidays.US(), "NZ": holidays.NZ(), "AU": holidays.AU(), "DE": holidays.DE(), "AT": holidays.AT(), "DK": holidays.DK(), "UK": holidays.UK(), "IE": holidays.IE(), "ES": holidays.ES(), "CZ": holidays.CZ(), "SK": holidays.SK(), "PL": holidays.PL(), "PT": holidays.PT(), "NL": holidays.NL(), "NO": holidays.NO(), "IT": holidays.IT(), "SE": holidays.SE(), "JP": holidays.JP(),
def find_good_epics(): spreads_and_epics = [] i_count = 0 pick_from_epics = [] full_hol_list = [] ################################################################### tz = pytz.timezone('Europe/Berlin') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) ger_today = str(str("GER_" + str(todays_date))) print("Europe/Berlin :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('Europe/London') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) gb_today = str(str("GB_" + str(todays_date))) print("Europe/London :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('America/New_York') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) us_today = str(str("US_" + str(todays_date))) print("America/New_York :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('Australia/Sydney') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) aus_today = str(str("AUS_" + str(todays_date))) print("Australia/Sydney :- Today's Date is ..." + str(todays_date)) ################################################################### tz = pytz.timezone('Asia/Tokyo') todays_date = str(datetime.datetime.now(tz=tz).strftime('%Y-%m-%d')) jp_today = str(str("JP_" + str(todays_date))) print("Asia/Tokyo :- Today's Date is ..." + str(todays_date)) ################################################################### b_ger_hol = False b_uk_hol = False b_us_hol = False b_aus_hol = False b_jp_hol = False for date, name in sorted(holidays.DE(years=YEAR_var).items()): full_hol_list.append(str("GER_" + str(date))) for date, name in sorted(holidays.UK(years=YEAR_var).items()): full_hol_list.append(str("GB_" + str(date))) for date, name in sorted(holidays.US(years=YEAR_var).items()): full_hol_list.append(str("US_" + str(date))) for date, name in sorted(holidays.AU(years=YEAR_var).items()): full_hol_list.append(str("AUS_" + str(date))) for date, name in sorted(holidays.JP(years=YEAR_var).items()): full_hol_list.append(str("JP_" + str(date))) full_hol_list = sorted(full_hol_list) for d in full_hol_list: #print (d) if str(d) == ger_today: b_ger_hol = True if str(d) == gb_today: b_uk_hol = True if str(d) == us_today: b_us_hol = True if str(d) == aus_today: b_aus_hol = True if str(d) == jp_today: b_jp_hol = True for epic_id in main_epic_ids: tmp_lst = [] base_url = REAL_OR_NO_REAL + '/markets/' + epic_id auth_r = requests.get(base_url, headers=authenticated_headers) d = json.loads(auth_r.text) try: i_count = i_count + 1 if epic_id.find('MXN') != -1: #print("!!DEBUG!!...skipping, FOUND MXN in..." + str(epic_id)) time.sleep(1) elif epic_id.find('SEK') != -1: #print("!!DEBUG!!...skipping, FOUND SEK in..." + str(epic_id)) time.sleep(1) elif epic_id.find('NOK') != -1: #print("!!DEBUG!!...skipping, FOUND NOK in..." + str(epic_id)) time.sleep(1) elif epic_id.find('CNH') != -1: #print("!!DEBUG!!...skipping, FOUND CNH in..." + str(epic_id)) time.sleep(1) else: b_TRADE_OK = False while True: ###################EUROPE############################ ###################EUROPE############################ ###################EUROPE############################ tz = pytz.timezone('Europe/Berlin') now_time = datetime.datetime.now(tz=tz).strftime('%H:%M') #print ("!!DEBUG!! Europe/Berlin:" + str(now_time)) if is_between(str(now_time), ("08:00", "16:00")): #print("!!DEBUG!!...FRANKFURT MARKET OPEN!!") time.sleep(1) STR_CHECK = "EUR" if STR_CHECK in epic_id and b_ger_hol == False: b_TRADE_OK = True break ###################LONDON############################ ###################LONDON############################ ###################LONDON############################ tz = pytz.timezone('Europe/London') now_time = datetime.datetime.now(tz=tz).strftime('%H:%M') while True: if is_between(str(now_time), ("22:00", "22:59")): time.sleep(1) # Sleeping for the tally up hour print("!!DEBUG!! Tally Up hour:" + str(now_time)) now_time = datetime.datetime.now( tz=tz).strftime('%H:%M') else: break #print ("!!DEBUG!! Europe/London:" + str(now_time)) if is_between(str(now_time), ("08:00", "16:00")): #print("!!DEBUG!!...LONDON MARKET OPEN!!") time.sleep(1) STR_CHECK = "GBP" if STR_CHECK in epic_id and b_uk_hol == False: b_TRADE_OK = True break ###################NY############################ ###################NY############################ ###################NY############################ tz = pytz.timezone('America/New_York') now_time = datetime.datetime.now(tz=tz).strftime('%H:%M') #print ("!!DEBUG!! America/New_York:" + str(now_time)) if is_between(str(now_time), ("08:00", "16:00")): #print("!!DEBUG!!...NEW YORK MARKET OPEN!!") time.sleep(1) STR_CHECK = "USD" if STR_CHECK in epic_id and b_us_hol == False: b_TRADE_OK = True break ###################AUS############################ ###################AUS############################ ###################AUS############################ tz = pytz.timezone('Australia/Sydney') now_time = datetime.datetime.now(tz=tz).strftime('%H:%M') #print ("!!DEBUG!! Australia/Sydney:" + str(now_time)) if is_between(str(now_time), ("08:00", "16:00")): #print("!!DEBUG!!...SYDNEY MARKET OPEN!!") time.sleep(1) STR_CHECK = "AUD" if STR_CHECK in epic_id and b_aus_hol == False: b_TRADE_OK = True break ###################TOKYO############################ ###################TOKYO############################ ###################TOKYO############################ tz = pytz.timezone('Asia/Tokyo') now_time = datetime.datetime.now(tz=tz).strftime('%H:%M') #print ("!!DEBUG!! Asia/Tokyo:" + str(now_time)) if is_between(str(now_time), ("08:00", "16:00")): #print("!!DEBUG!!...TOKYO MARKET OPEN!!") time.sleep(1) STR_CHECK = "JPY" if STR_CHECK in epic_id and b_jp_hol == False: b_TRADE_OK = True break break if b_TRADE_OK: current_bid = d['snapshot']['bid'] ask_price = d['snapshot']['offer'] spread = float(current_bid) - float(ask_price) if float(spread) >= -1: # tmp_lst.append(epic_id) # spreads_and_epics.append(tmp_lst) pick_from_epics.append(epic_id) # print ("bid : " + str(current_bid)) # print ("ask : " + str(ask_price)) # print ("-------------------------") # print ("spread : " + str(spread)) # print ("-------------------------") print("!!DEBUG!!...FOUND GOOD EPIC..." + str(i_count) + "/" + str(len(main_epic_ids))) time.sleep(1) else: print( "!!DEBUG!!...skipping, NO GOOD EPIC....Checking next epic spreads..." + str(i_count) + "/" + str(len(main_epic_ids))) time.sleep(1) continue except Exception as e: print(e) pass return (pick_from_epics)
def setUp(self): self.holidays = holidays.AU(observed=True) self.state_hols = { state: holidays.AU(observed=True, prov=state) for state in holidays.AU.PROVINCES }