def getStepRangeAlgDesires(algKey, alginfo, startStep, stopStep): buyList = [] sellList = [] desireDict = {} buyCue = alginfo.BuyCue sellCue = alginfo.SellCue buyStartKey = meTools.buildDesireKey(startStep, buyCue, 0) buyStopKey = meTools.buildDesireKey(stopStep, buyCue, 99) buyQuery = "Select * From desire Where CueKey = '%s' " % (buyCue) buyQuery += " AND __key__ >= Key('desire','%s') AND __key__ <= Key('desire','%s')" % (buyStartKey, buyStopKey) sellStartKey = meTools.buildDesireKey(startStep, sellCue, 0) sellStopKey = meTools.buildDesireKey(stopStep, sellCue, 99) sellQuery = "Select * From desire Where CueKey = '%s' " % (sellCue) sellQuery += " AND __key__ >= Key('desire','%s') AND __key__ <= Key('desire','%s')" % (sellStartKey, sellStopKey) buyList = meTools.cachepy.get(buyQuery) if buyList is None: buyList = memcache.get(buyQuery) if buyList is None: buyList = db.GqlQuery(buyQuery).fetch(4000) memcache.set(buyQuery, buyList) meTools.cachepy.set(buyQuery, buyList) sellList = meTools.cachepy.get(sellQuery) if sellList is None: sellList = memcache.get(sellQuery) if sellList is None: sellList = db.GqlQuery(sellQuery).fetch(4000) memcache.set(sellQuery, sellList) meTools.cachepy.set(sellQuery, sellList) if len(buyList) > len(sellList): # If there are more buys than sells, fill dict with buys first # Then overwrite with sells. Else, do reverse. # If there is a buy and a sell for a given stock on a certain step, # the less frequent action will be given precedence. for buy in buyList: keyname = buy.key().name() keyname = keyname.replace("_" + buyCue + "_", "_" + algKey + "_") desireDict[keyname] = processDesires.convertDesireToDict(buy, 1, alginfo.TradeSize, alginfo.Cash) for sell in sellList: keyname = sell.key().name() keyname = keyname.replace("_" + sellCue + "_", "_" + algKey + "_") desireDict[keyname] = processDesires.convertDesireToDict(sell, -1, alginfo.TradeSize, alginfo.Cash) else: for sell in sellList: keyname = sell.key().name() keyname = keyname.replace("_" + sellCue + "_", "_" + algKey + "_") desireDict[keyname] = processDesires.convertDesireToDict(sell, -1, alginfo.TradeSize, alginfo.Cash) for buy in buyList: keyname = buy.key().name() keyname = keyname.replace("_" + buyCue + "_", "_" + algKey + "_") desireDict[keyname] = processDesires.convertDesireToDict(buy, 1, alginfo.TradeSize, alginfo.Cash) return desireDict
def updateAlgStats(step,alphaAlg=1,omegaAlg=3540): algstats = getAlgStats(alphaAlg,omegaAlg) desires = getDesires(step,alphaAlg,omegaAlg) alglist = {} for alg in algstats: desireKey = meTools.buildDesireKey(step,alg) if desires[desireKey] is not None: tradeCash, PandL, position = mergePosition(eval(desires[desireKey].desire),eval(algstats[alg].Positions)) cash = tradeCash + algstats[alg].Cash if cash > 0: cashdelta = eval(decompress(algstats[alg].CashDelta)) # Get CashDelta collection cashdelta.appendleft({'value': tradeCash, 'PandL': PandL, 'step': step}) # Must check len() before pop() since not padding cashdelta. if len(cashdelta) > 800: cashdelta.pop() algstats[alg].Cash = cash algstats[alg].Positions = repr(position) algstats[alg].PandL = PandL algstats[alg].CashDelta = compress(repr(cashdelta),9) alglist[alg] = algstats[alg] else: pass meTools.memPut_multi(meSchema.algStats,alglist)
def makeDesire(stckID,keyname,step): symbol = meTools.getStckSymbol(stckID) pricekey = str(stckID) + "_" + str(step) price = meTools.memGet(meSchema.stck,pricekey,priority=0).quote key_name = meTools.buildDesireKey(step,keyname,stckID) meDesire = meSchema.desire(key_name = key_name, CueKey = keyname, Symbol = symbol, Quote = price) return meDesire
def getDesires(step,alphaAlg=1,omegaAlg=3540): keylist = [] model = meSchema.desire for i in range(alphaAlg,omegaAlg+1): key_name = meTools.buildDesireKey(step, str(i)) keylist.append(key_name) desires = meTools.memGet_multi(model,keylist) return desires
def doAlgSteps(algKey, startStep, stopStep, stats, stckIDs, MaxTrade = False): if len(stckIDs) == 1: accumulate = True else: accumulate = False alginfo = meTools.memGet(meSchema.meAlg, algKey) if MaxTrade: alginfo.Cash = alginfo.Cash + stats['PandL'] alginfo.TradeSize = (0.94/len(stckIDs)) desires = liveAlg.getStepRangeAlgDesires(algKey, alginfo, startStep, stopStep) buydelta = meTools.memGet(meSchema.tradeCue, alginfo.BuyCue).TimeDelta selldelta = meTools.memGet(meSchema.tradeCue, alginfo.SellCue).TimeDelta orderDesires = desires.keys() orderDesires.sort() for step in range(startStep, stopStep + 1): stopRange = 80 if (step - startStep - 44)%stopRange == 0: stats = doStops(step, copy.deepcopy(stats), alginfo, stopRange) potentialDesires = [meTools.buildDesireKey(step, algKey, stckID) for stckID in stckIDs] # must feed stckIDs into func? for key in potentialDesires: if key in orderDesires: currentDesire = eval(desires[key]) for des in currentDesire: # Eventually re-do desires[key] to be just dict with 'Symbol' 'Shares' keys. buysell = cmp(currentDesire[des]['Shares'], 0) Symbol = des tradeCash, PandL, position = princeFunc.mergePosition(eval(desires[key]), copy.deepcopy(stats['Positions']), step, accumulate) cash = tradeCash + copy.deepcopy(stats['Cash']) if buysell == -1: timedelta = selldelta lastTradeStep = stats['lastSell'] elif buysell == 1: timedelta = buydelta lastTradeStep = stats['lastBuy'] if cash > 0 and lastTradeStep <= step - timedelta: if buysell == -1: stats['lastSell'] = step elif buysell == 1: stats['lastBuy'] = step stats['CashDelta'].appendleft({'Symbol' : Symbol, 'buysell' : buysell, 'value' : tradeCash, 'PandL' : PandL, 'step' : step}) if len(stats['CashDelta']) > 800: stats['CashDelta'].pop() stats['Cash'] = cash stats['PandL'] += PandL stats['Positions'] = position return stats
def getDesireQueryStr(startStep,stopStep): alpha = meTools.buildDesireKey(startStep,0,0) omega = meTools.buildDesireKey(stopStep,61,5) # Technically, this value should now be 60 since there are only 60 tradeCues. model = 'desire' query = "Select * from %s Where __key__ > Key('%s','%s') AND __key__ < Key('%s','%s')" % (model,model,alpha,model,omega) return query