def plots():
     FTSE100_AdjClose = pickle.load(open('pickle_jar/DJIA_AdjClose.pkl'))
     results = pickle.load(open('pickle_jar/results_DJIA_coint.pkl','rb'))        
     results = pd.DataFrame(results, columns = ['SeriesX_name','SeriesY_name','Adf_pvalue', 'reg_scaling', 'reg_intercept'])
     results = results.sort('Adf_pvalue', ascending=True)
     
     print results.head(15)
     
     for row in range(0,5):
         rowinfo = results.iloc[row].tolist()
         seriesX = FTSE100_AdjClose[rowinfo[0]]
         seriesX = seriesX[seriesX.notnull()]
         
         seriesY = FTSE100_AdjClose[rowinfo[1]]
         seriesY = seriesY[seriesY.notnull()]
         
         
         df_coint_check = pd.merge(pd.DataFrame(seriesX),
                               pd.DataFrame(seriesY), 
                               how='inner', 
                               left_index=True, 
                               right_index=True)
         
         
         df_coint_check.columns = ['x','y']
         pmd = pairs_md(df_coint_check, xInd='x', yInd = 'y')
         pmd.fitOLS()
         pmd.generateTradeSigs(windowLength=20, entryScale=2, exitScale=0)
         
         pmd.plot_spreadAndSignals()
         plt.title( "Row {3}: Spread between {0} and {1}, scaling {2}".format(rowinfo[0],rowinfo[1],rowinfo[3], row))
         
         
         
     plt.show()
 def search_for_CI(series_names):
     
     FTSE100_AdjClose = pickle.load(open('pickle_jar/DJIA_AdjClose.pkl'))
     results = []
     combs = [cm for cm in  combinations(series_names,2)]
     
     i = 0
     for cmb in combs:
         
         print 'Starting row', i
         i +=1
         
         seriesX_name = cmb[0]
         seriesY_name = cmb[1]
         
         try:
             seriesX = FTSE100_AdjClose[seriesX_name]
             seriesX = seriesX[seriesX.notnull()]
             
             seriesY = FTSE100_AdjClose[seriesY_name]
             seriesY = seriesY[seriesY.notnull()]
         
             df_coint_check = pd.merge(pd.DataFrame(seriesX),
                                   pd.DataFrame(seriesY), 
                                   how='inner', 
                                   left_index=True, 
                                   right_index=True)
             
             df_coint_check.columns = ['x','y']
             pmd = pairs_md(df_coint_check, xInd='x', yInd = 'y')
             pmd.fitOLS()
             reg_params = pmd.results.params.tolist()
             adf_pvalue = pmd.adfResids()[1]
             
             results.append([seriesX_name, seriesY_name, adf_pvalue] + reg_params)
             
         except ValueError as e:
             
             print "Problem with pairs X = {0}, Y = {1}".format(seriesX_name,seriesY_name)
             print "Error is ", e.what()
         
     
     pickle.dump(results , open('pickle_jar/results_DJIA_coint.pkl','wb'))
示例#3
0
 def PairTradeSP500(stdWindowLength, entryScale, exitScale):
     
     #Read in data and setup object
     dbpath = "/home/phcostello/Documents/Data/FinanceData.sqlite"
     the_data_reader = dr.DBReader(dbpath)
     SP500 = the_data_reader.readSeries("SP500")
     BA = the_data_reader.readSeries("BA")
     
     dim = 'Adj_Close' #Choose dim to analyse
     SP500AdCl = SP500[dim]
     BAAdCl = BA[dim]
 #        print SP500AdCl.head()
 #        print BAAdCl.head()
     dataObj = pd.merge(pd.DataFrame(BAAdCl), pd.DataFrame(SP500AdCl), how='inner',left_index = True, right_index = True)
     dataObj.columns = ['y','x']
     
     #Construct data object for pairs trading strat
     pmd = md.pairs_md(dataOb=dataObj,xInd='x',yInd='y')
     maxdate = datetime.date(2013,1,1)
     pmd.core_data = pmd.core_data[:maxdate]
     pmd.fitOLS()
     pmd.generateTradeSigs(stdWindowLength, entryScale, exitScale)
     
     #Setup portfolio
     spreadTrade = td.TradeEquity("spread", notional=0, price_series_label="spread")
     port = pf.Portfolio("portfolio", cashAmt=100)
     port.add_trade(spreadTrade)
     #No more trade types
     port.fixed_toggle()
     
     #Setup Strategy
     pairsStrat = tsc.Reversion_EntryExitTechnical(market_data=pmd, portfolio=port, initial_time_index=1)
     
     #return pairsStrat
 
     tic = time.clock()
     pairsStrat.run_strategy()
     toc = time.clock()
     print "strategy took {} seconds to run".format(toc - tic)
     outfile = open("pickled_pairs.pkl", 'wb')
     pairs_strategy_run = pickle.dump(pairsStrat,outfile)
     outfile.close()
示例#4
0
 def test_pairs_md(self):
 #prepare data
     import DataHandler.DBReader as dbr
     dbpath = "/home/phcostello/Documents/Data/FinanceData.sqlite"
     dbreader = dbr.DBReader(dbpath)
     SP500 = dbreader.readSeries("SP500")
     BA = dbreader.readSeries("BA")
     dim = 'Adj_Close'
     SP500AdCl = SP500[dim]
     BAAdCl = BA[dim]
     dataObj = pd.merge(pd.DataFrame(BAAdCl), pd.DataFrame(SP500AdCl), how='inner',left_index = True, right_index = True)
     dataObj.columns = ['y','x']
     
     pmd = pairs_md(dataOb=dataObj,xInd='x',yInd='y')
     pmd.fitOLS()
     resid = pmd.results.resid
     resid.plot()
     rllstd = pd.rolling_std(resid,50)
     rllstd.plot()
     
     pmd.generateTradeSigs(50, entryScale=1, exitScale=0)
     self.assertTrue( (pmd['entryUpper'][50:] == rllstd[50:]).all())
示例#5
0
def grid_tester(run_strat, do_plots):
        
    ##################Initialise##########################
    #Read in data and setup object
    
#    dbpath = "/home/phcostello/Documents/Data/FinanceData.sqlite"
#    dbreader = dbr.DBReader(dbpath)
    #SP500 = dbreader.readSeries("SP500")
    #BA = dbreader.readSeries("BA")
    #dim = 'Adj_Close'
    #SP500AdCl = SP500[dim]
    #BAAdCl = BA[dim]
    #dataObj = pd.merge(pd.DataFrame(BAAdCl), pd.DataFrame(SP500AdCl), how='inner',left_index = True, right_index = True)
    #dataObj.columns = ['y','x']
    
    #Read data for all pairs
    #Note have checked top 10 in and constructed data in
    #CheckingSeriesCoint.py
    
    dataObj = pickle.load(open('pickle_jar/DJIA_AdjClose.pkl'))
    cointAnalysis = pickle.load(open('pickle_jar/results_DJIA_coint.pkl','rb'))        
    cointAnalysis = pd.DataFrame(cointAnalysis, columns = ['SeriesX_name','SeriesY_name','Adf_pvalue', 'reg_scaling', 'reg_intercept'])
    cointAnalysis = cointAnalysis.sort('Adf_pvalue', ascending=True)
    #cointAnalysis.info()
    seriesNames = cointAnalysis[['SeriesX_name','SeriesY_name']]
    #dataObj.info()
    
    top10 = seriesNames.iloc[0:5]
    top10['rank']= range(1, len(top10)+1)
    top10.head()
    top10 =top10.values.tolist()
    
    #Filter to what we want
    #setup logging
    logfile = 'logs/GridTester_ma.txt'
    logging.basicConfig(filename= logfile, filemode='w', level = logging.ERROR)
    
    ##################Make par Table##########################
    #Parameter table rows are
    #[ pairs_labes, strategy_params, train_ranges, test_ranges ]
    
    #series
    series_names = top10
    
    #strat pars
    entry_scale = np.arange(1,3.5,0.5)
    exit_scale = np.arange(0.0,3.0,0.5)
    strat_parameters = [ [sw, lw] for sw in entry_scale for lw in exit_scale if sw >= lw]
    
    #Train - test date pars
    dmin = datetime.datetime(2006,1,1)
    num_periods = 15
    period_delta = datetime.timedelta(182)
    #Create range start/end offsets from min data
    train_test_ranges = [ [dmin + i*period_delta, #train start
                         dmin + (i+1)* period_delta, #train end
                         dmin + (i+1)* period_delta, #test start
                         dmin + (i+2)* period_delta] #test end
                         for i in range(0,num_periods)]
    
    #Make table of parameters + dates
    parameter_table = [ series + pars + date_ranges for series in series_names for pars in strat_parameters for date_ranges in train_test_ranges]
    parameter_table = [ parameter_table[i] + [i] for i in range(0,len(parameter_table))]
    
    
    colnames =['runtype',\
    'series_X',\
    'series_Y',\
    'rank',\
    'entry_scale',\
    'exit_scale',\
    'train_start',\
    'train_end',\
    'test_start',\
    'test_end',\
    'par_rownumber']
    
    
    ##################Make Res Table##########################
    
    #We just define   
    result_cols = ['returns',\
                   'volatility',\
                   'sharpe',\
                   'sortino']
   
    #Use x will be analyser object which has result functions
    #result cols should have same order as function below
    
    result_func = lambda x : [x.get_result().iat[-1,0]/x.get_result().iat[0,0] - 1 ,
                              x.get_volatility(),
                              x.sharpe_ratio(),
                              x.sortino_ratio()]
    
    result_table_cols = colnames + result_cols
        
    ##################Test strat on pars in Table##########################
    
    if run_strat:
        
        #open file for output
        outfile = open('Results/dummy.csv','wb')
        
        #run simulations
        tic = time.clock()
        runStrat(parameter_table,dataObj, outfile, result_table_cols, result_func)
        toc = time.clock()
        print "All strategies took {} seconds to run".format(toc - tic)
        
        outfile.close()
    
    #################$ Plot for one set of parameters ########################
    
    #Setup Data
    if do_plots:
        this_pars = parameter_table[496]
        #Setup Data

        #Setup initial portfolio
        trade_equity_spread = TradeEquity('spread', 
                                          notional=0, 
                                          price_series_label='spread')
        
        portfolio_0 = Portfolio("portfolio", cashAmt=100)
        portfolio_0.add_trade(trade_equity_spread)
        #No more trade types
        portfolio_0.fixed_toggle()
        
        
        print this_pars
        strategy, train_data, test_data = setup_strategy(this_pars,dataObj)
        data = test_data
        strategy.run_strategy(data,portfolio_0)
        analyser = ResultsAnalyser(strategy.result,referenceIndex=None)
        
        
        fig =plt.figure()
        ax1 = fig.add_subplot(2,1,1)
        ax2 = fig.add_subplot(2,1,2)
        
        portfolioVal = analyser.get_result()
        print portfolioVal['Value']
        start = 0
        end = len(portfolioVal)
        portfolioVal['Value'].iloc[start:end].plot(ax=ax1)
        
#        trades_at = portfolioVal[portfolioVal['Signal_bs'].isin(['buy','sell'])]
#        
#        #Putting trade bars on
#        for dt in trades_at.index:
#            plt.axvline( dt, ymin =0, ymax =1 )
        
        plt.subplot(2,1,1)
        pairs_md(data.loc[portfolioVal.index].iloc[start:end],0,0).plot_spreadAndSignals(ax2)
        
        
        #plt.title(seriesName)
        plt.show()