Ejemplo n.º 1
0
    def discretize(self,
                   symbol="IBM",
                   sd=dt.datetime(2008, 1, 1),
                   ed=dt.datetime(2009, 12, 31)):
        syms = [symbol]
        dates = pd.date_range(sd, ed)
        indicator_dates = pd.date_range(sd - dt.timedelta(days=28), ed)
        prices_all = ut.get_data(syms, dates)  # automatically adds SPY
        prices = prices_all[syms]  # only portfolio symbols
        indicator_prices_all = ut.get_data(
            syms, indicator_dates
        )  #will this be the same size as the number of days traded?
        indicator_prices = indicator_prices_all[syms]
        prices_SPY = prices_all["SPY"]  # only SPY, for comparison later

        #get 3 indicators using passed in start date minus window required for each
        rm = ind.get_rolling_mean(indicator_prices[symbol], window=20)
        percent_bb = ind.get_rolling_std(indicator_prices[symbol],
                                         window=20)  #aka rstd
        upper_band, lower_band = ind.get_bollinger_bands(rm, percent_bb)

        percent_bb = percent_bb.iloc[19:]
        rm = rm.iloc[19:]
        upper_band = upper_band.iloc[19:]
        lower_band = lower_band.iloc[19:]
        for x in range(rm.size):  #did i format that right??
            #print(prices.iloc[x])
            #print(type(rm.iloc[x]))
            if (prices.iloc[x][0] == rm.iloc[x]):
                percent_bb.iloc[x] = 0.0
            elif (prices.iloc[x][0] > rm.iloc[x]):
                percent_bb.iloc[x] = (prices.iloc[x][0] - rm.iloc[x]) / (
                    upper_band.iloc[x] - rm.iloc[x])
            elif (prices.iloc[x][0] < rm.iloc[x]):
                percent_bb.iloc[x] = (rm.iloc[x] - prices.iloc[x][0]) / (
                    lower_band.iloc[x] - rm.iloc[x])

        percent_bb = pd.Series(percent_bb)
        #print(percent_bb)
        indicator_prices = indicator_prices.iloc[9:]
        #print(indicator_prices)
        momentum = pd.Series(
            ind.get_momentum(indicator_prices[symbol], window=10))
        momentum = momentum.iloc[10:]
        #print(momentum)
        volatility = pd.Series(
            ind.get_volatility(indicator_prices[symbol], window=10))
        volatility = volatility.iloc[10:]
        #print(volatility.values.shape)

        uglystupidnobodylikesyouvol, vol = pd.qcut(volatility.values,
                                                   10,
                                                   retbins=True)
        esrfergs, momnt = pd.qcut(momentum.values, 10, retbins=True)
        rgefsegrf, bb = pd.qcut(percent_bb.values, 10, retbins=True)
        vol = vol[1:-1]
        momnt = momnt[1:-1]
        bb = bb[1:-1]

        return vol, momnt, bb
Ejemplo n.º 2
0
    def get_features1(self, prices, symbol, print=False):
        '''
        Compute technical indicators and use them as features to be fed
        into a Q-learner.
        :param: prices: Adj Close prices dataframe
        :param: Whether adding data for printing to dataframe or not
        :return: Normalize Features dataframe
        '''

        # Fill NAN values if any
        prices.fillna(method="ffill", inplace=True)
        prices.fillna(method="bfill", inplace=True)
        prices.fillna(1.0, inplace=True)

        # Price
        adj_close = prices[symbol]

        # Compute Momentum
        mom = get_momentum(adj_close, window=10)

        # Compute RSI
        rsi = get_RSI(adj_close)

        # Compute rolling mean
        rm = get_rolling_mean(adj_close, window=10)

        # Compute rolling standard deviation
        rstd = get_rolling_std(adj_close, window=10)

        # Compute upper and lower bands
        upper_band, lower_band = get_bollinger_bands(rm, rstd)



        # Compute SMA
        sma = get_sma_indicator(adj_close, rm)

        df = prices.copy()
        df['Momentum'] = mom
        df['Adj. Close/SMA'] = adj_close/sma
        df['middle_band'] = rm

        # Delete 'Adj Close' column
        del df[symbol]

        if set(['cash']).issubset(df.columns):
            del df['cash']

        # Normalize dataframe
        df_norm = normalize_data(df)

        # Add Adj Close, Bollinrt Bands and RSY if printing
        if print:
            df_norm['Adj Close'] = prices[symbol]
            df_norm['upper_band'] = upper_band
            df_norm['lower_band'] = lower_band
            df_norm['middle_band'] = rm
            df_norm['RSI'] = rsi



        # Drop NaN
        df_norm.dropna(inplace=True)

        return df_norm
def usePossibleStrategy(df_prices, symbols, sd, ed, start_val):

    orders = []
    short_Xs = []
    long_Xs = []
    trade_num = 1000
    shares_min = -1000
    shares_max = 1000

    # rm = get_rolling_mean(df_prices['SPY'], window=20)
    # rstd = get_rolling_std(df_prices['SPY'], window=20)
    #
    # market = get_bollinger_bands(rm, rstd)
    # market['SPY'] = df_prices['SPY']
    # market = market.fillna(method='bfill')
    # market = market.fillna(method='ffill')

    # Get the Rolling Mean
    rm = get_rolling_mean(df_prices['JPM'], window=10)

    # Get the Rolling Standard Deviation
    rstd = get_rolling_std(df_prices['JPM'], window=10)
    bband = get_bollinger_bands(rm, rstd)
    bband[symbols] = df_prices[symbols]
    bband = bband.fillna(method='bfill')
    current_number_of_shares = 0

    for i in range(1, bband.shape[0]):
        if (bband.iloc[i - 1][symbols] > bband.iloc[i - 1]['Upper'])[0] and (
                bband.iloc[i][symbols] < bband.iloc[i]['Upper']
        )[0] and current_number_of_shares - trade_num >= shares_min:
            short_Xs.append([bband.index[i]])
            orders.append([bband.index[i], symbols, 'SELL', trade_num])
            current_number_of_shares -= trade_num
        elif (bband.iloc[i - 1][symbols] < bband.iloc[i - 1]['Lower'])[0] and (
                bband.iloc[i][symbols] > bband.iloc[i]['Lower']
        )[0] and current_number_of_shares + trade_num <= shares_max:
            long_Xs.append([bband.index[i]])
            orders.append([bband.index[i], symbols, 'BUY', trade_num])
            current_number_of_shares += trade_num

    orders.append([ed - dt.timedelta(days=1), symbols, 'BUY', 0])
    df_trades = pd.DataFrame(orders,
                             columns=['Date', 'Symbol', 'Order', 'Shares'])
    df_shortXs = pd.DataFrame(short_Xs, columns=['Shorts'])
    df_longXs = pd.DataFrame(long_Xs, columns=['Longs'])

    df_benchmark_orders = pd.DataFrame(
        [[min(df_prices.index), symbols, 'BUY', 1000],
         [max(df_prices.index), symbols, 'BUY', 0]],
        columns=['Date', 'Symbol', 'Order', 'Shares'])
    df_ms_value = get_portfolio_value(df_prices,
                                      df_trades,
                                      start_val,
                                      commission=9.95,
                                      impact=0.005)
    df_benchmark_value = get_portfolio_value(df_prices,
                                             df_benchmark_orders,
                                             start_val,
                                             commission=9.95,
                                             impact=0.005)

    fig, ax = plt.subplots()
    ax.set_title('Manual vs Benchmark Strategy', fontsize=20)
    ax.plot(df_ms_value.index,
            df_ms_value / df_ms_value.ix[0],
            color='black',
            label='Manual Strategy')
    ax.plot(df_benchmark_value.index,
            df_benchmark_value / df_benchmark_value.ix[0],
            color='blue',
            label='Benchmark Strategy')
    ax.legend(loc='best')
    plt.xticks(rotation=45)
    for i in range(0, df_shortXs.shape[0]):
        plt.axvline(x=pd.to_datetime(df_shortXs.iloc[i]['Shorts']),
                    color='red')
    for i in range(0, df_longXs.shape[0]):
        plt.axvline(x=pd.to_datetime(df_longXs.iloc[i]['Longs']),
                    color='green')
    plt.show()

    avg_daily_ret, std_daily_ret, sharpe_ratio, cum_ret = get_stats(
        df_ms_value)

    # Comparative Analysis Stuff
    print('Manual Strategy Performance Data; ')
    print('Cumulative Return of Fund: {}'.format(cum_ret))
    print('Standard Deviation of Fund: {}'.format(std_daily_ret))
    print('Average Daily Return of Fund: {}\n'.format(avg_daily_ret))

    return df_trades, df_shortXs, df_longXs, bband
Ejemplo n.º 4
0
	def testPolicy(self, symbol = "AAPL", sd=dt.datetime(2010,1,1), ed=dt.datetime(2011,12,31), sv = 100000):
		start_date = sd
		end_date = ed
		dates = pd.date_range(start_date, end_date)
		symbols = symbol
		window = 20
		df = get_data([symbols],dates)
		df = df.fillna(method='ffill')
		df = df.fillna(method='bfill')
				
		rm = get_rolling_mean(df[symbols],window)
		rstd = get_rolling_std(df[symbols],window)
		upper_band, lower_band = get_bollinger_bands(rm,rstd)

		SMA = get_rolling_mean(df[symbols],window)
		BB = get_bollinger_value(df[symbols],window)
		Momentum = get_momentum_value(df[symbols],window)
		mom_max = Momentum.max()
		mom_min = Momentum.min()
		#print mom_max,mom_min
		
		nethold = 0
		share = []
		date = []
		position = 0 # -1,0,1 stand for short, out, long
		
		for i in range(len(upper_band.index)):
			if df[symbols][i-1] > upper_band.loc[upper_band.index[i-1]] and df[symbols][i] < upper_band.loc[upper_band.index[i]] and nethold != -1000:
				share.append(-1000-nethold)
				date.append(df.index[i])
				nethold = -1000
				position = -1
				self.short_entry.append(df.index[i])
				
			elif df[symbols][i-1] > rm.loc[upper_band.index[i-1]] and df[symbols][i] < rm.loc[upper_band.index[i]] and nethold == -1000:
				share.append(1000)
				date.append(df.index[i])
				nethold = 0 
				position = 0
			
			elif df[symbols][i-1] < lower_band.loc[upper_band.index[i-1]] and df[symbols][i] > lower_band.loc[upper_band.index[i]] and nethold != 1000:
				share.append(1000-nethold)
				date.append(df.index[i])
				nethold = 1000
				position = 1
				self.long_entry.append(df.index[i])
		
			elif df[symbols][i-1] < rm.loc[upper_band.index[i-1]] and df[symbols][i] > rm.loc[upper_band.index[i]] and nethold == 1000:
				share.append(-1000)
				date.append(df.index[i])
				nethold = 0
				position = 0
			
			elif Momentum.loc[upper_band.index[i]] > 0.6 and nethold != 1000:
				share.append(1000-nethold)
				date.append(df.index[i])
				nethold = 1000
				position = 1
				self.long_entry.append(df.index[i])
				
			elif Momentum.loc[upper_band.index[i]] < -0.5 and nethold != -1000:
				share.append(-1000-nethold)
				date.append(df.index[i])
				nethold = -1000
				position = -1
				self.short_entry.append(df.index[i])

		if nethold != 0:
			share.append(-nethold)
			date.append(df.index[len(df.index)-1])
			
		df_trades = pd.DataFrame(data = share, index = date, columns = ['orders'])
		#print df_trades
		return df_trades
Ejemplo n.º 5
0
def showvalues():
    # Specify the start and end dates for this period.
    start_d = dt.date(2008, 1, 1)
    #end_d = dt.datetime(2018, 10, 30)
    yesterday = dt.date.today() - dt.timedelta(1)

    # Get portfolio values from Yahoo
    symbol = request.form['symbol']
    portf_value = fetchOnlineData(start_d, yesterday, symbol)

    # ****Stock prices chart****
    plot_prices = plot_stock_prices(portf_value.index,
                                    portf_value['Adj Close'], symbol)

    # ****Momentum chart****
    # Normalize the prices Dataframe
    normed = pd.DataFrame()
    normed['Adj Close'] = portf_value['Adj Close'].values / portf_value[
        'Adj Close'].iloc[0]

    # Compute momentum
    sym_mom = get_momentum(normed['Adj Close'], window=10)

    # Create momentum chart
    plot_mom = plot_momentum(portf_value.index, normed['Adj Close'], sym_mom,
                             "Momentum Indicator", (12, 8))

    # ****Bollinger Bands****
    # Compute rolling mean
    rm_JPM = get_rolling_mean(portf_value['Adj Close'], window=10)

    # Compute rolling standard deviation
    rstd_JPM = get_rolling_std(portf_value['Adj Close'], window=10)

    # Compute upper and lower bands
    upper_band, lower_band = get_bollinger_bands(rm_JPM, rstd_JPM)

    # Plot raw symbol values, rolling mean and Bollinger Bands
    dates = pd.date_range(start_d, yesterday)
    plot_boll = plot_bollinger(dates,
                               portf_value.index,
                               portf_value['Adj Close'],
                               symbol,
                               upper_band,
                               lower_band,
                               rm_JPM,
                               num_std=1,
                               title="Bollinger Indicator",
                               fig_size=(12, 6))

    # ****Simple moving average (SMA)****
    # Compute SMA
    sma_JPM, q = get_sma(normed['Adj Close'], window=10)

    # Plot symbol values, SMA and SMA quality
    plot_sma = plot_sma_indicator(dates, portf_value.index,
                                  normed['Adj Close'], symbol, sma_JPM, q,
                                  "Simple Moving Average (SMA)")

    # ****Relative Strength Index (RSI)****
    # Compute RSI
    rsi_value = get_RSI(portf_value['Adj Close'])

    # Plot RSI
    plot_rsi = plot_rsi_indicator(dates,
                                  portf_value.index,
                                  portf_value['Adj Close'],
                                  symbol,
                                  rsi_value,
                                  window=14,
                                  title="RSI Indicator",
                                  fig_size=(12, 6))

    # Session variables
    session['start_val'] = request.form['start_val']
    session['symbol'] = request.form['symbol']
    session['start_d'] = start_d.strftime('%Y/%m/%d')
    session['num_shares'] = request.form['num_shares']
    session['commission'] = request.form['commission']
    session['impact'] = request.form['impact']

    return render_template(
        # name of template
        "stockpriceschart.html",

        # now we pass in our variables into the template
        start_val=request.form['start_val'],
        symbol=request.form['symbol'],
        commission=request.form['commission'],
        impact=request.form['impact'],
        num_shares=request.form['num_shares'],
        start_date=start_d,
        end_date=yesterday,
        tables=[portf_value.to_html(classes=symbol)],
        titles=['na', 'Stock Prices '],
        div_placeholder_stock_prices=Markup(plot_prices),
        div_placeholder_momentum=Markup(plot_mom),
        div_placeholder_bollinger=Markup(plot_boll),
        div_placeholder_sma=Markup(plot_sma),
        div_placeholder_rsi=Markup(plot_rsi))
Ejemplo n.º 6
0
    def add_evidence(  #this is where you could use the inspiration for the grading function from project 7  		  		  		    	 		 		   		 		  
            self,
            symbol="JPM",
            sd=dt.datetime(2008, 1, 1),
            ed=dt.datetime(2009, 1, 1),
            sv=10000,
    ):
        """  		  	   		     		  		  		    	 		 		   		 		  
        Trains your strategy learner over a given time frame.  		  	   		     		  		  		    	 		 		   		 		  
  		  	   		     		  		  		    	 		 		   		 		  
        :param symbol: The stock symbol to train on  		  	   		     		  		  		    	 		 		   		 		  
        :type symbol: str  		  	   		     		  		  		    	 		 		   		 		  
        :param sd: A datetime object that represents the start date, defaults to 1/1/2008  		  	   		     		  		  		    	 		 		   		 		  
        :type sd: datetime  		  	   		     		  		  		    	 		 		   		 		  
        :param ed: A datetime object that represents the end date, defaults to 1/1/2009  		  	   		     		  		  		    	 		 		   		 		  
        :type ed: datetime  		  	   		     		  		  		    	 		 		   		 		  
        :param sv: The starting value of the portfolio  		  	   		     		  		  		    	 		 		   		 		  
        :type sv: int  		  	   		     		  		  		    	 		 		   		 		  
        """

        # add your code to do learning here
        syms = [symbol]
        dates = pd.date_range(sd, ed)
        indicator_dates = pd.date_range(sd - dt.timedelta(days=28), ed)
        prices_all = ut.get_data(syms, dates)  # automatically adds SPY
        prices = prices_all[syms]  # only portfolio symbols
        indicator_prices_all = ut.get_data(
            syms, indicator_dates
        )  #will this be the same size as the number of days traded?
        indicator_prices = indicator_prices_all[syms]
        prices_SPY = prices_all["SPY"]  # only SPY, for comparison later

        #get 3 indicators using passed in start date minus window required for each
        rm = ind.get_rolling_mean(indicator_prices[symbol], window=20)
        percent_bb = ind.get_rolling_std(indicator_prices[symbol],
                                         window=20)  #aka rstd
        upper_band, lower_band = ind.get_bollinger_bands(rm, percent_bb)

        percent_bb = percent_bb.iloc[19:]
        rm = rm.iloc[19:]
        upper_band = upper_band.iloc[19:]
        lower_band = lower_band.iloc[19:]
        for x in range(rm.size):  #did i format that right??
            #print(prices.iloc[x])
            #print(type(rm.iloc[x]))
            if (prices.iloc[x][0] == rm.iloc[x]):
                percent_bb.iloc[x] = 0.0
            elif (prices.iloc[x][0] > rm.iloc[x]):
                percent_bb.iloc[x] = (prices.iloc[x][0] - rm.iloc[x]) / (
                    upper_band.iloc[x] - rm.iloc[x])
            elif (prices.iloc[x][0] < rm.iloc[x]):
                percent_bb.iloc[x] = (rm.iloc[x] - prices.iloc[x][0]) / (
                    lower_band.iloc[x] - rm.iloc[x])

        percent_bb = pd.Series(percent_bb)
        #print(percent_bb)
        indicator_prices = indicator_prices.iloc[9:]
        #print(indicator_prices)
        momentum = pd.Series(
            ind.get_momentum(indicator_prices[symbol], window=10))
        momentum = momentum.iloc[10:]
        #print(momentum)
        volatility = pd.Series(
            ind.get_volatility(indicator_prices[symbol], window=10))
        volatility = volatility.iloc[10:]
        #print(volatility.values.shape)

        uglystupidnobodylikesyouvol, vol = pd.qcut(volatility.values,
                                                   10,
                                                   retbins=True)
        esrfergs, momnt = pd.qcut(momentum.values, 10, retbins=True)
        rgefsegrf, bb = pd.qcut(percent_bb.values, 10, retbins=True)
        vol = vol[1:-1]
        momnt = momnt[1:-1]
        bb = bb[1:-1]
        #print(volatility.iloc[0])
        #print(vol)
        #print(volatility.iloc[0] in vol[0])

        total_reward = 0
        performance_list = np.zeros(10)
        for z in range(10):
            cash_value = float(sv)
            stock_value = 0.0
            portfolio_value = cash_value + stock_value
            #figure out state
            vol_set = False
            mo_set = False
            bb_set = False
            vol_bin = ""
            mo_bin = ""
            bb_bin = ""

            for x in range(vol.size):
                if (volatility.iloc[0] <= vol[x] and vol_set == False):

                    vol_bin = str(x)
                    vol_set = True

                elif (x == vol.size - 1 and vol_set == False):
                    vol_bin = str(x + 1)
                    vol_set = True
                if (momentum.iloc[0] <= momnt[x] and mo_set == False):
                    mo_bin = str(x)
                    mo_set = True
                elif (x == momnt.size - 1 and mo_set == False):
                    mo_bin = str(x + 1)
                    mo_set = True
                if (percent_bb.iloc[0] <= bb[x] and bb_set == False):
                    bb_bin = str(x)
                    bb_set = True
                elif (x == bb.size - 1 and bb_set == False):
                    bb_bin = str(x + 1)
                    bb_set = True

            #print(volatility.iloc[0])
            #position = 0   actually this could be action variable below
            final_str = vol_bin + mo_bin + bb_bin
            state = int(final_str)
            #print(state)
            action = self.learner.querysetstate(state) - 1
            old_action = action
            same_action = False
            count = 0
            stock_value = action * 1000.0 * prices.iloc[0]
            cash_value -= stock_value

            #print("Daily Value for end of Day 1" + ": " + str(portfolio_value))
            for x in range(
                    1, prices.size
            ):  #while the trading simualtion hasnt reached last day

                #at this point assume day 1 or x is over and now calculate the reward of the prior day's
                #decision to pass in next action call as r. also calculate the
                # holdings of the bot(maybe just check the action variable for this)
                daily_gain = (prices.values[x, 0] - prices.values[x - 1, 0]
                              ) / prices.values[x - 1,
                                                0]  #daily percentage gain

                #print(type(daily_gain))
                portfolio_value = (
                    (1.0 + daily_gain) * stock_value) + cash_value
                #print(portfolio_value)

                if (action == 1):
                    if (daily_gain - self.impact > 0.02):
                        reward = 35.0
                    elif (daily_gain - self.impact > 0.012):
                        reward = 20.0
                    elif (daily_gain - self.impact > 0.004):
                        reward = 7.5
                    elif (daily_gain - self.impact < -0.019):
                        reward = -25.0
                    elif (daily_gain - self.impact < -0.009):
                        reward = -10.0
                    else:
                        reward = daily_gain * 10.0
                elif (action == 0):
                    reward = abs(daily_gain - self.impact) * 3.5
                elif (action == -1):
                    if (daily_gain - self.impact < -0.026):
                        reward = 35.0
                    elif (daily_gain - self.impact < -0.015):
                        reward = 20.0
                    elif (daily_gain - self.impact < -0.004):
                        reward = 7.5
                    elif (daily_gain - self.impact > 0.019):
                        reward = -25.0
                    elif (daily_gain - self.impact > 0.007):
                        reward = -10.0
                    else:
                        reward = daily_gain * -10.0

                if (same_action == True):
                    reward += (7.5 + self.impact)
                #determine state
                vol_set = False
                mo_set = False
                bb_set = False
                vol_bin = ""
                mo_bin = ""
                bb_bin = ""
                for y in range(vol.size):
                    if (volatility.iloc[x] <= vol[y] and vol_set == False):
                        #print(x)
                        vol_bin = str(y)
                        vol_set = True
                    elif (y == vol.size - 1 and vol_set == False):
                        vol_bin = str(y + 1)
                        vol_set = True
                    if (momentum.iloc[x] <= momnt[y] and mo_set == False):
                        mo_bin = str(y)
                        mo_set = True
                    elif (y == momnt.size - 1 and mo_set == False):
                        mo_bin = str(y + 1)
                        mo_set = True
                    if (percent_bb.iloc[x] <= bb[y] and bb_set == False):
                        bb_bin = str(y)
                        bb_set = True
                    elif (y == bb.size - 1 and bb_set == False):
                        bb_bin = str(y + 1)
                        bb_set = True
                state = int(vol_bin + mo_bin + bb_bin)
                old_action = action
                action = self.learner.query(state, reward) - 1
                same_action = False

                if (old_action == action and reward > 0.0):
                    same_action = True
                stock_value = action * 1000.0 * prices.iloc[x]
                cash_value = portfolio_value - stock_value
                total_reward += reward

                #print("Daily Value for end of Day " + str(x+1) + ": " + str(portfolio_value))
            #print("Ending value for test phase " + str(z+1) + ": " + str(portfolio_value))
            performance_list[z] = portfolio_value
        mat.pyplot.plot(performance_list)
Ejemplo n.º 7
0
    def testPolicy(
            self,
            symbol="IBM",
            sd=dt.datetime(2008, 1, 1),
            ed=dt.datetime(2009, 1, 31),
            sv=10000,
    ):
        """  		  	   		     		  		  		    	 		 		   		 		  
        Tests your learner using data outside of the training data  		  	   		     		  		  		    	 		 		   		 		  
  		  	   		     		  		  		    	 		 		   		 		  
        :param symbol: The stock symbol that you trained on on  		  	   		     		  		  		    	 		 		   		 		  
        :type symbol: str  		  	   		     		  		  		    	 		 		   		 		  
        :param sd: A datetime object that represents the start date, defaults to 1/1/2008  		  	   		     		  		  		    	 		 		   		 		  
        :type sd: datetime  		  	   		     		  		  		    	 		 		   		 		  
        :param ed: A datetime object that represents the end date, defaults to 1/1/2009  		  	   		     		  		  		    	 		 		   		 		  
        :type ed: datetime  		  	   		     		  		  		    	 		 		   		 		  
        :param sv: The starting value of the portfolio  		  	   		     		  		  		    	 		 		   		 		  
        :type sv: int  		  	   		     		  		  		    	 		 		   		 		  
        :return: A DataFrame with values representing trades for each day. Legal values are +1000.0 indicating  		  	   		     		  		  		    	 		 		   		 		  
            a BUY of 1000 shares, -1000.0 indicating a SELL of 1000 shares, and 0.0 indicating NOTHING.  		  	   		     		  		  		    	 		 		   		 		  
            Values of +2000 and -2000 for trades are also legal when switching from long to short or short to  		  	   		     		  		  		    	 		 		   		 		  
            long so long as net holdings are constrained to -1000, 0, and 1000.  		  	   		     		  		  		    	 		 		   		 		  
        :rtype: pandas.DataFrame  		

        """
        syms = [symbol]
        dates = pd.date_range(sd, ed)
        indicator_dates = pd.date_range(sd - dt.timedelta(days=28), ed)
        prices_all = ut.get_data(syms, dates)  # automatically adds SPY
        prices = prices_all[syms]  # only portfolio symbols
        indicator_prices_all = ut.get_data(
            syms, indicator_dates
        )  #will this be the same size as the number of days traded?
        indicator_prices = indicator_prices_all[syms]
        prices_SPY = prices_all["SPY"]  # only SPY, for comparison later

        rm = ind.get_rolling_mean(indicator_prices[symbol], window=20)
        percent_bb = ind.get_rolling_std(indicator_prices[symbol],
                                         window=20)  #aka rstd
        upper_band, lower_band = ind.get_bollinger_bands(rm, percent_bb)

        percent_bb = percent_bb.iloc[19:]
        rm = rm.iloc[19:]
        upper_band = upper_band.iloc[19:]
        lower_band = lower_band.iloc[19:]
        for x in range(rm.size):  #did i format that right??
            #print(prices.iloc[x])
            #print(type(rm.iloc[x]))
            if (prices.iloc[x][0] == rm.iloc[x]):
                percent_bb.iloc[x] = 0.0
            elif (prices.iloc[x][0] > rm.iloc[x]):
                percent_bb.iloc[x] = (prices.iloc[x][0] - rm.iloc[x]) / (
                    upper_band.iloc[x] - rm.iloc[x])
            elif (prices.iloc[x][0] < rm.iloc[x]):
                percent_bb.iloc[x] = (rm.iloc[x] - prices.iloc[x][0]) / (
                    lower_band.iloc[x] - rm.iloc[x])

        percent_bb = pd.Series(percent_bb)
        #print(percent_bb)
        indicator_prices = indicator_prices.iloc[9:]
        #print(indicator_prices)
        momentum = pd.Series(
            ind.get_momentum(indicator_prices[symbol], window=10))
        momentum = momentum.iloc[10:]
        #print(momentum)
        volatility = pd.Series(
            ind.get_volatility(indicator_prices[symbol], window=10))
        volatility = volatility.iloc[10:]
        # 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
        trades = prices_all[[
            symbol,
        ]]  # only portfolio symbols
        trades_SPY = prices_all["SPY"]  # only SPY, for comparison later
        trades.values[:, :] = 0  # set them all to nothing
        vol, momnt, bb = self.discretize(symbol)
        #print(volatility.iloc[0])
        #print(vol)
        vol_bin = ""
        mo_bin = ""
        bb_bin = ""

        holdings = 0
        for x in range(trades.size):
            vol_set = False
            mo_set = False
            bb_set = False
            for y in range(vol.size):
                if (volatility.iloc[x] <= vol[y] and vol_set == False):
                    #print(x)
                    vol_bin = str(y)
                    vol_set = True
                elif (y == vol.size - 1 and vol_set == False):
                    vol_bin = str(y + 1)
                    vol_set = True
                if (momentum.iloc[x] <= momnt[y] and mo_set == False):
                    mo_bin = str(y)
                    mo_set = True
                elif (y == momnt.size - 1 and mo_set == False):
                    mo_bin = str(y + 1)
                    mo_set = True
                if (percent_bb.iloc[x] <= bb[y] and bb_set == False):
                    bb_bin = str(y)
                    bb_set = True
                elif (y == bb.size - 1 and bb_set == False):
                    bb_bin = str(y + 1)
                    bb_set = True
            state = int(vol_bin + mo_bin + bb_bin)
            #print(state)
            action = self.learner.querysetstate(state) - 1
            #print(action)
            if (action == 1):
                if (holdings == 0):
                    trades.values[x, :] = 1000
                elif (holdings == -1000):
                    trades.values[x, :] = 2000
                elif (holdings == 1000):
                    trades.values[x, :] = 0
                holdings = 1000
            elif (action == 0):
                if (holdings == 0):
                    trades.values[x, :] = 0
                elif (holdings == -1000):
                    trades.values[x, :] = 1000
                elif (holdings == 1000):
                    trades.values[x, :] = -1000
                holdings = 0
            elif (action == -1):
                if (holdings == 0):
                    trades.values[x, :] = -1000
                elif (holdings == -1000):
                    trades.values[x, :] = 0
                elif (holdings == 1000):
                    trades.values[x, :] = -2000
                holdings = -1000
        #print(trades)
        if self.verbose:
            print(type(trades))  # it better be a DataFrame!
        if self.verbose:
            print(trades)
        if self.verbose:
            print(prices_all)
        #print(trades)
        return trades
Ejemplo n.º 8
0
def bollinger_bands_test():
    data = load_data(DATA_STOCK)
    bollinger_band = get_bollinger_bands(data["close"], 20, 20, 2)
    print(bollinger_band)