Skip to content

Commit 2bca78e

Browse files
committed
Update strategy library tutorial QuantConnect#1
1 parent 6758098 commit 2bca78e

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

04 Strategy Library/01 CAPM Alpha Ranking Strategy on Dow 30 Companies/03 Method.html

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,44 @@ <h3>Step 1: Setup Event Handler</h3>
1515
In the initialize method we define a Scheduled Event to trigger a monthly re-balancing of the portfolio. For more details about how to use Scheduled Events, you can read the <a href="https://www.quantconnect.com/docs#Scheduled-Events">Documentation</a> or see the example <a href="https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/ScheduledEventsAlgorithm.py">ScheduledEventsAlgorithm</a>.
1616
</p>
1717
<div class="section-example-container">
18-
<pre class="python">def Initialize(self):
19-
self.Schedule.On(self.DateRules.MonthStart(self.symbols[0]),
20-
self.TimeRules.AfterMarketOpen(self.symbols[0]),
21-
self.Rebalance)</pre>
18+
<pre class="python">def initialize(self):
19+
self.schedule.on(self.date_rules.month_start(self._symbols[0]),
20+
self.time_rules.after_market_open(self._symbols[0]),
21+
self._rebalance)</pre>
2222
</div>
2323
<h3>Step 2: History Function</h3>
2424
<p>
2525
Each month we get the historical prices of the DOW30 components using the <a href="https://www.quantconnect.com/docs/algorithm-reference/historical-data">History</a> API. The data is returned from the API as a pandas.DataFrame indexed by <em>Symbol</em> objects. The close data is selected and the data frame is unstack to create columns of <em>Symbol</em> objects.
2626
</p>
2727
<div class="section-example-container">
2828
<pre class="python"># Fetch the historical data to perform the linear regression
29-
history = self.History(
30-
self.symbols + [self.benchmark],
31-
self.lookback,
32-
Resolution.Daily).close.unstack(level=0)</pre>
29+
history = self.history(
30+
self._symbols + [self._benchmark],
31+
self._lookback,
32+
Resolution.DAILY).close.unstack(level=0)</pre>
3333
</div>
3434
<h3>Step 3: Symbol Selection Function</h3>
3535
<p>
3636
We aim to trade the two assets with the highest alpha to the benchmark. In order to conduct linear regression to find the alpha (linear regression intercept), we need to compute returns (percentage change of closing price) benchmark and the asset then conduct a linear regression.
3737
</p>
3838
<div class="section-example-container">
39-
<pre class="python">def SelectSymbols(self, history):
39+
<pre class="python">def _select_symbols(self, history):
4040
'''Select symbols with the highest intercept/alpha to the benchmark
4141
'''
4242
alphas = dict()
4343

4444
# Get the benchmark returns
45-
benchmark = history[self.benchmark].pct_change().dropna()
45+
benchmark = history[self._benchmark].pct_change().dropna()
4646

4747
# Conducts linear regression for each symbol and save the intercept/alpha
48-
for symbol in self.symbols:
49-
48+
for symbol in self._symbols:
49+
5050
# Get the security returns
5151
returns = history[symbol].pct_change().dropna()
52-
returns = np.vstack([returns, np.ones(len(returns))]).T
52+
bla = np.vstack([benchmark, np.ones(len(returns))]).T
5353

5454
# Simple linear regression function in Numpy
55-
result = np.linalg.lstsq(returns, benchmark)
55+
result = np.linalg.lstsq(bla , returns)
5656
alphas[symbol] = result[0][1]
5757

5858
# Select symbols with the highest intercept/alpha to the benchmark
@@ -64,23 +64,23 @@ <h3>Step 4: Rebalance Function:</h3>
6464
This function is where all the action happens, it will be executed on the first trading day of each month as a scheduled event. The algorithm closes all positions of securities that were not selected using <a href="https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders#Trading-and-Orders-Liquidating-Portfolio">Liquidate</a> and go 100% long for both of the selected symbols using <a href="https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders#Trading-and-Orders-Automatic-Position-Sizing-SetHoldings">SetHoldings</a>.
6565
</p>
6666
<div class="section-example-container">
67-
<pre class="python">def Rebalance(self):
67+
<pre class="python">def _rebalance(self):
6868

6969
# Fetch the historical data to perform the linear regression
70-
history = self.History(
71-
self.symbols + [self.benchmark],
72-
self.lookback,
73-
Resolution.Daily).close.unstack(level=0)
74-
75-
symbols = self.SelectSymbols(history)
70+
history = self.history(
71+
self._symbols + [self._benchmark],
72+
self._lookback,
73+
Resolution.DAILY).close.unstack(level=0)
74+
75+
symbols = self._select_symbols(history)
7676

7777
# Liquidate positions that are not held by selected symbols
78-
for holdings in self.Portfolio.Values:
79-
symbol = holdings.Symbol
80-
if symbol not in symbols and holdings.Invested:
81-
self.Liquidate(symbol)
78+
for holdings in self.portfolio.values():
79+
symbol = holdings.symbol
80+
if symbol not in symbols and holdings.invested:
81+
self.liquidate(symbol)
8282

83-
# Invest 100% in the each of the selected symbols
83+
# Invest 100% in the selected symbols
8484
for symbol in symbols:
85-
self.SetHoldings(symbol, 1)</pre>
85+
self.set_holdings(symbol, 1)</pre>
8686
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
22
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
33
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
4-
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_9e87249fbe109fdfd9ce156c269807fc.html"></iframe>
4+
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_fde8160ae4ec5ce7349ad4db0e0f3764.html"></iframe>
55
</div>
66
</div>

04 Strategy Library/01 CAPM Alpha Ranking Strategy on Dow 30 Companies/05 算法.cn.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
55
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
66
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
7-
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache/embedded_backtest_2f525825fa6fb72ee4b23b9e63ecf025.html"></iframe>
7+
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_fde8160ae4ec5ce7349ad4db0e0f3764.html.html"></iframe>
88
</div>
99
</div>

0 commit comments

Comments
 (0)