Skip to content

Commit

Permalink
Merge pull request #1925 from QuantConnect/refactor-reality-model-sni…
Browse files Browse the repository at this point in the history
…ppets-wrapping

Refactor Method/Class Wrapping of Reality Modeling Snippets
  • Loading branch information
AlexCatarino authored Nov 5, 2024
2 parents 208997d + a1a8a37 commit a37df3a
Show file tree
Hide file tree
Showing 37 changed files with 555 additions and 245 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<p>The brokerage model of your algorithm automatically sets the fill model for each security, but you can override it. To manually set the fill model of a security, call the <code class="csharp">SetFillModel</code><code class="python">set_fill_model</code> method on the Security object.</p>
<div class="section-example-container">
<pre class="csharp">// In Initialize
var security = AddEquity("SPY");
security.SetFillModel(new ImmediateFillModel());</pre>
<pre class="python"># In Initialize
security = self.add_equity("SPY")
security.set_fill_model(ImmediateFillModel())</pre>
<pre class="csharp">public override void Initialize()
{
var security = AddEquity("SPY");
// Set the fill model for the requested security to backtest with the most realistic scenario
// ImmediateFillModel provide no delay from network issue or brokerage rules
security.SetFillModel(new ImmediateFillModel());
}</pre>
<pre class="python">def initialize(self) -&gt; None:
security = self.add_equity("SPY")
# Set the fill model for the requested security to backtest with the most realistic scenario
# ImmediateFillModel provide no delay from network issue or brokerage rules
security.set_fill_model(ImmediateFillModel())</pre>
</div>

<p>You can also set the fill model in a <a href='/docs/v2/writing-algorithms/initialization#07-Set-Security-Initializer'>security initializer</a>. If your algorithm has a dynamic universe, use the security initializer technique. In order to initialize single security subscriptions with the security initializer, call <code class="csharp">SetSecurityInitializer</code><code class="python">set_security_initializer</code> before you create the subscriptions.</p><p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<p>Fill Models should extend the <code>FillModel</code> class. To implement your own fill model, override the methods in the <code>FillModel</code> class you wish to change.
The class has a dedicated method for each order type. Most of the methods receive a <code>Security</code> and <code>Order</code> object and return an <code>OrderEvent</code> object that contains information about the order status, fill quantity, and errors. <br></p>
<div class="section-example-container">
<pre class="csharp">// In the Initialize method, set the custom fill model
security.SetFillModel(new MyFillModel());

<pre class="csharp">public class CustomFillModelExampleAlgorithm : QCAlgorithm
{
public override void Initialize()
{
// In the Initialize method, set the custom fill model for an added security to use the custom model
var security = AddEquity("SPY");
security.SetFillModel(new MyFillModel());
}
}

// Define the custom fill model outside of the algorithm
public class MyFillModel : FillModel {

Expand Down Expand Up @@ -52,8 +59,11 @@
}
}
</pre>
<pre class="python"># In the Initialize method, set the custom fill model
security.set_fill_model(MyFillModel())
<pre class="python">class CustomFillModelExampleAlgorithm(QCAlgorithm):
def initialize(self) -&gt; None:
# In the Initialize method, set the custom fill model for an added security to use the custom model
security = self.add_equity("SPY")
security.set_fill_model(MyFillModel())

# Define the custom fill model outside of the algorithm
class MyFillModel(FillModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<p>The brokerage model of your algorithm automatically sets the slippage model for each security, but you can override it. To manually set the slippage model of a security, call the <code class="csharp">SetSlippageModel</code><code class="python">set_slippage_model</code> method on the <code>Security</code> object.</p>

<div class="section-example-container">
<pre class="csharp">// In Initialize
var security = AddEquity("SPY");
security.SetSlippageModel(new VolumeShareSlippageModel());</pre>
<pre class="python"># In Initialize
security = self.add_equity("SPY")
security.set_slippage_model(VolumeShareSlippageModel())</pre>
<pre class="csharp">public override void Initialize()
{
var security = AddEquity("SPY");
// Set the slippage model for the requested security to backtest with the most realistic scenario
// VolumeShareSlippageModel has slippage size affected by the volume of the order compared to the actual filled volume of the bar
// It is only valid for the security with a volume property, while being more accurate with denser resolution
security.SetSlippageModel(new VolumeShareSlippageModel());
}</pre>
<pre class="python">def initialize(self) -&gt; None:
security = self.add_equity("SPY")
# Set the slippage model for the requested security to backtest with the most realistic scenario
# VolumeShareSlippageModel has slippage size affected by the volume of the order compared to the actual filled volume of the bar
# It is only valid for the security with a volume property, while being more accurate with denser resolution
security.set_slippage_model(VolumeShareSlippageModel())</pre>
</div>

<p>You can also set the slippage model in a <a href='/docs/v2/writing-algorithms/initialization#07-Set-Security-Initializer'>security initializer</a>. If your algorithm has a dynamic universe, use the security initializer technique. In order to initialize single security subscriptions with the security initializer, call <code class="csharp">SetSecurityInitializer</code><code class="python">set_security_initializer</code> before you create the subscriptions.</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<p>Slippage models should implement the <code>ISlippageModel</code> interface. Extensions of the <code>ISlippageModel</code> interface must implement the <code class="csharp">GetSlippageApproximation</code><code class="python">get_slippage_approximation</code> method, which calculates the slippage quantity.</p>

<div class="section-example-container">
<pre class="csharp">// In the Initialize method, set the slippage model
security.SetSlippageModel(new MySlippageModel());
<pre class="csharp">public class CustomSlippageModelExampleAlgorithm : QCAlgorithm
{
public override void Initialize()
{
// In the Initialize method, set the custom slippage model for an added security to use the custom model
var security = AddEquity("SPY");
security.SetSlippageModel(new MySlippageModel());
}
}

// Define the custom slippage model
public class MySlippageModel : ISlippageModel
Expand All @@ -13,8 +20,11 @@
}
}
</pre>
<pre class="python"># In the Initialize method, set the slippage model
security.set_slippage_model(MySlippageModel())
<pre class="python">class CustomSlippageModelExampleAlgorithm(QCAlgorithm):
def initialize(self) -&gt; None:
# In the Initialize method, set the custom slippage model for an added security to use the custom model
security = self.add_equity("SPY")
security.set_slippage_model(MySlippageModel())

# Define the custom slippage model
class MySlippageModel:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
<pre class="csharp">public override Initialize()
{
var security = AddEquity("SPY");
// Set the fee model for the requested security to backtest with the most realistic scenario
// Constant fee model of 0 dollar is accurate for the commission-free brokerage
security.SetFeeModel(new ConstantFeeModel(0));
}</pre>
<pre class="python">def initialize(self):
security = self.add_equity("SPY")
# Set the fee model for the requested security to backtest with the most realistic scenario
# Constant fee model of 0 dollar is accurate for the commission-free brokerage
security.set_fee_model(ConstantFeeModel(0))</pre>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<p>Fee models should extend the <code>FeeModel</code> class. Extensions of the <code>FeeModel</code> class must implement the <code class="csharp">GetOrderFee</code><code class="python">get_order_fee</code> method, which receives <code>OrderFeeParameters</code> and returns an <code>OrderFee</code> that represents a cash amount in a currency.</p>

<div class="section-example-container">
<pre class="csharp">// In the Initialize method, set the fee model
security.SetFeeModel(new MyFeeModel());
<pre class="csharp">public class CustomFeeModelExampleAlgorithm : QCAlgorithm
{
public override void Initialize()
{
// In the Initialize method, set the custom fee model for an added security to use the custom model
var security = AddEquity("SPY");
security.SetFeeModel(new MyFeeModel());
}
}

// Define the custom fee model
public class MyFeeModel : FeeModel
Expand All @@ -12,8 +19,11 @@
return new OrderFee(new CashAmount(0.5m, "USD"));
}
}</pre>
<pre class="python"># In the Initialize method, set the fee model
security.set_fee_model(MyFeeModel())
<pre class="python">class CustomFeeModelExampleAlgorithm(QCAlgorithm):
def initialize(self) -&gt; None:
# In the Initialize method, set the custom fee model for an added security to use the custom model
security = self.add_equity("SPY")
security.set_fee_model(MyFeeModel())

# Define the custom fee model
class MyFeeModel(FeeModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<p>To set a brokerage model, in the <code class="csharp">Initialize</code><code class="python">initialize</code> method, call the <code class="csharp">SetBrokerageModel</code><code class="python">set_brokerage_model</code> method with a <code>BrokerageName</code> and an <code>AccountType</code>. If you set a brokerage model, it overrides any security level models you manually set in your algorithm.</p>

<div class="section-example-container">
<pre class="csharp">SetBrokerageModel(BrokerageName.OandaBrokerage); // Defaults to margin account
SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin); // Overrides the default account type
</pre>
<pre class="python">self.set_brokerage_model(BrokerageName.OANDA_BROKERAGE) # Defaults to margin account
self.set_brokerage_model(BrokerageName.BITFINEX, AccountType.MARGIN) # Overrides the default account type
<pre class="csharp">public override void Initialize()
{
// Set the brokerage model to backtest with the most realistic scenario to handle the order validity, margin rate and transaction fee
SetBrokerageModel(BrokerageName.OandaBrokerage); // Defaults to margin account
SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin); // Overrides the default account type, which is AccountType.Cash
}</pre>
<pre class="python">def initialize(self) -&gt; None:
# Set the brokerage model to backtest with the most realistic scenario to handle the order validity, margin rate and transaction fee
self.set_brokerage_model(BrokerageName.OANDA_BROKERAGE) # Defaults to margin account
self.set_brokerage_model(BrokerageName.BITFINEX, AccountType.MARGIN) # Overrides the default account type, which is AccountType.Cash
</pre></div>

<p>In live trading, LEAN doesn't ignore your <code class="csharp">SetBrokerageModel</code><code class="python">set_brokerage_model</code> method calls. LEAN uses some of the brokerage model rules to catch invalid orders before they reach your real brokerage.</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<p>Brokerage models should extend the <code>DefaultBrokerageModel</code> class. Extensions of the <code>DefaultBrokerageModel</code> class should implement the following methods:</p>

<div class="section-example-container">
<pre class="csharp">// In the Initialize method, set the custom brokerage model
SetBrokerageModel(new MyBrokerageModel());
<pre class="csharp">public class CustomBrokerageModelExampleAlgorithm : QCAlgorithm
{
public override void Initialize()
{
// In the Initialize method, set the custom brokerage model
SetBrokerageModel(new MyBrokerageModel());
}
}

// Define the custom brokerage model outside of the algorithm
class MyBrokerageModel : DefaultBrokerageModel
Expand Down Expand Up @@ -88,8 +94,10 @@
}
}
</pre>
<pre class="python"># In the Initialize method, set the custom brokerage model
self.set_brokerage_model(MyBrokerageModel())
<pre class="python">class CustomBrokerageModelExampleAlgorithm(QCAlgorithm):
def initialize(self) -&gt; None:
# In the Initialize method, set the custom brokerage model
self.set_brokerage_model(MyBrokerageModel())

# Define the custom brokerage model outside of the algorithm
class MyBrokerageModel(DefaultBrokerageModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<p>To set a brokerage message handler, in the <code class="csharp">Initialize</code><code class="python">initialize</code> method, call the <code class="csharp">SetBrokerageMessageHandler</code><code class="python">set_brokerage_message_handler</code> method.</p>

<div class="section-example-container">
<pre class="csharp">SetBrokerageMessageHandler(new MyBrokerageMessageHandler(this));</pre>
<pre class="python">self.set_brokerage_message_handler(MyBrokerageMessageHandler(self))</pre>
<pre class="csharp">public override void Initialize()
{
// Set a custom brokerage message handler for the algorithm in Initialize method
// It handles different type of returned message from the broker, helping you filter or abstract for the ones you care about
SetBrokerageMessageHandler(new MyBrokerageMessageHandler(this));
}</pre>
<pre class="python">def initialize(self) -&gt; None:
# Set a custom brokerage message handler for the algorithm in Initialize method
# It handles different type of returned message from the broker, helping you filter or abstract for the ones you care about
self.set_brokerage_message_handler(MyBrokerageMessageHandler(self))</pre>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
<p>The <code class="csharp">HandleOrder</code><code class="python">handle_order</code> method defines whether the transaction handler should process a new order you placed directly through the brokerage's website or third-party software.</p>

<div class="section-example-container">
<pre class="csharp">// In the Initialize method, set the brokerage message handler
SetBrokerageMessageHandler(new MyBrokerageMessageHandler(this));
<pre class="csharp">public class CustomBrokerageMessageHandlerExampleAlgorithm : QCAlgorithm
{
public override void Initialize()
{
// In the Initialize method, set the brokerage message handler
SetBrokerageMessageHandler(new MyBrokerageMessageHandler(this));
}
}

// Define the custom brokerage message handler
public class MyBrokerageMessageHandler : DefaultBrokerageMessageHandler
Expand All @@ -31,8 +37,10 @@
return false;
}
}</pre>
<pre class="python"># In the Initialize method, set the brokerage message handler
self.set_brokerage_message_handler(MyBrokerageMessageHandler(self))
<pre class="python">class CustomBrokerageMessageHandlerExampleAlgorithm(QCAlgorithm):
def initialize(self) -&gt; None:
# In the Initialize method, set the brokerage message handler
self.set_brokerage_message_handler(MyBrokerageMessageHandler(self))

# Define the custom brokerage message handler
class MyBrokerageMessageHandler(DefaultBrokerageMessageHandler):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<p>The brokerage model of your algorithm automatically sets the buying power model for each security, but you can override it. To manually set the buying power model of a security, call the <code class="csharp">SetBuyingPowerModel</code><code class="python">set_buying_power_model</code> method on the <code>Security</code> object.</p>

<div class="section-example-container">
<pre class="csharp">// In Initialize
var security = AddEquity("SPY");
security.SetBuyingPowerModel(new SecurityMarginModel(3m));</pre>
<pre class="python"># In Initialize
security = self.add_equity("SPY")
security.set_buying_power_model(SecurityMarginModel(3))</pre>
<pre class="csharp">public override void Initialize()
{
var security = AddEquity("SPY");
// Use 3x leverage on the selected security, which mimic the actual buying power of your broker
security.SetBuyingPowerModel(new SecurityMarginModel(3m));
}</pre>
<pre class="python">def initialize(self) -&gt; None:
security = self.add_equity("SPY")
# Use 3x leverage on the selected security, which mimic the actual buying power of your broker
security.set_buying_power_model(SecurityMarginModel(3))</pre>
</div>

<p>You can also set the buying power model in a <a href='/docs/v2/writing-algorithms/initialization#07-Set-Security-Initializer'>security initializer</a>. If your algorithm has a universe, use the security initializer technique. In order to initialize single security subscriptions with the security initializer, call <code class="csharp">SetSecurityInitializer</code><code class="python">set_security_initializer</code> before you create the subscriptions.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
Extensions of the <code>BuyingPowerModel</code> class should implement the following methods: </p>

<div class="section-example-container">
<pre class="csharp">// In the Initialize method, set the buying power model
security.SetBuyingPowerModel(new MyBuyingPowerModel());
<pre class="csharp">public class CustomBuyingPowerModelExampleAlgorithm : QCAlgorithm
{
public override void Initialize()
{
var security = AddEquity("SPY");
// Set the custom buying power model of the selected security to mimic its actual buying power in your used broker
security.SetBuyingPowerModel(new MyBuyingPowerModel());
}
}

// Define the custom buying power model
class MyBuyingPowerModel : BuyingPowerModel
Expand Down Expand Up @@ -77,8 +84,11 @@
return base.GetBuyingPower(parameters);
}
}</pre>
<pre class="python"># In the Initialize method, set the buying power model
security.set_buying_power_model(MyBuyingPowerModel())
<pre class="python">class CustomBuyingPowerModelExampleAlgorithm(QCAlgorithm):
def initialize(self) -&gt; None:
security = self.add_equity("SPY")
# Set the custom buying power model of the selected security to mimic its actual buying power in your used broker
security.set_buying_power_model(MyBuyingPowerModel())

# Define the custom buying power model
class MyBuyingPowerModel(BuyingPowerModel):
Expand Down
Loading

0 comments on commit a37df3a

Please sign in to comment.