Constraining Portfolio

Hello. How to constraint portfolio so that I don't use fractional shares in portfolio optimization. Thank you for your help.

Can you try to clarify your question a bit with this in mind please?

Sure. I was asking if there was a code where in portfolio, I add a constraint that I don't want fractional shares. Portfolio Optimization.

Well it really depends on what you are trying to do - it would be nice to more more about your specific problem. What have you tried? What are the characteristics of your portfolio? What are you optimizing for?

Depending on what’s in your portfolio, you are just fine running whatever optimization algorithm you are using, and then just rounding the results. For example, if you are running a long-only equity portfolio and run the typical Markowitz Mean-Variance optimization, then the suboptimality that you introduce by rounding is probably negligent. If your portfolio includes any leverage or contracts that trade in large increments, then rounding can be more concerning - like if your optimizing a strategy that writes options far OTM, then rounding to the nearest tradable strike could be problematic.

So we are using R Studio to optimize a student managed investment portfolio that only has $10k to invest. We can't even hold a share of Amazon, because it would be over 30% of our portfolio. So we are sensitive to fractional shares, based on our dollar limitation. Some holdings, we only have a single share of. What can we do in such a case? So in this case, we want to disallow fractional shares. We want percentages based on the best risk reward combination if we buy exact shares. Thank you so much for your help

You still need to do more work to define your investment universe and portfolio construction rules before you can get to optimization in R. For example, if you can't own AMZN, you have some limit for price per share. I would imagine you also have a minimum number of different stocks - Modern Portfolio theory suggests that you start to maximize the benefits of diversification at between about 10 and 40 stocks. Also, you need to determine what is "optimal" in your scenario. How would you pick between a portfolio that has 10% return and 4% standard deviation and a portfolio that has 20% return and 14% standard deviation?

So, in summary, to get to the optimal portfolio you are after, you need to:

  • Come up with what constraints your portfolio has
  • Define a target by which you determine optimality.

Thank you for your thorough reply.
I made a portfolio consisting of 30 stocks, and I am assigning weights . This is what I did:
tickers <- c("BAC","AAPL","AXP","TGT","COST","MCO","EL","COF","IBM","GS","JPM","KO","INTC","JNJ","JPM","TSLA","DFS","ABT","MA","MRK","MSFT","NKE","PFE","PG","TRV","UAL","SCHW","WMT","XOM","JBLU")

I got the symbols defined start and end dates, selected adjusted prices.
But now I want to use the function add.constraint for limiting fractional shares. How should I do it? I am relatively new and learning how to use R Studio. So, I would very much appreciate your help. I also had one more question, how can I allocate cash in portfolio? For instance, I want to say that 2000 dollars will be allocated to buy shares of Bank of America (BAC)? How would I do it using functions?

Some detective work on my part indicates you may be trying to make use of CRAN - Package PortfolioAnalytics (
If thats the case, have you availed yourself of the documentation, particularly the vignettes?
portfolio_vignette.pdf (

Also for technical issues that relate to this specific package you might avail yourself of the community at the issues board: Issues · braverock/PortfolioAnalytics (

1 Like

It's still quite unclear to me what you are trying to do. If your only goal is to use add.constraint to limit fractional shares, that seems to be overkill - I'd just say round down. But it seems like your goal is more nuanced?

Here is what I am understanding so far:

  • You need to invest $10K
  • You cannot use fractional shares
  • You want to own at least one share of the securities you listed in your tickers vector

But this still leaves the questions I asked above unanswered, namely:

  1. What constraints does your portfolio have? (This typically includes things like how much you can invest in a single security, how much cash can you leave uninvested, etc.)
  2. What determines what an "optimal" portfolio is? On a basic level, you can do three things: Pick the portfolio that maximizes return, the portfolio that minimizes risk, or the portfolio that strikes the "best" balance between the two - picking a portfolio that most closely resembles the Sharpe Ratio of the S&P 500 would be an example of this.

It sounds to me like you are trying to do something more than just put a bunch of securities in a portfolio and call it good so long as you only have whole shares.

1 Like

Hello guys.

I need help with a portfolio rebalancing topic.

I'm having trouble implementing a code in R to decide to Buy or Sell Bitcoin.

The idea is this: working with a 200-day moving average, whenever the current / day price is HIGHER than the 200-day average price, the decision is to SELL bitcoin.
Whenever the current/daily price is LOWER than the 200-day average price, the decision is to HOLD bitcoin.

The central idea is to verify if this buy x sell strategy proves to be more profitable than simply buying Bitcoin and holding it.

Can anyone help me? Thanks!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.