Ejemplo n.º 1
0
def market_pipeline(model, market_specs):
    r"""AlphaPy MarketFlow Pipeline

    Parameters
    ----------
    model : alphapy.Model
        The model object for AlphaPy.
    market_specs : dict
        The specifications for controlling the MarketFlow pipeline.

    Returns
    -------
    model : alphapy.Model
        The final results are stored in the model object.

    Notes
    -----
    (1) Define a group.
    (2) Get the market data.
    (3) Apply system features.
    (4) Create an analysis.
    (5) Run the analysis, which calls AlphaPy.

    """

    logger.info("Running MarketFlow Pipeline")

    # Get model specifications

    predict_mode = model.specs['predict_mode']
    target = model.specs['target']

    # Get market specifications

    create_model = market_specs['create_model']
    data_fractal = market_specs['data_fractal']
    data_history = market_specs['data_history']
    features = market_specs['features']
    forecast_period = market_specs['forecast_period']
    fractal = market_specs['fractal']
    functions = market_specs['functions']
    lag_period = market_specs['lag_period']
    leaders = market_specs['leaders']
    predict_history = market_specs['predict_history']
    target_group = market_specs['target_group']

    # Set the target group

    group = Group.groups[target_group]
    logger.info("All Symbols: %s", group.members)

    # Determine whether or not this is an intraday analysis.

    intraday = any(substring in fractal for substring in PD_INTRADAY_OFFSETS)

    # Get stock data. If we can't get all the data, then
    # predict_history resets to the actual history obtained.

    lookback = predict_history if predict_mode else data_history
    new_history = get_market_data(model, group, lookback, data_fractal,
                                  intraday)
    if new_history < data_history:
        logger.info("Maximum Data History is %d, not %d", new_history,
                    data_history)
        if new_history == 0:
            raise ValueError("Could not get market data from source")

    # Run an analysis to create the model

    if create_model:
        # apply features to all of the frames
        vmapply(group, features, functions)
        vmapply(group, [target], functions)
        # run the analysis, including the model pipeline
        a = Analysis(model, group)
        results = run_analysis(a, lag_period, forecast_period, leaders,
                               predict_history)

    # Run a system

    system_specs = market_specs['system']
    if system_specs:
        # get the system specs
        system_name = system_specs['name']
        longentry = system_specs['longentry']
        shortentry = system_specs['shortentry']
        longexit = system_specs['longexit']
        shortexit = system_specs['shortexit']
        holdperiod = system_specs['holdperiod']
        scale = system_specs['scale']
        logger.info("Running System %s", system_name)
        logger.info("Long Entry  : %s", longentry)
        logger.info("Short Entry : %s", shortentry)
        logger.info("Long Exit   : %s", longexit)
        logger.info("Short Exit  : %s", shortexit)
        logger.info("Hold Period : %d", holdperiod)
        logger.info("Scale       : %r", scale)
        # create and run the system
        system = System(system_name, longentry, shortentry, longexit,
                        shortexit, holdperiod, scale)
        tfs = run_system(model, system, group, intraday)
        # generate a portfolio
        gen_portfolio(model, system_name, group, tfs)

    # Return the completed model
    return model
Ejemplo n.º 2
0
def market_pipeline(model, market_specs):
    r"""AlphaPy MarketFlow Pipeline

    Parameters
    ----------
    model : alphapy.Model
        The model object for AlphaPy.
    market_specs : dict
        The specifications for controlling the MarketFlow pipeline.

    Returns
    -------
    model : alphapy.Model
        The final results are stored in the model object.

    Notes
    -----
    (1) Define a group.
    (2) Get the market data.
    (3) Apply system features.
    (4) Create an analysis.
    (5) Run the analysis, which calls AlphaPy.

    """

    logger.info("Running MarketFlow Pipeline")

    # Get any model specifications

    predict_mode = model.specs['predict_mode']
    target = model.specs['target']

    # Get any market specifications

    data_history = market_specs['data_history']
    features = market_specs['features']
    forecast_period = market_specs['forecast_period']
    functions = market_specs['functions']
    leaders = market_specs['leaders']
    predict_history = market_specs['predict_history']
    target_group = market_specs['target_group']

    # Get the system specifications

    system_specs = market_specs['system']
    if system_specs:
        system_name = system_specs['name']
        try:
            longshort = True
            longentry = system_specs['longentry']
            shortentry = system_specs['shortentry']
            longexit = system_specs['longexit']
            shortexit = system_specs['shortexit']
            holdperiod = system_specs['holdperiod']
            scale = system_specs['scale']
            logger.info("Running Long/Short System %s", system_name)
        except:
            longshort = False
            logger.info("Running System %s", system_name)

    # Set the target group

    group = Group.groups[target_group]
    logger.info("All Members: %s", group.members)

    # Get stock data

    lookback = predict_history if predict_mode else data_history
    daily = get_feed_data(group, lookback)

    # Apply the features to all of the frames

    vmapply(group, features, functions)
    vmapply(group, [target], functions)

    # Run a system or an analysis

    if system_specs:
        # create and run the system
        if longshort:
            system_ls = System(system_name, longentry, shortentry, longexit,
                               shortexit, holdperiod, scale)
            tfs = run_system(model, system_ls, group)
        else:
            tfs = run_system(model, system_name, group)
        # generate a portfolio
        gen_portfolio(model, system_name, group, tfs)
    else:
        # run the analysis, including the model pipeline
        a = Analysis(model, group)
        results = run_analysis(a, forecast_period, leaders, predict_history)

    # Return the completed model
    return model