def load_dme_data(self):
        if not config.dme_pre_load_data:
            x_test = np.empty((2, 4 * 24 * config.coverage))
            for link in os.listdir(config.dme_root_files):
                link_name = link.split('_')[0]
                link_type = link.split('_')[1]
                print("now processing link: {} of type: {}".format(
                    link_name, link_type))
                df = pd.read_csv(config.dme_root_files + '/' + link)
                init_date = config.dme_scrape_config['link_objects']['date'][
                    'value']
                time_value = dt.strptime(
                    f"{init_date['yyyy']}-{init_date['mm']}-{init_date['dd']} 00:00:00",
                    "%Y-%m-%d %H:%M:%S") + dt_delta(days=1)

                df_min_header = 'PowerRLTMmin' if link_type == 'SINK' else 'PowerTLTMmin'
                df_max_header = 'PowerTLTMmax' if link_type == 'SOURCE' else 'PowerRLTMmax'
                min_max_values = np.empty(2)
                for row_min, row_max, row_time, row_interval in zip(
                        list(df[df_min_header]), list(df[df_max_header]),
                        list(df.Time), list(df.Interval)):

                    if row_interval != 24:

                        while time_value < dt.strptime(row_time,
                                                       "%Y-%m-%d %H:%M:%S"):
                            min_max_values = np.vstack(
                                (min_max_values, np.array([np.nan, np.nan])))
                            time_value = dt.strptime(
                                row_time,
                                "%Y-%m-%d %H:%M:%S") + dt_delta(minutes=15)

                        min_max_values = np.vstack(
                            (min_max_values, np.array([row_min, row_max])))

                        time_value = dt.strptime(
                            row_time,
                            "%Y-%m-%d %H:%M:%S") + dt_delta(minutes=15)

                min_max_values = min_max_values[1:].T

                if min_max_values.shape == (2, 4 * 24 * config.coverage):
                    x_test = np.vstack((x_test, min_max_values))
                else:
                    print('dim missmatch: {}'.format(min_max_values.shape))

            x_test = x_test[2:]

            with open(config.dme_root_values + '/' + 'dme_values.pkl',
                      'wb') as f:
                pickle.dump(x_test, f)

            return x_test

        else:
            with open(config.dme_root_values + '/' + 'dme_values.pkl',
                      'rb') as f:
                x_test = pickle.load(f)
            return x_test
Exemple #2
0
 def equalDayWeek(date_a, date_b, interval):
     """
     Compare if two dates are in the same week
     """
     if interval == "day":
         return date_a == date_b
     monday_a = date_a - dt_delta(days=date_a.weekday())
     monday_b = date_b - dt_delta(days=date_b.weekday())
     return monday_a == monday_b
Exemple #3
0
    def __returnTodayStockHistory(self, symbol):
        """
        Return today's stock history for a givin symbol
        """
        expected_date = self.__end_datetime.date()

        # 5minute, 10minute, hour
        interval = "10minute"
        delta = {"5minute": 5, \
                 "10minute": 10, \
                 "hour": 60}

        stock_history = {}
        trade_date = None

        stock_historicals = rs.get_stock_historicals(symbol,
                                                     span="day",
                                                     interval=interval)
        for stock_historical in stock_historicals:
            begins_datetime_est = dt_parse(
                stock_historical["begins_at"]).astimezone(self.__time_zone)
            trade_info = {"start_time": begins_datetime_est, \
                          "end_time": begins_datetime_est + dt_delta(minutes=delta[interval]), \
                          "open_price": stock_historical["open_price"], \
                          "close_price": stock_historical["close_price"] }

            trade_date = begins_datetime_est.date()
            if trade_date in stock_history:
                # stock_history[trade_date]
                if trade_info["start_time"] < stock_history[trade_date][
                        "start_time"]:
                    stock_history[trade_date]["start_time"] = trade_info[
                        "start_time"]
                    stock_history[trade_date]["open_price"] = trade_info[
                        "open_price"]
                if trade_info["end_time"] > stock_history[trade_date][
                        "end_time"]:
                    stock_history[trade_date]["end_time"] = trade_info[
                        "end_time"]
                    stock_history[trade_date]["close_price"] = trade_info[
                        "close_price"]
            else:
                stock_history[trade_date] = trade_info

        return stock_history[trade_date]
Exemple #4
0
def format_for_psb(player, args):
    all_weeks = list()
    date = None
    if args:
        date = tools.date_parser(" ".join(args))
    if not date:
        date = dt.now(tz.utc)
    req_date = date.strftime("%Y-%m-%d")

    date = date + dt_delta(weeks=1)
    start, end = get_previous_week(date)
    all_weeks.append(PsbWeekUsage(player, 0, start, end))
    date = start

    for i in range(8):
        start, end = get_previous_week(date)
        all_weeks.append(PsbWeekUsage(player, i + 1, start, end))
        date = start

    return req_date, all_weeks
Exemple #5
0
def get_previous_week(date):
    iso_date = date.isocalendar()
    end = dt.combine(dt_date.fromisocalendar(iso_date[0], iso_date[1], 1),
                     dt_time(tzinfo=tz.utc))
    start = end - dt_delta(weeks=1)
    return start, end
from datetime import date as dt_date
from datetime import timedelta as dt_delta
import queries

this_week = (dt_date.today() - dt_delta(days=dt_date.today().weekday()))
this_week_str = (dt_date.today() - dt_delta(days=dt_date.today().weekday())).strftime("%m-%d-%Y")
next_week_str = (this_week + dt_delta(weeks=1)).strftime("%m-%d-%Y")

this_week = getattr(__import__('config.allotment', fromlist=[this_week_str]), this_week_str)
next_week = getattr(__import__('config.allotment', fromlist=[next_week_str]), next_week_str)


class Week:

    def __init__(self, appointments):
        """ Appointments is an array of the # of appointments for each
            available time slot.
            Starts with tomorrow.
        """
        week_array = []

        # creating array of appointments
        for i in range(7):      # first week
            week_array.append([])
            for j in range(24):
                week_array[i].append([])
                for k in range(4):
                    week_array[i][j].append(this_week.week[i][j])
        for i in range(7, 14):   # second week
            week_array.append([])
Exemple #7
0
    def __returnDayWeekStockHistory(self, symbol, offset, interval):
        """
        Return stock history for a givin day or week.
        """
        def returnSpan(offset, interval):
            """
            docstring
            """
            if interval == "day":
                if offset <= 7:
                    return "week"
                elif offset <= 27:
                    return "month"
                elif offset <= 90:
                    return "3month"
                elif offset <= 365:
                    return "year"
                elif offset <= 1780:
                    return "5year"
                else:
                    raise ValueError(
                        "offset is in wrong range (expect >= 0 and <= 1780).")
            elif interval == "week":
                if offset <= 4:
                    return "month"
                elif offset <= 12:
                    return "3month"
                elif offset <= 52:
                    return "year"
                elif offset <= 260:
                    return "5year"
                else:
                    raise ValueError(
                        "offset is in wrong range (expect >= 0 and <= 260).")

        def equalDayWeek(date_a, date_b, interval):
            """
            Compare if two dates are in the same week
            """
            if interval == "day":
                return date_a == date_b
            monday_a = date_a - dt_delta(days=date_a.weekday())
            monday_b = date_b - dt_delta(days=date_b.weekday())
            return monday_a == monday_b

        if interval != "week" and interval != "day":
            raise ValueError("interval is in wrong, expected day or week.")

        offset = abs(offset)
        interval_day = 1
        if interval == "week":
            interval_day = 7

        expected_date = (self.__end_datetime -
                         dt_delta(days=offset * interval_day)).date()

        expected_historical = None
        stock_historicals = rs.get_stock_historicals(symbol,
                                                     span=returnSpan(
                                                         offset, interval),
                                                     interval=interval)
        for stock_historical in stock_historicals:
            # Fix UTC to EST
            begins_date = dt_parse(stock_historical["begins_at"]).astimezone(
                self.__time_zone) + dt_delta(minutes=870)
            stock_historical["begins_at"] = begins_date
            stock_historical["ends_at"] = begins_date + dt_delta(minutes=390)
            if interval == "week":
                stock_historical["ends_at"] += dt_delta(days=4 -
                                                        begins_date.weekday())
            if equalDayWeek(begins_date.date(), expected_date, interval):
                expected_historical = stock_historical

        return expected_historical