API Docs

Quick Introduction to Intraday Portfolio Metrics

In this tutorial we will construct a portfolio with a few positions and will compute some key risk and performance metrics for it.

Creating Portfolio

Here we are creating are simple portfolio with only two positions. By default SPY would be used for internal Single Index Model, unless other market index is selected. As it is a simple buy-and-hold strategy without rebalancing, we specify constant position quantity across all time span.

``````require(PortfolioEffectHFT)

# Create empty portfolio!
portfolio=portfolio_create("2014-10-01 09:30:00","2014-10-02 16:00:00")

# Add position AAPL and GOOG to portfolio

# Display general information about the portfolio at the end of the period
print(portfolio)
plot(portfolio)

`````` ``````% Create empty portfolio
dateStart = '2014-10-01 09:30:00';
dateEnd = '2014-10-02 16:00:00';

portfolio=portfolio_create('fromTime',dateStart,'toTime',dateEnd);

% Add position AAPL and GOOG to portfolio

% Display general information about the portfolio at the end of the period
display(portfolio);
plot(portfolio)
`````` Portfolio Value

Let's first plot evolution of portfolio value over our data sample.

``````# Plot portfolio value changes over time
print(plot(value(portfolio),title="Portfolio value, in USD"))
`````` ``````close
figure('position',[800 200 1000 700])

util_plot2d(portfolio_value(portfolio),'Portfolio value, in USD','Title','Portfolio value, in USD')
`````` Portfolio Expected Return

Portfolio and position expected return is a statistical measure and estimates mean value of return distribution.

``````#  Compute portfolio and position expected return (daily)
print(plot(expected_return(positionAAPL),expected_return(positionGOOG),expected_return(portfolio),
title="Expected Return, daily",legend=c("AAPL","GOOG","Portfolio")))
`````` ``````% Compute portfolio and position expected return (daily)
util_plot2d(position_expectedReturn(portfolio,'AAPL'),'AAPL','Title','Expected Return, daily')+...
util_line2d(position_expectedReturn(portfolio,'GOOG'),'GOOG')+...
util_line2d(portfolio_expectedReturn(portfolio),'Portfolio')
`````` Portfolio Variance

Below are the charts of portfolio and position daily variance of price returns. Return variance is higher at the beginning of a trading day.

``````# Compute portfolio and position variance (daily)
print(plot(variance(positionAAPL),variance(positionGOOG),variance(portfolio),
title="Variance, daily",legend=c("AAPL","GOOG","Portfolio")))
`````` ``````% Compute portfolio and position variance (daily)
util_plot2d(position_variance(portfolio,'AAPL'),'AAPL','Title','Variance, daily')+...
util_line2d(position_variance(portfolio,'GOOG'),'GOOG')+...
util_line2d(portfolio_variance(portfolio),'Portfolio')
`````` Portfolio Value-at-Risk

Portfolio and position Value-at-Risk expresses an average loss in the N% of worst cases. In this example, we use 95% confidence interval over a daily horizon. Note that in PortfolioEffect, both return variance and higher order moments (like skewness and kurtosis) are used to compute resulting VaR figure.

``````# Compute portfolio and position VaR (daily, 95% c.i.)
print(plot(value_at_risk(positionAAPL,0.95),value_at_risk(positionGOOG,0.95),value_at_risk(portfolio,0.95),
title="Value at Risk in %, daily (95% c.i.)",legend=c("AAPL","GOOG","Portfolio")))

`````` ``````% Compute portfolio and position VaR (daily, 95% c.i.)
util_plot2d(position_VaR(portfolio,'AAPL',0.05),'AAPL','Title','Value at Risk in %, daily (95% c.i.)')+...
util_line2d(position_VaR(portfolio,'GOOG',0.05),'GOOG')+...
util_line2d(portfolio_VaR(portfolio,0.05),'Portfolio')
`````` Portfolio Sharpe Ratio

Now let's compute Sharpe Ratio (expressed in daily terms) for our positions and portfolio. Sharpe Ratio of our portfolio is somewhere in-between the Sharpe Ratios of individual positions.

``````#  Compute portfolio and position Sharpe Ratio (daily)
print(plot(sharpe_ratio(positionAAPL),sharpe_ratio(positionGOOG),sharpe_ratio(portfolio),
title="Sharpe Ratio, daily",legend=c("AAPL","GOOG","Portfolio")))
`````` ``````% Compute portfolio and position Sharpe Ratio (daily)
util_plot2d(position_sharpeRatio(portfolio,'AAPL'),'AAPL','Title','Sharpe Ratio, daily')+...
util_line2d(position_sharpeRatio(portfolio,'GOOG'),'GOOG')+...
util_line2d(portfolio_sharpeRatio(portfolio),'Portfolio')
`````` 