Skip to content

Commit 81b3f27

Browse files
authored
Update Algorithm (v1).py
1 parent 6351131 commit 81b3f27

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Constrained Model/Algorithm (v1).py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Constrained Model
2-
# =============================================================================================
2+
#-------------------
33

44
# I have developed the below algorithm using the resources found in the Quantopian Lectures and Forums.
55
# This notebook was created and written by Brooks Woolfolk. All materials are for educational purposes.
66

77
# Visit https://www.quantopian.com/algorithms/ if you would like to test and adjust the below ^_^
88

9+
910
# IMPORT LIBRARIES
10-
# =============================================================================================
11+
#------------------
1112
import pandas as pd
1213

1314
import quantopian.algorithm as algo
@@ -22,8 +23,9 @@
2223
from quantopian.pipeline.filters import QTradableStocksUS
2324
from quantopian.pipeline.data import factset
2425

26+
2527
# ALGORITHM PARAMETERS
26-
# =============================================================================================
28+
#----------------------
2729

2830
def initialize(context):
2931

@@ -39,12 +41,14 @@ def initialize(context):
3941

4042
asset_turnover = Fundamentals.assets_turnover.latest.winsorize(.05, .95).zscore()
4143
ciwc = Fundamentals.change_in_working_capital.latest.winsorize(.05, .95).zscore()
44+
4245

4346
# ALPHA COMBINATION
4447
# -----------------
4548
# ASSIGN EVERY ASSET AN ALPHA RANK AND CENTER VALUES AT 0 (DEMEAN).
4649
alpha = (asset_turnover + 2*ciwc).rank().demean()
4750

51+
4852
# BETA DEFINITION
4953
beta = 0.66*RollingLinearRegressionOfReturns(
5054
target=sid(8554),
@@ -55,7 +59,7 @@ def initialize(context):
5559

5660

5761
# CREATE AND REGISTER PIPELINE
58-
# =========================================================================================
62+
#-------------------------------
5963
# COMPUTES COMBINES ALPHA AND SECTOR CODE FOR UNIVERSE OF STOCKS TO BE USED IN OPTIMIZATION
6064
pipe = Pipeline(
6165
columns={
@@ -73,7 +77,7 @@ def initialize(context):
7377

7478

7579
# SCHEDULE FUNCTIONS
76-
# =========================================================================================
80+
#--------------------
7781
algo.schedule_function(
7882
do_portfolio_construction,
7983
date_rule=algo.date_rules.week_start(),
@@ -83,19 +87,19 @@ def initialize(context):
8387

8488

8589
# BEFORE TRADING START
86-
# =============================================================================================
90+
#----------------------
8791
def before_trading_start(context, data):
8892
context.pipeline_data = algo.pipeline_output('pipe')
8993
context.risk_loading_pipeline = algo.pipeline_output('risk_loading_pipeline')
9094

9195

9296
# PORTFOLIO CONSTRUCTION
93-
# =============================================================================================
97+
#------------------------
9498
def do_portfolio_construction(context, data):
9599
pipeline_data = context.pipeline_data
96100

97101
# OBJECTIVE
98-
# =========================================================================================
102+
#-----------
99103
# OBJECTIVE: MAXIMIZE ALPHA BASED ON NAIVE RANKINGS
100104
# RANK ALPHA COEFFICIENT AND TRADE TO MAXIMIZE THAT ALPHA
101105
#

0 commit comments

Comments
 (0)