Skip to content

Commit

Permalink
Inline PEP8: Writing Algoritm > Universes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCatarino committed Apr 25, 2024
1 parent 2120682 commit 93d3bfa
Show file tree
Hide file tree
Showing 79 changed files with 335 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
}</pre>
<pre class="python">def on_securities_changed(self, changes: SecurityChanges) -> None:
for security in changes.added_securities:
for security in changes.added_securities::
self.debug(f"{self.time}: Added {security.symbol}")

for security in changes.removed_securities:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
<td>
<h4>Property: <span><code class="csharp">DataNormalizationMode</code><code class="pythpn">data_normalization_mode</code></span></h4>
<p class='property-description'>How should historical prices be adjusted? This setting is only available for Equity and Futures assets.</p>
<p>Data Type: <span><code>DataNormalizationMode</code></span><span class='pipe-separator'> | </span> Default Value: <span><code>DataNormalizationMode.Adjusted</code></span></p>
<p>Data Type: <span><code>DataNormalizationMode</code></span><span class='pipe-separator'> | </span> Default Value: <span><code class="csharp">DataNormalizationMode.Adjusted</code><code class="python">DataNormalizationMode.ADJUSTED</code></span></p>
</td>
</tr>
<tr>
<td>
<h4>Property: <span><code class="csharp">Leverage</code><code class="python">leverage</code></span></h4>
<p class='property-description'>What leverage should assets use in the universe? This setting is not available for derivative assets.</p>
<p>Data Type: <span><code class='csharp'>decimal</code><code class='python'>float</code></span><span class='pipe-separator'> | </span> Default Value: <span><code>Security.NullLeverage</code></span></p>
<p>Data Type: <span><code class='csharp'>decimal</code><code class='python'>float</code></span><span class='pipe-separator'> | </span> Default Value: <span><code class="csharp">Security.NullLeverage</code><code class="python">Security.NULL_LEVERAGE</code></span></p>
</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<td>
<h4>Property: <span><code class="csharp">FreePortfolioValue</code><code class="python">free_portfolio_value</code></span></h4>
<p class="property-description">The <a href="https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/position-sizing#05-Buying-Power-Buffer">buying power buffer </a>value.</p>
<p>Data Type: <span><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>250</code></span></p>
<p>Data Type: <span class="csharp"><code>decimal</code></span><span class="python"><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>250</code><code class="csharp">m</code></span></p>
</td>
</tr>
<tr>
<td>
<h4>Property: <span><code class="csharp">FreePortfolioValuePercentage</code><code class="python">free_portfolio_value_percentage</code></span></h4>
<p class="property-description">The buying power buffer percentage value.</p>
<p>Data Type: <span><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>0.0025</code></span></p>
<p>Data Type: <span class="csharp"><code>decimal</code></span><span class="python"><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>0.0025</code><code class="csharp">m</code></span></p>
</td>
</tr>
<tr>
Expand All @@ -27,21 +27,21 @@ <h4>Property: <span><code class="csharp">LiquidateEnabled</code><code class="pyt
<td>
<h4>Property: <span><code class="csharp">MaxAbsolutePortfolioTargetPercentage</code><code class="python">max_absolute_portfolio_target_percentage</code></span></h4>
<p class="property-description">The absolute maximum valid total portfolio value target percentage.</p>
<p>Data Type: <span><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>1000000000</code></span></p>
<p>Data Type: <span class="csharp"><code>decimal</code></span><span class="python"><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>1000000000</code><code class="csharp">m</code></span></p>
</td>
</tr>
<tr>
<td>
<h4>Property: <span><code class="csharp">MinAbsolutePortfolioTargetPercentage</code><code class="python">min_absolute_portfolio_target_percentage</code></span></h4>
<p class="property-description">The absolute minimum valid total portfolio value target percentage.</p>
<p>Data Type: <span><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>0.0000000001</code></span></p>
<p>Data Type: <span class="csharp"><code>decimal</code></span><span class="python"><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>0.0000000001</code><code class="csharp">m</code></span></p>
</td>
</tr>
<tr>
<td>
<h4>Property: <span><code class="csharp">MinimumOrderMarginPortfolioPercentage</code><code class="python">minimum_order_margin_portfolio_percentage</code></span></h4>
<p class="property-description">The minimum order margin portfolio percentage to ignore bad orders and orders with small sizes.</p>
<p>Data Type: <span><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code class='csharp'>0.001m</code><code class='python'>0.001</code></span></p>
<p>Data Type: <span class="csharp"><code>decimal</code></span><span class="python"><code>float</code></span><span class="pipe-separator"> | </span> Default Value: <span><code>0.001</code><code class="csharp">m</code></span></p>
</td>
</tr>
<tr>
Expand All @@ -62,7 +62,7 @@ <h4>Property: <span><code class="csharp">RebalancePortfolioOnSecurityChanges</co
<td>
<h4>Property: <span><code class="csharp">StalePriceTimeSpan</code><code class="python">stale_price_time_span</code></span></h4>
<p class="property-description">The minimum time span elapsed to consider a market fill price as stale</p>
<p>Data Type: <span><code>timedelta</code></span><span class="pipe-separator"> | </span> Default Value: <span><code class='csharp'>TimeSpan.FromHours(1)</code><code class='python'>timedelta(hours=1)</code></span></p>
<p>Data Type: <span class="csharp"><code>TimeSpan</code></span><span class="python"><code>timedelta</code></span><span class="pipe-separator"> | </span> Default Value: <span><code class='csharp'>TimeSpan.FromHours(1)</code><code class='python'>timedelta(hours=1)</code></span></p>
</td>
</tr>
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div class="section-example-container">
<pre class="csharp">_symbol = AddCfd("SPY", market: Market.InteractiveBrokers).Symbol;</pre>
<pre class="python">self._symbol = self.add_cfd("SPY", market = Market.INTERACTIVE_BROKERS).symbol</pre>
<pre class="python">self._symbol = self.add_cfd("SPY", market=Market.INTERACTIVE_BROKERS).symbol</pre>
</div>

<p>Historical data for backtesting IB CFDs is unavailable.</p>
Expand All @@ -22,7 +22,7 @@
_symbol = QuantConnect.Symbol.Create("SPY", securityType, Market.InteractiveBrokers);
AddSecurity(_symbol);</pre>
<pre class="python">security_type = SecurityType.CFD if self.live_mode else SecurityType.EQUITY
market = Market.INTERACTIVE_BROKERS if self.live_mode else Market.USA
market=Market.INTERACTIVE_BROKERS if self.live_mode else Market.USA
self._symbol = Symbol.create("SPY", security_type, market)
self.add_security(self.symbol)</pre>
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>When your universe adds and removes assets, LEAN notifies your algorithm through the <code class="csharp">OnSecuritiesChanged</code><code class="python">on_securities_changed</code> event handler. The event handler receives a <code>SecurityChanges</code> object, which contains references to the added and removed securities. To access the added securities, check the <code>changes.AddedSecurities</code> property. To access the removed securities, check the <code>changes.RemovedSecurities</code> property.</p>
<p>When your universe adds and removes assets, LEAN notifies your algorithm through the <code class="csharp">OnSecuritiesChanged</code><code class="python">on_securities_changed</code> event handler. The event handler receives a <code>SecurityChanges</code> object, which contains references to the added and removed securities. To access the added securities, check the <code class="csharp">changes.AddedSecurities</code><code class="python">changes.added_securities</code> method property. To access the removed securities, check the <code class="csharp">changes.RemovedSecurities</code><code class="python">changes.removed_securities</code> method property.</p>

<div class="section-example-container">
<pre class="csharp">public override void OnSecuritiesChanged(SecurityChanges changes)
Expand All @@ -18,7 +18,7 @@
}
}</pre>
<pre class="python">def on_securities_changed(self, changes: SecurityChanges) -> None:
for security in changes.added_securities:
for security in changes.added_securities::
self.debug(f"{self.time}: Added {security.symbol}")

for security in changes.removed_securities:
Expand All @@ -45,7 +45,7 @@

def on_securities_changed(self, changes: SecurityChanges) -> None:
for security in changes.removed_securities:
if security in self.securities:
if security in self._securities:
self._securities.remove(security)

self._securities.extend(changes.added_securities)</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
DeregisterIndicator((security as dynamic).Indicator);
}
}</pre>
<pre class="python">def OnSecuritiesChanged(self, changes: SecurityChanges) -> None:
for security in changes.AddedSecurities:
<pre class="python">def on_securities_changed(self, changes: SecurityChanges) -> None:
for security in changes.added_securities:
# Create an indicator
security.indicator = self.sma(security.Symbol, 10)

# Warm up the indicator
self.WarmUpIndicator(security.Symbol, security.indicator)
self.warm_up_indicator(security.symbol, security.indicator)

for security in changes.RemovedSecurities:
self.DeregisterIndicator(security.indicator)</pre>
for security in changes.removed_securities:
self.deregister_indicator(security.indicator)</pre>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
self._universe = self.add_universe(self.my_fundamental_filter_function)

# In OnData
universe_members = self.universe_manager[self.universe.configuration.symbol].members
universe_members = self.universe_manager[self._universe.configuration.symbol].members
for kvp in universe_members:
symbol = kvp.key
security = kvp.value</pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p>
The <code>Selected</code> property of your <code>Universe</code> contains references to all the assets that are currently in the universe.
The <code>Universe.Selected</code> property differs from the <code>Universe.Members</code> property because the <code>Universe.Members</code> property can contain more assets than <code>Universe.Selected</code>.
The <code>QCAlgorithm.ActiveSecurities</code> is a collection of <code>Universe.Members</code> of all universes.
The <code class="csharp">Selected</code><code class="python">selected</code> property of your <code>Universe</code> contains references to all the assets that are currently in the universe.
The <code class="csharp">Universe.Selected</code><code class="python">Universe.selected</code> property differs from the <code class="csharp">Universe.Members</code><code class="python">Universe.members</code> property because the <code class="csharp">Universe.Members</code><code class="python">Universe.members</code> property can contain more assets than <code class="csharp">Universe.Selected</code><code class="python">Universe.selected</code>.
The <code class="csharp">QCAlgorithm.ActiveSecurities</code><code class="python">QCAlgorithm.active_securities</code> is a collection of <code class="csharp">Universe.Members</code><code class="python">Universe.members</code> of all universes.
To access the <code>Universe</code> object, save a reference to the result of the <code class="csharp">AddUniverse</code><code class="python">add_universe</code> method.
The following algorithm demonstrates how to use the <code>Universe.Selected</code> property to create simple rebalancing strategies:
The following algorithm demonstrates how to use the <code class="csharp">Universe.Selected</code><code class="python">Universe.selected</code> property to create simple rebalancing strategies:
</p>

<div class="section-example-container">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<? include(DOCS_RESOURCES."/universes/settings/extended-market-hours.php"); ?>

<p>To enable extended market hours in non-derivative universes, in the <a href='/docs/v2/writing-algorithms/initialization'>Initialize</a> method, adjust the algorithm's <code>UniverseSettings</code> before you add the universe.</p>
<p>To enable extended market hours in non-derivative universes, in the <a href='/docs/v2/writing-algorithms/initialization'>Initialize</a> method, adjust the algorithm's <code class="csharp">UniverseSettings</code><code class="python">universe_settings</code> before you add the universe.</p>

<div class="section-example-container">
<pre class="csharp">UniverseSettings.ExtendedMarketHours = true;
Expand All @@ -13,5 +13,5 @@

<div class="section-example-container">
<pre class="csharp">AddFuture(Futures.Currencies.BTC, extendedMarketHours: true);</pre>
<pre class="python">self.add_universe(Futures.Currencies.BTC, extended_market_hours=True)</pre>
<pre class="python">self.add_future(Futures.Currencies.BTC, extended_market_hours=True)</pre>
</div>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p>The <code>DataNormalizationMode</code> setting is an enumeration that defines how historical data is adjusted. This setting is only applicable for US Equities and Futures.
<p>The <code class="csharp">DataNormalizationMode</code><code class="python">data_normalization_mode</code> setting is an enumeration that defines how historical data is adjusted. This setting is only applicable for US Equities and Futures.

<h4>US Equities</h4>

<p>In the case of US Equities, the data normalization mode affects how historical data is adjusted for <a href='/docs/v2/writing-algorithms/securities/asset-classes/us-equity/corporate-actions'>corporate actions</a>. To view all the available options, see <a href='/docs/v2/writing-algorithms/securities/asset-classes/us-equity/requesting-data#11-Data-Normalization'>Data Normalization</a>. The default value is <code>DataNormalizationMode.Adjusted</code>. To change the data normalization mode, in the <a href='/docs/v2/writing-algorithms/initialization'>Initialize</a> method, adjust the algorithm's <code>UniverseSettings</code> before you add the universe.</p>
<p>In the case of US Equities, the data normalization mode affects how historical data is adjusted for <a href='/docs/v2/writing-algorithms/securities/asset-classes/us-equity/corporate-actions'>corporate actions</a>. To view all the available options, see <a href='/docs/v2/writing-algorithms/securities/asset-classes/us-equity/requesting-data#11-Data-Normalization'>Data Normalization</a>. The default value is <code class="csharp">DataNormalizationMode.Adjusted</code><code class="python">DataNormalizationMode.ADJUSTED</code>. To change the data normalization mode, in the <a href='/docs/v2/writing-algorithms/initialization'>Initialize</a> method, adjust the algorithm's <code class="csharp">UniverseSettings</code><code class="python">universe_settings</code> before you add the universe.</p>
<div class="section-example-container">
<pre class="csharp">UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw;
Expand All @@ -12,7 +12,7 @@
</div>
<h4>Futures</h4>
<p>In the case of Futures, the data normalization mode affects how historical data of two contracts is stitched together to form the <a href='/docs/v2/writing-algorithms/universes/futures#12-Continous-Contracts'>continuous contract</a>. To view all the available options, see <a href='/docs/v2/writing-algorithms/securities/asset-classes/futures/requesting-data#09-Data-Normalization'>Data Normalization</a>. The default value is <code>DataNormalizationMode.Adjusted</code>. To change the data normalization mode, in the <a href='/docs/v2/writing-algorithms/initialization'>Initialize</a> method, pass a <code class="csharp">dataNormalizationMode</code><code class="python">data_normalization_mode</code> argument to the <code class="csharp">AddFuture</code><code class="python">add_future</code> method.</p>
<p>In the case of Futures, the data normalization mode affects how historical data of two contracts is stitched together to form the <a href='/docs/v2/writing-algorithms/universes/futures#12-Continous-Contracts'>continuous contract</a>. To view all the available options, see <a href='/docs/v2/writing-algorithms/securities/asset-classes/futures/requesting-data#09-Data-Normalization'>Data Normalization</a>. The default value is <code class="csharp">DataNormalizationMode.Adjusted</code><code class="python">DataNormalizationMode.ADJUSTED</code>. To change the data normalization mode, in the <a href='/docs/v2/writing-algorithms/initialization'>Initialize</a> method, pass a <code class="csharp">dataNormalizationMode</code><code class="python">data_normalization_mode</code> argument to the <code class="csharp">AddFuture</code><code class="python">add_future</code> method.</p>
<div class="section-example-container">
<pre class="csharp">AddFuture(Futures.Currencies.BTC, dataNormalizationMode: DataNormalizationMode.BackwardsRatio);</pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<? include(DOCS_RESOURCES."/universes/settings/contract-depth-offset.php"); ?> pass a <code>contractDepthOffset</code> argument to the <code class="csharp">AddFuture</code><code class="python">add_future</code> method.</p>
<? include(DOCS_RESOURCES."/universes/settings/contract-depth-offset.php"); ?> pass a <code class="csharp">contractDepthOffset</code><code class="python">contract_depth_offset</code> argument to the <code class="csharp">AddFuture</code><code class="python">add_future</code> method.</p>

<div class="section-example-container">
<pre class="csharp">AddFuture(Futures.Currencies.BTC, contractDepthOffset: 3);</pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p>
The <code>Schedule</code> setting defines the selection schedule of the universe.
The <code class="csharp">Schedule</code><code class="python">schedule</code> setting defines the selection schedule of the universe.
Most universes run on a daily schedule.
To change the selection schedule, call the <code>UniverseSettings.Schedule.On</code> method with an <code>IDateRule</code> object before you add the universe.
To change the selection schedule, call the <code class="csharp">UniverseSettings.Schedule.On</code><code class="python">universe_settings.schedule.on</code> method with an <code>IDateRule</code> object before you add the universe.
</p>

<div class="section-example-container">
Expand Down
Loading

0 comments on commit 93d3bfa

Please sign in to comment.