def getStock(ticker, d1, d2, verbose=False): if not iterable(d1): d1 = (d1, 1, 1) if not iterable(d2): d2 = (d2, 1, 1) saveStock(ticker, d1, d2) pickleFile = "%s/%s.pklz" % (storageDir, ticker) d = { } d["days"] = { } try: fh = gzip.open(pickleFile, "rb") dOld = pickle.load(fh) fh.close() except: if(verbose): print "[MM] Error: file (%s/%s.pklz) does not exist" % (storageDir, ticker) return d dt1, dt2 = datetime.datetime(*d1), datetime.datetime(*d2) day1, day2 = u.tuple2inum(d1), u.tuple2inum(d2) for day in dOld["days"].keys(): if( day1 <= day <= day2 ): d["days"][day] = dOld["days"][day] return d
def getStock(ticker, d1, d2, verbose=False): if not iterable(d1): d1 = (d1, 1, 1) if not iterable(d2): d2 = (d2, 1, 1) saveStock(ticker, d1, d2) pickleFile = "%s/%s.pklz" % (storageDir, ticker) d = {} d["days"] = {} try: fh = gzip.open(pickleFile, "rb") dOld = pickle.load(fh) fh.close() except: if (verbose): print "[MM] Error: file (%s/%s.pklz) does not exist" % (storageDir, ticker) return d dt1, dt2 = datetime.datetime(*d1), datetime.datetime(*d2) day1, day2 = u.tuple2inum(d1), u.tuple2inum(d2) for day in dOld["days"].keys(): if (day1 <= day <= day2): d["days"][day] = dOld["days"][day] return d
def crossover(quotes, d1, d2, params={}): # define a function that takes a quotes object, so it is an array with elements [day,o,h,l,c] # and returns a dictionary with keys of buy and sell times # values are not used (1 here), so that gives us room to pack in more information with this function print d1, d2 timesclose = quotes[:,[0,4]] p1, p2 = params["p1"], params["p2"] p1, p2 = min(p1,p2), max(p1,p2) if(p2 - p1 <= 1): return { }, { } emas = [ ind.ematimes(timesclose,i) for i in [p1, p2] ] crossovertimes = ind.crossovertimes(emas) for t in crossovertimes: print u.inum2tuple(t[0]) dCrossovers = {} # turn into dict for fast lookup for time,rising in crossovertimes: dCrossovers[time] = rising dBuy = { } dSell = { } hasBought = False boughtPrice = -1.0 for i,day in enumerate(quotes[:,0]): # only trade within specified window if( day > u.tuple2inum(d2) or day < u.tuple2inum(d1) ): continue if(day in dCrossovers): rising = dCrossovers[day] if(rising): dBuy[day] = 1 hasBought = True boughtPrice = quotes[i][4] else: if(hasBought): # don't sell if we already sold dSell[day] = 1 hasBought = False # if price is x% lower than what we bought for, get the hell out and sell a certain % if( hasBought and (0.0 < quotes[i][4] / boughtPrice < 0.90) ): dSell[day] = 1 hasBought = False return dBuy, dSell
rising = dCrossovers[day] if(rising): dBuy[day] = 1 hasBought = True boughtPrice = quotes[i][4] return dBuy symbols = [line.strip() for line in open("../data/nasdaqlisted.txt").readlines()][:1000] today = datetime.datetime.today() dtuple = (today.year,today.month,today.day-3) goodsymbols = [] for symbol in symbols: stock = gs.getStock(symbol, (2015, 8, 1), dtuple) quotes = u.dictToList(stock) # [day,o,h,l,c] array_quotes = np.array(quotes) print symbol if(len(quotes) < 25): continue if np.mean(quotes[:,4]) > 20: continue if np.mean(quotes[:,4]) < 0.25: continue buys = crossover(quotes) if u.tuple2inum(dtuple) in buys: print symbol, "#"*40 goodsymbols.append(symbol) print "-"*40 print " ".join(goodsymbols) print "-"*40