def bbstrat(quotes, 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 timesclose = quotes[:, [0, 4]] timesclose = np.array(timesclose) bbands = ind.bbtimes(timesclose, 20) quotes = np.array(quotes[-len(bbands):]) dBuy = {} dSell = {} previous = 0 price = 0 position = False for quote, band in zip(quotes, bbands): last_quote = quote[1] if position == True: if quote[1] > 1.20 * price or band[6] > 0.7 or quote[ 1] < 0.93 * price or band[6] < -0.03: position = False dSell[quote[0]] = 1 else: if band[6] > 0.06 and previous < 0.06: position = True dBuy[quote[0]] = 1 price = quote[1] previous = band[6] return dBuy, dSell
nh = round(h - o, 2) nl = round(l - o, 2) nc = round(c - o, 2) quotes.append([day, o, h, l, c]) normquotes.append([day, no, nh, nl, nc]) # fractions.append( abs(o-c)/(h-l) ) # what fraction of (high-low) is |open-close|? fractions.append(((o + c) / 2 - l) / (h - l)) # how far from the low is the center of the body? timesclose.append([day, c]) # makeHist(prices[:,0], "openhist.png") u.makeHist(fractions, "../plots/fractions.png", title="|o-c|/(h-l)", nbins=50) timesclose = np.array(timesclose) bbands = ind.bbtimes(timesclose, 14) u.makePlot(bbands[:, 0], bbands[:, 1], "../plots/bbands_upper.png", title="Bollinger Bands") u.makePlot(bbands[:, 0], bbands[:, 3], "../plots/bbands_lower.png", title="Bollinger Bands") normquotes = np.array(normquotes) quotes = np.array(quotes) km = skc.KMeans(n_clusters=5) clusters = km.fit_predict(normquotes[:, [1, 2, 3, 4]]) shading = [] colors = list("krbgcmyw") # only allows up to 8 colors :( for i, day in enumerate(quotes[:, 0]): shading.append([day, colors[clusters[i]]])
nc = round(c - o, 2) quotes.append([day, o, h, l, c]) normquotes.append([day, no, nh, nl, nc]) # fractions.append( abs(o-c)/(h-l) ) # what fraction of (high-low) is |open-close|? fractions.append( ((o + c) / 2 - l) / (h - l)) # how far from the low is the center of the body? timesclose.append([day, c]) # makeHist(prices[:,0], "openhist.png") u.makeHist(fractions, "../plots/fractions.png", title="|o-c|/(h-l)", nbins=50) timesclose = np.array(timesclose) bbands = ind.bbtimes(timesclose, 14) u.makePlot(bbands[:, 0], bbands[:, 1], "../plots/bbands_upper.png", title="Bollinger Bands") u.makePlot(bbands[:, 0], bbands[:, 3], "../plots/bbands_lower.png", title="Bollinger Bands") normquotes = np.array(normquotes) quotes = np.array(quotes) km = skc.KMeans(n_clusters=5) clusters = km.fit_predict(normquotes[:, [1, 2, 3, 4]])