{"record":{"id":"222472d5b2bc99d3","repo":"QuantConnect/Lean","slug":"volumeshareslippagemodel-getslippageapproximation","errorCode":null,"errorMessage":"VolumeShareSlippageModel.GetSlippageApproximation(): Cannot use this model with market data type {data.GetType()}","messagePattern":"VolumeShareSlippageModel\\.GetSlippageApproximation\\(\\): Cannot use this model with market data type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Common/Orders/Slippage/VolumeShareSlippageModel.py","lineNumber":44,"sourceCode":"\n    def get_slippage_approximation(self, asset: Security, order: Order) -> float:\n        '''Slippage Model. Return a decimal cash slippage approximation on the order.\n        Args:\n            asset: The Security instance of the security of the order.\n            order: The Order instance being filled.'''\n        last_data = asset.get_last_data()\n        if not last_data:\n           return 0\n\n        bar_volume = 0\n        slippage_percent = self.volume_limit * self.volume_limit * self.price_impact\n\n        if last_data.data_type == MarketDataType.TRADE_BAR:\n            bar_volume = last_data.volume\n        elif last_data.data_type == MarketDataType.QUOTE_BAR:\n            bar_volume = last_data.last_bid_size if order.direction == OrderDirection.BUY else last_data.last_ask_size\n        else:\n           raise InvalidOperationException(Messages.VolumeShareSlippageModel.invalid_market_data_type(last_data))\n\n        # If volume is zero or negative, we use the maximum slippage percentage since the impact of any quantity is infinite\n        # In FX/CFD case, we issue a warning and return zero slippage\n        if bar_volume <= 0:\n            security_type = asset.symbol.id.security_type\n            if security_type == SecurityType.CFD or security_type == SecurityType.FOREX or security_type == SecurityType.CRYPTO:\n                Log.error(Messages.VolumeShareSlippageModel.volume_not_reported_for_market_data_type(security_type))\n                return 0\n\n            Log.error(Messages.VolumeShareSlippageModel.negative_or_zero_bar_volume(bar_volume, slippage_percent))\n        else:\n            # Ratio of the order to the total volume\n            volume_share = min(order.absolute_quantity / bar_volume, self.volume_limit)\n\n            slippage_percent = volume_share * volume_share * self.price_impact\n\n        return slippage_percent * last_data.Value;\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Common/Orders/Slippage/VolumeShareSlippageModel.py#L26-L62","documentation":"VolumeShareSlippageModel.get_slippage_approximation computes slippage from the ratio of order size to bar volume, so it needs a volume figure. It reads asset.get_last_data() and only knows how to extract volume from MarketDataType.TRADE_BAR (uses .volume) and MarketDataType.QUOTE_BAR (uses last_bid_size/last_ask_size). For any other data type (Tick, Base, Auxiliary, OptionChain, FuturesChain) it raises InvalidOperationException naming the runtime type. The error is thrown by the slippage model during fill processing, not at setup.","triggerScenarios":"A fill is evaluated for a security whose last cached data is not a TradeBar or QuoteBar — most commonly a Tick (the security is subscribed at tick resolution, or the last data is a Tick) or a custom/Base data point. The model's if/elif/else falls through to the else branch and raises. This is typical when a user sets Resolution.TICK (or add_equity with tick data) while keeping the default VolumeShareSlippageModel, or trades a custom-data security.","commonSituations":"Trading at tick resolution with the default slippage model. Trading custom/Base data (no volume concept). A security whose cache's last data is an Auxiliary event (e.g. dividend/split) at the moment of filling. Subscribing a security type whose fill data is Tick rather than bars.","solutions":["Switch the security to a bar-based resolution (Minute/Hour/Daily) so the last data is a TradeBar/QuoteBar that carries volume.","Replace the slippage model with one that handles your data type: security.set_slippage_model(ConstantSlippageModel(...)) or a custom ISlippageModel that handles Tick/Base data.","If you must keep tick data, implement a custom slippage model that derives volume from the Tick (last_bid_size/last_ask_size) instead of asserting.","Confirm the security's data subscription actually produces TradeBar/QuoteBar; add the fill-forward/quote configuration if needed."],"exampleFix":"# before — default slippage model fails on tick-resolution data\nself.add_equity(\"SPY\", Resolution.TICK)\n# fill triggers: InvalidOperationException ... market data type Tick\n\n# after — use a slippage model that does not require bar volume\nself.add_equity(\"SPY\", Resolution.TICK).set_slippage_model(\n    ConstantSlippageModel(0.01))\n# or switch to a bar resolution so VolumeShareSlippageModel has volume\nself.add_equity(\"SPY\", Resolution.MINUTE)","handlingStrategy":"validation","validationCode":"# Before trading, ensure the slippage model can handle the security's data type\nfrom QuantConnect.Orders.Slippage import VolumeShareSlippageModel\nsec = self.add_equity('SPY', Resolution.TICK)\nlast = sec.get_last_data()\nuses_bars = last is not None and last.data_type in (MarketDataType.TRADE_BAR, MarketDataType.QUOTE_BAR)\nif isinstance(sec.slippage_model, VolumeShareSlippageModel) and not uses_bars:\n    sec.set_slippage_model(ConstantSlippageModel(0.01))","typeGuard":"def slippage_model_supports_data(slippage_model, last_data):\n    \"\"\"True when the configured slippage model can consume the security's last data type.\"\"\"\n    if isinstance(slippage_model, VolumeShareSlippageModel):\n        return last_data is None or last_data.data_type in (\n            MarketDataType.TRADE_BAR, MarketDataType.QUOTE_BAR)\n    return True  # assume non-volume-share models are permissive","tryCatchPattern":null,"preventionTips":["Use bar-based resolutions (Minute/Hour/Daily) when keeping the default VolumeShareSlippageModel.","For tick/custom-data securities, set a slippage model that does not require bar volume.","Confirm the security's last cached data is a TradeBar/QuoteBar before relying on volume-based slippage."],"tags":["quantconnect","lean","slippage","fills","market-data-type","tick-data","python"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}