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 doStops(step, statDict, alginfo, stopRange, scaleFactor = 0.0): from random import random stopDesires = [] stckKeys = [str(stckID) + '_' + str(step) for stckID in [1,2,3,4]] stocks = memGetStcks(stckKeys) stckQuotes = {} for stock in stocks: if stock is None: return statDict else: stckQuotes[meTools.getStckSymbol(stock.ID)] = stock.quote for pos in statDict['Positions']: stckID = meTools.getStckID(pos) stckDeltas = calculateDeltas(stckID,step) shares = statDict['Positions'][pos]['Shares'] longshort = cmp(shares,0) # -1 for short, +1 for long stckQuote = stckQuotes[pos] offsetDesire = meSchema.desire(Symbol = pos, Quote = stckQuote, CueKey = '0000') dictDesire = convertDesireToDict(offsetDesire, -1*longshort, alginfo.TradeSize, alginfo.Cash, -1*shares) # Now only using maxmin deviations for stops. maxDevStop, minDevStop = getMaxMinDevMeans(stckDeltas) # Using scaleFactor for metaAlgs. Moves stop 40% closer to 1.0 stopLoss = statDict['Positions'][pos]['StopLoss'] stopProfit = statDict['Positions'][pos]['StopProfit'] if longshort == 1: if stckQuote < stopLoss or stckQuote > stopProfit: stopDesires.append(dictDesire) else: stopLoss = max(statDict['Positions'][pos]['StopLoss'], stckQuote*minDevStop) if stopProfit > 1.15*statDict['Positions'][pos]['Price']: stopPrice = stckQuote*(maxDevStop - ((maxDevStop-1)*scaleFactor)) stopProfit = min(statDict['Positions'][pos]['StopProfit'], stopPrice) elif longshort == -1: if stckQuote > stopLoss or stckQuote < stopProfit: stopDesires.append(dictDesire) else: stopLoss = min(statDict['Positions'][pos]['StopLoss'], stckQuote*maxDevStop) if stopProfit < 0.85*statDict['Positions'][pos]['Price']: stopPrice = stckQuote*(minDevStop + ((1-minDevStop)*scaleFactor)) stopProfit = max(statDict['Positions'][pos]['StopProfit'], stopPrice) statDict['Positions'][pos]['StopLoss'] = stopLoss statDict['Positions'][pos]['StopProfit'] = stopProfit for stop in stopDesires: tradeCash, PandL, position = princeFunc.mergePosition(eval(stop), copy.deepcopy(statDict['Positions']), step, True) cash = tradeCash + copy.deepcopy(statDict['Cash']) Symbol = eval(stop).keys()[0] buysell = cmp(eval(stop)[Symbol]['Shares'], 0) statDict['CashDelta'].appendleft({'Symbol' : Symbol, 'buysell' : str(buysell) + '_stop', 'value' : tradeCash, 'PandL' : PandL, 'step' : step}) if len(statDict['CashDelta']) > 800: statDict['CashDelta'].pop() statDict['Cash'] = cash statDict['PandL'] += PandL statDict['Positions'] = position return statDict