コード例 #1
0
def testPolicy(symbol="JPM",
               sd=dt.datetime(2010, 1, 1),
               ed=dt.datetime(2011, 12, 31),
               sv=100000):

    symbols = [symbol]
    date_range = pd.date_range(sd, ed)

    #symbols = ['SPY','AAPL','GOOG','XOM']
    #start_date = '2011-01-01'
    #end_date = '2012-02-01'
    #date_range = pd.date_range(start_date, end_date)

    stock_price = get_data(symbols, date_range)
    stock_price = stock_price[symbols]
    window = 10  #10 for in sample, 13 14,18,10 for out sample
    bbu, bbl, SMA, SMAi, bbi = BollingerBands(stock_price, window=window)

    orders = stock_price.copy()
    orders = orders * 0.
    '''    
    RSIi = RSI(stock_price, window = window)
    momentum_window = 5
    momentum_df = momentum(stock_price, window = momentum_window)

    RSIi_SPY = RSIi.copy()
    RSIi_SPY = RSIi_SPY.where(RSIi_SPY == np.NaN, 1.)    
    RSIi_SPY.values[:,:] = RSIi.iloc[:,0:1]
    
    x10 = SMAi < 0.95
    x20 = bbi < -1
    x30 = RSIi < 30 
    x40 = RSIi_SPY > 30
    
    x11 = SMAi > 1.05
    x21 = bbi > 1
    x31 = RSIi > 70
    x41 = RSIi_SPY < 70
    
    #x50 = x10 & x20 & x30 & x40
    #x51 = x11 & x21 & x31 & x41
    '''
    #BollingerBand
    #buying signal
    orders[bbi < -1] = 1

    #selling signal
    orders[bbi > 1] = -1
    '''
    SMAi_diff = SMAi.diff()     
    #move up momentum
    orders[((SMAi-1)*(SMAi.shift(1)-1) < 0) & (SMAi_diff > 0) & (momentum_df > 0.05)] = 1    
    #move down momentum
    orders[((SMAi-1)*(SMAi.shift(1)-1) < 0) & (SMAi_diff < 0) & (momentum_df < -0.05)] = -1
    '''
    return assemble_order(orders)
コード例 #2
0
 def testPolicy(self, symbol = "JPM", \
     sd=dt.datetime(2009,1,1), \
     ed=dt.datetime(2010,1,1), \
     sv = 10000):
     
     #symbols = [symbol]
     date_range = pd.date_range(sd, ed)
 
     #symbols = ['SPY','AAPL','GOOG','XOM']
     #start_date = '2011-01-01'
     #end_date = '2012-02-01'
     #date_range = pd.date_range(start_date, end_date)
     
     stock_price = get_data([symbol], date_range)
     stock_price = stock_price[[symbol]]
     window = 10  #10 for in sample, 13 14,18,10 for out sample
     bbu,bbl,SMA,SMAi,bbi = BollingerBands(stock_price,window = window)
     
     orders = stock_price.copy()
     orders = orders*0.
     
     #BollingerBand
     #buying signal 
     
     bbi = bbi.dropna()
     holding = 0
     
     for date in bbi.index:
         state = self.discretize(bbi[symbol][date], holding)
         action = self.learner.querysetstate(state) - 1
         orders[symbol][date] = action
         
         if action == -1:                      
             holding = -1
         elif action == 1:
             holding = 1
         #else 
         #    holding = holding     
     #ordersDF = orders.to_frame()    
     trades = ms.assemble_order(orders)
     del trades['Symbol']
     trades = trades.rename(columns={'Shares':symbol})
     
     return trades    
     ''' 			 	 	 		 		 	  		   	  			  	
コード例 #3
0
def test_code_indicator():
    stock_symbol = 'JPM' #'APPL'
    start_date = '2008-01-01'
    #end_date = '2009-12-31'
    end_date = '2009-12-31'
    date_range = pd.date_range(start_date, end_date)
    stock_price = get_data([stock_symbol], date_range)
    stock_price = stock_price[stock_symbol]
    
    stock_price = stock_price / stock_price.values[0]    
    window = 10    
    bbu,bbl,SMA,SMAi,bbi = BollingerBands(stock_price,window = window)
    
    SMAu = SMA*1.05
    SMAl = SMA*0.95    
    #RSI_i = RSI(stock_price, window = window)
    #momentum_df = momentum(stock_price, window = window)
    
    plt.plot(bbu, label='upper BollingerBand')
    plt.plot(bbl, label='lower BollingerBand')
    plt.plot(SMA, label='SMA')
    plt.plot(stock_price, label='Stock Price')
    #plt.axis([dt.date(2008,1,1), dt.date(2009,12,31), 0.8, 1.2])
    #plt.axis([0, 300, -256, 100])
    plt.xticks(rotation=30)
    plt.xlabel('Date')
    plt.ylabel('Normalized Price')
    plt.title('BollingerBand Indicator')
    #plt.legend(['Theoretically Optimal Strategy','Benchmark'])
    plt.legend(loc='lower left')
    
    #plt.annotate('Selling Signal', xy=(dt.date(2010,4,15), 1.12), xytext=(dt.date(2010,4,1), 1.17),
    #        arrowprops=dict(arrowstyle="->", connectionstyle="arc3")
    #        )
    #plt.annotate('Buying Signal', xy=(dt.date(2010,5,20), 0.89), xytext=(dt.date(2010,4,20), 0.82),
    #        arrowprops=dict(arrowstyle="->", connectionstyle="arc3")
    #        )
    #    
    plt.tight_layout()
    plt.savefig('Manual_BollingerBand_Indicators.png')
    #plt.show()
    plt.close()
    
    plt.plot(SMAu, label='Price/SMA - over estimate')
    plt.plot(SMAl, label='Price/SMA - under estimate')
    plt.plot(SMA, label='SMA')
    plt.plot(stock_price,label='Stock Price')
    #plt.axis([dt.date(2008,1,1), dt.date(2009,12,31), 0.8, 1.2])
    plt.xticks(rotation=30)
    plt.xlabel('Date')
    plt.ylabel('Normalized Price')
    plt.title('Price/SMA Indicator')
    #plt.legend(['Theoretically Optimal Strategy','Benchmark'])
    plt.legend(loc='lower left')
    #
    #plt.annotate('Selling Signal', xy=(dt.date(2010,4,15), 1.12), xytext=(dt.date(2010,4,1), 1.17),
    #        arrowprops=dict(arrowstyle="->", connectionstyle="arc3")
    #        )
    #plt.annotate('Buying Signal', xy=(dt.date(2010,5,20), 0.89), xytext=(dt.date(2010,4,20), 0.82),
    #        arrowprops=dict(arrowstyle="->", connectionstyle="arc3")
    #        )
    #    
    plt.tight_layout()
    plt.savefig('Manual_SMA_Indicators.png')
    #plt.show()
    plt.close()
コード例 #4
0
def testPolicy(symbol="JPM",
               sd=dt.datetime(2008, 1, 1),
               ed=dt.datetime(2009, 12, 31),
               sv=100000):

    ############################
    #
    # Loading Data
    #
    ############################

    prices = get_data([symbol], pd.date_range(sd, ed), True)
    prices = prices.drop(['SPY'], axis=1)
    prices = prices.fillna(method='ffill')
    prices = prices.fillna(method='bfill')
    prices = prices / prices.ix[0, ]

    ############################
    #
    # Indicators
    #
    ############################

    SMA_20 = SMA(prices, symbol, 20)
    SMA_50 = SMA(prices, symbol, 50)
    EMA_20 = EMA(prices, symbol, 20)
    EMA_50 = EMA(prices, symbol, 50)
    [SMA_20, SMA_20_std, BB_upper_band, BB_lower_band,
     BB_value] = BollingerBands(prices, symbol, 20, 2)
    momentum_20 = Momentum(prices, symbol, 20)

    indicators_df = pd.DataFrame(index=prices.index,
                                 columns=[
                                     'JPM', 'SMA_20days', 'SMA_50days',
                                     'EMA_20days', 'EMA_50days',
                                     'BB_upper_band', 'BB_lower_band',
                                     'Momentum_20days'
                                 ])
    indicators_df['JPM'] = prices[symbol]
    indicators_df['SMA_20days'] = SMA_20
    indicators_df['SMA_50days'] = SMA_50
    indicators_df['EMA_20days'] = EMA_20
    indicators_df['EMA_50days'] = EMA_50
    indicators_df['BB_upper_band'] = BB_upper_band
    indicators_df['BB_lower_band'] = BB_lower_band
    indicators_df['Momentum_20days'] = momentum_20
    indicators_df = indicators_df.fillna(method='bfill')

    #print indicators_df

    ############################
    #
    # Signal
    #
    ############################

    signal_df = pd.DataFrame(index=prices.index,
                             columns=['SMA', 'BB', 'Momentum'])

    signal_df.loc[signal_df.index[0], 'SMA'] = 0
    signal_df.loc[signal_df.index[0], 'BB'] = 0
    signal_df.loc[signal_df.index[0], 'Momentum'] = 0

    row_counter = 1

    for i, row in indicators_df[1:].iterrows():

        current_price = row['JPM']
        current_SMA20 = row['SMA_20days']
        current_SMA50 = row['SMA_50days']
        current_EMA20 = row['EMA_20days']
        current_EMA50 = row['EMA_50days']
        current_BB_up = row['BB_upper_band']
        current_BB_down = row['BB_lower_band']
        current_momentum = row['Momentum_20days']

        previous_price = indicators_df['JPM'][row_counter - 1]
        previous_SMA20 = indicators_df['SMA_20days'][row_counter - 1]
        previous_SMA50 = indicators_df['SMA_50days'][row_counter - 1]
        previous_EMA20 = indicators_df['EMA_20days'][row_counter - 1]
        previous_EMA50 = indicators_df['EMA_50days'][row_counter - 1]
        previous_BB_up = indicators_df['BB_upper_band'][row_counter - 1]
        previous_BB_down = indicators_df['BB_lower_band'][row_counter - 1]
        previous_momentum = indicators_df['Momentum_20days'][row_counter - 1]

        row_counter += 1

        #print "Current: ", current_momentum
        #print "Previous: ", previous_momentum

        ############################
        #
        # SMA
        #
        ############################

        if (previous_SMA50 > previous_SMA20) and (current_SMA50 <
                                                  current_SMA20):
            signal_df.loc[i, 'SMA'] = 1
        elif (previous_SMA50 < previous_SMA20) and (current_SMA50 >
                                                    current_SMA20):
            signal_df.loc[i, 'SMA'] = -1
        else:
            signal_df.loc[i, 'SMA'] = 0

        ############################
        #
        # BB
        #
        ############################

        if (previous_price < previous_BB_down) and (current_price >
                                                    current_BB_down):
            signal_df.loc[i, 'BB'] = 1
        elif (previous_price > previous_BB_up) and (current_price <
                                                    current_BB_up):
            signal_df.loc[i, 'BB'] = -1
        else:
            signal_df.loc[i, 'BB'] = 0

        ############################
        #
        # EMA
        #
        ############################

        #if (previous_EMA50 > previous_EMA20) and (current_EMA50 < current_EMA20):
        #    signal_df.loc[i, 'EMA'] = 1
        #elif (previous_EMA50 < previous_EMA20) and (current_EMA50 > current_EMA20):
        #    signal_df.loc[i, 'EMA'] = -1
        #else:
        #    signal_df.loc[i, 'EMA'] = 0

        ############################
        #
        # Momentum
        #
        ############################

        if (previous_momentum < 0) and (current_momentum >= 0):
            signal_df.loc[i, 'Momentum'] = 1
        elif (previous_momentum >= 0) and (current_momentum < 0):
            signal_df.loc[i, 'Momentum'] = -1
        else:
            signal_df.loc[i, 'Momentum'] = 0

    signal_df["SUM"] = signal_df.sum(axis=1)
    signal_df["Signal"] = signal_df["SUM"]

    signal_df.loc[signal_df["SUM"] <= -1, "Signal"] = -1
    signal_df.loc[signal_df["SUM"] >= 1, "Signal"] = 1
    signal_df.loc[signal_df["SUM"] == 0, "Signal"] = 0

    #print signal_df.head(10)

    signal_df.to_csv("signal.csv")

    df_trades = pd.DataFrame(index=signal_df.index, columns=[symbol])
    df_trades[symbol] = np.zeros(signal_df.shape[0])
    holding = 0
    indicators_df['Final_Signal'] = np.zeros(indicators_df.shape[0])

    # Trading Scheme

    for i in df_trades.index:

        #print i

        if holding == 0:

            if signal_df.loc[i, "Signal"] == 1:
                df_trades.loc[i, symbol] = 1000
                holding = 1000
                indicators_df.loc[i, 'Final_Signal'] = 1
            elif signal_df.loc[i, "Signal"] == -1:
                df_trades.loc[i, symbol] = -1000
                holding = -1000
                indicators_df.loc[i, 'Final_Signal'] = -1
            else:
                df_trades.loc[i, symbol] = 0

        elif holding == 1000:

            if signal_df.loc[i, "Signal"] == -1:
                df_trades.loc[i, symbol] = -2000
                indicators_df.loc[i, 'Final_Signal'] = -1
                holding = -1000

        elif holding == -1000:

            if signal_df.loc[i, "Signal"] == 1:
                df_trades.loc[i, symbol] = 2000
                indicators_df.loc[i, 'Final_Signal'] = 1
                holding = 1000

    df_trades.to_csv("trades.csv")

    return (df_trades, indicators_df)
コード例 #5
0
    def addEvidence(self, symbol = "JPM", \
        sd=dt.datetime(2008,1,1), \
        ed=dt.datetime(2009,1,1), \
        sv = 10000): 
			  		 			 	 	 		 		 	  		   	  			  	
        """ 			  		 			 	 	 		 		 	  		   	  			  	
        # add your code to do learning here 			  		 			 	 	 		 		 	  		   	  			  	
 			  		 			 	 	 		 		 	  		   	  			  	
        # example usage of the old backward compatible util function 			  		 			 	 	 		 		 	  		   	  			  	
        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 			  		 			 	 	 		 		 	  		   	  			  	
 			  		 			 	 	 		 		 	  		   	  			  	
        # 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 	

        ########
        """        
	
    
        stock_symbol = symbol
        date_range = pd.date_range(sd, ed)
        stock_price = get_data([stock_symbol], date_range)
        stock_price = stock_price[stock_symbol]
        
        #stock_price = stock_price / stock_price.values[0]    
        window = 10
        bbu,bbl,SMA,SMAi,bbi = BollingerBands(stock_price,window = window)
        bbi.dropna(inplace=True)
        
        """      
        SMAu = SMA*1.05
        SMAl = SMA*0.95    
        
        EMA_df, EMA_i_df = EMA(stock_price, window = window)
        EMAu = EMA_df*1.05
        EMAl = EMA_df*0.95 
        #RSI_i = RSI(stock_price, window = window)
        #momentum_df = momentum(stock_price, window = window)
        
        plt.plot(bbu, label='upper BollingerBand')
        plt.plot(bbl, label='lower BollingerBand')
        plt.plot(SMA, label='SMA')
        plt.plot(stock_price, label='Stock Price')
        plt.axis([dt.date(2010,1,1), dt.date(2010,6,1), 0.8, 1.2])
        #plt.axis([0, 300, -256, 100])
        plt.xticks(rotation=30)
        plt.xlabel('Date')
        plt.ylabel('Normalized Price')
        plt.title('BollingerBand Indicator')
        #plt.legend(['Theoretically Optimal Strategy','Benchmark'])
        plt.legend(loc='upper left')
        plt.annotate('Selling Signal', xy=(dt.date(2010,4,15), 1.12), xytext=(dt.date(2010,4,1), 1.17),
                arrowprops=dict(arrowstyle="->", connectionstyle="arc3")
                )
        plt.annotate('Buying Signal', xy=(dt.date(2010,5,20), 0.89), xytext=(dt.date(2010,4,20), 0.82),
                arrowprops=dict(arrowstyle="->", connectionstyle="arc3")
                )    
        plt.tight_layout()
        plt.savefig('BollingerBand_Indicators.png')
        plt.show()
        plt.close()
        """        
        
        self.N_indicator = 10
        self.N_position = 3
                
        #bbi.columns = ['bbi']
        df = pd.DataFrame()
        df['bbi'] = bbi
        df['price'] = stock_price
        #df.dropna(inplace=True)
        
        indicators = df['bbi'].values.tolist()
        prices     = df['price'].values.tolist()
        
        self.init_discretize(indicators) 
        num_states = self.N_indicator * self.N_position
        num_actions = 3
			  		 			 	 	 		 		 	  		   	  			  	
        self.learner = ql.QLearner(num_states=num_states,\
            num_actions = num_actions, \
            alpha = 0.2, \
            gamma = 0.9, \
            rar = 0.99, \
            radr = 0.999, \
            dyna = 0, \
            verbose=False) #initialize the learner 	 	

        # each epoch involves one trip to the goal 	
        epochs = 500		  		 			 	 	 		 		 	  		   	  			  	
        #startpos = getrobotpos(map) #find where the robot starts 			  		 			 	 	 		 		 	  		   	  			  	
        #goalpos = getgoalpos(map) #find where the goal is 			  		 			 	 	 		 		 	  		   	  			  	
        scores = np.zeros((epochs,1)) 		
        rar_list = np.zeros((epochs,1))	  		 			 	 	 		 		 	  		   	  			  	
        for epoch in range(1,epochs+1): 			  		 			 	 	 		 		 	  		   	  			  	
            total_reward = 0 			  		 			 	 	 		 		 	  		   	  			  	
            #data = map.copy() 			  		 			 	 	 		 		 	  		   	  			  	
            #robopos = startpos
            holding = 0 			  		 			 	 	 		 		 	  		   	  			  	
            state = self.discretize(indicators[0],holding) #convert the location to a state 			  		 			 	 	 		 		 	  		   	  			  	
            action = self.learner.querysetstate(state) - 1 #set the state and get first action 			  		 			 	 	 		 		 	  		   	  			  	
            n = 0 			  		 			 	 	 		 		 	  		   	  			  	
            while (n < len(prices)-1) & (n<10000): 
                
                indicator_prime = indicators[n+1]
                delt_p = prices[n+1]-prices[n]
                
                # consider repeat three times for action
                if action == 0:
                    r = delt_p*holding
                elif action == -1 :
                    if holding < 0:
                        r = delt_p*holding
                    elif holding > 0:
                        r = delt_p*(-1) - self.impact*prices[n]                        
                    else:# holding == 0:  
                        r = 0.5*(delt_p*(-1) - self.impact*prices[n])                        
                    holding = -1
                else:# action == 1 :
                    if holding > 0:
                        r = delt_p*holding
                    elif holding < 0:
                        r = delt_p*(1) - self.impact*prices[n]
                    else:# holding == 0:
                        r = 0.5*(delt_p*(1) - self.impact*prices[n])	
                    holding = 1                        		  		 			 	 	 		 		 	  		   	  			  	
 			  		 			 	 	 		 		 	  		   	  			  	
                state = self.discretize(indicator_prime, holding) 	
		  		 			 	 	 		 		 	  		   	  			  	
                action = self.learner.query(state,r) - 1 	
                
                if (holding > 0) & (action > 0):
                    action = 0
                    self.learner.a = 0 + 1
                if (holding < 0) & (action < 0):
                    action = 0
                    self.learner.a = 0 + 1                 	  		 			 	 	 		 		 	  		   	  			  	
     			  		 			 		 		 		 	  		   	  			  	
                stepreward = r 			  		 			 	 	 		 		 	  		   	  			  	
                total_reward += stepreward 			  		 			 	 	 		 		 	  		   	  			  	
                n = n + 1 			  		 			 	 	 		 		 	  		   	  			  	
            if n == 100000: 			  		 			 	 	 		 		 	  		   	  			  	
                print "timeout" 			  		 			 	 	 		 		 	  		   	  			  	
            #if verbose: printmap(data) 			  		 			 	 	 		 		 	  		   	  			  	
            if self.verbose: print epoch, total_reward 			  		 			 	 	 		 		 	  		   	  			  	
            scores[epoch-1,0] = total_reward
            rar_list[epoch-1,0] = self.learner.rar 
            if self.verbose: 
                print 'learner.rar'
                print(self.learner.rar)
                print 'learner.Q'
                print(self.learner.Q) 
            if (epoch >= 20) & (abs(total_reward-scores[epoch-2,0]) / total_reward < 0.001): #converge
                break
    	if self.verbose:
            print scores.flatten()
            print rar_list.flatten()
        return 0