Пример #1
0
 def testPolicy(self,
                symbol='JPM',
                sd=dt.datetime(2008, 1, 1),
                ed=dt.datetime(2009, 12, 31),
                sv=100000):
     # Prepare symbols and dates to retrieve data
     symbols = [symbol]
     dates = pd.date_range(sd, ed)
     data = util.get_data(symbols, dates, addSPY=False, colname='Adj Close')
     data.dropna(axis=0, how='all', inplace=True)
     # Get indicators
     SMA = ind.calculate_SMA(data, period=20)
     EMA = ind.calculate_EMA(data, period=20)
     ROC = ind.calculate_ROC(data, period=22)
     Bollinger = ind.calculate_Bollinger(data, period=16, dev=2)
     Stochastic = ind.calculate_Stochastic(data, lookback=20, smooth=3)
     MACD = ind.calculate_MACD(data)
     # Get BB% signal
     sig_BB = self.get_BB_signal(data, SMA, Bollinger, lower=0, upper=1)
     # Get stochastic signal
     sig_stochastic = self.get_stochastic_signal(Stochastic,
                                                 lower=0.2,
                                                 upper=0.8)
     # Get the momentum
     sig_momentum = self.get_momentum(ROC, threshold=0)
     # Get MACd signal
     sig_MACD = self.get_MACD_signal(MACD)
     #
     sig_composite = self.get_composite_signal(sig_momentum, sig_BB,
                                               sig_stochastic)
     df_trades = self.generate_trade_from_signal(sig_composite)
     return df_trades
Пример #2
0
    def testPolicy(self, symbol = "IBM", \
        sd=dt.datetime(2009,1,1), \
        ed=dt.datetime(2010,1,1), \
        sv = 10000):

        steps = 100

        dates = pd.date_range(sd, ed)

        prices = calculate_prices([symbol],sd,ed)
        sma = calculate_SMA([symbol],sd,ed)
        orders_df = prices.copy().drop([symbol],axis=1).assign(Shares = 0)

        bbval = checkBBVal(prices,sma,calculate_volatility([symbol],sd,ed))
        bbval = bbval.fillna(0)
        disc_thresholds = []

        current_holdings = 0

        ## discretize our indicator using prof's method
        bb_copy = bbval.copy()
        stepsize = bbval.shape[0]/steps
        bbval_sorted = bb_copy.sort_values(bb_copy.columns[0],ascending=True)

        bb_copy = bbval.copy()
        

        for x in range(0,steps - 1):
            y = x + 1
            # print bbval_sorted.iloc[y*stepsize][0]
            disc_thresholds.append(bbval_sorted.iloc[y*stepsize][0])

        discretized_bb = np.digitize(bb_copy.values,disc_thresholds)

        checkdate = 0
        holdings = 0
        for index, row in orders_df.iterrows():
            state = discretized_bb[checkdate][0]
            action = self.learner.querysetstate(state)
            if checkdate == 0:
                    orders_df.loc[index] = -1000
                    holdings = -1000
                    
            else:
                if action == 0 and holdings > -1000:
                    self.tradecount+=1
                    orders_df.loc[index] = -2000
                    holdings = -1000
                elif action == 1:
                    orders_df.loc[index] = 0
                elif action == 2 and holdings < 1000:
                    self.tradecount+=1
                    orders_df.loc[index] = 2000
                    holdings = 1000
            # print holdings
            checkdate+=1
        orders_df.columns = [symbol]
        return orders_df
Пример #3
0
def testPolicy(symbol = 'JPM', sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv = 100000):
    prices = calculate_prices([symbol],sd,ed)
    orders_df = prices.copy().drop([symbol],axis=1).assign(Shares = 0)

    sma = calculate_SMA([symbol],sd, ed)
    std = calculate_volatility([symbol],sd,ed)

    current_holdings = 0
    for i in range(0,len(prices.values[:,0])):

      if(i==0):
        
        orders_df.loc[orders_df.index[i],'Shares'] = -1000
        current_holdings = -1000
        
        #do nothing in this iteration
      elif(i >= 21):
        #get current price, current sma, and current std for equation
        curr_price = prices.loc[prices.index[i],'JPM']
        curr_sma = sma.loc[sma.index[i], 'JPM']
        curr_std = std.loc[std.index[i], 'JPM']

        bb_val = checkBBVal(curr_price,curr_sma,curr_std)
        
        if(bb_val < -1):
          if(current_holdings < 1000  and curr_std < 2):
            
            orders_df.loc[orders_df.index[i],'Shares'] = 2000
            current_holdings += 2000
            
            # matplotlib.pyplot.axvline(x=orders_df.index[i], color='g', linestyle='--')
          
        elif(bb_val > 1):
          if(current_holdings > -1000  and curr_std < 2):
            
            orders_df.loc[orders_df.index[i],'Shares'] = -2000
            current_holdings -= 2000
            
            # matplotlib.pyplot.axvline(x=orders_df.index[i], color='r', linestyle='--')
          
      else:
        
        orders_df.loc[orders_df.index[i],'Shares'] = 0
    #   print current_holdings
    #   print orders_df.index[i]
    # print orders_df
    orders_df.columns = [symbol]
    
    return orders_df
Пример #4
0
    def addEvidence(self, symbol = "IBM", \
        sd=dt.datetime(2008,1,1), \
        ed=dt.datetime(2009,1,1), \
        sv = 10000): 

        self.tradecount = 0
        # add your code to do learning here

        #pseudocode
        ## first day
          # calculate indicators, use querysetstate with the indicators
        ## rest of days
          # calculate reward
          # query q learner for action using indicators and reward
        
        steps = 100
        

        prices = calculate_prices([symbol],sd,ed)
        sma = calculate_SMA([symbol],sd,ed)
        
        orders_df = prices.copy().drop([symbol],axis=1).assign(Shares = 0)
        
        
        bbval = checkBBVal(prices,sma,calculate_volatility([symbol],sd,ed))
        bbval = bbval.fillna(0)
        disc_thresholds = []

        current_holdings = 0

        self.learner = ql.QLearner(num_states=steps, \
        num_actions = 3, \
        alpha = 0.2, \
        gamma = 0.9, \
        rar = 0.9, \
        radr = 0.99, \
        dyna = 0, \
        verbose = False)
        
        prices_ratio_1 = prices[:-1].values
        prices_ratio_2 = prices[1:]/prices_ratio_1
        dr = prices_ratio_2 - 1 # daily returns calculation

        ## discretize our indicator using prof's method
        bb_copy = bbval.copy()
        stepsize = bbval.shape[0]/steps
        bbval_sorted = bb_copy.sort_values(bb_copy.columns[0],ascending=True)

        bb_copy = bbval.copy()
        

        for x in range(0,steps - 1):
            y = x + 1
            # print bbval_sorted.iloc[y*stepsize][0]
            disc_thresholds.append(bbval_sorted.iloc[y*stepsize][0])

        count = 0
        # for i in bbval.itertuples():
            
            
        #     for j in disc_thresholds:
                    
        #         past_j = j - 1
        #         if i[1] < j[0] and i[1] >= past_j[0]:
        #             bb_copy.iloc[count] = j
        
        # print disc_thresholds
        discretized_bb = np.digitize(bb_copy.values,disc_thresholds)
        
        # print discretized_bb[0][0]
        epochs = 50
        current = 0
        
        while current < epochs:
            checkdate = 0
            holdings = 0
            for index, row in orders_df.iterrows():
                state = discretized_bb[checkdate][0]
                if checkdate == 0:
                    self.learner.querysetstate(state)
                    orders_df.loc[index] = -1000
                    holdings = -1000
                else:
                    reward_component = dr.loc[index][0] * holdings
                    reward = reward_component - self.impact*reward_component

                    action = self.learner.query(state,reward)
                    if action == 0 and holdings > -1000:
                        orders_df.loc[index] = -2000
                        holdings = -1000
                    elif action == 1:
                        orders_df.loc[index] = 0
                    elif action == 2 and holdings < 1000:
                        orders_df.loc[index] = 2000
                        holdings = 1000
                # print holdings    
                
                    # print action

                checkdate+=1
            current+=1

        # print orders_df
        return orders_df
Пример #5
0
    def testPolicy(self,
                   symbol="AAPL",
                   sd=dt.datetime(2008, 1, 1),
                   ed=dt.datetime(2009, 12, 31),
                   sv=100000):

        lookback = 14
        lkb_d = sd - dt.timedelta(lookback * 2)

        dates = pd.date_range(lkb_d, ed)
        symbols = []
        symbols.append(symbol)
        prices_all = util.get_data(symbols, dates, addSPY=True)

        prices_all.fillna(method='ffill', inplace=True)
        prices_all.fillna(method='bfill', inplace=True)
        prices_all['Future'] = prices_all[symbol].shift(-1)

        prices_sym = prices_all[symbols]

        sma, price_sma = ind.calculate_SMA(prices_sym,
                                           start_date=sd,
                                           end_date=ed)
        momemtum = ind.calculate_momemtum(prices_sym,
                                          start_date=sd,
                                          end_date=ed)
        upper, lower, bb = ind.calculate_BB(prices_sym,
                                            start_date=sd,
                                            end_date=ed)

        df_inds = price_sma.join(momemtum)
        df_inds = df_inds.join(bb)

        #util.plot_data(df_inds[dt.datetime(2009,1,1):dt.datetime(2009,3,1)+dt.timedelta(days=10)])

        df_trade = pd.DataFrame(data=0, index=sma.index, columns=symbols)

        current_holding = 0

        bb['previous'] = bb['BB'].shift(1)
        bb.ix[0, 'previous'] = bb.ix[0, 'BB']
        #print bb.head()

        for date, trade in df_trade.iterrows():

            signal = {'DN': 0, 'BUY': 0, 'SELL': 0}

            #BB
            '''
                if bb.loc[date,'BB'] > 1.0:
                    print 'date = '+str(date)+'BB = ' + str(bb.loc[date, 'BB']) +'BB Prev = ' + str(bb.loc[date, 'previous'])+ ' mome = ' + str(momemtum.loc[
                                                                                   date, 'Momemtum']) + ' p/sma = ' + str(
                        price_sma.loc[date, 'Price/SMA'])

                    if bb.loc[date,'previous'] > bb.loc[date,'BB']:#momemtum.loc[date,'Momemtum'] <= 0.2:#0.1
                        signal['SELL'] = signal['SELL'] + 0.6
                    else: signal['DN'] = signal['DN']+0.6
                elif bb.loc[date,'BB'] < -1.0:
                    print 'date = '+str(date)+'BB = ' + str(bb.loc[date, 'BB']) +'BB Prev = ' + str(bb.loc[date, 'previous'])+  ' mome = ' + str(momemtum.loc[
                        date, 'Momemtum']) + ' p/sma = ' + str(price_sma.loc[date, 'Price/SMA'])
                    if bb.loc[date,'previous'] < bb.loc[date,'BB']:#momemtum.loc[date,'Momemtum'] >= 0.2:#0.1
                        signal['BUY'] = signal['BUY'] + 0.6
                    else:
                        signal['DN'] = signal['DN'] + 0.6
                
                '''
            if bb.loc[date, 'previous'] > 1.0:

                if bb.loc[date, 'previous'] > bb.loc[
                        date,
                        'BB']:  # momemtum.loc[date,'Momemtum'] <= 0.2:#0.1
                    signal['SELL'] = signal['SELL'] + 0.6
                else:
                    signal['DN'] = signal['DN'] + 0.6
            elif bb.loc[date, 'previous'] < -1.0:

                if bb.loc[date, 'previous'] < bb.loc[
                        date,
                        'BB']:  # momemtum.loc[date,'Momemtum'] >= 0.2:#0.1
                    signal['BUY'] = signal['BUY'] + 0.6
                else:
                    signal['DN'] = signal['DN'] + 0.6

            else:
                signal['DN'] = signal['DN'] + 0.2  #0.2

            #BB+SMA_M

            #Price/SMA
            if price_sma.loc[date, 'Price/SMA'] >= 0.1:  #0.5:
                if price_sma.loc[date, 'Price/SMA'] >= 0.4:
                    signal['SELL'] = signal['SELL'] + 0.3
                elif price_sma.loc[date, 'Price/SMA'] >= 0.25:
                    signal['SELL'] = signal['SELL'] + 0.2
                else:
                    if bb.loc[date, 'BB'] >= 0.5 and bb.loc[
                            date, 'previous'] < 1.0 and momemtum.loc[
                                date, 'Momemtum'] >= 0.1:
                        signal['SELL'] = signal['SELL'] + 0.5
                    else:
                        signal['SELL'] = signal['SELL'] + 0.1
            elif price_sma.loc[date, 'Price/SMA'] <= -0.1:  #-0.5:
                if price_sma.loc[date, 'Price/SMA'] <= -0.4:
                    signal['BUY'] = signal['BUY'] + 0.3
                elif price_sma.loc[date, 'Price/SMA'] <= -0.2:
                    signal['BUY'] = signal['BUY'] + 0.2
                else:
                    signal['BUY'] = signal['BUY'] + 0.1

            else:
                signal['DN'] = signal['DN'] + 0.2  #0.2

            #Momentum
            if momemtum.loc[date, 'Momemtum'] <= 0:
                if trade[symbol] >= sma.loc[date, 'SMA']:
                    signal['SELL'] = signal['SELL'] + 0.1
                else:
                    signal['DN'] = signal['DN'] + 0.1
            else:
                if trade[symbol] < sma.loc[date, 'SMA']:
                    signal['BUY'] = signal['BUY'] + 0.1
                else:
                    signal['DN'] = signal['DN'] + 0.1

            keymax = max(signal.keys(), key=(lambda k: signal[k]))
            '''
                if current_holding == 0:
                    if keymax == 'DN' and signal['DN'] < 0.6:
                        #within  bollinger check if price > SMA
                        #then sell
                        if trade[symbol] > sma.loc[date, 'SMA']:
                            keymax='SELL'
                        else:keymax='BUY'
                '''

            if keymax == 'BUY':
                #BUY signal
                df_trade.loc[date, symbol] = 1000 - current_holding
                current_holding = current_holding + df_trade.loc[date, symbol]
            elif keymax == 'SELL':
                #SELL signal
                df_trade.loc[date, symbol] = (-1000 - current_holding)
                current_holding = current_holding + df_trade.loc[date, symbol]

        return df_trade