-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo.py
154 lines (154 loc) · 5.5 KB
/
algo.py
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
149
150
151
152
153
154
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from quantopian.algorithm import order_optimal_portfolio\n",
"from quantopian.algorithm import attach_pipeline, pipeline_output\n",
"from quantopian.pipeline import Pipeline\n",
"from quantopian.pipeline.data.builtin import USEquityPricing\n",
"from quantopian.pipeline.factors import SimpleMovingAverage\n",
"from quantopian.pipeline.filters import QTradableStocksUS\n",
"import quantopian.optimize as opt\n",
"from quantopian.pipeline.factors import Returns\n",
"\n",
"def initialize(context):\n",
" # Schedule our rebalance function to run at the end of\n",
" # each day, when the market closes\n",
" #set_slippage(slippage.FixedSlippage(spread=0.0, volume_limit=1))\n",
" #set_slippage(slippage.FixedBasisPointsSlippage(basis_points=0, volume_limit=100))\n",
" #set_slippage(slippage.VolumeShareSlippage(0))\n",
" schedule_function(\n",
" my_rebalance,\n",
" date_rules.every_day(),\n",
" time_rules.market_close(minutes=1 )\n",
" )\n",
"\n",
" # Create our pipeline and attach it to our algorithm.\n",
" my_pipe = make_pipeline()\n",
" attach_pipeline(my_pipe, 'my_pipeline')\n",
"\n",
"\n",
"\n",
"def make_pipeline():\n",
" \n",
" #longs = Returns(window_length=2).percentile_between(0,20,mask=QTradableStocksUS())\n",
" #shorts = Returns(window_length=2).percentile_between(80,100,mask=QTradableStocksUS())\n",
" longs = Returns(window_length=2).bottom(3,mask=QTradableStocksUS())\n",
" shorts = Returns(window_length=2).top(3,mask=QTradableStocksUS()) \n",
"\n",
" return Pipeline(\n",
" columns={\n",
" 'longs': longs,\n",
" 'shorts': shorts,\n",
" },\n",
" screen=QTradableStocksUS()& (shorts | longs)\n",
" )\n",
"\n",
"def compute_target_weights(context, data):\n",
" \"\"\"\n",
" Compute ordering weights.\n",
" \"\"\"\n",
"\n",
" # Initialize empty target weights dictionary.\n",
" # This will map securities to their target weight.\n",
" weights = {}\n",
"\n",
" # If there are securities in our longs and shorts lists,\n",
" # compute even target weights for each security.\n",
" if context.longs :\n",
" long_weight = 0.2 / len(context.longs)\n",
" if context.shorts:\n",
" short_weight = -0.2 / len(context.shorts)\n",
" #if ~(context.longs & context.shorts):\n",
" # return weights\n",
"\n",
" # Exit positions in our portfolio if they are not\n",
" # in our longs or shorts lists.\n",
" for security in context.portfolio.positions:\n",
" if security not in context.longs and security not in context.shorts and data.can_trade(security):\n",
" weights[security] = 0\n",
"\n",
" for security in context.longs:\n",
" weights[security] = long_weight\n",
"\n",
" for security in context.shorts:\n",
" weights[security] = short_weight\n",
"\n",
" return weights\n",
"\n",
"def before_trading_start(context, data):\n",
" \"\"\"\n",
" Get pipeline results.\n",
" \"\"\"\n",
"\n",
" # Gets our pipeline output every day.\n",
" pipe_results = pipeline_output('my_pipeline')\n",
"\n",
" # Go long in securities for which the 'longs' value is True,\n",
" # and check if they can be traded.\n",
" context.longs = []\n",
" for sec in pipe_results[pipe_results['longs']].index.tolist():\n",
" if data.can_trade(sec):\n",
" context.longs.append(sec)\n",
" #print(context.longs)\n",
" #print('Longs: ') \n",
" #print(context.longs)\n",
" # Go short in securities for which the 'shorts' value is True,\n",
" # and check if they can be traded.\n",
" context.shorts = []\n",
" for sec in pipe_results[pipe_results['shorts']].index.tolist():\n",
" if data.can_trade(sec):\n",
" context.shorts.append(sec)\n",
" #print('Shorts: ')\n",
" #print(context.shorts)\n",
" \n",
" \n",
" \n",
"def my_rebalance(context, data):\n",
" \"\"\"\n",
" Rebalance daily\n",
" \"\"\"\n",
" for stock in context.portfolio.positions:\n",
" #print('selling everything')\n",
" #print(stock)\n",
" order_target_percent(stock, 0.0) \n",
" # Calculate target weights to rebalance\n",
" #print(context)\n",
" target_weights = compute_target_weights(context, data)\n",
" #print(target_weights)\n",
"\n",
" # If we have target weights, rebalance our portfolio\n",
" if target_weights:\n",
" order_optimal_portfolio(\n",
" objective=opt.TargetWeights(target_weights),\n",
" constraints=[],\n",
" )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}