I'm trying to figure out where my mistake is. I'm trying to optimize a parameter for a stop-loss type contract with respect to conditional value-at-risk. I have attempted to differentiate with respect to the parameter of interest, but I'm afraid that the final expression is nonsensical. Can anyone spot a mistake in the following calculations?
Assume $X$ is a bounded continuous stochastic variable taking positive values measuring financial losses, with density function $f(x)$. We wish to hedge against losses by insuring parts of this risk: Let \begin{align} I_a(X) = \begin{cases} X, X \leq a, \\ a, X > a.\end{cases}\end{align} For this we pay the premium $\pi_a = (1+\theta)E[X - I_a(X)]$, where $\theta > 0$ is a risk premium. The aim is to find an optimal value for $a$ to minimize the conditional value-at-risk of $I_a(X) + \pi_a$; Let $\alpha \in (0, 1) $ be given, and $q_{\beta}(X) := \inf \{c \mid P(X> c)\leq \alpha \}$ be the $\alpha$-quantile of $X$. The conditional value-at-risk is defined as \begin{align} CVaR_{\alpha}(I_a(X)) := \frac{1}{1-\alpha}\int_{\alpha}^{1}I_a(q_{\beta}(X))\ d\beta. \end{align}
The goal is to solve the following optimization problem: \begin{align} \min_{a} CVaR_{\alpha}(I_a(X) + \pi_a) = \min_{a} CVaR_{\alpha}(I_a(X)) + \pi_a, \end{align} as $CVaR$ is translation invariant. I attempt to do this by differentiating this with respect to $a$. First, I note that
\begin{align} \int_{\alpha}^{1} I_a(q_{\beta}(X))\ d\beta &= \int_{q_{\alpha}}^{sup X} I_a(x)f(x)dx. \end{align}
Moreover, if $a \leq q_{\alpha}(X)$, we get that $CVaR_{\alpha}(I_a(X)) = a$. Assuming then $a > q_{\alpha}(X)$, we get:
\begin{align} \int_{\alpha}^{1} I(q_{\beta}(X))\ d\beta &= \int_{q_{\alpha}}^{a} I_a(x)f(x)dx + \int_{a}^{\sup X} a f(x)dx. \end{align} Differentiating with respect to $a$ gives:
\begin{align} \frac{d}{da} \left[ \int_{q_{\alpha}}^{a} I_a(x)f(x)dx + \int_{a}^{\sup X} a f(x)dx \right] &= I_a(a)f(a) + \frac{d}{da}\left( aP(X\geq a)\right)\\ &= a f(a) + P(X\geq a) - a f(a) \\ &= P(X\geq a), \end{align} using the fundamental theorem of calculus (and assuming continuity of $X$ and hence $I_a(X)$.)
On the other hand, \begin{align} \frac{d}{da} E[X-I_a(X)] &= \frac{d}{da} \int_{a}^{\sup X} (x-a) f(x)dx \\ &= a f(a) - af(a) -\int_{a}^{\sup X} f(x) dx \\ &= -P(X\geq a). \end{align}
Puting this together, we get:
\begin{align} \frac{d}{da} \left[ CVaR_{\alpha}(I_a(X)) + \pi_a \right] &= \frac{1}{1-\alpha} P(X\geq a) + -(1+\theta) \cdot P(X \geq a), \\ \end{align}
But this seems suspicious, as setting this equal to $0$ yields $a = \sup X$ or $\frac{1}{1-\alpha} = 1+\theta$.

Let $W_0$ be the initial total wealth. Insuring on minimum final wealth $W_0-a$ ($a$ is the maximum loss) means buying a cash settled put option with strike $K=W_0-a$. Indeed the final payoff is $(K-W_t)^++W_t=(W_0-a-W_t)^++W_t$ so that if $K\geq W_t$ you receive $K-W_t$ and final wealth is $K-W_t+W_t=K=W_0-a$ and if $K<W_t$ you end up with $W_t$. The price of the option is $\pi(a)$
In terms of wealth, the $\textrm{CVaR}$ of the strategy given a $\textrm{VaR}_\alpha$ is $$\textrm{CVaR}=\mathbb{E}[(K-W_t)^++W_t|(K-W_t)^++W_t\leq\textrm{VaR}_\alpha\}]$$ So clearly $$P((K-W_t)^++W_t\leq\textrm{VaR}_\alpha)=1-\alpha$$ therefore $$\textrm{CVaR}=\frac{1}{1-\alpha}\mathbb{E}[((K-W_t)^++W_t)\mathbb{I}_{\{(K-W_t)^++W_t\leq \textrm{VaR}_\alpha\}}]$$ If $\textrm{VaR}_\alpha\leq K$ then $\textrm{CVaR}=W_0-a$ otherwise $$(1-\alpha)\textrm{CVaR}=(W_0-a)P(W_t\leq K)+\int_{W_0-a}^{\textrm{VaR}_\alpha}wf_{W_t}(w)dw$$ Derivative wrt $a$: $$-W_0f_{W_t}(W_0-a)-P(W_t\leq W_0-a)+af_{W_t}(W_0-a)+(W_0-a)f_{W_t}(W_0-a)=-P(W_t\leq W_0-a)$$ which is equivalent to your result $-P(X\geq a)$. Now we want to change $a$ to minimize the $\textrm{CVaR}+\pi(a)$, by taking the derivative and putting it equal to $0$: $$-\frac{P(W_t\leq W_0-a)}{1-\alpha}+\frac{\partial}{\partial a}\pi(a)=0$$ But we know that $$\pi(a)=m\mathbb{E}[(K-W_t)^+]$$ So $$\frac{\partial}{\partial a}\pi(a)=mP(W_t\leq W_0-a) \implies m(1-\alpha)=1$$ Which does look suspicious and it is equal to your result. So my guess is that the optimization setting is flawed and you must choose a loss function to minimize (like a quadratic loss or something).