def doDesires(step, startKey=None, stopKey=None): global cvalDict if stopKey is None and startKey is None: startKey = 1 stopKey = int(meSchema.tradeCue.all(keys_only=True).order('-__key__').get().name()) elif stopKey is None or startKey is None: raise(BaseException('Must define both startKey and stopKey, or both must be None!')) # Check cachepy clock and sync with memcache if necessary. syncProcessCache(step,startKey,stopKey) # Construct cvalDict for this step. for stckID in [1,2,3,4]: deltakey = str(stckID) + "_" + str(step) cvalDict[deltakey] = calculateDeltas(stckID, step) medesires = [] count = 0 for i in range(startKey, stopKey + 1): cuekey = meTools.buildTradeCueKey(i) desires = doDesire(step, cuekey) if len(desires) != 0: medesires.extend(desires) meTools.batchPut(medesires) # Remove from global cvalDict since not sure how that will act with same running process. for stckID in [1,2,3,4]: del cvalDict[str(stckID) + '_' + str(step)]
def persistBackTestReturns(backTestReturns): putList = [] for algKey in backTestReturns: for startMonth in backTestReturns[algKey]['returns']: currentResult = backTestReturns[algKey]['returns'][startMonth] resultKey = algKey + "_" + str(startMonth).rjust(7,'0') + "_" + str(currentResult['stopStep']).rjust(7,'0') backTestResult = meSchema.backTestResult(key_name = resultKey, algKey = algKey, startStep = startMonth, stopStep = currentResult['stopStep'], percentReturn = currentResult['return'], lastBuy = currentResult['lastBuy'], lastSell = currentResult['lastSell'], numTrades = currentResult['numTrades'], PandL = currentResult['PandL'], PosVal = currentResult['PosVal'], CashDelta = currentResult['CashDelta'], Positions = currentResult['Positions']) putList.append(backTestResult) meTools.batchPut(putList)
def doCompounds(stopStep, startStep, entities): putList = [] memKeys = [] model = type(entities[0]) if model == meSchema.backTestResult: idProp = 'algKey' prefix = 'BTR-' elif model == meSchema.liveAlg: idProp = 'technique' prefix = 'LAR-' else: raise BaseException('Model must be backTestResult or liveAlg!') maxR = getMaxRnum(model) stepBacks = [week for week in range(1,maxR)] for entity in entities: identifier = getattr(entity, idProp) for stepback in stepBacks: newStop = stopStep - 400*stepback newStart = startStep - 400*stepback memkey = buildMemKey(newStop, newStart, identifier, prefix) memKeys.append(memkey) prevReturns = memGetPercentReturns(memKeys, prefix) for entity in entities: Rdict = {1: (1.0 + entity.percentReturn)} for Rnum in range(2, maxR + 1): identifier = getattr(entity,idProp) lastPercentReturn = getRReturn(stopStep, startStep, identifier, Rnum-1, prevReturns, prefix) if lastPercentReturn is not None: try: Rdict[Rnum] = Rdict[Rnum-1]*(1.0 + lastPercentReturn) except TypeError: memkey = buildMemKey(stopStep, startStep, identifier, prefix) message = 'Rnum: %s, memkey: %s, entity.key().name(): %s - V2' % (Rnum, memkey, entity.key().name()) raise(BaseException(message)) else: Rdict[Rnum] = None setattr(entity, 'R' + str(Rnum), Rdict[Rnum]) putList.append(entity) meTools.batchPut(putList)