1
1
# Constrained Model
2
- # =============================================================================================
2
+ #-------------------
3
3
4
4
# I have developed the below algorithm using the resources found in the Quantopian Lectures and Forums.
5
5
# This notebook was created and written by Brooks Woolfolk. All materials are for educational purposes.
6
6
7
7
# Visit https://www.quantopian.com/algorithms/ if you would like to test and adjust the below ^_^
8
8
9
+
9
10
# IMPORT LIBRARIES
10
- # =============================================================================================
11
+ #------------------
11
12
import pandas as pd
12
13
13
14
import quantopian .algorithm as algo
22
23
from quantopian .pipeline .filters import QTradableStocksUS
23
24
from quantopian .pipeline .data import factset
24
25
26
+
25
27
# ALGORITHM PARAMETERS
26
- # =============================================================================================
28
+ #----------------------
27
29
28
30
def initialize (context ):
29
31
@@ -39,12 +41,14 @@ def initialize(context):
39
41
40
42
asset_turnover = Fundamentals .assets_turnover .latest .winsorize (.05 , .95 ).zscore ()
41
43
ciwc = Fundamentals .change_in_working_capital .latest .winsorize (.05 , .95 ).zscore ()
44
+
42
45
43
46
# ALPHA COMBINATION
44
47
# -----------------
45
48
# ASSIGN EVERY ASSET AN ALPHA RANK AND CENTER VALUES AT 0 (DEMEAN).
46
49
alpha = (asset_turnover + 2 * ciwc ).rank ().demean ()
47
50
51
+
48
52
# BETA DEFINITION
49
53
beta = 0.66 * RollingLinearRegressionOfReturns (
50
54
target = sid (8554 ),
@@ -55,7 +59,7 @@ def initialize(context):
55
59
56
60
57
61
# CREATE AND REGISTER PIPELINE
58
- # =========================================================================================
62
+ #-------------------------------
59
63
# COMPUTES COMBINES ALPHA AND SECTOR CODE FOR UNIVERSE OF STOCKS TO BE USED IN OPTIMIZATION
60
64
pipe = Pipeline (
61
65
columns = {
@@ -73,7 +77,7 @@ def initialize(context):
73
77
74
78
75
79
# SCHEDULE FUNCTIONS
76
- # =========================================================================================
80
+ #--------------------
77
81
algo .schedule_function (
78
82
do_portfolio_construction ,
79
83
date_rule = algo .date_rules .week_start (),
@@ -83,19 +87,19 @@ def initialize(context):
83
87
84
88
85
89
# BEFORE TRADING START
86
- # =============================================================================================
90
+ #----------------------
87
91
def before_trading_start (context , data ):
88
92
context .pipeline_data = algo .pipeline_output ('pipe' )
89
93
context .risk_loading_pipeline = algo .pipeline_output ('risk_loading_pipeline' )
90
94
91
95
92
96
# PORTFOLIO CONSTRUCTION
93
- # =============================================================================================
97
+ #------------------------
94
98
def do_portfolio_construction (context , data ):
95
99
pipeline_data = context .pipeline_data
96
100
97
101
# OBJECTIVE
98
- # =========================================================================================
102
+ #-----------
99
103
# OBJECTIVE: MAXIMIZE ALPHA BASED ON NAIVE RANKINGS
100
104
# RANK ALPHA COEFFICIENT AND TRADE TO MAXIMIZE THAT ALPHA
101
105
#
0 commit comments