def test_show_graph(self): f = SomeFactor() p = Pipeline(columns={"f": SomeFactor()}) # The real display_graph call shells out to GraphViz, which isn't a # requirement, so patch it out for testing. def mock_display_graph(g, format="svg", include_asset_exists=False): return (g, format, include_asset_exists) self.assertEqual( getargspec(display_graph), getargspec(mock_display_graph), msg="Mock signature doesn't match signature for display_graph.", ) patch_display_graph = patch( "zipline.pipeline.graph.display_graph", mock_display_graph, ) with patch_display_graph: graph, format, include_asset_exists = p.show_graph() self.assertIs(graph.outputs["f"], f) # '' is a sentinel used for screen if it's not supplied. self.assertEqual( sorted(graph.outputs.keys()), ["f", graph.screen_name], ) self.assertEqual(format, "svg") self.assertEqual(include_asset_exists, False) with patch_display_graph: graph, format, include_asset_exists = p.show_graph(format="png") self.assertIs(graph.outputs["f"], f) # '' is a sentinel used for screen if it's not supplied. self.assertEqual(sorted(graph.outputs.keys()), ["f", graph.screen_name]) self.assertEqual(format, "png") self.assertEqual(include_asset_exists, False) with patch_display_graph: graph, format, include_asset_exists = p.show_graph(format="jpeg") self.assertIs(graph.outputs["f"], f) self.assertEqual(sorted(graph.outputs.keys()), ["f", graph.screen_name]) self.assertEqual(format, "jpeg") self.assertEqual(include_asset_exists, False) expected = (r".*\.show_graph\(\) expected a value in " r"\('svg', 'png', 'jpeg'\) for argument 'format', " r"but got 'fizzbuzz' instead.") with self.assertRaisesRegex(ValueError, expected): p.show_graph(format="fizzbuzz")
def test_show_graph(self): f = SomeFactor() p = Pipeline(columns={'f': SomeFactor()}) # The real display_graph call shells out to GraphViz, which isn't a # requirement, so patch it out for testing. def mock_display_graph(g, format='svg', include_asset_exists=False): return (g, format, include_asset_exists) self.assertEqual( inspect.getargspec(display_graph), inspect.getargspec(mock_display_graph), msg="Mock signature doesn't match signature for display_graph." ) patch_display_graph = patch( 'zipline.pipeline.graph.display_graph', mock_display_graph, ) with patch_display_graph: graph, format, include_asset_exists = p.show_graph() self.assertIs(graph.outputs['f'], f) # '' is a sentinel used for screen if it's not supplied. self.assertEqual(sorted(graph.outputs.keys()), ['', 'f']) self.assertEqual(format, 'svg') self.assertEqual(include_asset_exists, False) with patch_display_graph: graph, format, include_asset_exists = p.show_graph(format='png') self.assertIs(graph.outputs['f'], f) # '' is a sentinel used for screen if it's not supplied. self.assertEqual(sorted(graph.outputs.keys()), ['', 'f']) self.assertEqual(format, 'png') self.assertEqual(include_asset_exists, False) with patch_display_graph: graph, format, include_asset_exists = p.show_graph(format='jpeg') self.assertIs(graph.outputs['f'], f) # '' is a sentinel used for screen if it's not supplied. self.assertEqual(sorted(graph.outputs.keys()), ['', 'f']) self.assertEqual(format, 'jpeg') self.assertEqual(include_asset_exists, False) expected = ( r".*\.show_graph\(\) expected a value in " r"\('svg', 'png', 'jpeg'\) for argument 'format', " r"but got 'fizzbuzz' instead." ) with self.assertRaisesRegexp(ValueError, expected): p.show_graph(format='fizzbuzz')
def test_show_graph(self): f = SomeFactor() p = Pipeline(columns={"f": SomeFactor()}) # The real display_graph call shells out to GraphViz, which isn't a # requirement, so patch it out for testing. def mock_display_graph(g, format="svg", include_asset_exists=False): return (g, format, include_asset_exists) assert getargspec(display_graph) == getargspec( mock_display_graph ), "Mock signature doesn't match signature for display_graph." patch_display_graph = patch( "zipline.pipeline.graph.display_graph", mock_display_graph, ) with patch_display_graph: graph, format, include_asset_exists = p.show_graph() assert graph.outputs["f"] is f # '' is a sentinel used for screen if it's not supplied. assert sorted(graph.outputs.keys()) == ["f", graph.screen_name] assert format == "svg" assert include_asset_exists is False with patch_display_graph: graph, format, include_asset_exists = p.show_graph(format="png") assert graph.outputs["f"] is f # '' is a sentinel used for screen if it's not supplied. assert sorted(graph.outputs.keys()) == ["f", graph.screen_name] assert format == "png" assert include_asset_exists is False with patch_display_graph: graph, format, include_asset_exists = p.show_graph(format="jpeg") assert graph.outputs["f"] is f assert sorted(graph.outputs.keys()) == ["f", graph.screen_name] assert format == "jpeg" assert include_asset_exists is False expected = ( r".*\.show_graph\(\) expected a value in " r"\('svg', 'png', 'jpeg'\) for argument 'format', " r"but got 'fizzbuzz' instead." ) with pytest.raises(ValueError, match=expected): p.show_graph(format="fizzbuzz")
# # ### Filters # # In general, a **Filter** is a function from an asset at a particular moment in time to a boolean value (True of False). An example of a filter is a function indicating whether a security's price is below \$5. Given a security and a specific moment in time, this evaluates to either **True** or **False**. Filters are most commonly used for selecting sets of securities to include or exclude from your stock universe. Filters are usually applied using comparison operators, such as <, <=, !=, ==, >, >=. # # Viewing the Pipeline as a Diagram # # Zipline's Pipeline class comes with the attribute `.show_graph()` that allows you to render the Pipeline as a Directed Acyclic Graph (DAG). This graph is specified using the DOT language and consequently we need a DOT graph layout program to view the rendered image. In the code below, we will use the Graphviz pakage to render the graph produced by the `.show_graph()` attribute. Graphviz is an open-source package for drawing graphs specified in DOT language scripts. # In[ ]: import graphviz # Render the pipeline as a DAG pipeline.show_graph() # Right now, our pipeline is empty and it only contains a screen. Therefore, when we rendered our `pipeline`, we only see the diagram of our `screen`: # # ```python # AverageDollarVolume(window_length = 60).top(10) # ``` # # By default, the `.AverageDollarVolume()` class uses the `USEquityPricing` dataset, containing daily trading prices and volumes, to compute the average dollar volume: # # ```python # average_dollar_volume = np.nansum(close_price * volume, axis=0) / len(close_price) # ``` # The top of the diagram reflects the fact that the `.AverageDollarVolume()` class gets its inputs (closing price and volume) from the `USEquityPricing` dataset. The bottom of the diagram shows that the output is determined by the expression `x_0 <= 10`. This expression reflects the fact that we used `.top(10)` as a filter in our `screen`. We refer to each box in the diagram as a Term.
# Also rank and zscore (don't need to de-mean by sector, s) factor_smoothed = (SimpleMovingAverage(inputs=[factor], window_length=5).rank().zscore()) # add the unsmoothed factor to the pipeline p.add(factor, 'Momentum_Factor') # add the smoothed factor to the pipeline too p.add(factor_smoothed, 'Smoothed_Momentum_Factor') # ## visualize the pipeline # # Note that if the image is difficult to read in the notebook, right-click and view the image in a separate tab. # In[ ]: p.show_graph(format='png') # ## run pipeline and view the factor data # In[ ]: df = engine.run_pipeline(p, factor_start_date, universe_end_date) # In[ ]: df.head() # ## Evaluate Factors # # We'll go over some tools that we can use to evaluate alpha factors. To do so, we'll use the [alphalens library](https://github.com/quantopian/alphalens) #