def getCurrentReturn(liveAlgInfo, stopStep, Cash=None): originalNameSpace = namespace_manager.get_namespace() namespace_manager.set_namespace("") alginfo = meTools.memGet(meSchema.meAlg, meTools.buildAlgKey(1)) namespace_manager.set_namespace(originalNameSpace) if Cash is not None: startCash = Cash else: startCash = alginfo.Cash stopStepQuotes = princeFunc.getStepQuotes(stopStep) for liveAlgKey in liveAlgInfo: positions = eval(liveAlgInfo[liveAlgKey].Positions) positionsValue = 0.0 for symbol in positions: currentPrice = stopStepQuotes[symbol] posPrice = positions[symbol]["Price"] shares = positions[symbol]["Shares"] positionsValue += (currentPrice - posPrice) * shares liveAlgInfo[liveAlgKey].PosVal = positionsValue liveAlgInfo[liveAlgKey].percentReturn = ( liveAlgInfo[liveAlgKey].PosVal + liveAlgInfo[liveAlgKey].PandL ) / startCash liveAlgInfo[liveAlgKey].numTrades = len(eval(liveAlgInfo[liveAlgKey].CashDelta)) history = eval(liveAlgInfo[liveAlgKey].history) history.appendleft({"step": stopStep, "return": liveAlgInfo[liveAlgKey].percentReturn}) liveAlgInfo[liveAlgKey].history = repr(history) return liveAlgInfo
def getBackTestReturns(memkeylist, stopStep, stats, namespace): ''' Using list(set()) to de-duplicate key list. ''' algkeys = list(set([key.split('_')[-1] for key in stats])) algs = meTools.memGet_multi(meSchema.meAlg,algkeys) cuekeys = [algs[key].BuyCue for key in algs] cuekeys.extend([algs[key].SellCue for key in algs]) cuekeys = list(set(cuekeys)) tradecues = meTools.memGet_multi(meSchema.tradeCue,cuekeys) stopStepQuotes = princeFunc.getStepQuotes(stopStep) backTestReturns = {} # Pre-populate dict to avoid doing __contains__ on every key. for memkey in stats: algkey = memkey.split('_')[-1] startMonth = memkey.split('_')[0] if not backTestReturns.__contains__(algkey): buycue = tradecues[algs[algkey].BuyCue] sellcue = tradecues[algs[algkey].SellCue] fingerprint = { 'buy' : [buycue.QuoteDelta, buycue.TimeDelta], 'sell' : [sellcue.QuoteDelta, sellcue.TimeDelta] } backTestReturns[algkey] = {'fingerprint' : fingerprint, 'returns' : {} } for memkey in stats: algkey = memkey.split('_')[-1] # Memkey has form: startStep_stopStep_algKey startMonth = memkey.split('_')[0] testStopStep = memkey.split('_')[1] positionsValue = 0.0 for key in stats[memkey]['Positions']: currentPrice = stopStepQuotes[key] posPrice = stats[memkey]['Positions'][key]['Price'] shares = stats[memkey]['Positions'][key]['Shares'] positionsValue += (currentPrice-posPrice)*shares stepReturn = (positionsValue + stats[memkey]['PandL'])/algs[algkey].Cash # 'algKey' : # { 'fingerprint' : 'buy = TradeCue, TimeDelta : sell = TradeCue, TimeDelta', # 'returns' : # { 'startMonth(1)' : # { 'return' : 'x1%', 'PandL' : '$y1', 'PosVal' : '$z1'}, # 'startMonth(2)' : # { 'return' : 'x2%', 'PandL' : '$y2', 'PosVal' : '$z2'} # } backTestReturns[algkey]['returns'][int(startMonth)] = {'return' : stepReturn, 'lastBuy' : stats[memkey]['lastBuy'], 'lastSell' : stats[memkey]['lastSell'], 'numTrades' : len(stats[memkey]['CashDelta']), 'PandL' : stats[memkey]['PandL'], 'PosVal' : positionsValue, 'stopStep' : int(testStopStep), 'CashDelta' : repr(stats[memkey]['CashDelta']), 'Positions' : repr(stats[memkey]['Positions'])} return backTestReturns