Skip to content

Commit

Permalink
Update 99 Examples.html
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekMelchin authored Sep 13, 2024
1 parent 1084113 commit 7b66b18
Showing 1 changed file with 72 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,83 +1,101 @@
<p>The following examples demonstrate some common practices for dataless scheduled universes.</p>

<h4>Example 1: Quarter End Selection</h4>
<p>The following example selects SPY if the current month is a quarter's end to ride on the possible quarter ending accounting period. Otherwise, it returns an empty universe.</p>
<p>The following example selects SPY on the last month of each quarter. For the remaining months, it selects no assets.</p>
<div class="section-example-container">
<pre class="csharp">public override void Initialize()
<pre class="csharp">public class DatalessScheduledUniverseDemoAlgorithm : QCAlgorithm
{
AddUniverse(new ScheduledUniverse(DateRules.MonthStart(), TimeRules.At(8, 0), (dt) =&gt;
public override void Initialize()
{
if (dt.Month % 3 == 0)
{
return new[] { Symbol.Create("SPY", SecurityType.Equity, Market.USA) };
}
return Enumrable.Empty&lt;Symbol&gt;();
}));
AddUniverse(new ScheduledUniverse(
DateRules.MonthStart(),
TimeRules.At(8, 0),
(dt) =>
{
if (dt.Month % 3 == 0)
{
return new[] { QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA) };
}
return Enumerable.Empty&lt;Symbol&gt;();
}
));
}
}</pre>
<pre class="python">def initialize(self):
self.add_universe(
ScheduledUniverse(
self.date_rules.month_start(),
self.time_rules.at(8, 0),
self._select_symbols
<pre class="python">class DatalessScheduledUniverseDemoAlgorithm(QCAlgorithm):

def initialize(self):
self.add_universe(
ScheduledUniverse(
self.date_rules.month_start(),
self.time_rules.at(8, 0),
self._select_symbols
)
)
)

def _select_symbols(self, dt: datetime) -&gt; List[Symbols]:
if dt.month % 3 == 0:
return [Symbol.create("SPY", SecurityType.EQUITY, Market.USA)]
return []</pre>

def _select_symbols(self, dt: datetime) -> List[Symbol]:
if dt.month % 3 == 0:
return [Symbol.create("SPY", SecurityType.EQUITY, Market.USA)]
return []</pre>
</div>

<h4>Example 2: Third Week VIX</h4>
<p>Standard Options expire at end of the the third week of each month. You can filter the universe to select VIX related products on the third week to trade the foreseeable increasing volatility.</p>
<p>Standard Options expire at end of the third week of each month. The following algorithm selects VIX-related products on the third week to trade the foreseeable increasing volatility:</p>
<div class="section-example-container">
<pre class="csharp">public class SampleAlgorithm : QCAlgorithm
<pre class="csharp">public class DatalessScheduledUniverseDemoAlgorithm : QCAlgorithm
{
private int _month = -1;
private int _week = -1;

public override void Initialize()
{
AddUniverse(new ScheduledUniverse(DateRules.WeekStart(), TimeRules.At(8, 0), (dt) =&gt;
{
if (dt.Month == _month)
AddUniverse(new ScheduledUniverse(
DateRules.WeekStart(),
TimeRules.At(8, 0),
(dt) =>
{
if (++_week == 3)
if (dt.Month == _month)
{
return new[] { Symbol.Create("VXZ", SecurityType.Equity, Market.USA) };
if (++_week == 3)
{
return new[] { QuantConnect.Symbol.Create("VXZ", SecurityType.Equity, Market.USA) };
}
return Enumerable.Empty&lt;Symbol&gt;();
}
return Enumrable.Empty&lt;Symbol&gt;();
_month = dt.Month;
_week = 0;
return Enumerable.Empty<Symbol>();
}

_month = dt.Month;
_week = 0;
return Enumrable.Empty&lt;Symbol&gt;();
}));
));
}
}</pre>
<pre class="python">def initialize(self):
self.month = -1
self.week = -1
<pre class="python">class DatalessScheduledUniverseDemoAlgorithm(QCAlgorithm):

self.add_universe(
ScheduledUniverse(
self.date_rules.week_start(),
self.time_rules.at(8, 0),
self._select_symbols
def initialize(self):
self._month = -1
self._week = -1

self.add_universe(
ScheduledUniverse(
self.date_rules.week_start(),
self.time_rules.at(8, 0),
self._select_symbols
)
)
)

def _select_symbols(self, dt: datetime) -&gt; List[Symbols]:
if dt.month == self.month:
self.week += 1
if self.week == 3:
return [Symbol.create("VXZ", SecurityType.EQUITY, Market.USA)]
return []

self.month = dt.month
self.week = 0
return []</pre>

def _select_symbols(self, dt: datetime) -> List[Symbol]:
if dt.month == self._month:
self._week += 1
if self._week == 3:
return [Symbol.create("VXZ", SecurityType.EQUITY, Market.USA)]
return []

self._month = dt.month
self._week = 0
return []</pre>
</div>

<h4>Other Examples</h4>
<p>For more examples, see the following algorithms:</p>
<div class="example-fieldset">
<div class="example-legend">Demonstration Algorithms</div>
<a class="python example-algorithm-link" href="https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/CustomDataUniverseScheduledRegressionAlgorithm.py" target="_BLANK">CustomDataUniverseScheduledRegressionAlgorithm.py <span class="badge-python pull-right">Python</span></a>
Expand Down

0 comments on commit 7b66b18

Please sign in to comment.