示例#1
0
def saveTrades(testingdata, classifiersTest, cutoffTest, printToScreen=False):
    goodTrades = testingdata[classifiersTest > cutoffTest][:,[1,2,3,4,5]] # FIXME FIXME FIXME
    # goodTrades = testingdata[testingdata[:,0] > 0.5][classifiersTest[testingdata[:,0] > 0.5] > cutoffTest][:,[1,2,3,4,5]]
    goodClassifiers = classifiersTest[classifiersTest > cutoffTest] # FIXME FIXME FIXME
    # goodClassifiers = classifiersTest[testingdata[:,0] > 0.5][classifiersTest[testingdata[:,0] > 0.5] > cutoffTest]
    for itrade, trade in enumerate(goodTrades):
        day, gain1, gain2, iticker, close0 = trade
        day, iticker, close0 = int(day), int(iticker), float(close0)
        if printToScreen:
            print dTickers[iticker], u.inum2tuple(day), close0, gain1, gain2, gain1+gain2
        else:
            dt = "%04i-%02i-%02i" % u.inum2tuple(day)
            fhTrades.write("%s %s %.3f %.2f\n" % (dTickers[iticker], dt, goodClassifiers[itrade], close0))
示例#2
0
def saveTrades(testingdata, classifiersTest, cutoffTest, printToScreen=False):
    goodTrades = testingdata[classifiersTest > cutoffTest][:, [
        1, 2, 3, 4, 5
    ]]  # FIXME FIXME FIXME
    # goodTrades = testingdata[testingdata[:,0] > 0.5][classifiersTest[testingdata[:,0] > 0.5] > cutoffTest][:,[1,2,3,4,5]]
    goodClassifiers = classifiersTest[classifiersTest >
                                      cutoffTest]  # FIXME FIXME FIXME
    # goodClassifiers = classifiersTest[testingdata[:,0] > 0.5][classifiersTest[testingdata[:,0] > 0.5] > cutoffTest]
    for itrade, trade in enumerate(goodTrades):
        day, gain1, gain2, iticker, close0 = trade
        day, iticker, close0 = int(day), int(iticker), float(close0)
        if printToScreen:
            print dTickers[iticker], u.inum2tuple(
                day), close0, gain1, gain2, gain1 + gain2
        else:
            dt = "%04i-%02i-%02i" % u.inum2tuple(day)
            fhTrades.write(
                "%s %s %.3f %.2f\n" %
                (dTickers[iticker], dt, goodClassifiers[itrade], close0))
示例#3
0
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
示例#4
0
    def printLedger(self,showTrades=True):
        self.cleanup()
        print "-"*25
        print "Money remaining: %.2f" % self.money
        print "-"*25
        if(showTrades):
            for t in self.trades:
                date = ", ".join(map(str,list(u.inum2tuple(t[3]))))
                print "[%s] %-4s %i stocks of %s @ $%.2f" % (date,"BUY" if t[2]>0 else "SELL", abs(t[2]), t[0], t[1])
            print "-"*25

        for ass in self.assets.keys():
            print "Have %i stocks of %s" % (self.assets[ass], ass)
        print
示例#5
0
    def printLedger(self, showTrades=True):
        self.cleanup()
        print "-" * 25
        print "Money remaining: %.2f" % self.money
        print "-" * 25
        if (showTrades):
            for t in self.trades:
                date = ", ".join(map(str, list(u.inum2tuple(t[3]))))
                print "[%s] %-4s %i stocks of %s @ $%.2f" % (
                    date, "BUY" if t[2] > 0 else "SELL", abs(t[2]), t[0], t[1])
            print "-" * 25

        for ass in self.assets.keys():
            print "Have %i stocks of %s" % (self.assets[ass], ass)
        print
示例#6
0
    elif algorithm == "RandomForest": alg = RandomForestClassifier()
    elif algorithm == "Bagging": alg = BaggingClassifier()
    elif algorithm == "XGB": alg = XGBoostClassifier(num_boost_round=3000,num_class=2,eta=0.01,max_depth=9,sub_sample=0.9)
    # elif algorithm == "XGB": alg = XGBoostClassifier(num_boost_round=30,num_class=2,eta=0.01,max_depth=9,sub_sample=0.9)

    alg.fit(Xtrain, Ytrain)

    # try: printImportances(alg.feature_importances_, whichIndicators)
    # except: pass
    # try: printImportances(alg.coef_, whichIndicators)
    # except: pass


    minday = int(np.min(testingdata[:,[1]]))
    maxday = int(np.max(testingdata[:,[1]]))
    mind, maxd = u.inum2tuple(minday), u.inum2tuple(maxday)
    fhTrades.write("# %s %i to %i (%i-%i-%i to %i-%i-%i) \n" % (algorithm, minday, maxday, mind[0], mind[1], mind[2], maxd[0], maxd[1], maxd[2]) )

    plotTrainTest(Xtrain, Ytrain, Xtest, Ytest, trainingdata, testingdata, \
        # title = "%s LR: %.3f N-estimators: %i" % (algorithm, rate, nest), \
        title = "%s" % (algorithm), \
        # filename = basedir+"TrainTest_%s_%s_%s.png" % (algorithm, ("%.4f" % rate).replace(".",""), ("%i" % nest).replace(".","")) \
        filename = basedir+"TrainTest_%s.png" % (algorithm) \
    )

    Yscore = alg.predict(Xtest)
    # needed for when we test inclusively -1, 0, 1
    # want to just make the -1 into background (0)
    Ytest[Ytest < -0.5] = 0
    fpr, tpr, _ = roc_curve(Ytest,Yscore) 
    forROCs[algorithm] = [fpr,tpr]
示例#7
0
                                num_class=2,
                                eta=0.01,
                                max_depth=9,
                                sub_sample=0.9)
    # elif algorithm == "XGB": alg = XGBoostClassifier(num_boost_round=30,num_class=2,eta=0.01,max_depth=9,sub_sample=0.9)

    alg.fit(Xtrain, Ytrain)

    # try: printImportances(alg.feature_importances_, whichIndicators)
    # except: pass
    # try: printImportances(alg.coef_, whichIndicators)
    # except: pass

    minday = int(np.min(testingdata[:, [1]]))
    maxday = int(np.max(testingdata[:, [1]]))
    mind, maxd = u.inum2tuple(minday), u.inum2tuple(maxday)
    fhTrades.write("# %s %i to %i (%i-%i-%i to %i-%i-%i) \n" %
                   (algorithm, minday, maxday, mind[0], mind[1], mind[2],
                    maxd[0], maxd[1], maxd[2]))

    plotTrainTest(Xtrain, Ytrain, Xtest, Ytest, trainingdata, testingdata, \
        # title = "%s LR: %.3f N-estimators: %i" % (algorithm, rate, nest), \
        title = "%s" % (algorithm), \
        # filename = basedir+"TrainTest_%s_%s_%s.png" % (algorithm, ("%.4f" % rate).replace(".",""), ("%i" % nest).replace(".","")) \
        filename = basedir+"TrainTest_%s.png" % (algorithm) \
    )

    Yscore = alg.predict(Xtest)
    # needed for when we test inclusively -1, 0, 1
    # want to just make the -1 into background (0)
    Ytest[Ytest < -0.5] = 0