Skip to content

Commit

Permalink
Moves cash brokerage/IExecutionModel test to post-init (QuantConnect#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aarjaneiro authored Oct 12, 2020
1 parent 03efc1b commit 888c443
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Algorithm/QCAlgorithm.Framework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public partial class QCAlgorithm
/// </summary>
public void FrameworkPostInitialize()
{
//Prevents execution in the case of cash brokerage with IExecutionModel and IPortfolioConstructionModel
if (PortfolioConstruction.GetType() != typeof(NullPortfolioConstructionModel)
&& Execution.GetType() != typeof(NullExecutionModel)
&& BrokerageModel.AccountType == AccountType.Cash)
{
throw new InvalidOperationException($"Non null {nameof(IExecutionModel)} and {nameof(IPortfolioConstructionModel)} are currently unsuitable for Cash Modeled brokerages (e.g. GDAX) and may result in unexpected trades."
+ " To prevent possible user error we've restricted them to Margin trading. You can select margin account types with"
+ $" SetBrokerage( ... AccountType.Margin). Or please set them to {nameof(NullExecutionModel)}, {nameof(NullPortfolioConstructionModel)}");
}
foreach (var universe in UniverseSelection.CreateUniverses(this))
{
// on purpose we don't call 'AddUniverse' here so that these universes don't get registered as user added
Expand Down Expand Up @@ -217,16 +226,7 @@ private void ProcessInsights(Insight[] insights)
Log($"{Time}: RISK ADJUSTED TARGETS: {string.Join(" | ", riskAdjustedTargets.Select(t => t.ToString()).OrderBy(t => t))}");
}
}

if (riskAdjustedTargets.Length > 0
&& Execution.GetType() != typeof(NullExecutionModel)
&& BrokerageModel.AccountType == AccountType.Cash)
{
throw new InvalidOperationException($"Non null {nameof(IExecutionModel)} and {nameof(IPortfolioConstructionModel)} are currently unsuitable for Cash Modeled brokerages (e.g. GDAX) and may result in unexpected trades."
+ " To prevent possible user error we've restricted them to Margin trading. You can select margin account types with"
+ $" SetBrokerage( ... AccountType.Margin). Or please set them to {nameof(NullExecutionModel)}, {nameof(NullPortfolioConstructionModel)}");
}


Execution.Execute(this, riskAdjustedTargets);
}

Expand Down

0 comments on commit 888c443

Please sign in to comment.