def get_service_inputs(app_ns: str):
    '''
        Returns the required inputs for the recommendation service given
        the application namespace used to identify the appropriate cloud resources.

        Returns
        ----------
        A tuple containing the latest SecurityRecommendationSet
        and Portfolio objects. If a portfolio does not exist, it will
        create a new one.
    '''

    log.info("Loading latest recommendations")
    recommendation_set = SecurityRecommendationSet.from_s3(app_ns)

    if not recommendation_set.is_current(datetime.now()):
        raise ValidationError("Current recommendation set is not valid", None)

    try:
        log.info("Loading current portfolio")
        pfolio = Portfolio.from_s3(app_ns)
    except AWSError as e:
        if e.resource_not_found():
            pfolio = None
        else:
            raise e

    return (pfolio, recommendation_set)
예제 #2
0
def get_service_inputs(app_ns: str):
    '''
        Returns the required inputs for the recommendation service given
        the application namespace used to identify the appropriate cloud resources.

        Returns
        ----------
        A tuple containing the latest SecurityRecommendationSet
        and Portfolio objects. If a portfolio does not exist, it will
        create a new one.
    '''

    log.info("Loading latest recommendations")
    recommendation_set = SecurityRecommendationSet.from_s3(
        app_ns, constants.S3_PRICE_DISPERSION_RECOMMENDATION_SET_OBJECT_NAME)

    business_date = util.get_business_date(
        constants.BUSINESS_DATE_DAYS_LOOKBACK,
        constants.BUSINESS_DATE_CUTOVER_TIME)

    if not recommendation_set.is_current(business_date):
        raise ValidationError("Current recommendation set is not valid", None)

    try:
        log.info("Loading current portfolio")
        pfolio = Portfolio.from_s3(app_ns, constants.S3_PORTFOLIO_OBJECT_NAME)
    except AWSError as e:
        if e.resource_not_found():
            pfolio = None
        else:
            raise e

    return (pfolio, recommendation_set)
def main():
    """
        Main function for this script
    """
    try:
        app_ns = parse_params()

        log.info("Parameters:")
        log.info("Application Namespace: %s" % app_ns)

        business_date = util.get_business_date(
            constants.BUSINESS_DATE_DAYS_LOOKBACK, constants.BUSINESS_DATE_CUTOVER_TIME)
        log.info("Business Date is: %s" % business_date)

        # test all connectivity upfront, so if there any issues
        # the problem becomes more apparent
        connector_test.test_aws_connectivity()
        connector_test.test_intrinio_connectivity()

        log.info('Loading Strategy Configuration "%s" from S3' %
                 constants.STRATEGY_CONFIG_FILE_NAME)
        configuration = Configuration.try_from_s3(
            constants.STRATEGY_CONFIG_FILE_NAME, app_ns)

        log.info("Initalizing Trading Strategies")
        strategies = [
            PriceDispersionStrategy.from_configuration(configuration, app_ns),
            MACDCrossoverStrategy.from_configuration(configuration, app_ns)
        ]

        notification_list = []
        for strategy in strategies:
            recommendation_set = None
            try:
                log.info("Executing %s strategy" % strategy.STRATEGY_NAME)
                recommendation_set = SecurityRecommendationSet.from_s3(
                    app_ns, strategy.S3_RECOMMENDATION_SET_OBJECT_NAME)
            except AWSError as awe:
                if not awe.resource_not_found():
                    raise awe
                log.info("No recommendation set was found in S3.")

            if recommendation_set == None  \
                    or not recommendation_set.is_current(business_date):

                strategy.generate_recommendation()
                strategy.display_results()

                recommendation_set = strategy.recommendation_set

                recommendation_set.save_to_s3(
                    app_ns, strategy.S3_RECOMMENDATION_SET_OBJECT_NAME)

                notification_list.append(recommendation_set)
            else:
                log.info(
                    "Recommendation set is still valid. There is nothing to do")

        recommendation_svc.notify_new_recommendation(
            notification_list, app_ns)
    except Exception as e:
        stack_trace = traceback.format_exc()
        log.error("Could run script, because: %s" % (str(e)))
        log.error(stack_trace)
예제 #4
0
def main():
    """
        Main function for this script
    """
    try:
        (environment, ticker_file_name, output_size, month, year,
         current_price_date, app_ns) = parse_params()

        log.info("Parameters:")
        log.info("Environment: %s" % environment)
        log.info("Ticker File: %s" % ticker_file_name)
        log.info("Output Size: %d" % output_size)
        log.info("Analysis Month: %d" % month)
        log.info("Analysis Year: %d" % year)

        if environment == "TEST":
            log.info("reading ticker file from local filesystem")
            ticker_list = TickerFile.from_local_file(
                constants.TICKER_DATA_DIR, ticker_file_name).ticker_list

            log.info("Performing Recommendation Algorithm")
            strategy = PriceDispersionStrategy(ticker_list, year, month,
                                               output_size)
            strategy.generate_recommendation()
            display_calculation_dataframe(month, year, strategy,
                                          current_price_date)
        else:  # environment == "PRODUCTION"
            # test all connectivity upfront, so if there any issues
            # the problem becomes more apparent
            connector_test.test_aws_connectivity()
            connector_test.test_intrinio_connectivity()

            log.info("Reading ticker file from s3 bucket")
            ticker_list = TickerFile.from_s3_bucket(ticker_file_name,
                                                    app_ns).ticker_list

            log.info("Loading existing recommendation set from S3")
            recommendation_set = None

            try:
                recommendation_set = SecurityRecommendationSet.from_s3(app_ns)
            except AWSError as awe:
                if not awe.resource_not_found():
                    raise awe
                log.info("No recommendation set was found in S3.")

            if recommendation_set == None  \
                    or not recommendation_set.is_current(datetime.now()):

                log.info("Performing Recommendation Algorithm")
                strategy = PriceDispersionStrategy(ticker_list, year, month,
                                                   output_size)

                strategy.generate_recommendation()
                recommendation_set = strategy.recommendation_set
                display_calculation_dataframe(month, year, strategy,
                                              current_price_date)

                recommendation_set.save_to_s3(app_ns)
                recommendation_svc.notify_new_recommendation(
                    recommendation_set, app_ns)
            else:
                log.info(
                    "Recommendation set is still valid. There is nothing to do"
                )

    except Exception as e:
        stack_trace = traceback.format_exc()
        log.error("Could run script, because: %s" % (str(e)))
        log.error(stack_trace)

        if environment == "PRODUCTION":
            aws_service_wrapper.notify_error(
                e, "Securities Recommendation Service", stack_trace, app_ns)