Created
September 16, 2013 02:46
-
-
Save nuttt/6576228 to your computer and use it in GitHub Desktop.
Find Sharpe Ratio with QSTK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import QSTK.qstkutil.qsdateutil as du | |
| import QSTK.qstkutil.tsutil as tsu | |
| import QSTK.qstkutil.DataAccess as da | |
| import datetime as dt | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import numpy as np | |
| def sharpe_ratio(ls_symbols, ls_alloc, dt_start, dt_end): | |
| # ls_symbols = ["BRCM", "TXN", "IBM", "HNZ"] | |
| # ls_alloc = [0.25, 0.25, 0.25, 0.25] | |
| # dt_start = dt.datetime(2010,1,1) | |
| # dt_end = dt.datetime(2010,12,31) | |
| dt_timeofdate = dt.timedelta(hours=16) | |
| ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofdate) | |
| c_dataobj = da.DataAccess('Yahoo') | |
| ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] | |
| ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys) | |
| d_data = dict(zip(ls_keys, ldf_data)) | |
| na_price = d_data['close'].values | |
| tsu.returnize0(na_price) | |
| na_port_price = np.sum(na_price * ls_alloc, axis=1) | |
| avg = np.average(na_port_price) | |
| std = np.std(na_port_price) | |
| return np.sqrt(250) * avg / std | |
| # ls_symbols = ["BRCM", "TXN", "IBM", "HNZ"] | |
| ls_symbols = ['C', 'GS', 'IBM', 'HNZ'] | |
| dt_start = dt.datetime(2010,1,1) | |
| dt_end = dt.datetime(2010,12,31) | |
| max_sharpe = -999999 | |
| max_alloc = [] | |
| for i in range(0,11): | |
| for j in range(0,11-i): | |
| for k in range(0,11-i-j): | |
| l = 10 - i - j - k | |
| ls_alloc = [ i,j,k,l] | |
| ls_alloc = map(lambda x: x/10.0, ls_alloc) | |
| sharpe = sharpe_ratio(ls_symbols, ls_alloc, dt_start, dt_end) | |
| if sharpe > max_sharpe: | |
| max_sharpe = sharpe | |
| max_alloc = ls_alloc | |
| print max_sharpe | |
| print max_alloc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment