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 getAlgStats(alphaAlg=1,omegaAlg=3540): keylist = [] model = meSchema.algStats for i in range(alphaAlg,omegaAlg+1): key_name = meTools.buildAlgKey(str(i)) keylist.append(key_name) algs = meTools.memGet_multi(model,keylist) return algs
def resetAlgstats(memprefix = "unpacked_",algCash=20000.0,alphaAlg=1,omegaAlg=10620): memkeylist = [] cashdelta = {} statDict = {} for i in range(alphaAlg,omegaAlg+1): key_name = meTools.buildAlgKey(str(i)) memkey = memprefix + key_name memkeylist.append(memkey) for key in memkeylist: cashdelta[key] = deque() statDict[key] = { 'Cash' : algCash, 'CashDelta' : cashdelta[key], 'PandL' : 0.0, 'Positions' : {}, 'lastBuy' : 0, 'lastSell' : 0} return statDict
def post(self): JobID = self.request.get('JobID') totalBatches = int(self.request.get('totalBatches')) callback = self.request.get('callback') taskname = self.request.headers['X-AppEngine-TaskName'] startAlg = int(self.request.get('startAlg')) stopAlg = int(self.request.get('stopAlg')) algBatch = [meTools.buildAlgKey(i) for i in range(startAlg, stopAlg + 1)] monthBatch = self.request.get_all('monthBatch') stopStep = self.request.get('stopStep') namespace = str(self.request.get('namespace')) backTestBatch(algBatch, monthBatch, stopStep, namespace) if callback: meTools.taskAdd('callback-' + taskname, callback, 'default', 0.5, JobID = JobID, taskname = taskname, totalBatches = totalBatches, model = '', jobtype = 'callback', stepType = 'weeklyBackTests')
def calculateWeeklyLiveAlgs(stopStep, stepRange, namespace, JobID, totalBatches, callback): from google.appengine.api.labs import taskqueue namespace_manager.set_namespace("") alginfo = meTools.memGet(meSchema.meAlg, meTools.buildAlgKey(1)) namespace_manager.set_namespace(namespace) Cash = alginfo.Cash initializeLiveAlgs(stopStep, stepRange, Cash) # Get liveAlgs and branch out the different FTL, dnFTL Ntypes. # keys like: 0003955-0001600-FTLe-N1 liveAlgs = ( meSchema.liveAlg.all(keys_only=True) .filter("stopStep =", stopStep) .filter("stepRange =", stepRange) .filter("percentReturn =", 0.0) .fetch(1000) ) algGroup = [alg.name() for alg in liveAlgs] taskname = "liveAlg-" + JobID + "-" + str(stopStep) + "-" + str(stopStep - stepRange) try: deferred.defer( processLiveAlgStepRange, stopStep - stepRange, stopStep, stepRange, algGroup, namespace, JobID, callback, totalBatches, taskname, _name=taskname, ) except (taskqueue.TaskAlreadyExistsError, taskqueue.TombstonedTaskError), e: pass
def getAlgQueryStr(alphaAlg='0',omegaAlg='999999'): alpha = meTools.buildAlgKey(alphaAlg) omega = meTools.buildAlgKey(omegaAlg) model = 'algStats' query = "Select * from %s Where __key__ > Key('%s','%s') AND __key__ < Key('%s','%s')" % (model,model,alpha,model,omega) return query