I am trying to solve a optimization portfolio in R in which I do the following constraints:

Set weight sum to within a boundary

Set return to a certain value

Set portfolio beta to 0

The purpose is then to minimize risk subject to the constraints above.

While I have no trouble in doing that my issue comes next. I want to make the weights such that the portfolio's total exposure to a factor is 0. Imagine asset a has a beta of 0.3, asset b has 0.7 and asset c -0.3. How can I set this constraint? My issue is that using quadprog I can only add an external parameter vector(mean returns in this case). Is there a way to go around this issue or am i seeing thing s the wrong way?

The code I have so far is as follows :

6 assets and six mean returns. Also, a 6*6 var-cov matrix. The upper weight bound is lev_ub and the lower bound is lev_lb. Returns_full is the vector with returns.

My issue comes now from the fact that assume I have another vector of length 6 with one beta for each asset. I want to make it such that the sum of the product of weights and betas is zero or,in other words, set the total portfolio beta to 0. How can I add this constraint ?

Say we have $n$ assets. Suppose that the covariance matrix is $\Sigma$. Now suppose that $x$ is the vector of weights of your assets, $\mu$ is the vector of mean returns on the assets, $\mathbb{1}$ is the vector of all ones, $B$ is the vector of betas on each asset, and $\tau$ your target return. You want to solve the following quadratic program
$$Min_x\;\; \frac{1}{2}x^T\Sigma x$$
Under the constraints
$$x^T \mathbb{1}=1$$
$$\mu^Tx \geq \tau$$
$$B^Tx=0$$
The constraint $B^Tx=0$ will ensure that the portfolios beta is zero.

You have the budget constaint on the sum of weights, the constraint that the portfolio return equal the target return, and the constraint on beta. With three equality constraints, you can set a row in Amat for each constraint, and then take the transpose to pass to solve.QP. bvec is set to the right-hand side of the constraint equations and the parameter meq equals the number of equality constraints. An example for you problem is shown below: