-
Notifications
You must be signed in to change notification settings - Fork 0
/
parcialQuantopian
148 lines (101 loc) · 4.23 KB
/
parcialQuantopian
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import quantopian.algorithm as algo
import quantopian.optimize as opt
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.experimental import risk_loading_pipeline
from quantopian.pipeline.data.psychsignal import stocktwits
#var1
from quantopian.pipeline.data.sentdex import sentiment
#var2
from quantopian.pipeline.factors import Returns
#var3
from quantopian.pipeline.data.morningstar import Fundamentals
from quantopian.pipeline.domain import US_EQUITIES
# Constraint Parameters
MAX_GROSS_LEVERAGE = 1.0
TOTAL_POSITIONS = 600
MAX_SHORT_POSITION_SIZE = 2.0 / TOTAL_POSITIONS
MAX_LONG_POSITION_SIZE = 2.0 / TOTAL_POSITIONS
#var2
RETURNS_LOOKBACK_DAYS = 5
def initialize(context):
algo.attach_pipeline(make_pipeline(), 'long_short_equity_template')
algo.attach_pipeline(risk_loading_pipeline(), 'risk_factors')
algo.schedule_function(func=rebalance,
date_rule=algo.date_rules.week_start(),
time_rule=algo.time_rules.market_open(hours=0, minutes=30),
half_days=True)
algo.schedule_function(func=record_vars,
date_rule=algo.date_rules.every_day(),
time_rule=algo.time_rules.market_close(),
half_days=True)
def make_pipeline():
value = Fundamentals.ebit.latest / Fundamentals.enterprise_value.latest
quality = Fundamentals.roe.latest
universe = QTradableStocksUS()
sentiment_score = SimpleMovingAverage(
inputs=[stocktwits.bull_minus_bear],
window_length=3,
)
#var1
mean_sentiment_5day = SimpleMovingAverage(inputs=[sentiment.sentiment_signal], window_length=1)
#var2
recent_returns = Returns(window_length=RETURNS_LOOKBACK_DAYS,mask=universe)
universe = QTradableStocksUS()
recent_returns_zscore = recent_returns.zscore()
recent_returns_zscore.winsorize(min_percentile=0.05, max_percentile=0.95)
#var 3
total_revenue = Fundamentals.total_revenue.latest
value_winsorized = value.winsorize(min_percentile=0.05, max_percentile=0.95)
quality_winsorized = quality.winsorize(min_percentile=0.05, max_percentile=0.95)
sentiment_score_winsorized = sentiment_score.winsorize(min_percentile=0.05, max_percentile=0.95)
#var1
mean_sentiment_5day_winsorized = mean_sentiment_5day.winsorize(min_percentile=0.05, max_percentile=0.95)
combined_factor = (
value_winsorized.zscore() +
quality_winsorized.zscore() +
sentiment_score_winsorized.zscore() +
mean_sentiment_5day_winsorized.zscore() +
recent_returns_zscore.zscore() +
total_revenue.zscore()
)
longs = combined_factor.top(TOTAL_POSITIONS//2, mask=universe)
shorts = combined_factor.bottom(TOTAL_POSITIONS//2, mask=universe)
long_short_screen = (longs | shorts)
pipe = Pipeline(
columns={
'longs': longs,
'shorts': shorts,
'combined_factor': combined_factor
},
screen=long_short_screen,
domain=US_EQUITIES,
)
return pipe
def before_trading_start(context, data):
context.pipeline_data = algo.pipeline_output('long_short_equity_template')
context.risk_loadings = algo.pipeline_output('risk_factors')
def record_vars(context, data):
algo.record(num_positions=len(context.portfolio.positions))
def rebalance(context, data):
pipeline_data = context.pipeline_data
risk_loadings = context.risk_loadings
objective = opt.MaximizeAlpha(pipeline_data.combined_factor)
constraints = []
constraints.append(opt.MaxGrossExposure(MAX_GROSS_LEVERAGE))
constraints.append(opt.DollarNeutral())
neutralize_risk_factors = opt.experimental.RiskModelExposure(
risk_model_loadings=risk_loadings,
version=0
)
constraints.append(neutralize_risk_factors)
constraints.append(
opt.PositionConcentration.with_equal_bounds(
min=-MAX_SHORT_POSITION_SIZE,
max=MAX_LONG_POSITION_SIZE
))
algo.order_optimal_portfolio(
objective=objective,
constraints=constraints
)