예제 #1
0
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
예제 #2
0
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 doNext(JobID, stepType, model):
    '''
      JobID passed between all steps.  Used to get globalStop, initialStop.
      stepType in ['weeklyDesires', 'weeklyBackTests', 'calculateRvals', 'weeklyLiveAlgs']
      model in ['', 'backTestResult', 'liveAlg']
    '''
    callback = '/simulate/processCallback'
    unique = JobID.split('-')[1]
    stops = meTools.memGet(meSchema.WorkQueue, JobID)
    globalStop = stops.globalStop
    initialStop = stops.initialStop
    if None in [globalStop, initialStop]:
        raise(BaseException('globalStop or initialStop is None!'))

    if stepType == 'weeklyDesires':
        import doBackTests
        # batchSize = 639 takes 4 minutes.
        doBackTests.addTaskRange(initialStop, globalStop, unique, '', batchSize = 284, callback = callback )
    elif stepType == 'weeklyBackTests':
        import calculateCompoundReturns
        calculateCompoundReturns.fanoutTaskAdd(initialStop, initialStop - 1600, globalStop, '', unique, 'backTestResult', callback = callback)
    elif stepType == 'calculateRvals' and model == 'backTestResult':
        import liveAlg
        liveAlg.doAllLiveAlgs(initialStop, 1600, globalStop, '', unique, callback = callback)
    elif stepType == 'weeklyLiveAlgs':
        import calculateCompoundReturns
        calculateCompoundReturns.fanoutTaskAdd(initialStop, initialStop - 1600, globalStop, '', unique, 'liveAlg', callback = callback)
    elif stepType == 'calculateRvals' and model == 'liveAlg':
        print 'Done? or goto metaAlgs?'
    else:
        raise(BaseException('Received unknown stepType, model: %s, %s' % (stepType, model)))
예제 #4
0
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
예제 #5
0
def getOppositeAlg(meAlgKey):
    originalNameSpace = namespace_manager.get_namespace()
    namespace_manager.set_namespace("")
    meAlg = meTools.memGet(meSchema.meAlg, meAlgKey)
    query = meSchema.meAlg.all(keys_only=True).filter("BuyCue =", meAlg.SellCue).filter("SellCue =", meAlg.BuyCue)
    oppositeAlg = query.get()
    oppositeAlgKey = oppositeAlg.name()
    namespace_manager.set_namespace(originalNameSpace)
    return oppositeAlgKey
예제 #6
0
def updateAlgStat(algKey, startStep, stopStep, namespace, memprefix = "unpacked_"):
    if namespace == '':
        stckIDs = [1,2,3,4]
        accumulate = False
    else:
        stckIDs = [meTools.getStckID(namespace)]
        accumulate = True

    alginfo = meTools.memGet(meSchema.meAlg, algKey)
    stats = resetAlgstats(memprefix, alginfo.Cash, int(algKey), int(algKey))[memprefix+algKey]
    
    stats = doAlgSteps(algKey, int(startStep), int(stopStep), stats, stckIDs)
    
    bTestReturns = getBackTestReturns([memprefix + algKey],stopStep, {memprefix + algKey: stats}, namespace)
    return bTestReturns
예제 #7
0
def closeoutPositions(step):
    algstats = getAlgStats()
    alglist = {}
    desires = {}
    prices = {}
    for stckID in [1,2,3,4]:
        symbol = meTools.getStckSymbol(stckID)
        pricekey = str(stckID)+"_"+str(step)
        price = meTools.memGet(meSchema.stck,pricekey,priority=0).quote
        prices[symbol] = price
    for alg in algstats:
        desires[alg] = eval(algstats[alg].Positions)
        for stck in desires[alg]:
            desires[alg][stck]['Shares'] *= -1
            desires[alg][stck]['Price']   = prices[stck]
            desires[alg][stck]['Value']   = prices[stck]*(desires[alg][stck]['Shares'])
        cash,positions = mergePosition(desires[alg],eval(algstats[alg].Positions))
        cash += algstats[alg].Cash
        algstats[alg].Cash = cash
        algstats[alg].Positions = repr(positions)
        alglist[alg] = algstats[alg]
    return alglist
예제 #8
0
def doDesire(step, cuekey):
    # see if tradeCue for key results in a new desire.
    desires = []
    tradecue = meTools.memGet(meSchema.tradeCue, cuekey)
    for stckID in [1,2,3,4]:
        deltakey = str(stckID) + '_' + str(step)
        cval = cvalDict[deltakey]
        if cval is None or len(cval) < tradecue.TimeDelta + 1:
            return desires
        cue = cval[tradecue.TimeDelta]
        qDelta = tradecue.QuoteDelta
        if (qDelta > 0 and cmp(cue,qDelta) == 1) or (qDelta < 0 and cmp(cue,qDelta) == -1):
            recent = recency(tradecue,step,stckID)
            if not recent:
                action = makeDesire(stckID, cuekey, step)
                recency_key = 'desire_' + tradecue.key().name() + '_' + str(stckID)
                # Maybe combine this into function?
                memcache.set(recency_key, step)
                cachepy.set(recency_key, step, priority=1)
                
                desires.append(action)
    return desires
예제 #9
0
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