__author__ = 'jjin' from datetime import datetime from utilities import getPrices, calculateBollingerValues, createBollingerEventFilter, plotBollingerBands if __name__ == '__main__': startDate = datetime(2010, 1, 1) endDate = datetime(2010, 12, 31) lookBackPeriod = 20 numOfStds = 2 symbols = ['AAPL', 'GOOG', 'IBM', 'MSFT'] # ----- compute prices and Bollinger stuff ----- prices = getPrices(startDate, endDate, symbols, 'close') bollingerVals, means, stds, lowerBand, upperBand = calculateBollingerValues(prices, lookBackPeriod, numOfStds, verbose=False) _, _, buyDates, sellDates = createBollingerEventFilter(bollingerVals, {'type': 'SIMPLE_CROSS', 'upperVal': 1, 'lowerVal': -1}, verbose=False) # ----- plot ----- for symbol in symbols: title = symbol + ': ' + str(numOfStds) + '-stds Bollinger Bands' filename = "%s_%dlookback_%dstds_%s-%s.pdf" % (symbol, lookBackPeriod, numOfStds, str(startDate.date()), str(endDate.date())) plotBollingerBands(prices[symbol], means[symbol], lowerBand[symbol], upperBand[symbol], title, bollingerVals[symbol], buyDates[symbol], sellDates[symbol], filename=filename)
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 ----- orders = makeOrdersAccordingToEvents(eventFilter, holdingPeriod, numStocksTraded, saveIntermediateResults=True) portValues = marketsim(orders, initialInvestment) # calculateMetrics(portValues, saveIntermediateResults=True) analyze(portValues, '$SPX', plotFname='performancePlot.pdf', verbosity=0)