Пример #1
0
 def process(self,
             symbol="AAPL",
             sd=dt.datetime(2008, 1, 1),
             ed=dt.datetime(2009, 12, 31)):
     dates = pd.date_range(sd, ed)
     prices_all = get_data([symbol], dates)
     indi = Indicators()
     indi.get_indicators(prices_all, self.indicator_window)
     pd.concat([
         indi.momentum.iloc[:, 1], indi.smap.iloc[:, 1], indi.bbp.iloc[:, 1]
     ],
               axis=1)
     prices_norm = prices_all / prices_all.iloc[0]
     on_coming_returns = prices_norm.iloc[:, 1].pct_change(
         periods=self.return_window).shift(-self.return_window).dropna(
             axis=0)
     on_coming_returns[on_coming_returns > self.order_threshold] = 1
     on_coming_returns[on_coming_returns < -self.order_threshold] = -1
     on_coming_returns[on_coming_returns.abs() <> 1] = 0
     data = pd.concat([
         indi.momentum.iloc[:, 1], indi.smap.iloc[:, 1],
         indi.bbp.iloc[:, 1], on_coming_returns
     ],
                      axis=1).dropna(axis=0)
     data.columns = ['MOMENTUM', 'SMAP', 'BBP', 'ORDER']
     x = data.loc[:, ['MOMENTUM', 'SMAP', 'BBP']].values
     y = data.loc[:, ['ORDER']].values
     return data, x, y
def testPolicy(symbol='JPM',
               sd=dt.datetime(2008, 1, 1),
               ed=dt.datetime(2009, 12, 31)):

    dates = pd.date_range(sd, ed)
    prices = get_data([symbol], dates)
    prices_norm = prices / prices.iloc[0]
    indi = Indicators()
    indi.get_indicators(prices, 21)
    momentum = indi.momentum
    smap = indi.smap
    bbp = indi.bbp
    upper_limit = 1000
    lower_limit = -1000
    prices['Holding'] = 0
    prices['Trade'] = 0

    for i in range(30, prices.shape[0]):
        # if  smap.iloc[i, 1] < -0.05 and bbp.iloc[i, 1] < 0:
        if momentum.iloc[
                i, 1] < 0 or smap.iloc[i, 1] < -0.05 and bbp.iloc[i, 1] < 0:
            prices.iloc[i, 3] = upper_limit - prices.iloc[i - 1, 2]

        elif momentum.iloc[i, 1] > 0 and smap.iloc[i, 1] > 0.05 and bbp.iloc[
                i, 1] > 1:
            # elif smap.iloc[i, 1] > 0.05 and bbp.iloc[i, 1] > 1:
            prices.iloc[i, 3] = lower_limit - prices.iloc[i - 1, 2]
        # prices.iloc[i, 2] = prices.iloc[i - 1, 2] + prices.iloc[i, 3]
        prices.iloc[i, 2] = prices.iloc[i - 1, 2] + prices.iloc[i, 3]

    price_order = prices.loc[prices.iloc[:, 3] != 0]
    price_order['Order'] = ''
    price_order.loc[price_order['Trade'] > 0, 'Order'] = 'BUY'
    price_order.loc[price_order['Trade'] < 0, 'Order'] = 'SELL'
    price_order['Shares'] = price_order.loc[:, 'Trade'].abs()
    price_order['Symbol'] = symbol
    price_order.reset_index(inplace=True)
    price_order = price_order.iloc[:, [0, 7, 5, 6]]
    price_order.columns = ['Date', 'Symbol', 'Order', 'Shares']
    return price_order