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
def crossover(quotes): timesclose = quotes[:,[0,4]] emas = [ ind.ematimes(timesclose,i) for i in [5, 10] ] crossovertimes = ind.crossovertimes(emas) dCrossovers = {} # turn into dict for fast lookup for time,rising in crossovertimes: dCrossovers[time] = rising dBuy = { } hasBought = False boughtPrice = -1.0 for i,day in enumerate(quotes[:,0]): #print day if(day in dCrossovers): rising = dCrossovers[day] if(rising): dBuy[day] = 1 hasBought = True boughtPrice = quotes[i][4] return dBuy
import numpy as np import getStocks as gs import utils as u import indicators as ind import tradeReport as tr #symbol = "WMT" symbol = "NFLX" # symbol = "F" stock = gs.getStock(symbol, (2011, 6, 1), (2015, 6, 1)) # for calculating quotes = u.dictToList(stock) # [day,o,h,l,c] timesclose = quotes[:,[0,4]] emas = [] for i in [1,2]: emas.append( ind.ematimes(timesclose,10*i) ) crossovertimes = ind.crossovertimes(emas) dCrossovers = {} # turn into dict for fast lookup for time,rising in crossovertimes: dCrossovers[time] = rising # CROSSOVER STRATEGY price = 0 ledger = tr.Ledger(1000) for quote in quotes: day,price,h,l,c = quote if(day in dCrossovers): rising = dCrossovers[day] if(rising): ledger.buyStock(symbol, price)
stock = gs.getStock("F", (2010, 1, 1), (2011, 10, 5))["days"] # for calculating d1,d2 = (2010,8,1),(2011,3,5) # for plotting quotes = [] timesclose = [] for day in sorted(stock.keys()): vals = stock[day] o, h, l, c = vals["o"], vals["h"], vals["l"], vals["c"] quotes.append( [day,o,h,l,c] ) timesclose.append([day,c]) timesclose = np.array(timesclose) bbands = ind.bbtimes(timesclose, 20) emas = [ ind.ematimes(timesclose,i) for i in [5,10] ] quotes = np.array(quotes) rsis = ind.rsitimes(quotes[:,[0,1]],14) # bbands discards early dates since it can't do a moving average for those # we reshape the other arrays to match up with bbands' size quotes = quotes[-len(bbands):] nclusters=5 ncandles=10 # remember to change to "saveTo" below when you change these values times, clusters, clusterCenters = cl.clusterCandles(quotes, nclusters=nclusters, ncandles=ncandles, saveTo="test.txt") clusterQuotes, vlines = cl.clusterCentersForPlotting(clusterCenters) clustersAndProfits = cl.clustersAndTheirNextProfits(clusters,clusterCenters)