{"record":{"id":"4d3d663aaf09d5a8","repo":"freqtrade/freqtrade","slug":"could-not-set-leverage-due-to-e-class-name","errorCode":null,"errorMessage":"Could not set leverage due to {e.__class__.__name__}. Message: {e}","messagePattern":"Could not set leverage due to (.+?)\\. Message: (.+?)","errorType":"exception","errorClass":"TemporaryError","httpStatus":null,"severity":"error","filePath":"freqtrade/exchange/okx.py","lineNumber":173,"sourceCode":"    def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False):\n        if self.trading_mode != TradingMode.SPOT and self.margin_mode is not None:\n            try:\n                res = self._api.set_leverage(\n                    leverage=leverage,\n                    symbol=pair,\n                    params={\n                        \"mgnMode\": self.margin_mode.value,\n                        \"posSide\": self._get_posSide(side, False),\n                    },\n                )\n                self._log_exchange_response(\"set_leverage\", res)\n\n            except ccxt.DDoSProtection as e:\n                raise DDosProtection(e) from e\n            except (ccxt.OperationFailed, ccxt.ExchangeError) as e:\n                already_set = self.__fetch_leverage_already_set(pair, leverage, side)\n                if not already_set:\n                    raise TemporaryError(\n                        f\"Could not set leverage due to {e.__class__.__name__}. Message: {e}\"\n                    ) from e\n            except ccxt.BaseError as e:\n                raise OperationalException(e) from e\n\n    def get_max_pair_stake_amount(self, pair: str, price: float, leverage: float = 1.0) -> float:\n        if self.trading_mode == TradingMode.SPOT:\n            return float(\"inf\")  # Not actually inf, but this probably won't matter for SPOT\n\n        if pair not in self._leverage_tiers:\n            return float(\"inf\")\n\n        pair_tiers = self._leverage_tiers[pair]\n        last_max_notional = pair_tiers[-1][\"maxNotional\"]\n        if last_max_notional is None:\n            return float(\"inf\")\n        return last_max_notional / leverage\n","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/freqtrade/freqtrade/blob/1c8edfe4d1e8d11bd4b40e8fc3237c26c3a60e15/freqtrade/exchange/okx.py#L155-L191","documentation":"TemporaryError raised by Okx._set_leverage() when set_leverage fails with ccxt.OperationFailed/ExchangeError AND __fetch_leverage_already_set() confirms the leverage is NOT already at the target. OKX rejects redundant set-leverage calls with an error, so the code first checks whether the error is benign (leverage already equals the requested value); only a genuine failure raises.","triggerScenarios":"Placing an order whose leverage differs from the account's current setting while OKX rejects the change: leverage above the tier limit for the pair/margin mode, insufficient margin for requested leverage, or 'Instrument not found' style errors.","commonSituations":"Strategy max_leverage exceeds OKX's per-pair cap for the position tier, margin mode mismatch (isolated vs cross), or a pair delisted/renamed on OKX.","solutions":["Read the embedded ccxt message — OKX usually states 'leverage greater than the max' or similar; lower max_leverage in the strategy.","Verify margin_mode config (isolated/cross) matches what the account and pair support.","Confirm the pair is still tradable on OKX.","Update ccxt so leverage tier data and error codes are current."],"exampleFix":"# strategy.py — clamp leverage to what OKX allows for the pair\n# before\ndef leverage(self, pair, current_time, current_rate, proposed_leverage, max_leverage, entry_tag, side, **kwargs) -> float:\n    return 10.0\n# after\ndef leverage(self, pair, current_time, current_rate, proposed_leverage, max_leverage, entry_tag, side, **kwargs) -> float:\n    return min(10.0, max_leverage)","handlingStrategy":"validation","validationCode":"tiers = exchange.get_leverage_tiers(pair) if hasattr(exchange, 'get_leverage_tiers') else {}\nmax_ok = tiers.get(pair, [{}])[-1].get('maxLeverage', 100) if tiers else 100\nlev = min(requested_leverage, max_ok)","typeGuard":null,"tryCatchPattern":"from freqtrade.exceptions import TemporaryError\ntry:\n    exchange.set_leverage(lev, pair, side)\nexcept TemporaryError as e:\n    if 'leverage' in str(e).lower():\n        lev = min(lev, 3)  # retry with conservative leverage\n        exchange.set_leverage(lev, pair, side)","preventionTips":["Clamp strategy leverage() return to max_leverage argument.","Match margin_mode config to what the pair supports (isolated vs cross).","Update leverage tier data via ccxt refresh after listing/delisting changes."],"tags":["freqtrade","okx","leverage","margin","transient","ccxt"],"backgroundTag":null,"analyzedSha":"1c8edfe4d1e8d11bd4b40e8fc3237c26c3a60e15","analyzedAt":"2026-08-15T05:09:08.096Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}