def post(self):
     startDayIndex = int(self.request.get('startDayIndex'))
     Symbol = str(self.request.get('Symbol'))
     stckID = meSchema.getStckID(Symbol)
     csvFile = Symbol + '_01-2005_11-2010.csv'
     dailyQuotes = list(csv.reader(open(csvFile)))
     dailyQuotes.reverse()
     # Index 0 is for step 1000001 to 1000080
     # Index 1 is for step 1000081 to 1000160
     # Index N is for step N*80 + 1 to (N+1)*80  # plus 1000000
     putList = []
     for N in range(len(dailyQuotes)):
         dailyOpenStep   = 1000000 + N*80 + 1
         dailyCloseStep  = 1000000 + N*80 + 80
         '''Must draw random steps for High and Low.'''
         daySteps = [step for step in range(dailyOpenStep+1,dailyCloseStep)]
         highLow = sample(daySteps, 2)
         dailyHighStep = highLow[0]
         dailyLowStep = highLow[1]
         ''' Set Quote Values '''
         OpenQuote  = dailyQuotes[N][1]
         HighQuote  = dailyQuotes[N][2]
         LowQuote   = dailyQuotes[N][3]
         CloseQuote = dailyQuotes[N][4]
         ''' Create stck Entities for stckID Quotes'''
         openStck = meSchema.stck(key_name=str(stckID) + '_' + str(dailyOpenStep),
                                  ID = stckID, quote = float(OpenQuote), step = dailyOpenStep)
         highStck = meSchema.stck(key_name=str(stckID) + '_' + str(dailyHighStep),
                                  ID = stckID, quote = float(HighQuote), step = dailyHighStep)
         lowStck  = meSchema.stck(key_name=str(stckID) + '_' + str(dailyLowStep),
                                  ID = stckID, quote = float(LowQuote), step = dailyLowStep)
         closeStck = meSchema.stck(key_name=str(stckID) + '_' + str(dailyCloseStep),
                                  ID = stckID, quote = float(CloseQuote), step = dailyCloseStep)
         putList.extend([openStck,highStck,lowStck,closeStck])
     meSchema.batchPut(putList)
def convertStockDay(stock,startStep,start,end):
    stckID = meSchema.getStckID(stock)
    result = meSchema.getStck(stckID,startStep)
    if len(result) == 78:
        return True  # If already 78.. then most likely already converted.
    elif len(result) > 0:
        db.delete(result)

    step = startStep
    stockRange = meSchema.getStockRange(stock,start,end)
    k = len(stockRange)
    if k < 78:
        return False
    m = k - 78
    skip = 0
    meList = []

    for i in range(k):
        if i%5 in [0,3] and skip < m:
            skip += 1
        else:
            meStck = meSchema.stck(ID    = stckID,
                                   step  = step,
                                   quote = stockRange[i].lastPrice)
            meList.append(meStck)
            if len(meList) == 78:
                db.put(meList)
                return True
            step += 1
            
    return False
def putEm(step):
    positions = getPositions("*****@*****.**")
    meDatetime = datetime.now()
    entityDict = {}
    symbols = ["GOOG", "HBC", "INTC", "CME"]
    stckIDs = meTools.getStckIDs(symbols)
    for pos in positions:
        symbol = pos.ticker_id.split(":")[1]
        quote = float(str(pos.position_data.market_value).replace(" USD", ""))
        if symbol in symbols:
            stckID = stckIDs[symbol]
            meStck = meSchema.stck(key_name=str(stckID) + "_" + str(step), ID=stckID, step=step, quote=quote)
            entityDict[meStck.key().name()] = meStck

    meStepDate = meSchema.stepDate(key_name=str(step), step=step, date=meDatetime)
    entityDict[meStepDate.key().name()] = meStepDate

    wait = 0.1
    while True:
        try:
            meTools.memPut_multi(entityDict)
            break
        except db.Timeout:
            from time import sleep

            sleep(timeout)
            wait *= 2
def buildEntityList(stckDict, stepSeq):
    stckEntities = []
    for stckID in stckDict:
        for i in range(1,len(stepSeq[stckID])):
            # do random walk from stepSeq[stckID][i-1] to stepSeq[stckID][i]
            # use high,low data from [stckDict[stckID][stepSeq[stckID][1]], stckDict[stckID][stepSeq[stckID][2]]].sort()
            ''' For the sake of speed, just doing straight line. '''
            startStep = stepSeq[stckID][i-1]
            stopStep = stepSeq[stckID][i]
            startQuote = stckDict[stckID][startStep]
            stopQuote = stckDict[stckID][stopStep]
            stepQuotes = walkStraightLine(startQuote,stopQuote,startStep,stopStep)
            for step in stepQuotes:
                stckEntities.append(meSchema.stck(key_name = str(stckID) + '_' + str(step),
                                                  ID = stckID, quote = float(stepQuotes[step]),
                                                  step = step))
    return stckEntities
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        startStep = int(self.request.get('start'))
        stopStep = int(self.request.get('stop'))

        stocks = db.GqlQuery("Select * from stck Where step >= :1 AND step <= :2 Order By step",startStep,stopStep).fetch(1000)
        total = 0
        meList = []
        delList = []
        counter = 0

        for meStck in stocks:
            k_name = meStck.key().name()
            if k_name is None or k_name == "s" + str(meStck.ID) + "_" + str(meStck.step):
                newStock = meSchema.stck(key_name = str(meStck.ID) + "_" + str(meStck.step),
                                         ID = meStck.ID,
                                         ask = meStck.ask,
                                         bid = meStck.bid,
                                         quote = meStck.quote,
                                         step = meStck.step)
                meList.append(newStock)
                delList.append(meStck)
                counter += 1

                if counter == 100:
                    db.put(meList)
                    db.delete(delList)
                    meList = []
                    delList = []
                    total += counter
                    counter = 0
        if counter > 0:
            db.put(meList)
            db.delete(delList)
            total += counter
        self.response.out.write("Done putting %s new key_names for stck!!" % total)