Beispiel #1
0
def test_mean_reversion_5day_sector_neutral_smoothed(fn):
    column_name = 'Mean_Reversion_5Day_Sector_Neutral_Smoothed'
    start_date_str = '2015-01-05'
    end_date_str = '2015-01-07'

    # Build engine
    trading_calendar = get_calendar('NYSE')
    bundle_data = bundles.load(project_helper.EOD_BUNDLE_NAME)
    engine = project_helper.build_pipeline_engine(bundle_data,
                                                  trading_calendar)

    # Build pipeline
    universe_window_length = 2
    universe_asset_count = 4
    universe = AverageDollarVolume(
        window_length=universe_window_length).top(universe_asset_count)
    pipeline = Pipeline(screen=universe)

    run_pipeline_args = {
        'pipeline': pipeline,
        'start_date': pd.Timestamp(start_date_str, tz='utc'),
        'end_date': pd.Timestamp(end_date_str, tz='utc')
    }
    fn_inputs = {
        'window_length': 3,
        'universe': universe,
        'sector': project_helper.Sector()
    }
    fn_correct_outputs = OrderedDict([
        ('pipline_out',
         pd.DataFrame([
             0.44721360, 1.34164079, -1.34164079, -0.44721360, 1.34164079,
             0.44721360, -1.34164079, -0.44721360, 0.44721360, 1.34164079,
             -1.34164079, -0.44721360
         ],
                      engine.run_pipeline(**run_pipeline_args).index,
                      [column_name]))
    ])

    print('Running Integration Test on pipeline:')
    print('> start_dat = pd.Timestamp(\'{}\', tz=\'utc\')'.format(
        start_date_str))
    print('> end_date = pd.Timestamp(\'{}\', tz=\'utc\')'.format(end_date_str))
    print('> universe = AverageDollarVolume(window_length={}).top({})'.format(
        universe_window_length, universe_asset_count))
    print('> factor = {}('.format(fn.__name__))
    print('    window_length={},'.format(fn_inputs['window_length']))
    print('    universe=universe,')
    print('    sector=project_helper.Sector())')
    print('> pipeline.add(factor, \'{}\')'.format(column_name))
    print('> engine.run_pipeline(pipeline, start_dat, end_date)')
    print('')

    pipeline.add(fn(**fn_inputs), column_name)
    assert_output(engine.run_pipeline,
                  run_pipeline_args,
                  fn_correct_outputs,
                  check_parameter_changes=False)
Beispiel #2
0
    return factor * -1


project_tests.test_mean_reversion_5day_sector_neutral(mean_reversion_5day_sector_neutral)


# ### View Data
# Let's see what some of the factor data looks like. For calculating factors, we'll be looking back 2 years.
# 
# **Note:** _Going back 2 years falls on a day when the market is closed. Pipeline package doesn't handle start or end dates that don't fall on days when the market is open. To fix this, we went back 2 extra days to fall on the next day when the market is open._

# In[28]:


factor_start_date = universe_end_date - pd.DateOffset(years=2, days=2)
sector = project_helper.Sector()
window_length = 5

pipeline = Pipeline(screen=universe)
pipeline.add(
    mean_reversion_5day_sector_neutral(window_length, universe, sector),
    'Mean_Reversion_5Day_Sector_Neutral')
engine.run_pipeline(pipeline, factor_start_date, universe_end_date)


# ## Mean Reversion 5 Day Sector Neutral Smoothed Factor
# Taking the output of the previous factor, let's create a smoothed version. Implement `mean_reversion_5day_sector_neutral_smoothed` to generate a mean reversion 5 fay sector neutral smoothed factor. Call the `mean_reversion_5day_sector_neutral` function to get the unsmoothed factor, then use `SimpleMovingAverage` function to smooth it. You'll have to apply rank and zscore again.

# In[29]:

Beispiel #3
0
def test_mean_reversion_5day_sector_neutral_smoothed(fn):
    column_name = "Mean_Reversion_5Day_Sector_Neutral_Smoothed"
    start_date_str = "2015-01-05"
    end_date_str = "2015-01-07"

    # Build engine
    trading_calendar = get_calendar("NYSE")
    bundle_data = bundles.load(project_helper.EOD_BUNDLE_NAME)
    engine = project_helper.build_pipeline_engine(bundle_data,
                                                  trading_calendar)

    # Build pipeline
    universe_window_length = 2
    universe_asset_count = 4
    universe = AverageDollarVolume(
        window_length=universe_window_length).top(universe_asset_count)
    pipeline = Pipeline(screen=universe)

    run_pipeline_args = {
        "pipeline": pipeline,
        "start_date": pd.Timestamp(start_date_str, tz="utc"),
        "end_date": pd.Timestamp(end_date_str, tz="utc"),
    }
    fn_inputs = {
        "window_length": 3,
        "universe": universe,
        "sector": project_helper.Sector(),
    }
    fn_correct_outputs = OrderedDict([(
        "pipline_out",
        pd.DataFrame(
            [
                0.44721360,
                1.34164079,
                -1.34164079,
                -0.44721360,
                1.34164079,
                0.44721360,
                -1.34164079,
                -0.44721360,
                0.44721360,
                1.34164079,
                -1.34164079,
                -0.44721360,
            ],
            engine.run_pipeline(**run_pipeline_args).index,
            [column_name],
        ),
    )])

    print("Running Integration Test on pipeline:")
    print("> start_dat = pd.Timestamp('{}', tz='utc')".format(start_date_str))
    print("> end_date = pd.Timestamp('{}', tz='utc')".format(end_date_str))
    print("> universe = AverageDollarVolume(window_length={}).top({})".format(
        universe_window_length, universe_asset_count))
    print("> factor = {}(".format(fn.__name__))
    print("    window_length={},".format(fn_inputs["window_length"]))
    print("    universe=universe,")
    print("    sector=project_helper.Sector())")
    print("> pipeline.add(factor, '{}')".format(column_name))
    print("> engine.run_pipeline(pipeline, start_dat, end_date)")
    print("")

    pipeline.add(fn(**fn_inputs), column_name)
    assert_output(
        engine.run_pipeline,
        run_pipeline_args,
        fn_correct_outputs,
        check_parameter_changes=False,
    )