Skip to content

Commit 9172bf0

Browse files
committed
Addresses Peer-Review
The `AlphaStreamsBrokerageModel` was unnecesarily duplicating the `GetBuyingPowerModel` from the base class. Missing docs were added.
1 parent 63bd501 commit 9172bf0

3 files changed

Lines changed: 43 additions & 56 deletions

File tree

‎Algorithm.CSharp/BasicTemplateFuturesAlgorithm.cs‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace QuantConnect.Algorithm.CSharp
3434
/// <meta name="tag" content="futures" />
3535
public class BasicTemplateFuturesAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
3636
{
37+
private Symbol _contractSymbol;
38+
3739
// S&P 500 EMini futures
3840
private const string RootSP500 = Futures.Indices.SP500EMini;
3941
public Symbol SP500 = QuantConnect.Symbol.Create(RootSP500, SecurityType.Future, Market.USA);
@@ -82,12 +84,8 @@ select futuresContract
8284
// if found, trade it
8385
if (contract != null)
8486
{
85-
// Get the margin requirements
86-
var model = Securities[contract.Symbol].BuyingPowerModel as FutureMarginModel;
87-
var initialOvernight = model?.InitialMarginRequirement;
88-
var maintenanceOvernight = model?.MaintenanceMarginRequirement;
89-
90-
MarketOrder(contract.Symbol, 1);
87+
_contractSymbol = contract.Symbol;
88+
MarketOrder(_contractSymbol, 1);
9189
}
9290
}
9391
}
@@ -97,6 +95,19 @@ select futuresContract
9795
}
9896
}
9997

98+
public override void OnEndOfAlgorithm()
99+
{
100+
// Get the margin requirements
101+
var buyingPowerModel = Securities[_contractSymbol].BuyingPowerModel;
102+
var futureMarginModel = buyingPowerModel as FutureMarginModel;
103+
if (buyingPowerModel == null)
104+
{
105+
throw new Exception($"Invalid buying power model. Found: {buyingPowerModel.GetType().Name}. Expected: {nameof(FutureMarginModel)}");
106+
}
107+
var initialOvernight = futureMarginModel?.InitialMarginRequirement;
108+
var maintenanceOvernight = futureMarginModel?.MaintenanceMarginRequirement;
109+
}
110+
100111
/// <summary>
101112
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
102113
/// </summary>

‎Algorithm.Python/BasicTemplateFuturesAlgorithm.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def Initialize(self):
3737
self.SetEndDate(2013, 10, 10)
3838
self.SetCash(1000000)
3939

40+
self.contractSymbol = None
41+
4042
# Subscribe and set our expiry filter for the futures chain
4143
futureES = self.AddFuture(Futures.Indices.SP500EMini)
4244
futureES.SetFilter(timedelta(0), timedelta(182))
@@ -58,11 +60,16 @@ def OnData(self,slice):
5860
if len(contracts) == 0: continue
5961
front = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]
6062

61-
# Get the margin requirements
62-
model = self.Securities[front.Symbol].BuyingPowerModel
63-
initialMarginRequirement = model.InitialMarginRequirement
64-
maintenanceMarginRequirement = model.MaintenanceMarginRequirement
65-
63+
self.contractSymbol = front.Symbol
6664
self.MarketOrder(front.Symbol , 1)
6765
else:
6866
self.Liquidate()
67+
68+
def OnEndOfAlgorithm(self):
69+
# Get the margin requirements
70+
buyingPowerModel = self.Securities[self.contractSymbol].BuyingPowerModel
71+
name = type(buyingPowerModel).__name__
72+
if name != 'FutureMarginModel':
73+
raise Exception(f"Invalid buying power model. Found: {name}. Expected: FutureMarginModel")
74+
initialMarginRequirement = buyingPowerModel.InitialMarginRequirement
75+
maintenanceMarginRequirement = buyingPowerModel.MaintenanceMarginRequirement

‎Common/Brokerages/AlphaStreamsBrokerageModel.cs‎

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
using QuantConnect.Orders.Fees;
1818
using QuantConnect.Orders.Slippage;
1919
using QuantConnect.Securities;
20-
using QuantConnect.Securities.Future;
21-
using QuantConnect.Securities.Option;
2220

2321
namespace QuantConnect.Brokerages
2422
{
@@ -30,7 +28,7 @@ public class AlphaStreamsBrokerageModel : DefaultBrokerageModel
3028
/// <summary>
3129
/// Initializes a new instance of the <see cref="AlphaStreamsBrokerageModel"/> class
3230
/// </summary>
33-
/// <param name="accountType"></param>
31+
/// <param name="accountType">The type of account to be modelled, defaults to <see cref="AccountType.Margin"/> does not accept <see cref="AccountType.Cash"/>.</param>
3432
public AlphaStreamsBrokerageModel(AccountType accountType = AccountType.Margin)
3533
: base(accountType)
3634
{
@@ -39,63 +37,34 @@ public AlphaStreamsBrokerageModel(AccountType accountType = AccountType.Margin)
3937
throw new ArgumentException("The Alpha Streams brokerage does not currently support Cash trading.", nameof(accountType));
4038
}
4139
}
42-
40+
4341
/// <summary>
4442
/// Gets a new fee model that represents this brokerage's fee structure
4543
/// </summary>
46-
/// <param name="security"></param>
47-
/// <returns></returns>
44+
/// <param name="security">The security to get a fee model for</param>
45+
/// <returns>The new fee model for this brokerage</returns>
4846
public override IFeeModel GetFeeModel(Security security) => new AlphaStreamsFeeModel();
47+
4948
/// <summary>
50-
/// Gets a new slippage model that represents this brokerage's slippage costs
49+
/// Gets a new slippage model that represents this brokerage's fill slippage behavior
5150
/// </summary>
52-
/// <param name="security"></param>
53-
/// <returns></returns>
51+
/// <param name="security">The security to get a slippage model for</param>
52+
/// <returns>The new slippage model for this brokerage</returns>
5453
public override ISlippageModel GetSlippageModel(Security security) => new AlphaStreamsSlippageModel();
55-
/// <summary>
54+
5655
/// Force all security types to be restricted to 1.1x leverage
5756
/// - Current restriction to 1.1x is for the AS competition
5857
/// - Will be update in the future
5958
/// </summary>
6059
/// <param name="security"></param>
61-
/// <returns></returns>
60+
/// <returns>The leverage for the specified security</returns>
6261
public override decimal GetLeverage(Security security) => 1.1m;
62+
6363
/// <summary>
64-
/// Gets a new settlement model
64+
/// Gets a new settlement model for the security
6565
/// </summary>
66-
/// <param name="security"></param>
67-
/// <returns></returns>
66+
/// <param name="security">The security to get a settlement model for</param>
67+
/// <returns>The settlement model for this brokerage</returns>
6868
public override ISettlementModel GetSettlementModel(Security security) => new ImmediateSettlementModel();
69-
/// <summary>
70-
/// Get buying power model for the specific security type
71-
/// </summary>
72-
/// <param name="security"></param>
73-
/// <returns></returns>
74-
public override IBuyingPowerModel GetBuyingPowerModel(Security security)
75-
{
76-
var leverage = GetLeverage(security);
77-
IBuyingPowerModel model;
78-
79-
switch (security.Type)
80-
{
81-
case SecurityType.Crypto:
82-
model = new CashBuyingPowerModel();
83-
break;
84-
case SecurityType.Forex:
85-
case SecurityType.Cfd:
86-
model = new SecurityMarginModel(leverage, RequiredFreeBuyingPowerPercent);
87-
break;
88-
case SecurityType.Option:
89-
model = new OptionMarginModel(RequiredFreeBuyingPowerPercent);
90-
break;
91-
case SecurityType.Future:
92-
model = new FutureMarginModel(RequiredFreeBuyingPowerPercent);
93-
break;
94-
default:
95-
model = new SecurityMarginModel(leverage, RequiredFreeBuyingPowerPercent);
96-
break;
97-
}
98-
return model;
99-
}
10069
}
10170
}

0 commit comments

Comments
 (0)