示例#1
0
    def test_get_price_data_by_date_range(self):

        price_data = PriceData(self._get_test_data())
        start = utils.create_date(DATE1)
        end = utils.create_date(DATE2)

        result = price_data.get_price_data(['TEST'], 'Close', start, end)

        self.assertEqual(2, len(result))
        self.assertEqual(DATE1, result.index[0].strftime(DATE_FORMAT))
        self.assertEqual(DATE2, result.index[-1].strftime(DATE_FORMAT))
示例#2
0
def run():
    #symbols = data_loader.load_symbol_list(data_sources.SP_500_2012)
    symbols = ['ACN']

    start = utils.create_date('2012-01-01')
    end = utils.create_date('2012-12-31')

    close_price_data = _get_price_data(symbols, start, end)

    perform_backtests(symbols, close_price_data)
    perform_monte_carlo_simulation(symbols, close_price_data)
示例#3
0
    def create_observer(self, location, time="now"):
        """Creates an Observer in a specified location and at a given time.
        
        Parameters:
        db: a MasterDatabase instance.
        location: a string describing the location
        time: either a datetime instance, or one of the strings: 
              (now, sunrise, sunset)
        """

        location_found = False
        for r in self.db.root.locations.iterrows():
            if location.lower() in r["name"].lower():
                location_found = True
                break
        if not location_found:
            raise ValueError("Location %s not in the database" % location)

        latitude = r["latitude"]
        longitude = r["longitude"]
        height = r["height"]
        name = r["name"]

        observer = ephem.Observer()
        observer.name = name
        observer.lat = str(latitude)
        observer.lon = str(longitude)
        observer.elev = height

        t = utils.create_date(time)

        observer.horizon = 0
        observer.date = t
        return observer
示例#4
0
 def create_observer(self, location, time = "now"):
     """Creates an Observer in a specified location and at a given time.
     
     Parameters:
     db: a MasterDatabase instance.
     location: a string describing the location
     time: either a datetime instance, or one of the strings: 
           (now, sunrise, sunset)
     """
     
     location_found = False
     for r in self.db.root.locations.iterrows():
         if location.lower() in r["name"].lower():
             location_found = True
             break
     if not location_found:
         raise ValueError("Location %s not in the database" % location)
     
     latitude = r["latitude"]
     longitude = r["longitude"]
     height = r["height"]
     name = r["name"]
     
     observer = ephem.Observer()
     observer.name = name
     observer.lat = str(latitude)
     observer.lon = str(longitude)
     observer.elev = height    
     
     t = utils.create_date(time)
             
     observer.horizon = 0
     observer.date = t
     return observer
示例#5
0
 def setdate(self, date):
     date = ephem.localtime(utils.create_date(date))
     #yyyy-mm-dd hh:mm:ss
     date_str = "\"%d-%d-%d %d:%d:%d\"" % (date.year, date.month, date.day,
                                           date.hour, date.minute,
                                           date.second)
     cmd = "SETDATE " + date_str
     return self.__send_and_check(cmd)
示例#6
0
 def setdate(self, date):
     date = ephem.localtime(utils.create_date(date))
     #yyyy-mm-dd hh:mm:ss
     date_str = "\"%d-%d-%d %d:%d:%d\"" %(date.year, date.month, date.day,
                                      date.hour, date.minute, date.second)
     cmd = "SETDATE " +  date_str
     return self.__send_and_check(cmd)
     
     
         
示例#7
0
    def search_by_date(self):
        """ Search a task by date"""

        date = utils.create_date('Enter the date')

        # Format the date to dd/mm/yyyy
        date = utils.format_date(date)
        rows = (Task.select(
            Task, Employee).join(Employee).where(Task.date == date).naive())

        return rows
示例#8
0
    def search_by_dates_range(self):
        """Search task between range of dates"""

        date1 = utils.create_date('Start date')
        date2 = utils.create_date('Finish date')

        if date1 < date2:
            start_date = date1
            finish_date = date2
        elif date1 > date2:
            start_date = date2
            finish_date = date1

        # Format dates
        start_date = utils.format_date(start_date)
        finish_date = utils.format_date(finish_date)

        rows = (Task.select(Task, Employee).join(Employee).where(
            Task.date.between(start_date, finish_date)).naive())

        return rows
示例#9
0
    def can_observe(body):
        assert isinstance(body, Body)
        body = body.ephem_body

        copy_observer = utils.copy_observer(observer)

        if start_time is None:
            st = observer.date
        else:
            st = utils.create_date(start_time)
        if end_time is None:
            et = observer.date
        else:
            et = utils.create_date(end_time)

        if horizon is None:
            copy_observer.horizon = observer.horizon
        else:
            copy_observer.horizon = str(horizon)

        try:
            setting = copy_observer.next_setting(body, use_center=True)
            rising = copy_observer.next_rising(body, use_center=True)
        except ephem.NeverUpError:
            return False
        except ephem.AlwaysUpError:
            return True

        if rising > setting:
            rising = copy_observer.previous_rising(body, use_center=True)
        #====rise========set=======#
        #========observe======stop=#
        cond1 = rising < st < setting

        #===========rise========set=======#
        #===observe========stop===========#
        cond2 = st < rising < et

        return cond1 or cond2
示例#10
0
def task_prepare_stringency_data(depends_on, produces):
    stringency_data = pd.read_csv(depends_on)
    stringency_data = create_date(stringency_data, "date")
    stringency_data["date"] = stringency_data["date"].apply(lambda x: x.date())
    stringency_data = stringency_data.set_index(["country", "date"])
    stringency_data = stringency_data.sort_index()

    stringency_data = create_moving_average(
        stringency_data,
        ["stringency_index"],
        grouping_var="country",
        kind="forward",
        time=7,
    )
    german_stringency_data = stringency_data.loc["Germany"].drop(
        "country_code", axis=1)

    german_stringency_data.to_pickle(produces)
 def test_get_date(self, mock_get_input):
     date = datetime.datetime(2017, 8, 14, 0, 0)
     self.assertEqual(utils.create_date(), date)
示例#12
0
 def test_create_date(self):
     self.assertEqual(datetime.datetime(2014, 7, 10),
                      utils.create_date('2014-07-10'))
示例#13
0
def task_prepare_data(depends_on, produces):
    # Load in Google data
    google_data = pd.read_csv(depends_on["google"])

    # Keep only european countries in the dataset
    eu_data = google_data.query("country_region in @european_countries")

    # Replace NaN with "country" in "sub_region_1" column
    eu_data["sub_region_1"] = eu_data["sub_region_1"].replace(
        np.nan, "country")

    # Drop census_fips_code (contains no information)
    eu_data.drop("census_fips_code", axis=1)

    # Rename variables
    eu_data.columns = map(
        lambda x: x.replace("_percent_change_from_baseline", ""),
        eu_data.columns)
    eu_data.rename(columns={"country_region": "country"}, inplace=True)

    # Drop census_fips_code because contains only NaN's
    eu_data = eu_data.drop(["census_fips_code"], axis=1)

    # Change datatypes of some columns to string
    eu_data[[
        "sub_region_1", "sub_region_2", "metro_area", "iso_3166_2_code",
        "place_id"
    ]] = eu_data[[
        "sub_region_1", "sub_region_2", "metro_area", "iso_3166_2_code",
        "place_id"
    ]].astype(str)

    # Create date variables
    eu_data = create_date(eu_data)

    # Use MultiIndex for better overview
    eu_data = eu_data.set_index(["country", "date"])
    eu_data = eu_data.drop("date_str", axis=1)

    # reate dataset for state-level comparison
    germany_state_level = eu_data.loc["Germany"]
    germany_state_level = germany_state_level.drop(
        ["place_id", "metro_area", "sub_region_2", "country_region_code"],
        axis=1)
    germany_state_level = germany_state_level.rename(
        columns={"sub_region_1": "state"})

    germany_state_level = germany_state_level.reset_index()
    germany_state_level = germany_state_level.set_index(["state", "date"])

    germany_state_level.loc[list_city_states, "city_noncity"] = "city state"
    germany_state_level.loc[list_non_city_states,
                            "city_noncity"] = "territorial state"
    germany_state_level.loc[list_former_brd, "brd_ddr"] = "former BRD"
    germany_state_level.loc[list_former_ddr, "brd_ddr"] = "former DDR"
    germany_state_level.loc[list_west_germany, "four_regions"] = "West"
    germany_state_level.loc[list_south_germany, "four_regions"] = "South"
    germany_state_level.loc[list_north_germany, "four_regions"] = "North"
    germany_state_level.loc[list_east_germany, "four_regions"] = "East"

    germany_state_level = germany_state_level.drop("country")

    germany_state_level = create_moving_average(
        germany_state_level,
        [
            "retail_and_recreation",
            "grocery_and_pharmacy",
            "parks",
            "transit_stations",
            "workplaces",
            "residential",
        ],
        "state",
        kind="forward",
    )
    germany_state_level.to_pickle(produces["german_states"])

    # Create dataset for comparison between different european countries
    eu_country_level_data = eu_data[eu_data["sub_region_1"] == "country"]
    eu_country_level_data = eu_country_level_data.loc[
        eu_country_level_data["metro_area"] == "nan", ]
    eu_country_level_data = eu_country_level_data.drop(
        ["sub_region_1", "sub_region_2", "metro_area", "iso_3166_2_code"],
        axis=1)

    # Create moving average
    eu_country_level_data = create_moving_average(
        eu_country_level_data,
        [
            "retail_and_recreation",
            "grocery_and_pharmacy",
            "parks",
            "transit_stations",
            "workplaces",
            "residential",
        ],
        "country",
        kind="forward",
    )

    # Load in infection numbers
    eu_infect_numbers = pd.read_pickle(depends_on["infection"])
    # eu_infect_numbers = eu_infect_numbers.set_index(["country", "date"])

    # Join the two datasets
    eu_composed_data_country_level = eu_country_level_data.join(
        eu_infect_numbers)
    eu_composed_data_country_level = eu_composed_data_country_level.reset_index(
    )
    eu_composed_data_country_level = eu_composed_data_country_level.drop(
        "Unnamed: 0", axis=1)

    # Export the data to pickle format
    eu_composed_data_country_level.to_pickle(produces["eu_country_level"])
    def patch(self, task):

        task_obj = Task()

        while_breaker = 1

        while True:

            # This helps the testing, since these are kind of
            # infinite loops while testing, there's need for something
            # that can break them automatically
            if while_breaker > 100:
                break
            else:
                while_breaker += 1

            utils.cls()

            menu = ("What do you want to edit of the task?: \n"
                    "a) Date \n"
                    "b) Title \n"
                    "c) Time Spent \n"
                    "d) Notes \n"
                    "e) Save changes and return to searches \n"
                    "f) Return to menu \n"
                    ">")

            option = utils.get_input(menu)

            # Handling errors
            if not option:
                utils.pause('Error: Blank spaces are not allowed')
                continue
            elif option not in 'abcdef':
                utils.pause('Error: {} is not a valid option'.format(option))
                continue

            # Task Date
            if option == 'a':

                utils.cls()
                print('Current task date: {}'.format(task.date))
                new_task_date = utils.create_date('New Date: ')

                task.date = new_task_date.strftime(utils.DATEFORMAT)

            # Task Title
            elif option == 'b':

                utils.cls()
                current = 'Current task name: {}'.format(task.title)
                task.title = task_obj.ask_title(current + '\nNew title: ')

            # Task Time Spent
            elif option == 'c':

                utils.cls()
                current = 'Current task time spent: {}'.format(task.time_spent)
                task.time_spent = task_obj.ask_time_spent(
                    current + '\nNew time spent (rounded minutes): ')

            # Task Notes
            elif option == 'd':

                utils.cls()
                print('Actual Notes: {}'.format(task.notes))
                new_task_notes = input('New Notes (Optional can be empty): ')

                task.notes = new_task_notes

            # Save changes and commit them
            elif option == 'e':

                task.save()

                utils.cls()
                print('Nice, changes saved')
                input('Press ENTER to continue')
                return self.menu()

            # Return to initial menu
            elif option == 'f':
                break
示例#15
0
    def ask_date(self, inputtxt=None):
        """Ask the date for the task"""

        date = utils.create_date(inputtxt)
        return utils.format_date(date)