dictParam['stoploss'] = dictDataSpec['decimalStoploss'] if dictParam['stoploss'] == 0.99: dictParam['boolStoploss'] = False else: dictParam['boolStoploss'] = True # set time start, end, step dtDataStart = dfAll.index[0] dtDataEnd = dfAll.index[-1] NDayTrain = dictDataSpec['NDayTrain'] NDayTest = dictDataSpec['NDayTest'] NthFriday = dictDataSpec['NthFriday'] NDayShift = dictDataSpec['NDayShift'] NMonthStart = dictDataSpec['NMonthStart'] listDTTestStart = Utils.generateNthFriday(NthFriday, NMonthStart, NDayShift) listDTTestStart = [x for x in listDTTestStart if x <= Utils.dtLastData] dtDataStart = datetime.datetime.combine(dfAll.index[0], dtDataStart.time()) dtTrainStart = dtDataStart seriesIndicatorAll = pd.Series() seriesYAll = pd.Series() seriesIndicatorAll = dfAll['Close'].copy() seriesIndicatorAll = seriesIndicatorAll.apply(lambda x: np.nan) # back test this data spec if dtEnter > listDTTestStart[-1]: listDTTestStart.append(dtEnter) for nWindow, dtTestStart in enumerate(listDTTestStart): # the start & end of test window
def funcTopPort(strParamSweep): # param strMethodVolatility = 'EWMAN2' # dtBackTestStart & dtBackTestEnd dtBackTestStart = Utils.dtBackTestStart #dtBackTestEnd = Utils.dtBackTestEnd ######################################################### # read backtest result of rquired cases ######################################################### # read stored results listDictPerTopPort = Utils.getFileAddressForTopPort(strParamSweep) #---------------- clear fake tomorrow dictDataSpec = listDictPerTopPort[0] strTB = dictDataSpec['strModelName'][0] try: Utils.UtilsDB.clearTBFakeTomorrow(Utils.UtilsDB.DB_NAME_POSITION, strTB) Utils.UtilsDB.clearTBFakeTomorrow(Utils.UtilsDB.DB_NAME_PERFORMANCE, strTB) except: pass #---------------- iterate for dictPerTopPort in listDictPerTopPort: gc.collect() listDictDataSpec = [] for strFileAddress in dictPerTopPort['listFileAddress']: listDictDataSpec.append(pd.read_pickle(strFileAddress)) ######################################################### # prepare directory for saving ######################################################### strDirParamSweep = Utils.dirResultPerCase + '/' + strParamSweep if os.path.exists(strDirParamSweep) is False: os.mkdir(strDirParamSweep) strDirAddressCase = strDirParamSweep + '/' + dictPerTopPort[ 'strCase'] + '/' if os.path.exists(strDirAddressCase) is False: os.mkdir(strDirAddressCase) strFileAddressPrefix = strDirAddressCase # save config file of the port dictPerTopPortOut = dict( (key, value[0]) for key, value in dictPerTopPort.iteritems() if key not in ['strCase', 'listFileAddress', 'Secu']) pd.Series(dictPerTopPortOut).to_pickle(strFileAddressPrefix + '/' + 'portconfig.pickle') NDayTrain = dictPerTopPort['NDayTrain'][0] NDayTest = dictPerTopPort['NDayTest'][0] NDayShift = dictPerTopPort['NDayShift'][0] strModelName = dictPerTopPort['strModelName'][0] ######################################################### # determine the rebalance datetime ######################################################### if strModelName in ['TS', 'TSC']: listDTTestStart = Utils.generateNthFriday( dictPerTopPort['NthFriday'][0], dictPerTopPort['NMonthStart'][0]) elif strModelName in ['TSM', 'XSM']: listDTTestStart = Utils.generateDTTestStartCalendarDay( NDayTest, NDayShift, dictPerTopPort['NWeekStart'][0]) else: print 'incorrect model name: %s' % strModelName listDTTestStart = [x for x in listDTTestStart if x <= Utils.dtLastData] dtEnter = Utils.UtilsDB.dtTomorrow listDTTestStart.append(dtEnter) seriesDTRebalance = pd.Series(listDTTestStart) ######################################################### # calculate the portfolio return ######################################################### dtBackTestStart = seriesDTRebalance[ seriesDTRebalance >= Utils.dtBackTestStart].values[0] if strModelName in ['TSM', 'TS', 'TSC', 'XSM']: dfOut = UtilsPortSync.funcShowStrategyPortSum( dictPerTopPort, listDictDataSpec, seriesDTRebalance, strMethodVolatility, NDayTest, dtBackTestStart, #dtBackTestEnd, strFileAddressPrefix) dfOut = dfOut[dfOut.index >= dtBackTestStart] # plot & savefig dfOut = dfOut[['Cum Return', 'Max DD', 'Position']].ffill().dropna() #ax = dfOut[['Cum Return', 'Max DD', 'Position']].plot(secondary_y=['Position']) #ax.set_ylabel('Cum Return') #ax.set_ylabel('Position') #plt.savefig(strFileAddressPrefix + 'figure.png', format='png') #plt.close() print strFileAddressPrefix # output dfOut.to_pickle(strFileAddressPrefix + 'dfOut.pickle')