Example #1
0
from copy import deepcopy
import matplotlib.pyplot as plt
from time import time

from QSTK.qstkutil.qsdateutil import getNYSEdays
from QSTK.qstkutil.tsutil import returnize1

t0 = time()

startDate = datetime(2012, 9, 1)
endDate = datetime(2012, 9, 12)
symbols = ['AAPL', 'GLD', 'MCD', 'SPY']
forecastLength = 21
numPaths = 1000

origPrices = getPrices(startDate, endDate, symbols, 'close')

# get log returns
origLogRets = deepcopy(origPrices)
for col in origLogRets.columns:
    returnize1(origLogRets[col])
origLogRets = np.log(origLogRets)

print 'first 10 rows of origPrices:\n', origPrices[:10]
# print origLogRets

print '\nCorrelation matrix:\n', origLogRets.corr()

print '\nCholesky decomposition:\n', np.linalg.cholesky(origLogRets.corr())

# get correlated future log returns
from copy import deepcopy
import matplotlib.pyplot as plt


from QSTK.qstkutil.DataAccess import DataAccess
from QSTK.qstkutil.qsdateutil import getNYSEdays
from QSTK.qstkutil.tsutil import returnize1


startDate = datetime(2010, 8, 30)
endDate = datetime(2012, 8, 30)
symbols = ['AAPL', 'GLD', 'MCD', '$SPX']
forecastLength = 21
numPaths = 1000

origPrices = getPrices(startDate, endDate, symbols, 'close')

# get log returns
origLogRets = deepcopy(origPrices)
for col in origLogRets.columns: returnize1(origLogRets[col])
origLogRets = np.log(origLogRets)

print 'first 10 rows of origPrices:\n', origPrices[:10]
# print origLogRets

print '\nCorrelation matrix:\n', origLogRets.corr()

print '\nCholesky decomposition:\n', np.linalg.cholesky(origLogRets.corr())

# get correlated future log returns
meanVec = origLogRets.mean()
    startDate = datetime(2008, 1, 1)
    endDate = datetime(2009, 12, 30)

    lookBackPeriod = 20
    numOfStds = 1
    marketSymbol = 'SPY'
    symbols = 'SP5002012'
    bolBenchMark = -2
    marketBenchMark = 1

    holdingPeriod = 5
    numStocksTraded = 100
    initialInvestment = 100000

    # ----- compute prices and create events -----
    prices = getPrices(startDate, endDate, symbols, 'close', isSymbolsList=True, additionalSymbol=marketSymbol)
    # prices = getPrices(startDate, endDate, symbols, 'close', additionalSymbol=marketSymbol)
    bollingerVals, _, _, _, _ = calculateBollingerValues(prices, lookBackPeriod, numOfStds)

    eventFilter, actionDates = createBollingerEventFilter(bollingerVals,
                                                          {'type': 'CROSS_WRT_MARKET_FALL', 'bolBenchMark': bolBenchMark,
                                                           'marketBenchMark': marketBenchMark, 'marketSymbol': marketSymbol,
                                                           'lookBackPeriod': lookBackPeriod, 'numOfStds': numOfStds})

    try:
        eventprofiler(eventFilter, {'close': prices}, i_lookback=lookBackPeriod, i_lookforward=holdingPeriod,
                      b_market_neutral=True, b_errorbars=True, s_market_sym=marketSymbol, s_filename='eventPlot.pdf')
    except:
        print "There aren't enough events for the event profiler."

    # ----- trade and evaluate -----