def gen_manual_trades(self): indicators = Indicators(symbols = self.symbol, sd=self.sd, ed=self.ed) indicators.gen_all_indicators() momentum, sma, bollinger_minus, bollinger_plus = indicators.get_all_inidcators() # EXECUTE MANUAL STRATEGY cols = ["Symbol", "Order", "Shares"] trades = np.zeros(shape = (self.data.shape[0],3)) trades = pd.DataFrame(trades, index = self.data.index.values, columns = cols) holdings = 0 # initial holdsing value: momentum_cutoff = 0.075 for i in range(self.data.shape[0]): # if it's the first day, buy because the general trend of the economy is up: if i == 0 and holdings < 1000: to_buy = 1000 - holdings trades.ix[i] = (self.symbol, "BUY", to_buy) holdings = holdings + to_buy # first, buy/sell based on bollinger bands: # if price is below lower bollinger band, buy: elif bollinger_minus.ix[i] > self.data.ix[i].values[0] and holdings < 1000: to_buy = 1000 - holdings trades.ix[i] = (self.symbol, "BUY", to_buy) holdings = holdings + to_buy # elif price is above upper bollinger band, sell: elif bollinger_plus.ix[i] < self.data.ix[i].values[0] and holdings > -1000: to_sell = 1000 + holdings trades.ix[i] = (self.symbol, "SELL", to_sell) holdings = holdings - to_sell # if momentum slope is above a value, buy because we expect it to go up: elif self.get_momentum_slope(momentum, i) > momentum_cutoff and holdings < 1000: to_buy = 1000 - holdings trades.ix[i] = (self.symbol, "BUY", to_buy) holdings = holdings + to_buy # else if momentum slope is below a value, sell because we expect price to then go down: elif self.get_momentum_slope(momentum, i) < -momentum_cutoff and holdings > -1000: to_sell = 1000 + holdings trades.ix[i] = (self.symbol, "SELL", to_sell) holdings = holdings - to_sell # if sma is larger than the price, buy: elif self.sma_is_consistently_higher(sma, i) and holdings < 1000: to_buy = 1000 - holdings trades.ix[i] = (self.symbol, "BUY", to_buy) holdings = holdings + to_buy # elif sma is lower than price, sell: elif self.sma_is_consistently_lower(sma, i) and holdings > -1000: to_sell = 1000 + holdings trades.ix[i] = (self.symbol, "SELL", to_sell) holdings = holdings - to_sell trades["Symbol"] = self.symbol return trades
def get_indicators(self, prices): calc_indicators = Indicators(self.syms, self.sd, self.ed) calc_indicators.gen_all_indicators() momentum, sma, low_bollinger, high_bollinger = calc_indicators.get_normed_indicators( ) df = pd.DataFrame(index=prices.index) df['Momentum'] = momentum df['SMA'] = sma df['Bollinger Low'] = low_bollinger df['Bollinger High'] = high_bollinger return df