You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 <ahref="https://www.quantconnect.com/docs#Scheduled-Events">Documentation</a> or see the example <ahref="https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/ScheduledEventsAlgorithm.py">ScheduledEventsAlgorithm</a>.
Each month we get the historical prices of the DOW30 components using the <ahref="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.
26
26
</p>
27
27
<divclass="section-example-container">
28
28
<preclass="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>
33
33
</div>
34
34
<h3>Step 3: Symbol Selection Function</h3>
35
35
<p>
36
36
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.
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 <ahref="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 <ahref="https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders#Trading-and-Orders-Automatic-Position-Sizing-SetHoldings">SetHoldings</a>.
65
65
</p>
66
66
<divclass="section-example-container">
67
-
<preclass="python">def Rebalance(self):
67
+
<preclass="python">def _rebalance(self):
68
68
69
69
# 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)
76
76
77
77
# Liquidate positions that are not held by selected symbols
0 commit comments