Bradlee Speice, Thu 19 November 2015, Sat 05 December 2015, Blog
Hello! Glad to meet you. I'm currently a student at Columbia University studying Financial Engineering, and want to give an overview of the projects I'm working on!
To start things off, Columbia has been hosting a trading competition that myself and another partner are competing in. I'm including a notebook of the algorithm that we're using, just to give a simple overview of a miniature algorithm.
The competition is scored in 3 areas:
Our algorithm uses a basic momentum strategy: in the given list of potential portfolios, pick the stocks that have been performing well in the past 30 days. Then, optimize for return subject to the drawdown being below a specific level. We didn't include the Sharpe ratio as a constraint, mostly because we were a bit late entering the competition.
I'll be updating this post with the results of our algorithm as they come along!
UPDATE 12/5/2015: Now that the competition has ended, I wanted to update how the algorithm performed. Unfortunately, it didn't do very well. I'm planning to make some tweaks over the coming weeks, and do another forward test in January.
And some statistics for all teams participating in the competition:
Max Return | 74.1% |
Min Return | -97.4% |
Average Return | -.1% |
Std Dev of Returns | 19.6% |
from IPython.display import display
import Quandl
from datetime import datetime, timedelta
tickers = ['XOM', 'CVX', 'CLB', 'OXY', 'SLB']
market_ticker = 'GOOG/NYSE_VOO'
lookback = 30
d_col = 'Close'
data = {tick: Quandl.get('YAHOO/{}'.format(tick))[-lookback:] for tick in tickers}
market = Quandl.get(market_ticker)
We first want to know how much each ticker returned over the prior period.
returns = {tick: data[tick][d_col].pct_change() for tick in tickers}
display({tick: returns[tick].mean() for tick in tickers})
Sharpe: ${R - R_M \over \sigma}$
We use the average return over the lookback period, minus the market average return, over the ticker standard deviation to calculate the Sharpe. Shorting a stock turns a negative Sharpe positive.
market_returns = market.pct_change()
sharpe = lambda ret: (ret.mean() - market_returns[d_col].mean()) / ret.std()
sharpes = {tick: sharpe(returns[tick]) for tick in tickers}
display(sharpes)
This one is easy - what is the maximum daily change over the lookback period? That is, because we will allow short positions, we are not concerned strictly with maximum downturn, but in general, what is the largest 1-day change?
drawdown = lambda ret: ret.abs().max()
drawdowns = {tick: drawdown(returns[tick]) for tick in tickers}
display(drawdowns)
$\begin{align} max\ \ & \mu \cdot \omega\\ s.t.\ \ & \vec{1} \omega = 1\\ & \vec{S} \omega \ge s\\ & \vec{D} \cdot | \omega | \le d\\ & \left|\omega\right| \le l\\ \end{align}$
We want to maximize average return subject to having a full portfolio, Sharpe above a specific level, drawdown below a level, and leverage not too high - that is, don't have huge long/short positions.
import numpy as np
from scipy.optimize import minimize
#sharpe_limit = .1
drawdown_limit = .05
leverage = 250
# Use the map so we can guarantee we maintain the correct order
# sharpe_a = np.array(list(map(lambda tick: sharpes[tick], tickers))) * -1 # So we can write as upper-bound
dd_a = np.array(list(map(lambda tick: drawdowns[tick], tickers)))
returns_a = np.array(list(map(lambda tick: returns[tick].mean(), tickers))) # Because minimizing
meets_sharpe = lambda x: sum(abs(x) * sharpe_a) - sharpe_limit
def meets_dd(x):
portfolio = sum(abs(x))
if portfolio < .1:
# If there are no stocks in the portfolio,
# we can accidentally induce division by 0,
# or division by something small enough to cause infinity
return 0
return drawdown_limit - sum(abs(x) * dd_a) / sum(abs(x))
is_portfolio = lambda x: sum(x) - 1
def within_leverage(x):
return leverage - sum(abs(x))
objective = lambda x: sum(x * returns_a) * -1 # Because we're minimizing
bounds = ((None, None),) * len(tickers)
x = np.zeros(len(tickers))
constraints = [
{
'type': 'eq',
'fun': is_portfolio
}, {
'type': 'ineq',
'fun': within_leverage
#}, {
# 'type': 'ineq',
# 'fun': meets_sharpe
}, {
'type': 'ineq',
'fun': meets_dd
}
]
optimal = minimize(objective, x, bounds=bounds, constraints=constraints,
options={'maxiter': 500})
# Optimization time!
display(optimal.message)
display("Holdings: {}".format(list(zip(tickers, optimal.x))))
expected_return = optimal.fun * -100 # multiply by -100 to scale, and compensate for minimizing
display("Expected Return: {:.3f}%".format(expected_return))
expected_drawdown = sum(abs(optimal.x) * dd_a) / sum(abs(optimal.x)) * 100
display("Expected Max Drawdown: {0:.2f}%".format(expected_drawdown))
# TODO: Calculate expected Sharpe