Adam,
Both Quant Trader and Joakim Arvidsson are right.
In order to use the RSI to trade with the trend and not againts it you need:
Change your logic (do the opposite) and Increase the RSI window_lenght,
I would add change your triggers.
Try this:
import talib
# ---------------------------------------------------------------------------
STOCK, BOND, RSI, UB, LB, LEV = symbol('QQQ'), symbol('TLT'), 50, 55, 45, 1.0
# ---------------------------------------------------------------------------
def initialize(context):
schedule_function(trade, date_rules.week_start(2), time_rules.market_open(minutes = 65))
def trade(context, data):
if get_open_orders(): return
rsi = talib.RSI(data.history(STOCK, 'price', RSI + 2, '1d'), RSI)[-2]
if all(data.can_trade([STOCK, BOND])):
if rsi > UB:
order_target_percent(STOCK, LEV)
order_target_percent(BOND, 0)
elif rsi < LB:
order_target_percent(STOCK, 0)
order_target_percent(BOND, LEV)
record(leverage = context.account.leverage)