コード例 #1
0
    def testPolicy(self, symbol, sd, ed, sv):

        symbols = []
        symbols.append(symbol)
        dates = pd.date_range(sd, ed)
        price = get_data(symbols, dates)

        orders = []
        holdings = {sym: 0 for sym in symbols}
        lookback = 14

        # get the indicators values
        psr = get_Price_SMA_Ratio(symbols, dates, lookback=14)
        bbp = get_Bollinger_Bands_ratio(symbols, dates, lookback=14)
        mfi = get_MFI(symbols, dates)

        for day in range(price.shape[0]):
            for sym in symbols:
                if sym == "SPY":
                    continue
                # STOCK MAY BE OVERBOUGHT, INDEX DOES NOT APPEAR TO BE OVERSOLD
                if (psr.ix[day, sym] < 1.05) and (
                        bbp.ix[day, sym] <= 0.5) and (mfi.ix[day, sym] < 0.4):
                    if holdings[sym] < 1000:
                        holdings[sym] = holdings[sym] + 1000
                        orders.append(
                            [price.index[day].date(), sym, 'BUY', 1000])
                    else:
                        holdings[sym] = holdings[sym]
                        orders.append(
                            [price.index[day].date(), sym, 'NOTHING', 0])
                # Stock may be overbought, index does not appear to be overbought
                elif (psr.ix[day, sym] > 0.95) and (
                        bbp.ix[day, sym] > 0.3) and (mfi.ix[day, sym] > 1):
                    if holdings[sym] > -1000:
                        holdings[sym] = holdings[sym] - 1000
                        orders.append(
                            [price.index[day].date(), sym, 'SELL', 1000])
                    else:
                        holdings[sym] = holdings[sym]
                        orders.append(
                            [price.index[day].date(), sym, 'NOTHING', 0])
                elif (psr.ix[day, sym] >= 1) and (psr.ix[day - 1, sym] <
                                                  1) and (holdings[sym] > 0):
                    holdings[sym] = 0
                    orders.append([price.index[day].date(), sym, 'SELL', 1000])
                elif (psr.ix[day, sym] <= 1) and (psr.ix[day - 1, sym] >
                                                  1) and (holdings[sym] < 0):
                    holdings[sym] = 0
                    orders.append([price.index[day].date(), sym, 'BUY', 1000])
                else:
                    orders.append([price.index[day].date(), sym, 'NOTHING', 0])

        trades_df = pd.DataFrame(orders,
                                 columns=["Date", "Symbol", "Order", "Shares"])
        trades_df.set_index('Date', inplace=True)

        return trades_df
コード例 #2
0
ファイル: StrategyLearner.py プロジェクト: adamzolotarev/ML4T
    def addEvidence(self, symbol="AAPL", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv=100000):

        syms = [symbol]
        dates = pd.date_range(sd, ed)
        total_prices = ut.get_data(syms, dates)
        prices = total_prices[syms]
        psr = get_Price_SMA_Ratio(symbols=syms,dates=dates,lookback=14)
        rsi = get_RSI(symbols=syms,dates=dates,lookback=14)
        mfi = get_MFI(symbols=syms,dates=dates,lookback=14)
        spsr = psr.ix[:,symbol]
        srsi = rsi.ix[:,symbol]
        smfi = mfi.ix[:,symbol]
        dpsr=self.Discretization(spsr,steps=5)
        drsi=self.Discretization(srsi,steps=5)
        dmfi=self.Discretization(smfi,steps=5)

        prices = prices.ix[:,symbol]
        dr = prices.copy()
        dr[0] = np.nan
        dr[1:] = (prices[1:] / prices[:-1].values) - 1

        States = dpsr * 100 + drsi + dmfi
        States = States.ix[:,'discretized_value']
        self.learner = ql.QLearner(num_states=1000, num_actions=3)
        oldprofit = 0
        for iteration in range(100):
            current_holding = 0
            total_profit = 0
            rewards = 0
            for i in range(dr.shape[0] - 1):
                if (i > 0):
                    total_profit += prices[i - 1] * current_holding * dr[i]
                state = States[i]
                if i == 0:
                    action = self.learner.querysetstate(state)
                else:
                    action = self.learner.query(state, rewards)
                holding, rewards = self.trade_actions(current_holding, action, dr[i + 1])
                current_holding = holding
            total_profit += prices[-2] * current_holding * dr[-1]
            if total_profit == oldprofit:
                break
            oldprofit = total_profit
コード例 #3
0
ファイル: ManualStrategy.py プロジェクト: adamzolotarev/ML4T
    def testPolicy(self,symbol,sd,ed,sv):
        symbols = []
        symbols.append(symbol)
        dates = pd.date_range(sd, ed)
        price = get_data(symbols, dates)
        orders = []
        holdings = {sym: 0 for sym in symbols}
        lookback = 14
        psr = get_Price_SMA_Ratio(symbols,dates,lookback=14)
        rsi = get_RSI(symbols,dates,lookback=14)
        mfi = get_MFI(symbols,dates)
        
        for day in range(price.shape[0]):
            for sym in symbols:
                if (psr.ix[day, sym] < 1.05) and (rsi.ix[day, sym] < 15) and (mfi.ix[day, sym] < 10):
                    if holdings[sym] < 1000:
                        holdings[sym] = holdings[sym] + 1000
                        orders.append([price.index[day].date(), sym, 'BUY', 1000])
                    else:
                        orders.append([price.index[day].date(), sym, 'NOTHING', 0])
                elif (psr.ix[day, sym] > 0.95) and (rsi.ix[day, sym] > 55) and (mfi.ix[day, sym] > 40):
                    if holdings[sym] > -1000:
                        holdings[sym] = holdings[sym] - 1000
                        orders.append([price.index[day].date(), sym, 'SELL', 1000])
                    else:
                        orders.append([price.index[day].date(), sym, 'NOTHING', 0])
                elif (psr.ix[day, sym] < 1) and (psr.ix[day-1,sym] > 1) and (holdings[sym] < 0):
                    holdings[sym] = holdings[sym] + 1000
                    orders.append([price.index[day].date(), sym, 'BUY', 1000])
                elif (psr.ix[day, sym] > 1) and (psr.ix[day-1,sym] < 1) and (holdings[sym] > 0):
                    holdings[sym] = holdings[sym] - 1000
                    orders.append([price.index[day].date(), sym, 'SELL', 1000])
                else:
                    orders.append([price.index[day].date(), sym, 'NOTHING', 0])

        df_trades = pd.DataFrame(orders, columns=["Date", "Symbol", "Order", "Shares"])
        df_trades.set_index('Date', inplace=True)
        return df_trades
コード例 #4
0
ファイル: StrategyLearner.py プロジェクト: adamzolotarev/ML4T
    def testPolicy(self, symbol="AAPL", sd=dt.datetime(2010,1,1), ed=dt.datetime(2011,12,31), sv=100000):

        dates = pd.date_range(sd, ed)
        total_prices = ut.get_data([symbol], dates)
        syms = [symbol]
        prices = total_prices[syms]
        psr = get_Price_SMA_Ratio(symbols=syms, dates=dates, lookback=14)
        rsi = get_RSI(symbols=syms, dates=dates, lookback=14)
        mfi = get_MFI(symbols=syms, dates=dates, lookback=14)
        spsr = psr.ix[:, symbol]
        srsi = rsi.ix[:, symbol]
        smfi = mfi.ix[:, symbol]
        dpsr = self.Discretization(spsr, steps=5)
        drsi = self.Discretization(srsi, steps=5)
        dmfi = self.Discretization(smfi, steps=5)
        States = dpsr * 100 + drsi + dmfi
        States = States.ix[:, 'discretized_value']
        prices = prices.ix[:, symbol]
        dr = prices.copy()
        trades = prices.copy()
        trades[:] = 0
        dr[0] = 0
        dr[1:] = (prices[1:] / prices[:-1].values) - 1
        current_holding = 0
        total_profit = 0
        for i in range(len(dr)- 1):
            if (i > 0):
                total_profit += prices[i - 1] * current_holding * dr[i]
            state = States[i]
            action = self.learner.querysetstate(state)
            holding, rewards = self.trade_actions(current_holding, action, dr[i + 1])
            trades[i] = (holding - current_holding)
            current_holding = holding
        total_profit += prices[-2] * current_holding * dr[-1]
        trades = pd.DataFrame(trades, columns=[symbol])
        return trades
コード例 #5
0
    def addEvidence(self, symbol="JPM", \
                    sd=dt.datetime(2008, 1, 1), \
                    ed=dt.datetime(2009, 12, 31), \
                    sv=100000):

        # add your code to do learning here
        syms = [symbol]
        dates = pd.date_range(sd, ed)
        prices_all = ut.get_data(syms, dates)  # automatically adds SPY
        prices = prices_all[syms]  # only portfolio symbols
        prices_SPY = prices_all['SPY']  # only SPY, for comparison later
        if self.verbose: print prices

        # get indicators
        all_psr = ind.get_Price_SMA_Ratio(symbols=syms,
                                          dates=dates,
                                          lookback=14)
        all_bbp = ind.get_Bollinger_Bands_ratio(symbols=syms,
                                                dates=dates,
                                                lookback=14)
        all_mfi = ind.get_MFI(symbols=syms, dates=dates, lookback=14)

        sym_psr = all_psr.ix[:, symbol]
        sym_bbp = all_bbp.ix[:, symbol]
        sym_mfi = all_mfi.ix[:, symbol]

        # get daily returns
        prices = prices.ix[:, symbol]
        dr = prices.copy()
        dr[1:] = (prices[1:] / prices[:-1].values) - 1
        dr[0] = np.nan

        # discretize indicators
        dis_psr = self.dfDiscretize(sym_psr, steps=10)
        dis_bbp = self.dfDiscretize(sym_bbp, steps=10)
        dis_mfi = self.dfDiscretize(sym_mfi, steps=10)

        States = dis_psr * 100 + dis_bbp * 10 + dis_mfi
        States = States.ix[:, 'dis_value']

        # initial qlearner
        self.learner = ql.QLearner(num_states=1000, num_actions=3)

        # training learner
        oldprofit = 0
        for iteration in range(100):
            old_holding = 0
            profits = 0
            rewards = 0
            for i in range(dr.shape[0] - 1):
                if (i > 0):
                    profits += prices[i -
                                      1] * old_holding * self.holdlimit * dr[i]
                state = States[i]
                if i == 0:
                    action = self.learner.querysetstate(state)
                else:
                    action = self.learner.query(state, rewards)
                holding, rewards = self.execute_action(old_holding, action,
                                                       dr[i + 1])

                old_holding = holding
            profits += prices[-2] * old_holding * self.holdlimit * dr[-1]
            if iteration > 10:
                if profits == oldprofit:
                    break
            oldprofit = profits

        # example use with new colname
        volume_all = ut.get_data(syms, dates,
                                 colname="Volume")  # automatically adds SPY
        volume = volume_all[syms]  # only portfolio symbols
        volume_SPY = volume_all['SPY']  # only SPY, for comparison later
        if self.verbose: print volume
コード例 #6
0
    def testPolicy(self, symbol="JPM", \
                   sd=dt.datetime(2010, 1, 1), \
                   ed=dt.datetime(2011, 12, 31), \
                   sv=100000):

        # here we build a fake set of trades
        # your code should return the same sort of data
        dates = pd.date_range(sd, ed)
        prices_all = ut.get_data([symbol], dates)  # automatically adds SPY

        syms = [symbol]
        prices = prices_all[syms]  # only portfolio symbols
        # get indicators
        all_psr = ind.get_Price_SMA_Ratio(symbols=syms,
                                          dates=dates,
                                          lookback=14)
        all_bbp = ind.get_Bollinger_Bands_ratio(symbols=syms,
                                                dates=dates,
                                                lookback=14)
        all_mfi = ind.get_MFI(symbols=syms, dates=dates, lookback=14)

        sym_psr = all_psr.ix[:, symbol]
        sym_bbp = all_bbp.ix[:, symbol]
        sym_mfi = all_mfi.ix[:, symbol]

        # discretize indicators
        dis_psr = self.dfDiscretize(sym_psr, steps=10)
        dis_bbp = self.dfDiscretize(sym_bbp, steps=10)
        dis_mfi = self.dfDiscretize(sym_mfi, steps=10)

        States = dis_psr * 100 + dis_bbp * 10 + dis_mfi
        States = States.ix[:, 'dis_value']

        # get daily returns
        prices = prices.ix[:, symbol]
        dr = prices.copy()
        dr[1:] = (prices[1:] / prices[:-1].values) - 1
        dr[0] = np.nan
        trades = dr.copy()
        trades[:] = 0

        old_holding = 0
        profits = 0
        for i in range(dr.shape[0] - 1):

            if (i > 0):
                profits += prices[i - 1] * old_holding * self.holdlimit * dr[i]
            state = States[i]

            action = self.learner.querysetstate(state)

            holding, rewards = self.execute_action(old_holding, action,
                                                   dr[i + 1])
            trades[i] = (holding - old_holding) * self.holdlimit
            old_holding = holding
        profits += prices[-2] * old_holding * self.holdlimit * dr[-1]

        trades = pd.DataFrame(trades, columns=[symbol])

        if self.verbose: print type(trades)  # it better be a DataFrame!
        if self.verbose: print trades
        if self.verbose: print prices_all

        return trades