Beispiel #1
0
def test_default_calendars():
    for name in get_calendar_names():
        if name == "XKRX":
            with pytest.warns(UserWarning):
                assert get_calendar(name) is not None
        else:
            assert get_calendar(name) is not None
Beispiel #2
0
def market_open():
    """
    Checks if markets are open

    Returns:
        True if markets are open
        False if markets are closed
    """
    currentDT = str(datetime.datetime.now().strftime("%Y-%m-%d"))
    futureDT = str(datetime.datetime.now() + datetime.timedelta(days=+2))[:10]
    dt_string = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

    nyse = mcal.get_calendar('NYSE')
    today_nyse = nyse.schedule(start_date=dt_string, end_date=dt_string)
    open_nyse = nyse.open_at_time(today_nyse, pd.Timestamp(dt_string,
                                                           tz='UTC'))
    lse = mcal.get_calendar('LSE')
    today_lse = lse.schedule(start_date=dt_string, end_date=dt_string)
    open_lse = lse.open_at_time(today_lse, pd.Timestamp(dt_string, tz='UTC'))
    jpx = mcal.get_calendar('JPX')
    today_jpx = jpx.schedule(start_date=currentDT, end_date=futureDT)
    open_jpx = jpx.open_at_time(today_jpx, pd.Timestamp(dt_string, tz='UTC'))

    print(f'\nChecking Market hours On: {dt_string} \nMarkets Open:')
    print(f'Japan: {open_jpx}, London: {open_lse}, NY: {open_nyse}')
    print(f'Trading time:{open_jpx or open_lse or open_nyse}')

    return (open_jpx or open_lse or open_nyse)
def test_get_calendar():
    assert isinstance(mcal.get_calendar('NYSE'), NYSEExchangeCalendar)
    cal = mcal.get_calendar('NYSE', datetime.time(10, 0),
                            datetime.time(14, 30))
    assert isinstance(cal, NYSEExchangeCalendar)
    assert cal.open_time == datetime.time(10, 0)
    assert cal.close_time == datetime.time(14, 30)
Beispiel #4
0
def getTradingDates():
    """Get all dates in the range that the NYSE was trading on."""

    print("\nRETRIEVING TRADING DATES...\n")
    # Days NYSE was open
    trainingDates = (pandas_market_calendars.get_calendar('NYSE').valid_days(
        start_date=Constants.trainingStartDate,
        end_date=Constants.trainingEndDate)
    )
    testingDates = (pandas_market_calendars.get_calendar('NYSE').valid_days(
        start_date=Constants.testingStartDate,
        end_date=Constants.testingEndDate)
    )

    # Just save date (YYYY-MM-DD)
    trainingDates = [day.date() for day in trainingDates]
    testingDates = [day.date() for day in testingDates]

    if Constants.missingDate in trainingDates:
        trainingDates.remove(Constants.missingDate)
    if Constants.missingDate in testingDates:
        testingDates.remove(Constants.missingDate)

    pk.dump(trainingDates, open(Constants.dataDir + "trainingDates.p", "wb"))
    pk.dump(testingDates, open(Constants.dataDir + "testingDates.p", "wb"))
    return trainingDates, testingDates
Beispiel #5
0
 def __init__(self,
              entity_type=EntityType.Stock,
              exchanges=['NYSE', 'NASDAQ'],
              entity_ids=None,
              codes=None,
              batch_size=10,
              force_update=False,
              sleeping_time=5,
              default_size=2000,
              real_time=False,
              fix_duplicate_way='add',
              start_timestamp=None,
              end_timestamp=None,
              close_hour=0,
              close_minute=0,
              share_para=None) -> None:
     super().__init__(entity_type,
                      exchanges,
                      entity_ids, ['A'],
                      batch_size,
                      force_update,
                      sleeping_time,
                      default_size,
                      real_time,
                      fix_duplicate_way,
                      start_timestamp,
                      end_timestamp,
                      close_hour,
                      close_minute,
                      share_para=share_para)
     self.nyse = mcal.get_calendar('NYSE')
     self.nasdaq = mcal.get_calendar('NASDAQ')
def test_get_calendar():
    assert isinstance(mcal.get_calendar('NYSE'), NYSEExchangeCalendar)
    cal = mcal.get_calendar('NYSE', datetime.time(10, 0),
                            datetime.time(14, 30))
    assert isinstance(cal, NYSEExchangeCalendar)
    assert cal.open_time == datetime.time(10, 0)
    assert cal.close_time == datetime.time(14, 30)

    # confirm that import works properly
    cal = mcal.get_calendar('CME')
Beispiel #7
0
    def read(cls, rootdir):
        path = cls.metadata_path(rootdir)
        with open(path) as fp:
            raw_data = json.load(fp)

            try:
                version = raw_data['version']
            except KeyError:
                # Version was first written with version 1, assume 0,
                # if version does not match.
                version = 0

            default_ohlc_ratio = raw_data['ohlc_ratio']

            if version >= 1:
                minutes_per_day = raw_data['minutes_per_day']
            else:
                # version 0 always assumed US equities.
                minutes_per_day = US_EQUITIES_MINUTES_PER_DAY

            if version >= 2:
                calendar = get_calendar(raw_data['calendar_name'])
                start_session = pd.Timestamp(raw_data['start_session'],
                                             tz='UTC')
                end_session = pd.Timestamp(raw_data['end_session'], tz='UTC')
            else:
                # No calendar info included in older versions, so
                # default to NYSE.
                calendar = get_calendar('XNYS')

                start_session = pd.Timestamp(raw_data['first_trading_day'],
                                             tz='UTC')
                end_session = calendar.minute_to_session_label(
                    pd.Timestamp(raw_data['market_closes'][-1],
                                 unit='m',
                                 tz='UTC'))

            if version >= 3:
                ohlc_ratios_per_sid = raw_data['ohlc_ratios_per_sid']
                if ohlc_ratios_per_sid is not None:
                    ohlc_ratios_per_sid = keymap(int, ohlc_ratios_per_sid)
            else:
                ohlc_ratios_per_sid = None

            return cls(
                default_ohlc_ratio,
                ohlc_ratios_per_sid,
                calendar,
                start_session,
                end_session,
                minutes_per_day,
                version=version,
            )
Beispiel #8
0
def plotPrediction(model, prices, scaled, start_date, hist_size, predict_size, mean, std, instrument = ['Equity', 'Future', 'Crypto'][0]):
    if instrument == 'Equity':
        nyse = mcal.get_calendar('NYSE').regular_holidays
        holidays = [x.strftime("%Y-%m-%d") for x in nyse.holidays(start='2010-01-01', end='2023-12-31').to_list()]
        beginDate_hist = np.busday_offset(start_date, -hist_size, holidays=holidays)
        endDate_hist = np.busday_offset(start_date, -1, holidays=holidays)

        endDate_future = np.busday_offset(start_date, predict_size - 1, holidays=holidays)
        dates_future = [np.busday_offset(start_date, i, holidays=holidays) for i in range(0, predict_size)]
    elif instrument == 'Future':
        weekdays = "Mon Tue Wed Thu Fri Sun"
        cme = mcal.get_calendar('CME').regular_holidays
        holidays = [x.strftime("%Y-%m-%d") for x in cme.holidays(start='2010-01-01', end='2023-12-31').to_list()]
        beginDate_hist = np.busday_offset(start_date, -hist_size, holidays=holidays, weekmask=weekdays)
        endDate_hist = np.busday_offset(start_date, -1, holidays=holidays, weekmask=weekdays)

        endDate_future = np.busday_offset(start_date, predict_size - 1, holidays=holidays, weekmask=weekdays)
        dates_future = [np.busday_offset(start_date, i, holidays=holidays, weekmask=weekdays) for i in range(0, predict_size)]
    else:
        beginDate_hist = start_date - np.timedelta64(hist_size, 'D')
        endDate_hist = start_date - np.timedelta64(1, 'D')

        endDate_future = start_date + np.timedelta64(predict_size - 1, 'D')
        dates_future = np.arange(start_date, start_date + np.timedelta64(predict_size, 'D'), step=np.timedelta64(1, 'D'))

    # get scaled prices of history
    past_scaled = scaled.loc[beginDate_hist: endDate_hist]
    past_tensor = tf.convert_to_tensor(np.array(past_scaled).reshape(1, past_scaled.shape[0], past_scaled.shape[1]),
                                       dtype=tf.float32)

    # make predictions
    predicted = np.array(model.predict(past_tensor)[0])
    predicted_price = [(x * std) + mean for x in predicted]

    # plot
    trueHistClose = prices.loc[beginDate_hist:endDate_hist][['Close']].rename(columns = {'Close': 'Trailing Price'})
    trueClose = prices.loc[endDate_hist:endDate_future][['Close']].rename(columns = {'Close': 'True Price'})
    predClose = pd.DataFrame.from_dict({'date': dates_future, 'Predicted Price': predicted_price}).set_index('date')
    joined = pd.concat([trueHistClose, trueClose, predClose], axis=1)
    joined.loc[endDate_hist, 'Predicted Price'] = joined.loc[endDate_hist, 'True Price']

    plt.figure()
    joined.plot(figsize=(12,6),
                title='Predictions: {} - {}'.format(dates_future[0], dates_future[-1]),
                )
    plt.xlabel('Date')
    plt.ylabel('Price ($)')
    plt.show()

    return joined, predicted_price
 def get_market_days(self, period=relativedelta(years=1)):
     #will return a list of previous in perioddays that the market has been open up to the last valid day
     x = mcal.get_calendar('NYSE').schedule(
         start_date=datetime.now().date() - period,
         end_date=datetime.strptime(self.get_last_complete_market_day(),
                                    '%Y-%m-%d'))
     return x.index.strftime('%Y-%m-%d')  #return everything as strings
def current_valuation(value_date):
    spy_data = spy_data_df()

    '''Use value date as start date for model. Use mcal library to pull dates. Given start date, pulls trading days for entire year then chops so that only
     24 trading days are utlized. Craft binomial tree from start date quant features.'''
    end_date_year = str(value_date.year + 1)
    end_date = pd.to_datetime(str(end_date_year + '/12/31'))
    nyse = mcal.get_calendar('NYSE')
    dates = [date.replace(tzinfo = None) for date in nyse.valid_days(start_date = value_date, end_date = end_date)[:24]]

    valuation_tree_df = pd.DataFrame(columns = [dates], index = [i for i in range(24)])
    valuation_tree_df[valuation_tree_df.columns[0]][0] = spy_data['Close'].loc[spy_data['Date'] == dates[0]].values[0]

    for i in range(24):
        if i == 0: continue
        up_move_price = valuation_tree_df[valuation_tree_df.columns[i-1]][0]*spy_data['Up Move'].loc[spy_data['Date'] == dates[0]].values[0]
        valuation_tree_df[valuation_tree_df.columns[i]][0] = up_move_price
        for k in range(i):
            down_move_price = valuation_tree_df[valuation_tree_df.columns[i-1]][k]*spy_data['Down Move'].loc[spy_data['Date'] == dates[0]].values[0]
            valuation_tree_df[valuation_tree_df.columns[i]][k+1] = down_move_price

    prob = []
    for i in range(len(valuation_tree_df[valuation_tree_df.columns[-1]])):
        prob.append(binom.pmf(23-i, len(valuation_tree_df[valuation_tree_df.columns[-1]])-1, spy_data['P(U)'].loc[spy_data['Date'] == dates[0]].values[0]))
    prob_node_df = pd.DataFrame({'Predicted Price': valuation_tree_df[valuation_tree_df.columns[-1]], 'Probability': prob})
    prob_node_df['Weighted Value'] = prob_node_df['Predicted Price']*prob_node_df['Probability']

    return(prob_node_df['Weighted Value'].sum())
Beispiel #11
0
    def get_all_bars(self, ticker_symbol, start_date, end_date):
        # get a list of market opens and closes for each trading day from 2015 onwards
        trading_days = mcal.get_calendar('NYSE').schedule(start_date, end_date)

        # initialize an empty list of all bars
        all_bars = []

        # for each day in our list of trading days...
        for i in range(len(trading_days)):

            # get the time at the start of the request
            request_time = time.time()

            # get the list of bars for the next day
            next_bars = self.get_date_bars(ticker_symbol,
                                           trading_days['market_open'][i],
                                           trading_days['market_close'][i])

            # print a log statement
            #TODO: Change to human readable format

            print(
                f'Got bars for {pd.to_datetime(next_bars[-1]["t"], unit="s")}')

            # add the next bars to our growing list of all bars
            all_bars += next_bars

            # sleep to ensure that no more than 200 requests occur per 60 seconds
            time.sleep(max(request_time + 60 / 200 - time.time(), 0))

        # return the list of all bars
        return all_bars
Beispiel #12
0
 def test_filter_days(self):
     provider = CsvFileDataProvider(["data"])
     nyse = mcal.get_calendar('NYSE')
     days = nyse.valid_days(start_date='2000-01-01', end_date='2017-01-01')
     daily = provider.get_dataframes(
         [SymbolData('DIS', 'day', 'day', '2000-01-01', '2017-01-01')])[0]
     assert len(days) == len(daily)
Beispiel #13
0
    def check_latest_data_sqlite():
        update = False

        # see if any db exists
        path = os.path.expanduser(DB_LOC)
        db_path = os.path.join(path, DB_NAME)
        if os.path.exists(db_path):
            conn = sqlite3.connect(db_path)
            latest = get_latest_data_sqlite(conn)
            conn.close()

            # stonks market calendar
            now = pd.datetime.now(pytz.timezone('US/Eastern'))
            ndq = mcal.get_calendar('NASDAQ').schedule('2010-01-01', now)
            ndq.market_close = ndq.market_close.dt.tz_convert('US/Eastern')

            # if the latest trading day is after the latest date we have,
            # and we are past the last day's close, update 
            if ndq.index.max().date() > latest.index[-1].date():
                if ndq.iloc[-1].market_close < now:
                    update = True
        else:
            update = True
        
        return update
Beispiel #14
0
 def __init__(self, otype, S0, K, q=0, marketPrice=None, T=None, expDay=None, vol=None, r=0.025, ls='Long'):
     self.S0=S0
     self.K=K
     self.r=r
     self.otype=otype.title()
     self.daysInYear = 252
     self.q=q
     self.ls = ls
     if T is None and expDay is None:
         print('Please enter days to expiry or expiration day')
     elif T is None:
         self.expDayStr = expDay
         expDay = np.datetime64(expDay)
         nyse = mcal.get_calendar('NYSE')
         self.T= (np.busday_count(np.datetime64('today'), expDay, holidays=nyse.holidays().holidays)+1)/self.daysInYear
     else:
         self.T=T
     
     if marketPrice is None and vol is None:
         print('Please enter either market price or implied vol of option')
     elif marketPrice is None:
         self.vol=vol
         self.marketPrice=self.price()
     elif vol is None:
         self.marketPrice=marketPrice
         self.vol=self.IV()
Beispiel #15
0
def test_date_range_lower_freq():
    cal = mcal.get_calendar("NYSE")
    schedule = cal.schedule(pd.Timestamp('2017-09-05 20:00', tz='UTC'),
                            pd.Timestamp('2017-10-23 20:00', tz='UTC'))

    # cannot get date range of frequency lower than 1D
    with pytest.raises(ValueError) as e:
        mcal.date_range(schedule, frequency='3D')
    assert e.exconly(
    ) == "ValueError: Frequency must be 1D or higher frequency."

    # instead get for 1D and convert to lower frequency
    short = mcal.date_range(schedule, frequency='1D')
    actual = mcal.convert_freq(short, '3D')
    expected = pd.date_range('2017-09-05 20:00',
                             '2017-10-23 20:00',
                             freq='3D',
                             tz='UTC')
    assert_index_equal(actual, expected)

    actual = mcal.convert_freq(short, '1W')
    expected = pd.date_range('2017-09-05 20:00',
                             '2017-10-23 20:00',
                             freq='1W',
                             tz='UTC')
    assert_index_equal(actual, expected)
Beispiel #16
0
    def __init__(self, start=None, end=None):
        all_minute_loc = glob.glob(f'/home/dewe/samgame/datasets/minute/*')
        self.sym_dict = {
            s.split('\\')[-1].split('_')[0]: s
            for s in all_minute_loc
        }
        if start and end:
            nyse = mcal.get_calendar('NYSE')
            early = nyse.schedule(start_date=start, end_date=end)
            full_date_range = mcal.date_range(early,
                                              frequency='1min').tz_convert(NY)
            self.full_date_range = full_date_range
            with open(
                    f'/home/dewe/samgame/datasets/dates_{start.year}_{end.year}.pkl',
                    'wb') as pkl:
                pickle.dump(full_date_range, pkl)
        else:
            with open(f'/home/dewe/samgame/datasets/dates_2004_2020.pkl',
                      'rb') as pkl:
                self.full_date_range = pickle.load(pkl)

        self.all_syms = list(self.sym_dict.keys())
        self.live_data = {}
        self.tech_indicators = None
        self.done = False
def create_market_cal(start, end):
    nyse = mcal.get_calendar('NYSE')
    schedule = nyse.schedule(stocks_start, stocks_end)
    market_cal = mcal.date_range(schedule, frequency='1D')
    market_cal = market_cal.tz_localize(None)
    market_cal = [i.replace(hour=0) for i in market_cal]
    return market_cal
def get_business_date_offset(business_date: date, days_offset: int):
    '''
        Returns the business date offest by 'days_offset' business date.

        For example, the day before the observed 4th of July will return
        the following Monday:

        (2020/07/02, 1) -> 2020/07/06

        if the business date is not valid, the method will throw a ValidationError
    '''

    nyse_cal = mcal.get_calendar('NYSE')

    if days_offset > 0:
        market_calendar = nyse_cal.schedule(
            business_date,
            business_date + timedelta(days=int(days_offset * 1.5)))
    else:
        market_calendar = nyse_cal.schedule(
            business_date + timedelta(days=int(days_offset * 1.5)),
            business_date)
        # reverse the calendar order
        market_calendar = market_calendar.iloc[::-1]

    # if business_date is not valid, raise an exception
    try:
        market_calendar.index.get_loc(business_date)
    except Exception as e:
        raise ValidationError(
            "Cannot offset %s by %d days because %s is not a valid business date"
            % (business_date, days_offset, business_date), e)

    return market_calendar.index[abs(days_offset)].to_pydatetime().date()
Beispiel #19
0
    def get_market_schedule(self, market: str) -> MarketSchedule:
        tz = pytz.timezone(self.local_tz)
        exch_calendar = mcal.get_calendar(market)
        schedule_df = exch_calendar.schedule(
            self.scheduler.get_clock().get_start_time(tz),
            self.scheduler.get_clock().get_end_time(tz))

        class NullEvent(Event):
            def on_activate(self) -> bool:
                return True

        market_open = NullEvent()
        market_close = NullEvent()
        for index, row in schedule_df.iterrows():
            market_open_dt = row['market_open'].astimezone(tz)
            market_close_dt = row['market_close'].astimezone(tz)

            self.scheduler.get_network().attach(market_open)
            self.scheduler.get_network().attach(market_close)
            self.scheduler.schedule_event_at(
                market_open, Clock.to_millis_time(market_open_dt))
            self.scheduler.schedule_event_at(
                market_close, Clock.to_millis_time(market_close_dt))

        return MarketSchedule(schedule_df, market_open, market_close)
Beispiel #20
0
def market_open(now=None):
    """Is the market open?"""
    nyse = mcal.get_calendar('NYSE')
    if now is None:
        now = arrow.now('America/New_York')
    schedule = nyse.schedule(start_date=now.datetime, end_date=now.datetime)
    return nyse.open_at_time(schedule, now.datetime)  # Market currently open
Beispiel #21
0
def get_business_days_list(startdate, enddate):   
    nyse = mcal.get_calendar('NYSE')
    business_days = nyse.valid_days(start_date=startdate, end_date=enddate).strftime('%Y-%m-%d')
    date_list = []
    for date in range(0, len(business_days) - 1):
        date_list.append(business_days[date])  
    return date_list
Beispiel #22
0
def expected_index(df, exchange='nyse'):
    exchange_dates = mcal.get_calendar(name=exchange.upper())

    exchange_dates = exchange_dates.schedule(start_date=df.index[0],
                                             end_date=df.index[-1])

    return exchange_dates.index
Beispiel #23
0
def generate_data(ticker_list, start_date='2000-01-01', end_date='2010-01-01'):
    # Create a calendar
    nyse = mcal.get_calendar('NYSE')
    dates = nyse.schedule(start_date=start_date, end_date=end_date)
    dates['date'] = dates['market_open'].dt.date

    df = pd.DataFrame(index=range(len(dates['date'])))
    df = df.reset_index()
    df.index = dates['date']

    for ticker in ticker_list:
        a = np.random.uniform(1, 1000)
        b = np.random.uniform(0.05, 1)
        c = np.random.uniform(0.1, .4)
        d = np.random.uniform(0.1, (a - b) / 20)
        e = np.random.uniform(-0.03, 0.08)
        f = np.random.uniform(10, 25)
        g = np.random.uniform(0.003, 0.008)
        shift1 = np.random.uniform(0, 2 * math.pi)
        shift2 = np.random.uniform(0, 2 * math.pi)

        df[ticker] = np.maximum(
            a + b * np.sin(c * df['index'] + shift1) +
            np.random.normal(loc=0, scale=d, size=len(df['index'])) +
            e * df['index'] + f * np.sin(g * df['index'] + shift2), 0.01)

    df = df[df.columns[1:]]  # get rid of index col

    return df
Beispiel #24
0
def non_market_days_removal(ts, calendar_name='NYSE'):
    """
    This function removes week end and holidays from time series index
    :param ts: input time series with daily frequency
    :param calendar_name: a calendar name from the list at
    https://pandas-market-calendars.readthedocs.io/en/latest/calendars.html
    :return: a time series where all the days are all market days according to selected calendar
    """

    # retrieving calendar
    calendar = mcal.get_calendar(calendar_name)
    # retrieving calendar schedule
    schedule = calendar.schedule(start_date=ts.index[0], end_date=ts.index[-1])
    # fixing index within the the date range
    ts.index = calendar.date_range(schedule, frequency='1D')
    return ts

    for i, d in enumerate(ts):
        week_day_num = d.index.day().weekday()

        if week_day_num > 5:
            delta = datetime.timedelta((8 - week_day_num))
            ts.index[i] = d.index + delta
            already_used_days.append(d.index[i])
        else:
            ts.index[i], already_used_days = adjust_week_day(ts.index[i], already_used_days)

    return ts
Beispiel #25
0
def get_data(config, stock_file_name='000002.SS'):
    data_dir = config['data_dir']
    start_time = config['start_time']
    end_time = config['end_time']
    time_col = config['time_col']
    time_col = time_col[0]
    data_file_path = f'{data_dir}/{stock_file_name}.csv'

    if os.path.exists(data_file_path):
        df_org = pd.read_csv(data_file_path, parse_dates=[time_col])
        df_org = df_org.sort_values('<DTYYYYMMDD>')
        df_org = df_org.set_index('<DTYYYYMMDD>')
        df_org = df_org.tz_localize(None)

        # Do fill missing day
        stock_calendar = mcal.get_calendar('stock')
        stock_time = stock_calendar.valid_days(
            start_date=df_org.index.values[0],
            end_date=df_org.index.values[-1])
        stock_time.tz_localize(None)
        df_time = pd.DataFrame((stock_time), columns=['<DTYYYYMMDD>'])
        df_time = df_time.sort_values('<DTYYYYMMDD>')
        df_time = df_time.set_index('<DTYYYYMMDD>')
        df_time = df_time.tz_convert(None)

        df_org = df_org.join(df_time, how='right')
        df_org = df_org.fillna(method='backfill')
        # df_org = df_org[np.logical_and(df_org[time_col].dt.to_pydatetime() >= config['start_time'], df_org[time_col].dt.to_pydatetime() <= config['end_time'])]
    else:
        return None

    df_org = df_org.sort_values(time_col)
    df_org.reset_index(inplace=True)

    return df_org
Beispiel #26
0
def validateWorkingDays(startDate, endDate,companyTicker,company_name):
    
   nyse = mcal.get_calendar('NYSE')
   isValidWorkingDays = False 
   isvaliddate = nyse.valid_days(startDate, endDate)
   
   if ((abs(endDate-startDate).days)) <= 1:
       start_Day = calendar.day_name[startDate.weekday()]
       
       end_Day = calendar.day_name[endDate.weekday()]
       
       if( (start_Day == "Saturday" and end_Day=="Sunday") or (start_Day == "Saturday" and end_Day=="Saturday") or (start_Day == "Sunday" and end_Day=="Sunday")):
           print("The Day of end Date:", end_Day) 
           print("The Day of start Date:", start_Day)
          
           isValidWorkingDays = False
       elif((pd.Timestamp(startDate) not in isvaliddate) and (pd.Timestamp(endDate) not in isvaliddate)):
           isValidWorkingDays = False
       else:
           isValidWorkingDays =  True
   else:
     isValidWorkingDays = True
  
   if(isValidWorkingDays):
       getdata(startDate,endDate,companyTicker,company_name)
Beispiel #27
0
    def load_bar_data(
            self,
            symbol: str,
            exchange: Exchange,
            interval: Interval,
            start: datetime,
            end: datetime,
        ) -> Sequence[BarData]:
        calendar_name = EXCHANGE_CALENDAR[exchange]
        calendar = get_calendar(calendar_name)
        start_date = pd.Timestamp(start).tz_convert(pytz.utc)
        end_date = pd.Timestamp(end).tz_convert(pytz.utc)
        # start_session = minute_to_session_label(calendar, start)
        # end_session = minute_to_session_label(calendar, end)
        pair = get_date_range(calendar, start_date, end_date)
        final_data = []
        fields = self.class_bar.fields
        for p in pair:
            year = p[0].year
            try:
                meta = self.bcolz_meta.get(self.bcolz_meta.symbol==symbol, self.bcolz_meta.exchange==exchange.value, self.bcolz_meta.year==year)
                meta_inst = BcolzMinuteBarMetadata.open(meta)
                reader = BcolzMinuteBarReader(self.store_path, meta_inst)
                start_session = p[0] if p[0] >= reader.first_dt() else reader.first_dt()
                end_session = p[1] if p[1] < reader.last_dt() else reader.last_dt()

                result = reader.load_raw_arrays(fields, start_session, end_session, [meta.sid])
                print(result)
            except Exception as e:
                print(e)

        return final_data
Beispiel #28
0
    def __init__(self, chains):
        self.logger = logging.getLogger(__name__)

        # Internal Strategy Variables
        self.date = self.datas[0].datetime.date  # Reference
        self.order = None  # To keep track of pending orders
        # Future Chains
        self.chains = chains
        self.names = {}
        for idx, fc in enumerate(self.chains):
            names = [n for n in self.getdatanames() if n[:2] == fc.stem]
            self.chains[idx] = fc.filter_chain(names)
            self.names[fc] = names
        self.log(logging.INFO, 'Symbols per chain: {}'.format(self.names))
        # Internals
        self.calendars = {}
        self.exits = {}
        for fc in self.chains:
            # Calendar
            start, end = fc.get_start_end(extra_days=True)
            # TODO: Get proper exchange! NYSE covers all american holidays!
            self.calendars[fc] = pmc.get_calendar('NYSE').valid_days(
                start, end)
            # Exits
            self.exits[fc] = ore.get_dates(ore.Events.LastDay,
                                           self.calendars[fc], fc.chain, -2)
Beispiel #29
0
def check_if_today_trading_day():
    today_ny = datetime.datetime.now(pytz.timezone('America/New_York'))
    ndq = mcal.get_calendar('NASDAQ')
    open_days = ndq.schedule(start_date=today_ny -
                             pd.Timedelta(str(365) + ' days'),
                             end_date=today_ny)
    return open_days.index[-1].date() == today_ny.date()
Beispiel #30
0
def create_day_start_df(date, minutes):

    # get data path
    data_prefix = get_stonk_project_path() + '\\data\\raw_data_'

    # get previous trading day
    nyse = mcal.get_calendar('NYSE')
    dates = [
        date.strftime('%Y-%m-%d')
        for date in nyse.schedule(start_date='2020-08-01',
                                  end_date='2020-08-27').index
    ]
    previous_date = dates[dates.index(date) - 1]

    output_df = pd.DataFrame()
    for minute in range(390 - minutes, 390):

        # append a minute of data to the output
        minute_df = pd.read_parquet(data_prefix + previous_date + '\\Minute=' +
                                    str(minute))
        output_df = output_df.append(minute_df)

    # append the first minute of today
    minute_df = pd.read_parquet(data_prefix + date + '\\Minute=0')
    output_df = output_df.append(minute_df)

    return (output_df)
Beispiel #31
0
    def __init__(self):
        self._calendar = self.p.calendar

        if isinstance(self._calendar, string_types):  # use passed mkt name
            import pandas_market_calendars as mcal
            self._calendar = mcal.get_calendar(self._calendar)

        import pandas as pd  # guaranteed because of pandas_market_calendars
        self.dcache = pd.DatetimeIndex([0.0])
        self.idcache = pd.DataFrame(index=pd.DatetimeIndex([0.0]))
        self.csize = timedelta(days=self.p.cachesize)