class MaximumSectorExposureRiskManagementModel(RiskManagementModel): '''Provides an implementation of IRiskManagementModel that that limits the sector exposure to the specified percentage''' def __init__(self, maximumSectorExposure = 0.20): '''Initializes a new instance of the MaximumSectorExposureRiskManagementModel class Args: maximumDrawdownPercent: The maximum exposure for any sector, defaults to 20% sector exposure.''' if maximumSectorExposure <= 0: raise ValueError('MaximumSectorExposureRiskManagementModel: the maximum sector exposure cannot be a non-positive value.') self.maximumSectorExposure = maximumSectorExposure self.targetsCollection = PortfolioTargetCollection() def ManageRisk(self, algorithm, targets): '''Manages the algorithm's risk at each time step Args: algorithm: The algorithm instance''' maximumSectorExposureValue = float(algorithm.Portfolio.TotalPortfolioValue) * self.maximumSectorExposure self.targetsCollection.AddRange(targets) risk_targets = list() # Group the securities by their sector filtered = list(filter(lambda x: x.Value.Fundamentals.HasFundamentalData, algorithm.UniverseManager.ActiveSecurities)) filtered.sort(key = lambda x: x.Value.Fundamentals.CompanyReference.IndustryTemplateCode) groupBySector = groupby(filtered, lambda x: x.Value.Fundamentals.CompanyReference.IndustryTemplateCode) for code, securities in groupBySector: # Compute the sector absolute holdings value # If the construction model has created a target, we consider that # value to calculate the security absolute holding value quantities = {} sectorAbsoluteHoldingsValue = 0 for security in securities: symbol = security.Value.Symbol quantities[symbol] = security.Value.Holdings.Quantity absoluteHoldingsValue = security.Value.Holdings.AbsoluteHoldingsValue if self.targetsCollection.ContainsKey(symbol): quantities[symbol] = self.targetsCollection[symbol].Quantity absoluteHoldingsValue = (security.Value.Price * abs(quantities[symbol]) * security.Value.SymbolProperties.ContractMultiplier * security.Value.QuoteCurrency.ConversionRate) sectorAbsoluteHoldingsValue += absoluteHoldingsValue # If the ratio between the sector absolute holdings value and the maximum sector exposure value # exceeds the unity, it means we need to reduce each security of that sector by that ratio # Otherwise, it means that the sector exposure is below the maximum and there is nothing to do. ratio = float(sectorAbsoluteHoldingsValue) / maximumSectorExposureValue if ratio > 1: for symbol, quantity in quantities.items(): if quantity != 0: risk_targets.append(PortfolioTarget(symbol, float(quantity) / ratio)) return risk_targets
class ImmediateExecutionModel(ExecutionModel): '''Provides an implementation of IExecutionModel that immediately submits market orders to achieve the desired portfolio targets''' def __init__(self): '''Initializes a new instance of the ImmediateExecutionModel class''' self.targetsCollection = PortfolioTargetCollection() def Execute(self, algorithm, targets): '''Immediately submits orders for the specified portfolio targets. Args: algorithm: The algorithm instance targets: The portfolio targets to be ordered''' self.targetsCollection.AddRange(targets) for target in self.targetsCollection.OrderByMarginImpact(algorithm): open_quantity = sum([x.Quantity for x in algorithm.Transactions.GetOpenOrders(target.Symbol)]) existing = algorithm.Securities[target.Symbol].Holdings.Quantity + open_quantity quantity = target.Quantity - existing if quantity != 0: algorithm.MarketOrder(target.Symbol, quantity) self.targetsCollection.Clear()
class VolumeWeightedAveragePriceExecutionModel: '''Execution model that submits orders while the current market price is more favorable that the current volume weighted average price.''' def __init__(self): '''Initializes a new instance of the VolumeWeightedAveragePriceExecutionModel class''' self.targetsCollection = PortfolioTargetCollection() self.symbolData = {} # Gets or sets the maximum order quantity as a percentage of the current bar's volume. # This defaults to 0.01m = 1%. For example, if the current bar's volume is 100, # then the maximum order size would equal 1 share. self.MaximumOrderQuantityPercentVolume = 0.01 def Execute(self, algorithm, targets): '''Executes market orders if the standard deviation of price is more than the configured number of deviations in the favorable direction. Args: algorithm: The algorithm instance targets: The portfolio targets''' # update the complete set of portfolio targets with the new targets self.targetsCollection.AddRange(targets) for target in self.targetsCollection: symbol = target.Symbol # calculate remaining quantity to be ordered unorderedQuantity = OrderSizing.GetUnorderedQuantity( algorithm, target) # fetch our symbol data containing our VWAP indicator data = self.symbolData.get(symbol, None) if data is None: return # ensure we're receiving price data before submitting orders if data.Security.Price == 0: return # check order entry conditions if self.PriceIsFavorable(data, unorderedQuantity): # get the maximum order size based on total order value maxOrderSize = OrderSizing.PercentVolume( data.Security, self.MaximumOrderQuantityPercentVolume) orderSize = np.min([maxOrderSize, np.abs(unorderedQuantity)]) # round down to even lot size orderSize -= orderSize % data.Security.SymbolProperties.LotSize if orderSize != 0: algorithm.MarketOrder( symbol, np.sign(unorderedQuantity) * orderSize) # check to see if we're done with this target unorderedQuantity = OrderSizing.GetUnorderedQuantity( algorithm, target) if unorderedQuantity == 0: self.targetsCollection.Remove(target.Symbol) def OnSecuritiesChanged(self, algorithm, changes): '''Event fired each time the we add/remove securities from the data feed Args: algorithm: The algorithm instance that experienced the change in securities changes: The security additions and removals from the algorithm''' for removed in changes.RemovedSecurities: # clean up removed security data if removed.Symbol in self.symbolData: if self.IsSafeToRemove(algorithm, removed.Symbol): data = self.symbolData.pop(removed.Symbol) algorithm.SubscriptionManager.RemoveConsolidator( removed.Symbol, data.Consolidator) for added in changes.AddedSecurities: if added.Symbol not in self.symbolData: self.symbolData[added.Symbol] = SymbolData(algorithm, added) def PriceIsFavorable(self, data, unorderedQuantity): '''Determines if the current price is more than the configured number of standard deviations away from the mean in the favorable direction.''' if unorderedQuantity > 0: if data.Security.BidPrice < data.VWAP: return True else: if data.Security.AskPrice > data.VWAP: return True return False def IsSafeToRemove(self, algorithm, symbol): '''Determines if it's safe to remove the associated symbol data''' # confirm the security isn't currently a member of any universe return not any([ kvp.Value.ContainsMember(symbol) for kvp in algorithm.UniverseManager ])
class StandardDeviationExecutionModel(ExecutionModel): '''Execution model that submits orders while the current market prices is at least the configured number of standard deviations away from the mean in the favorable direction (below/above for buy/sell respectively)''' def __init__(self, period=60, deviations=2, resolution=Resolution.Minute): '''Initializes a new instance of the StandardDeviationExecutionModel class Args: period: Period of the standard deviation indicator deviations: The number of deviations away from the mean before submitting an order resolution: The resolution of the STD and SMA indicators''' self.period = period self.deviations = deviations self.resolution = resolution self.targetsCollection = PortfolioTargetCollection() self.symbolData = {} # Gets or sets the maximum order value in units of the account currency. # This defaults to $20,000. For example, if purchasing a stock with a price # of $100, then the maximum order size would be 200 shares. self.MaximumOrderValue = 20000 def Execute(self, algorithm, targets): '''Executes market orders if the standard deviation of price is more than the configured number of deviations in the favorable direction. Args: algorithm: The algorithm instance targets: The portfolio targets''' self.targetsCollection.AddRange(targets) for target in self.targetsCollection.OrderByMarginImpact(algorithm): symbol = target.Symbol # calculate remaining quantity to be ordered unorderedQuantity = OrderSizing.GetUnorderedQuantity( algorithm, target) # fetch our symbol data containing our STD/SMA indicators data = self.symbolData.get(symbol, None) if data is None: return # ensure we're receiving price data before submitting orders if data.Security.Price == 0: return # check order entry conditions if data.STD.IsReady and self.PriceIsFavorable( data, unorderedQuantity): # get the maximum order size based on total order value maxOrderSize = OrderSizing.Value(data.Security, self.MaximumOrderValue) orderSize = np.min([maxOrderSize, np.abs(unorderedQuantity)]) # round down to even lot size orderSize -= orderSize % data.Security.SymbolProperties.LotSize if orderSize != 0: algorithm.MarketOrder( symbol, np.sign(unorderedQuantity) * orderSize) # check to see if we're done with this target unorderedQuantity = OrderSizing.GetUnorderedQuantity( algorithm, target) if unorderedQuantity == 0: self.targetsCollection.Remove(target.Symbol) def OnSecuritiesChanged(self, algorithm, changes): '''Event fired each time the we add/remove securities from the data feed Args: algorithm: The algorithm instance that experienced the change in securities changes: The security additions and removals from the algorithm''' for removed in changes.RemovedSecurities: # clean up data from removed securities if removed.Symbol in self.symbolData: if self.IsSafeToRemove(algorithm, removed.Symbol): data = self.symbolData.pop(removed.Symbol) algorithm.SubscriptionManager.RemoveConsolidator( removed.Symbol, data.Consolidator) addedSymbols = [] for added in changes.AddedSecurities: if added.Symbol not in self.symbolData: self.symbolData[added.Symbol] = SymbolData( algorithm, added, self.period, self.resolution) addedSymbols.append(added.Symbol) if len(addedSymbols) > 0: # warmup our indicators by pushing history through the consolidators history = algorithm.History(addedSymbols, self.period, self.resolution) if history.empty: return tickers = history.index.levels[0] for ticker in tickers: symbol = SymbolCache.GetSymbol(ticker) symbolData = self.symbolData[symbol] for tuple in history.loc[ticker].itertuples(): bar = TradeBar(tuple.Index, symbol, tuple.open, tuple.high, tuple.low, tuple.close, tuple.volume) symbolData.Consolidator.Update(bar) def PriceIsFavorable(self, data, unorderedQuantity): '''Determines if the current price is more than the configured number of standard deviations away from the mean in the favorable direction.''' deviations = self.deviations * data.STD.Current.Value if unorderedQuantity > 0: if data.Security.BidPrice < data.SMA.Current.Value - deviations: return True else: if data.Security.AskPrice > data.SMA.Current.Value + deviations: return True return False def IsSafeToRemove(self, algorithm, symbol): '''Determines if it's safe to remove the associated symbol data''' # confirm the security isn't currently a member of any universe return not any([ kvp.Value.ContainsMember(symbol) for kvp in algorithm.UniverseManager ])