def _algo(self): """ Algo: 1. S&P 500 index closes above its 200 day moving average 2. The stock closes above its upper band, buy 3. S&P 500 index closes below its 200 day moving average 4. The stock closes below its lower band, sell your long position. """ pf.TradeLog.cash = self.capital pf.TradeLog.seq_num = 0 for i, row in enumerate(self.ts.itertuples()): date = row.Index.to_pydatetime() high = row.high low = row.low close = row.close end_flag = pf.is_last_row(self.ts, i) upper_band = row.sma + row.sma * self.percent_band lower_band = row.sma - row.sma * self.percent_band shares = 0 # Sell Logic # First we check if an existing position in symbol should be sold # - sell if (use_regime_filter and regime < 0) # - sell if price closes below lower_band # - sell if end of data if self.tlog.shares > 0: if ((self.regime_filter and row.regime < 0) or close < lower_band or end_flag): # enter sell in trade log shares = self.tlog.sell(date, close) # Buy Logic # First we check to see if there is an existing position, if so do nothing # - Buy if (regime > 0 or not use_regime_filter) # and price closes above upper_band # and (use_regime_filter and regime > 0) else: if ((row.regime > 0 or not self.regime_filter) and close > upper_band): # enter buy in trade log shares = self.tlog.buy(date, close) if shares > 0: pf.DBG("{0} BUY {1} {2} @ {3:.2f}".format( date, shares, self.symbol, close)) elif shares < 0: pf.DBG("{0} SELL {1} {2} @ {3:.2f}".format( date, -shares, self.symbol, close)) # record daily balance self.dbal.append(date, high, low, close)
def _algo(self): """ Algo: 1. The SPY is above its 200-day moving average 2. The SPY closes at a X-day low, buy some shares. If it falls further, buy some more, etc... 3. If the SPY closes at a X-day high, sell your entire long position. """ pf.TradeLog.cash = self.capital pf.TradeLog.margin = self.margin stop_loss = 0 for i, row in enumerate(self.ts.itertuples()): date = row.Index.to_pydatetime() high = row.high low = row.low close = row.close end_flag = pf.is_last_row(self.ts, i) shares = 0 # buy if (self.tlog.num_open_trades() < self.max_positions and row.regime > 0 and close == row.period_low and not end_flag): # calc number of shares buying_power = self.tlog.calc_buying_power(price=close) cash = buying_power / (self.max_positions - self.tlog.num_open_trades()) shares = self.tlog.calc_shares(price=close, cash=cash) # enter buy in trade log self.tlog.buy(date, close, shares) # set stop loss stop_loss = self.stop__loss_pct * close # sell elif (self.tlog.num_open_trades() > 0 and (close == row.period_high or low < stop_loss or end_flag)): # enter sell in trade log shares = self.tlog.sell(date, close) if shares > 0: pf.DBG("{0} BUY {1} {2} @ {3:.2f}".format( date, shares, self.symbol, close)) elif shares < 0: pf.DBG("{0} SELL {1} {2} @ {3:.2f} {4}".format( date, -shares, self.symbol, close, 'STOP' if low < stop_loss else '')) # record daily balance self.dbal.append(date, high, low, close)
def _algo(self): """ Algo: 1. S&P 500 index closes above its 200 day moving average 2. The stock closes above its upper band, buy 3. S&P 500 index closes below its 200 day moving average 4. The stock closes below its lower band, sell your long position. """ self._tlog.cash = self._capital for i, row in enumerate(self._ts.itertuples()): date = row.Index.to_pydatetime() high = row.high low = row.low close = row.close sma = row.sma upper_band = sma + sma * self._percent_band lower_band = sma - sma * self._percent_band regime = row.regime end_flag = True if (i == len(self._ts) - 1) else False shares = 0 # buy if (self._tlog.num_open_trades() == 0 and (regime > 0 or not self._regime_filter) and close > upper_band and not end_flag): # enter buy in trade log shares = self._tlog.enter_trade(date, close) # sell elif (self._tlog.num_open_trades() > 0 and ((self._regime_filter and regime < 0) or close < lower_band or end_flag)): # enter sell in trade log shares = self._tlog.exit_trade(date, close) if shares > 0: pf.DBG("{0} BUY {1} {2} @ {3:.2f}".format( date, shares, self._symbol, close)) elif shares < 0: pf.DBG("{0} SELL {1} {2} @ {3:.2f}".format( date, -shares, self._symbol, close)) # record daily balance self._dbal.append(date, high, low, close, self._tlog.shares, self._tlog.cash)
def _algo(self): """ Algo: 1. The SPY is above its 200-day moving average 2. The SPY closes at a X-day low, buy with full capital. 3. If the SPY closes at a X-day high, sell some. If it sets further highs, sell some more, etc... 4. If you have free cash, use it all when fresh lows are set. """ pf.TradeLog.cash = self.capital pf.TradeLog.margin = self.margin stop_loss = 0 for i, row in enumerate(self.ts.itertuples()): date = row.Index.to_pydatetime() high = row.high low = row.low close = row.close end_flag = pf.is_last_row(self.ts, i) shares = 0 # buy if (self.tlog.num_open_trades() < self.max_positions and row.regime > 0 and close == row.period_low and not end_flag): # calc number of shares buying_power = self.tlog.calc_buying_power(price=close) shares = self.tlog.calc_shares(price=close, cash=buying_power) # if we have enough cash to buy any shares, then buy them if shares > 0: # enter buy in trade log self.tlog.buy(date, close, shares) # set stop loss stop_loss = 0 * close # set positions to max_positions self.positions = self.max_positions # sell elif (self.tlog.num_open_trades() > 0 and (close == row.period_high or low < stop_loss or end_flag)): if end_flag: shares = self.tlog.shares else: shares = int(self.tlog.shares / (self.positions)) self.positions -= 1 # enter sell in trade log shares = self.tlog.sell(date, close, shares) if shares > 0: pf.DBG("{0} BUY {1} {2} @ {3:.2f}".format( date, shares, self.symbol, close)) elif shares < 0: pf.DBG("{0} SELL {1} {2} @ {3:.2f}".format( date, -shares, self.symbol, close)) # record daily balance self.dbal.append(date, high, low, close)
def _algo(self): """ Algo: 1. The SPY is higher than X days ago, buy 2. If the SPY is lower than X days ago, sell your long position. """ pf.TradeLog.cash = self.capital pf.TradeLog.margin = self.margin stop_loss = 0 for i, row in enumerate(self.ts.itertuples()): date = row.Index.to_pydatetime() high = row.high; low = row.low; close = row.close end_flag = pf.is_last_row(self.ts, i) shares = 0 if self.tlog.shares == 0: # if period is None, then select a random trading period of # 6,7,8,...,or 12 months if self.period is None: period = random.choice(range(6, 12+1)) else: period = self.period mom = getattr(row, 'mom'+str(period)) # Sell Logic # First we check if an existing position in symbol should be sold # - if first_dotm (or first_dotw) # sell if mom < 0 # sell if low < stop_loss # sell if end_flag if self.tlog.shares > 0: #if (row.first_dotm: if (row.first_dotw and (mom < 0 or low < stop_loss or end_flag)): # enter sell in trade log shares = self.tlog.sell(date, close) # Buy Logic # - if first_dotm (or first_dotw) # buy if mom > 0 else: #if (row.first_dotm if (row.first_dotw and mom > 0): # enter buy in trade log shares = self.tlog.buy(date, close) # set stop loss stop_loss = 0*close if shares > 0: pf.DBG("{0} BUY {1} {2} @ {3:.2f}".format( date, shares, self.symbol, close)) elif shares < 0: pf.DBG("{0} SELL {1} {2} @ {3:.2f}".format( date, -shares, self.symbol, close)) # record daily balance self.dbal.append(date, high, low, close)