def testPolicy(self, symbol = "IBM", \ sd=dt.datetime(2009,1,1), \ ed=dt.datetime(2010,1,1), \ sv = 10000): # here we build a fake set of trades # your code should return the same sort of data start = sd - dt.timedelta(days=60) dates = pd.date_range(start, ed) data = ut.get_data([symbol], dates) data.fillna(method="ffill", inplace=True) data.fillna(method="bfill", inplace=True) prices = indicators.normalized_prices(data[symbol]) trades = pd.DataFrame(index=prices[sd:].index, columns=[symbol]) self.rsi = indicators.rolling_rsi(prices, 5) self.bollinger_bands = indicators.bollinger_bands(prices, 15) self.momentum = indicators.rolling_momentum(prices, 2) action = 2 holding = 0 for i in range(len(trades)): prices_to_date = prices[:trades.index[i]] state = self.create_state(trades.index[i], prices_to_date, action) action = self.learner.querysetstate(state) if action is 0: trades.ix[i] = -1000 - holding holding = -1000 if action is 1: trades.ix[i] = 1000 - holding holding = 1000 if action is 2: trades.ix[i] = 0 return trades
def testPolicy(self, symbol, sd, ed, sv): dates = pd.date_range(sd, ed) data = util.get_data([symbol], dates) data.fillna(method="ffill", inplace=True) data.fillna(method="bfill", inplace=True) prices = indicators.normalized_prices(data[symbol]) trades = pd.DataFrame(index=prices.index, columns=[symbol]) trades[:] = 0 momentum = indicators.rolling_momentum(prices, 2) buy_days = [] sell_days = [] # when momentum is positive, buy on the last day its positive and vice versas for i in range(1, len(momentum) -1): if momentum[i] >=0 and momentum[i+1] < 0: sell_days.append(i) elif momentum[i] <=0 and momentum[i+1] > 0: buy_days.append(i) if buy_days[0] < sell_days[0]: trades.ix[0] = -1000 else: trades.ix[0] = 1000 for i in buy_days: trades.ix[i] = 2000 for i in sell_days: trades.ix[i] = -2000 return trades
def benchmark(self, symbol, sd, ed, sv): dates = pd.date_range(sd, ed) data = util.get_data([symbol], dates) data.fillna(method="ffill", inplace=True) data.fillna(method="bfill", inplace=True) prices = indicators.normalized_prices(data[symbol]) trades = pd.DataFrame(index=prices.index, columns=[symbol]) trades[:] = 0 trades.ix[0] = 1000 return trades
def testPolicy(self, symbol, sd, ed, sv): start_date = sd - timedelta(days=max(self.bol_window, self.mom_window, self.rsi_window)*2) dates = pd.date_range(start_date, ed) data = util.get_data([symbol], dates) data.fillna(method="ffill", inplace=True) data.fillna(method="bfill", inplace=True) prices = indicators.normalized_prices(data[symbol]) trades = pd.DataFrame(index=prices[sd:].index, columns=[symbol]) trades[:] = 0 current_shares = 0 for i in range(len(trades)): available_prices = prices[:trades.index[i]] decision = self.decide(available_prices) if decision > 0: trades.ix[i] = 1000 - current_shares current_shares = 1000 if decision < 0: trades.ix[i] = -1000 - current_shares current_shares = -1000 return trades
def addEvidence(self, symbol = "IBM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,1,1), \ sv = 10000): self.cash = sv start = sd - dt.timedelta(days=60) dates = pd.date_range(start, ed) data = ut.get_data([symbol], dates) data.fillna(method="ffill", inplace=True) data.fillna(method="bfill", inplace=True) prices = indicators.normalized_prices(data[symbol]) self.bollinger_bands = None self.momentum = None self.rsi = indicators.rolling_rsi(prices, 5) self.bollinger_bands = indicators.bollinger_bands(prices, 15) self.momentum = indicators.rolling_momentum(prices, 2) self.mom_mean = self.momentum.mean() self.mom_std = self.momentum.std() last_action = 2 current_state = self.create_state(sd, prices[:sd], last_action) action = self.learner.querysetstate(current_state) trades = pd.DataFrame(index=prices[sd:].index, columns=[symbol]) keepgoing = True iteration = 0 while keepgoing: for i in trades.index: reward = self.get_reward(action, last_action, prices[:i]) last_action = action current_state = self.create_state(i, prices[:i], last_action) action = self.learner.query(current_state, reward) cr = self.cash / sv - 1 if cr == self.cr or iteration == 20: keepgoing = False self.cr = cr iteration += 1