def question_3():
    startDate = datetime(2008, 1, 1)
    endDate = datetime(2009, 12, 31)
    timeStamps = getNYSEdays(startDate, endDate, timedelta(hours=16))
    dataReader = DataAccess('Yahoo')

    symbolsListName = 'sp5002012'
    symbols = dataReader.get_symbols_from_list(symbolsListName)
    symbols.append('SPY')
    data = fillNA(dataReader.get_data(timeStamps, symbols, 'actual_close')) # read data and filled na

    # create events and event-profile
    events = find_crossing_threshold_events(data, threshold=7)
    eventprofiler(events, {'close': data}, i_lookback=20, i_lookforward=20, s_filename = join(rootDir, symbolsListName + '_threshold7.pdf'),
                  b_market_neutral=True, b_errorbars=True, s_market_sym='SPY')
def graphStrategies(eventMaker, startDate, endDate, symbols, marketSymbol, fieldName, isSymbolsList, holdingPeriodsToTry,
                    outputDir='.'):
    """
    run eventprofiler and produce graphs for given the strategy
    @param eventMaker: a function that takes prices as its sole argument
    @return: nothing. save graphs (xxxdays.pdf) to outputDir
    """

    prices = pd.concat([getPrices(startDate, endDate, symbols, fieldName, isSymbolsList=isSymbolsList),
                        getPrices(startDate, endDate, [marketSymbol], fieldName)], axis=1)
    events = eventMaker(prices)

    # make holdingPeriodsToTry iterable
    if not isinstance(holdingPeriodsToTry, Iterable):
        holdingPeriodsToTry = [holdingPeriodsToTry]

    for holdingPeriod in holdingPeriodsToTry:
        eventprofiler(deepcopy(events), {'close': prices}, i_lookback=holdingPeriod, i_lookforward=holdingPeriod,
                      s_filename=os.path.join(outputDir, str(holdingPeriod) + 'days.pdf'),
                      b_market_neutral=True, b_errorbars=True, s_market_sym=marketSymbol)
    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)

    plt.show()
__author__ = 'jjin'

from datetime import datetime
from pprint import pprint

from utilities import getPrices, createBollingerEventFilter, calculateBollingerValues
from QSTK.qstkstudy.EventProfiler import eventprofiler

if __name__ == '__main__':
    startDate = datetime(2008, 1, 1)
    endDate = datetime(2009, 12, 31)
    lookBackPeriod = 20
    numOfStds = 1
    marketSymbol = 'SPY'
    # symbols = ['AAPL', 'GOOG', 'IBM', 'MSFT']
    symbols = 'SP5002012'

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

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

    eventprofiler(eventFilter, {'close': prices}, i_lookback=lookBackPeriod, i_lookforward=1,
                  s_filename = None, b_market_neutral=True, b_errorbars=True, s_market_sym=marketSymbol)