**FOSS Trading**, and kindly contributed to R-bloggers)

When optimizing leverage space portfolios, I frequently run into the issue of one or more f$ ([Max Loss]/f) being less than the margin of its respective instrument. For example, assume the required margin for an instrument is $500, f$ is $100, and $100,000 in equity. The optimal amount to trade is 1,000 shares ($100,000/$100). However, that would require $500,000 in equity, while you only have $100,000. What do you do?

Page 341 of The Handbook of Portfolio Mathematics outlines how to determine how many units to trade, given margin constraints. The methodology therein suggests finding optimal f values first, then calculating the portfolio that satisfies the margin constraints but keeps the ratio of each market system to one another the same.

For those without the book, the calculation is:

L = max(f$) / sum( ( max(f$) / f$[i] ) * margin[i] )

Where:

L = percentage of “active equity” to use when dividing by each f$

margin = initial margin for each market system

The maxUnits function included in this post uses the formula above to return the maximum number of tradable units. In this example, we assume our margin is equal to our maximum loss (as is the case with equities). The code below illustrates how to use the maxUnits function after optimization.

# Load the LSPM package

library(LSPM)

maxUnits <- function(lsp, margin, equity) {

# Make sure margin and f are same length

NRf <- NROW(lsp$f)

if(NROW(margin)!=NRf)

stop(paste(“‘margin’ must have length =”,NRf))

# Calculate maximum equity percentage

fDollar <- -lsp$maxLoss / lsp$f

maxfDollar <- max( fDollar[is.finite(fDollar)] )

den <- maxfDollar / fDollar * margin

den[!is.finite(den)] <- 0

eqPct <- min(1, maxfDollar/sum(den))

max.units <- eqPct * equity / fDollar

return(max.units)

}

data(port) # Multiple strategy data

initEq <- 100000 # Initial equity

margin <- -port$maxLoss # Margin amounts

opt <- optimalf(port) # Optimize portfolio

port$f <- opt$f # Assign optimal f values to lsp object

# Units to trade

fUnits <- initEq/(-port$maxLoss/port$f) # unconstrained

mUnits <- maxUnits(port, margin, initEq) # margin-constrained

# Equity needed to trade at f values

sum(fUnits*margin) # unconstrained

sum(mUnits*margin) # margin-constrained

# Implied f values based on maximum units

port$f <- mUnits*-port$maxLoss/initEq

GHPR(port) # 1.209931

Note that the effect of the maxUnits function is to lower the optimal f values to a level within the margin constraints. The GHPR for the portfolio falls from 1.2939 without margin constraints to 1.20991 when post-optimization margin constrains are imposed.

As I investigated this method, I wondered if optimal f values would be the same if the margin constraints were included in the objective function. I was concerned that the post-optimization decrease in f values would be sub-optimal because a different mix of f values–that also meet the margin constraints–may have a higher GHPR.

The next block of code optimizes the portfolio with margin constraints included in the objective function (this functionality is available starting in revision 43).

# Optimize portfolio with margin constraints

opt <- optimalf(port, equity=initEq, margin=margin)

port$f <- opt$f # Assign optimal f values to lsp object

# Units to trade

fUnits <- initEq/(-port$maxLoss/port$f) # unconstrained

mUnits <- maxUnits(port, margin, initEq) # margin-constrained

# Equity needed to trade at f values

sum(fUnits*margin) # unconstrained

sum(mUnits*margin) # margin-onstrained

# Implied f values based on maximum units

fImp <- mUnits*-port$maxLoss/initEq

When the margin contraints are included in the objective function, fUnits and mUnits are the same, which means the implied f values are the same as the optimal f values and required equity is less than or equal to available equity.

In addition, we see that the post-optimzation method arrives at sub-optimal f values, since it arrived at a GHPR of 1.209931 while including margin constraints in the objective function achived a GHPR of 1.2486.

**leave a comment**for the author, please follow the link and comment on his blog:

**FOSS Trading**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...