Created
December 13, 2016 00:53
-
-
Save TimSC/63ec8b809cd98fc44dcd55e09f3b1b70 to your computer and use it in GitHub Desktop.
Dead end approach to stock predition on hackerrank.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Dead end approach to https://www.hackerrank.com/challenges/stockprediction | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import scipy.stats as stats | |
| fi = open("sample.txt", "rt") | |
| lis = fi.read().split("\n") | |
| stocks = {} | |
| for li in lis: | |
| li = li.strip() | |
| if len(li) == 0: continue | |
| if li[0] == "*": continue | |
| vals = li.split(" ") | |
| stockName = vals.pop(0) | |
| stockPrices = map(float, vals) | |
| #av = sum(stockPrices) / len(stockPrices) | |
| stocks[stockName] = np.array(stockPrices) | |
| fi = open("models.txt", "wt") | |
| print stocks.keys() | |
| for sn in stocks: | |
| st = stocks[sn] | |
| fivedayhistory = st[5:] + 0.5 * st[4:-1] + 0.25 * st[3:-2] - 0.25 * st[2:-3] - 0.5 * st[1:-4] - st[:-5] | |
| fourdayhistory = st[5:] + 0.5 * st[4:-1] - 0.5 * st[3:-2] - st[2:-3] | |
| threedayhistory = st[5:] - st[3:-2] | |
| twodayhistory = st[5:] - st[4:-1] | |
| histories = [fivedayhistory, fourdayhistory, threedayhistory, twodayhistory] | |
| histNames = ["5h", "4h", "3h", "2h"] | |
| twodayfuture = st[6:] - st[5:-1] | |
| threedayfuture = st[7:] - st[5:-2] | |
| fourdayfuture = st[7:] + 0.5 * st[6:-1] - 0.5 * st[5:-2] - st[4:-3] | |
| futures = [twodayfuture, threedayfuture, fourdayfuture] | |
| futNames = ["2f", "3f", "4f"] | |
| bestModel = None | |
| bestPVal = 0.0 | |
| for hi, hin in zip(histories, histNames): | |
| for fu, fun in zip(futures, futNames): | |
| hi2 = hi[:len(fu)] | |
| reg = stats.linregress(hi2, fu) | |
| #print sn, hin, fun, reg | |
| if abs(reg.rvalue) > bestPVal: | |
| bestPVal = abs(reg.rvalue) | |
| bestModel = (hin, fun, reg) | |
| fi.write("{},{},{},{},{},{}\n".format(sn, bestModel[2].slope, bestModel[2].rvalue, bestModel[2].intercept, bestModel[0], bestModel[1])) | |
| #plt.plot(fivedayhistory[:-3], fourdayfuture) | |
| #plt.show() | |
| ################################################################# | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import scipy.stats as stats | |
| import math | |
| fi = open("sample.txt", "rt") | |
| lis = fi.read().split("\n") | |
| stocks = {} | |
| for li in lis: | |
| li = li.strip() | |
| if len(li) == 0: continue | |
| if li[0] == "*": continue | |
| vals = li.split(" ") | |
| stockName = vals.pop(0) | |
| stockPrices = map(float, vals) | |
| #av = sum(stockPrices) / len(stockPrices) | |
| stocks[stockName] = np.array(stockPrices) | |
| fi = open("models.txt", "rt") | |
| models = {} | |
| for li in fi.read().split("\n"): | |
| vals = li.strip().split(",") | |
| if len(vals) < 5: continue | |
| models[vals[0]] = vals[1:] | |
| money = 100 | |
| holding = {} | |
| days = len(stocks[list(stocks.keys())[0]]) | |
| for d in range(5,days): | |
| predUp = [] | |
| for sn in stocks: | |
| st = stocks[sn] | |
| mo = models[sn] | |
| history = {} | |
| history["5h"] = st[d] + 0.5 * st[d-1] + 0.25 * st[d-2] - 0.25 * st[d-3] - 0.5 * st[d-4] - st[d-5] | |
| history["4h"] = st[d] + 0.5 * st[d-1] - 0.5 * st[d-2] - st[d-3] | |
| history["3h"] = st[d] - st[d-2] | |
| history["2h"] = st[d] - st[d-1] | |
| hist = history[mo[3]] | |
| pred = float(mo[0]) * hist + float(mo[2]) | |
| predUp.append((pred, sn)) | |
| #print d, sn, hist, pred | |
| #Buying | |
| predUp2 = [] | |
| for row in predUp: | |
| if stocks[row[1]][d] < money and row[0] > 0.0: | |
| predUp2.append(row) | |
| if len(predUp2) > 0: | |
| predUp2.sort() | |
| bestStock = predUp2[-1][1] | |
| toBuy = int(math.floor(money / stocks[bestStock][d])) | |
| assert toBuy > 0 | |
| if bestStock not in holding: | |
| holding[bestStock] = 0 | |
| print "BUY", toBuy, bestStock, "at", stocks[bestStock][d] | |
| holding[bestStock] += toBuy | |
| money -= stocks[bestStock][d] * toBuy | |
| #Selling | |
| predDown = [] | |
| for row in predUp: | |
| if row[0] < 0.0: | |
| predDown.append(row) | |
| predDown.sort() | |
| for row in predDown: | |
| if row[1] in holding and holding[row[1]] > 0: | |
| toSell = holding[row[1]] | |
| assert toSell > 0 | |
| print "SELL", toSell, row[1], "at", stocks[row[1]][d] | |
| holding[row[1]] -= toSell | |
| money += stocks[row[1]][d] * toSell | |
| print d, money, holding | |
| for sn in holding: | |
| toSell = holding[sn] | |
| if toSell == 0: continue | |
| holding[sn] -= toSell | |
| money += stocks[sn][d] * toSell | |
| print d, money, holding | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment