Пример #1
0
    def testPolicy(self, symbol = "IBM", \
        sd=dt.datetime(2009,1,1), \
        ed=dt.datetime(2010,1,1), \
        sv = 10000):
        syms = [symbol]
        dates = pd.date_range(sd, ed)
        # Call indicators function to receive indicators with dates before starting range
        normalAverage, bollBand, MACDInd, momentum, prices = plotIndicators(
            sd - dt.timedelta(days=80), ed, syms)

        # Get indicators for the given dates
        bollBand = bollBand.loc[prices.index >= sd]
        normalAverage = normalAverage.loc[prices.index >= sd]
        MACDInd = MACDInd.loc[prices.index >= sd]
        momentum = momentum.loc[prices.index >= sd]
        prices = prices.loc[prices.index >= sd]

        # Discretize values in the dataframs

        bollBand = self.dfDiscretize(bollBand, steps=self.bins)
        normalAverage = self.dfDiscretize(normalAverage, steps=self.bins)
        MACDInd = self.dfDiscretize(MACDInd, steps=self.bins)
        momentum = self.dfDiscretize(momentum, steps=self.bins)
        firstState = (bollBand.iloc[0][symbol], normalAverage.iloc[0][symbol],
                      MACDInd.iloc[0][symbol], momentum.iloc[0][symbol])
        dims = (self.bins, self.bins, self.bins, self.bins)
        firstState = np.ravel_multi_index(firstState, dims=dims)
        self.learner = ql.QLearner(num_states=self.bins**4,
                                   num_actions=5,
                                   rar=0.98,
                                   verbose=False)

        action = self.learner.querysetstate(firstState)
        shares = 0
        orders = pd.DataFrame(columns=["Orders"], index=prices.index)
        for j in range(len(prices.index)):
            # Buy allowed
            if action == 0 and shares in [0, -500]:
                shares += 500
                orders.iloc[j]["Orders"] = 500
            elif action == 1 and shares == -500:
                shares += 1000
                orders.iloc[j]["Orders"] = 1000
            # sell allowed
            elif action == 2 and shares in [0, 500]:
                shares -= 500
                orders.iloc[j]["Orders"] = -500
            elif action == 3 and shares == 500:
                shares -= 1000
                orders.iloc[j]["Orders"] = -1000
            else:
                orders.iloc[j]["Orders"] = 0
            nextState = np.ravel_multi_index(
                (bollBand.iloc[j][symbol], normalAverage.iloc[j][symbol],
                 MACDInd.iloc[j][symbol], momentum.iloc[j][symbol]),
                dims=dims)

            action = self.learner.querysetstate(nextState)

        return orders
Пример #2
0
def main(gen_plot=False):

    of = "benchmark.csv"
    sv = 100000
    #Get Technical Values from indicators file
    normalAverage, bollBand, MACDInd,momentum,prices = indicators.plotIndicators(gen_plot=gen_plot)
    orders = ordersFile(normalAverage=normalAverage, bollBand=bollBand, MACDInd=MACDInd, momentum=momentum)
    # Process orders
    portvals = marketsim.compute_portvals(orders_file=of, start_val=sv)
    #Print statements to determine start and end value
    print portvals.head(1)
    print portvals.tail(1)
    of = "orders.csv"
    #default name of file is orders.csv
    portvals1 = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals1.head(1)
    print portvals1.tail(1)
    if gen_plot:
        df = pd.concat([portvals/portvals.iloc[0], portvals1/portvals1.iloc[0]], keys=['Benchmark', 'Rule-Based'], axis=1)
        ax = df.plot(title="Rule Based Trader", fontsize=12,color=["Black","Blue"])
        ax.set_xlabel("Date")
        ax.set_ylabel("Price Normalized")
        stock = 0
        #Loop through orders to add vertical lines
        for date,order in orders.iterrows():
            if order.loc["Order"]=="SELL":
                stock-=500
                if stock==0:
                    ax.axvline(date.date(),color="Black")
                else:
                    ax.axvline(date.date(), color="Red")
            else:
                stock+=500
                if stock==0:
                    ax.axvline(date.date(),color="Black")
                else:
                    ax.axvline(date.date(), color="Green")
        plt.show()
Пример #3
0
def main(gen_plot=False):
    normalAverage, bollBand, MACDInd, momentum, prices = indicators.plotIndicators(
        gen_plot=gen_plot)
    normalArray = normalAverage.as_matrix()
    bollBandArray = bollBand.as_matrix()
    MACDIndArray = MACDInd.as_matrix()
    momentumArray = momentum.as_matrix()
    indArrayTrain = np.concatenate(
        (normalArray, bollBandArray, MACDIndArray, momentumArray), axis=1)
    Y = prices.shift(-10) / prices - 1
    YBUY = .03
    YSELL = -.04
    for i in range(Y.shape[0]):
        #print Y.iloc[i]["IBM"]
        if Y.iloc[i]["IBM"] > YBUY:
            Y.iloc[i]["IBM"] = 1
        elif Y.iloc[i]["IBM"] < YSELL:
            Y.iloc[i]["IBM"] = -1
        else:
            Y.iloc[i]["IBM"] = 0

    YArray = Y['IBM'].values
    #Build Test Data
    normalAverage, bollBand, MACDInd, momentum, prices = indicators.plotIndicators(
        sd=dt.datetime(2010, 1, 4),
        ed=dt.datetime(2010, 12, 31),
        gen_plot=gen_plot)
    normalArray = normalAverage.as_matrix()
    bollBandArray = bollBand.as_matrix()
    MACDIndArray = MACDInd.as_matrix()
    momentumArray = momentum.as_matrix()
    indArrayTest = np.concatenate(
        (normalArray, bollBandArray, MACDIndArray, momentumArray), axis=1)
    orderInfo = mlTrade(indArrayTrain, YArray, indArrayTest)
    orders = generateFutureOrder(orderInfo, prices.index, name="Test.csv")
    sv = 100000
    of = "Test.csv"
    portvals = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals.head(1)
    print portvals.tail(1)
    of = "TestOrders.csv"
    rule_based.ordersFile(sd = dt.datetime(2010,1,4), ed = dt.datetime(2010,12,31),normalAverage=normalAverage,\
                          bollBand=bollBand,MACDInd=MACDInd,momentum=momentum,filename=of)
    portvals1 = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals1.head(1)
    print portvals1.tail(1)
    of = "benchmarkTest.csv"
    portvals2 = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals2.head(1)
    print portvals2.tail(1)
    if gen_plot:
        df = pd.concat([
            portvals2 / portvals2.iloc[0], portvals1 / portvals1.iloc[0],
            portvals / portvals.iloc[0]
        ],
                       keys=['Benchmark', 'Rule-Based', "ML-Based"],
                       axis=1)
        ax = df.plot(title="ML Based Trader",
                     fontsize=12,
                     color=["Black", "Blue", "Green"])
        ax.set_xlabel("Date")
        ax.set_ylabel("Price Normalized")
        plt.show()
Пример #4
0
    def addEvidence(self, symbol = "IBM", \
        sd=dt.datetime(2008,1,1), \
        ed=dt.datetime(2009,1,1), \
        sv = 10000):
        syms = [symbol]
        dates = pd.date_range(sd, ed)
        #Call indicators function to receive indicators with dates before starting range
        normalAverage, bollBand, MACDInd, momentum, prices = plotIndicators(
            sd - dt.timedelta(days=80), ed, syms)

        #Get indicators for the given dates
        bollBand = bollBand.loc[prices.index >= sd]
        normalAverage = normalAverage.loc[prices.index >= sd]
        MACDInd = MACDInd.loc[prices.index >= sd]
        momentum = momentum.loc[prices.index >= sd]
        prices = prices.loc[prices.index >= sd]

        #Discretize values in the dataframs
        bins = self.bins
        bollBand = self.dfDiscretize(bollBand, steps=self.bins)
        normalAverage = self.dfDiscretize(normalAverage, steps=self.bins)
        MACDInd = self.dfDiscretize(MACDInd, steps=self.bins)
        momentum = self.dfDiscretize(momentum, steps=self.bins)

        #loop varialbles
        iterationMin = 3
        iterationMax = 50
        rewardPos = 100
        rewardNeg = -2000
        firstState = (bollBand.iloc[0][symbol], normalAverage.iloc[0][symbol],
                      MACDInd.iloc[0][symbol], momentum.iloc[0][symbol])
        dims = (self.bins, self.bins, self.bins, self.bins)
        firstState = np.ravel_multi_index(firstState, dims=dims)
        returnArr = []
        self.learner = ql.QLearner(num_states=self.bins**4,
                                   num_actions=5,
                                   verbose=False)
        orders = pd.DataFrame(columns=["Cash", "Shares"], index=prices.index)

        for i in range(iterationMax):
            action = self.learner.querysetstate(firstState)
            shares = 0
            for j in range(len(prices.index)):
                lastRet = shares * prices.iloc[j - 1][symbol]
                #Buy allowed
                if action == 0 and shares in [0, -500]:
                    shares += 500
                elif action == 1 and shares == -500:
                    shares += 1000
                #sell allowed
                elif action == 2 and shares in [0, 500]:
                    shares -= 500
                elif action == 3 and shares == 500:
                    shares -= 1000
                ret = shares * prices.iloc[j][symbol]
                daily = ret / lastRet - 1
                if j == 0:
                    cash = sv
                else:
                    cash = orders.iloc[j - 1]["Cash"] + ret
                reward = rewardPos if daily > 0 else rewardNeg

                orders.iloc[j] = pd.Series({"Cash": cash, "Shares": shares})
                nextState = np.ravel_multi_index(
                    (bollBand.iloc[j][symbol], normalAverage.iloc[j][symbol],
                     MACDInd.iloc[j][symbol], momentum.iloc[j][symbol]),
                    dims=dims)

                action = self.learner.query(nextState, reward)
            returnArr.append(cash)
            if i > iterationMin - 1:
                converge = 0.1 * returnArr[i - 1]
                if (returnArr[i] - converge) < returnArr[i - 1] < (
                        returnArr[i] + converge):
                    break
Пример #5
0
def main(gen_plot=False):
    #Get values for Indicators
    normalAverage, bollBand, MACDInd, momentum,prices = indicators.plotIndicators(gen_plot=gen_plot)
    normalArray = normalAverage.as_matrix()
    bollBandArray = bollBand.as_matrix()
    MACDIndArray = MACDInd.as_matrix()
    momentumArray = momentum.as_matrix()
    indArray = np.concatenate((normalArray,bollBandArray,MACDIndArray,momentumArray),axis=1)
    #Create future Y predictions
    Y = prices.shift(-10)/prices - 1
    YBUY =.03
    YSELL=-.02
    #Convert predictions to 1, 0, -1 for classifier
    for i in range(Y.shape[0]):
        #print Y.iloc[i]["IBM"]
        if Y.iloc[i]["IBM"]>YBUY:
            Y.iloc[i]["IBM"]=1
        elif Y.iloc[i]["IBM"]<YSELL:
            Y.iloc[i]["IBM"]=-1
        else:
            Y.iloc[i]["IBM"] = 0

    YArray = Y['IBM'].values

    orderInfo = mlTrade(indArray,YArray)
    #create orders file
    orders = generateOrder(orderInfo,prices.index)
    sv =100000
    of = "MLBased.csv"
    portvals = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals.head(1)
    print portvals.tail(1)
    #orders.csv contains data for the rule based trader
    of = "orders.csv"
    portvals1 = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals1.head(1)
    print portvals1.tail(1)
    of = "benchmark.csv"
    portvals2 = marketsim.compute_portvals(orders_file=of, start_val=sv)
    print portvals2.head(1)
    print portvals2.tail(1)
    #generate the plot
    if gen_plot:
        df = pd.concat([portvals2/portvals2.iloc[0], portvals1/portvals1.iloc[0],portvals/portvals.iloc[0]], keys=['Benchmark', 'Rule-Based',"ML-Based"], axis=1)
        ax = df.plot(title="ML Based Trader", fontsize=12,color=["Black","Blue","Green"])
        ax.set_xlabel("Date")
        ax.set_ylabel("Price Normalized")
        stock = 0
        for date,order in orders.iterrows():
            if order.loc["Order"]=="SELL":
                stock-=500
                if stock==0:
                    ax.axvline(date.date(),color="Black")
                else:
                    ax.axvline(date.date(), color="Red")
            else:
                stock+=500
                if stock==0:
                    ax.axvline(date.date(),color="Black")
                else:
                    ax.axvline(date.date(), color="Green")
        plt.show()