def open_position(context, data):
    for ticker in context.tickers:
        macd, signal = _get_macd(data, ticker)

        if macd[-1] > signal[-1] and not context.bought[ticker]:
            order_target_percent(ticker, context.weights[ticker])

            context.boughtPrice[ticker] = data.current(ticker, 'price')
            context.bought[ticker] = True

            log.info(
                "Opened Long %s Position; MACD_val: %.5f; Open Position Price: %.5f; Bought: %s; Sold: %s"
                % (ticker, macd[-1] - signal[-1], context.boughtPrice[ticker],
                   context.bought[ticker], context.sold[ticker]))

        if macd[-1] < signal[-1] and not context.sold[ticker]:
            order_target_percent(ticker, -context.weights[ticker])

            context.boughtPrice[ticker] = data.current(ticker, 'price')
            context.sold[ticker] = True

            log.info(
                "Opened Short %s Position; MACD_val: %.5f; Open Position Price: %.5f; Bought: %s; Sold: %s"
                % (ticker, macd[-1] - signal[-1], context.boughtPrice[ticker],
                   context.bought[ticker], context.sold[ticker]))
def close_position(context, data):
    for ticker in context.tickers:
        #If we have a position check sell conditions
        macd, signal = _get_macd(data, ticker)

        if macd[-1] < signal[-1] and context.portfolio.positions[
                ticker].amount != 0 and context.bought[ticker]:
            order_target_percent(ticker, 0)

            context.bought[ticker] = False

            log.info(
                "Closed Long %s Position; MACD_val: %.5f; Close Position Price: %.5f; Bought: %s; Sold: %s"
                %
                (ticker, macd[-1] - signal[-1], data.current(ticker, 'price'),
                 context.bought[ticker], context.sold[ticker]))

        if macd[-1] > signal[-1] and context.portfolio.positions[
                ticker].amount != 0 and context.sold[ticker]:
            order_target_percent(ticker, 0)

            context.sold[ticker] = False

            log.info(
                "Closed Short %s Position; MACD_val: %.5f; Close Position Price: %.5f; Bought: %s; Sold: %s"
                %
                (ticker, macd[-1] - signal[-1], data.current(ticker, 'price'),
                 context.bought[ticker], context.sold[ticker]))
def handle_data(context, data):
    if context.portfolio.positions[context.XIV].amount > 0:
        price = data.current(context.XIV, 'price')
        # set break even
        if price - context.XIV_BuyPrice >= 1:
            if context.XIV_BuyPrice > context.XIV_SellLossPrice:
                context.XIV_SellLossPrice = context.XIV_BuyPrice

        #If we have a position check sell conditions
        if price <= context.XIV_SellLossPrice and len(get_open_orders()) == 0:
            order_target_percent(context.XIV, 0)
            context.xiv_sell = False
            context.xiv_buy = False

    if context.portfolio.positions[context.VXX].amount > 0:
        price = data.current(context.VXX, 'price')
        if price <= context.VXX_SellLossPrice and len(get_open_orders()) == 0:
            order_target_percent(context.VXX, 0)
            context.vxx_buy = False
            context.vxx_sell = False
        if price >= context.VXX_SellProfitPrice and len(
                get_open_orders()) == 0:
            order_target_percent(context.VXX, 0)
            context.vxx_buy = False
            context.vxx_sell = False
Exemplo n.º 4
0
def watch(context, data):  # On schedule, process any sids in watch_list

    prices = data.history(context.watch_list, 'close', 2,
                          '1d')  # Daily, couple days
    for s in context.watch_list:
        if s not in context.portfolio.positions:  # If sold elsewhere, drop it from watch
            context.watch_list.remove(s)
            continue

        # Slope of prices, minute
        slp = slope(
            data.history(s, 'close', 60,
                         '1m').dropna())  # Minutes, note dropna(), important
        if slp < 0:  # Close if downward
            log.info('sell {} at {}  prv {}  {}%'.format(
                s.symbol, data.current(s, 'price'), '%.2f' % prices[s][0],
                '%.0f' % (100 * data.current(s, 'price') / prices[s][0])))
            order_target(s, 0)  # Close/sell
            context.watch_list.remove(s)

    # Any new for price jump watch_list
    prices = data.history(context.portfolio.positions.keys(), 'close', 2,
                          '1d')  # Couple days
    for s in context.portfolio.positions:  # Add to watch_list with price jump
        if not data.can_trade(s): continue
        if data.current(
                s,
                'price') > 1.10 * prices[s][0]:  # If price has jumped upward
            if s in context.watch_list: continue
            log.info('{} to watch_list at {}  prv {}  {}%'.format(
                s.symbol, data.current(s, 'price'), '%.2f' % prices[s][0],
                '%.0f' % (100 * data.current(s, 'price') / prices[s][0])))
            context.watch_list.append(s)
def my_rebalance(context, data):
    DBC_prices = data.history(context.DBC, "price", 100, "1d")

    ema12 = talib.EMA(DBC_prices, 12)
    ema26 = talib.EMA(DBC_prices, 26)
    macd = ema12 - ema26
    signal = talib.EMA(macd, 9)

    record(MACD_val=macd[-1] - signal[-1])
    record(macd=macd[-1])
    record(exp3=signal[-1])

    if macd[-2] < signal[-2] and macd[-1] > signal[-1] and not context.bought:
        order(context.DBC, 40)
        log.info("------------------------------------")
        log.info(
            "Opened Long Position; MACD_val: %.3f; PNL: %.3f; Market Value: %.5f"
            % (macd[-1] - signal[-1], context.portfolio.pnl,
               context.portfolio.portfolio_value))
        # log.info(context.portfolio.positions)
        context.boughtPrice = data.current(context.DBC, 'price')
        context.bought = True
        context.sold = False

    if macd[-2] > signal[-2] and macd[-1] < signal[-1] and not context.sold:
        order(context.DBC, -40)
        log.info("------------------------------------")
        log.info(
            "Opened Short Position; MACD_val: %.5f; PNL: %.5f; Market Value: %.5f"
            % (macd[-1] - signal[-1], context.portfolio.pnl,
               context.portfolio.portfolio_value))
        # log.info(context.portfolio.positions)
        context.boughtPrice = data.current(context.DBC, 'price')
        context.bought = False
        context.sold = True
def set_fixed_stop_short(context, data):
    #Only call this once when the stock is bought
    if data.can_trade(context.GDXJ):
        price = data.current(context.GDXJ, 'price')
        context.BuyPrice = price
        context.SellLossPrice= price + (context.StopLossPct * price)
        context.SellProfitPrice= price - (price * context.TakeProfitPct)
def set_fixed_stop_vxx(context, data):
    #Only call this once when the stock is bought
    if data.can_trade(context.VXX):
        price = data.current(context.VXX, 'price')
        context.VXX_BuyPrice = price
        context.VXX_SellLossPrice= max(context.StopPrice, price - (context.VXX_StopLossPct * price))
        context.VXX_SellProfitPrice= (price * context.VXX_TakeProfitPct) + price
Exemplo n.º 8
0
def handle_data(context, data): 
    #If we have a position check sell conditions
    if context.portfolio.positions[context.XIV].amount > 0:
        price = data.current(context.XIV, 'price')
        if price < context.SellLossPrice and len(get_open_orders()) == 0:
            order_target_percent(context.XIV, 0)
            context.sell = False
            print("SL hit")
def handle_data(context, data): 
    #If we have a position check sell conditions
    if context.portfolio.positions[context.GDXJ].amount != 0 and context.bought:
        price = data.current(context.GDXJ, 'price')
        
        if price > context.SellProfitPrice and len(get_open_orders()) == 0:
            order_target_percent(context.GDXJ, 0)
            context.bought = False
        if price < context.SellLossPrice and len(get_open_orders()) == 0:
            order_target_percent(context.GDXJ, 0)
            context.bought = False
            
    if context.portfolio.positions[context.GDXJ].amount != 0 and context.sold:
        price = data.current(context.GDXJ, 'price')
        
        if price < context.SellProfitPrice and len(get_open_orders()) == 0:
            order_target_percent(context.GDXJ, 0)
            context.sold = False
        if price > context.SellLossPrice and len(get_open_orders()) == 0:
            order_target_percent(context.GDXJ, 0)
            context.sold = False
def handle_data(context, data):
    if context.portfolio.positions[
            context.XIV].amount > 0 and context.BuyPrice > 0:
        price = data.current(context.XIV, 'price')
        if price / context.BuyPrice >= 1.1:
            if context.BuyPrice > context.SellLossPrice:
                context.SellLossPrice = context.BuyPrice
                #context.SellLossPrice = price / 1.1
                log.info("New XIV stop loss price %.2f" %
                         context.SellLossPrice)

        #FR#1: Add profit take 50%
        if price / context.BuyPrice >= 1.5:
            if context.BuyPrice > context.SellLossPrice:
                context.SellLossPrice = price

        #If we have a position check sell conditions
        if price <= context.SellLossPrice and len(get_open_orders()) == 0:
            order_target_percent(context.XIV, 0)
            context.sell = False
            context.buy = False
Exemplo n.º 11
0
def daily(context, data):
    log.info("Weights: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %\
             (context.weights[context.DBC], context.weights[context.SPY],\
              context.weights[context.IEI], context.weights[context.GLD],\
              context.weights[context.IEF], context.weights[context.TLT]))
    # log.info("Amount: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %\
    #          (context.portfolio.positions[context.DBC].amount,\
    #           context.portfolio.positions[context.SPY].amount,\
    #           context.portfolio.positions[context.IEI].amount,\
    #           context.portfolio.positions[context.GLD].amount,\
    #           context.portfolio.positions[context.IEF].amount,\
    #           context.portfolio.positions[context.TLT].amount))

    # log.info("Prices: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %\
    #          (data.current(context.DBC,'price'), data.current(context.SPY,'price'),\
    #           data.current(context.IEI,'price'), data.current(context.GLD,'price'),\
    #           data.current(context.IEF,'price'), data.current(context.TLT,'price')))

    DBC = context.portfolio.positions[context.DBC].amount * data.current(
        context.DBC, 'price')
    SPY = context.portfolio.positions[context.SPY].amount * data.current(
        context.SPY, 'price')
    IEI = context.portfolio.positions[context.IEI].amount * data.current(
        context.IEI, 'price')
    GLD = context.portfolio.positions[context.GLD].amount * data.current(
        context.GLD, 'price')
    IEF = context.portfolio.positions[context.IEF].amount * data.current(
        context.IEF, 'price')
    TLT = context.portfolio.positions[context.TLT].amount * data.current(
        context.TLT, 'price')

    log.info("Market Value: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %\
             (DBC, SPY, IEI, GLD, IEF, TLT))
    log.info("Portfolio Value: %.5f" % (context.portfolio.portfolio_value))

    leverage = context.account.leverage
    log.info("Daily Leverage: %.5f" % (leverage))
    record(leverage=leverage)
def daily(context, data):
    log.info(
        "Weights: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %
        (context.weights[context.DBC], context.weights[context.SPY],
         context.weights[context.IEI], context.weights[context.GLD],
         context.weights[context.IEF], context.weights[context.TLT]))
    log.info(
        "Amount: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %
        (context.portfolio.positions[context.DBC].amount,
         context.portfolio.positions[context.SPY].amount,
         context.portfolio.positions[context.IEI].amount,
         context.portfolio.positions[context.GLD].amount,
         context.portfolio.positions[context.IEF].amount,
         context.portfolio.positions[context.TLT].amount))

    log.info(
        "Prices: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f" %
        (data.current(context.DBC, 'price'), data.current(
            context.SPY, 'price'), data.current(context.IEI, 'price'),
         data.current(context.GLD, 'price'), data.current(
             context.IEF, 'price'), data.current(context.TLT, 'price')))

    DBC = context.portfolio.positions[context.DBC].amount * data.current(
        context.DBC, 'price')
    SPY = context.portfolio.positions[context.SPY].amount * data.current(
        context.SPY, 'price')
    IEI = context.portfolio.positions[context.IEI].amount * data.current(
        context.IEI, 'price')
    GLD = context.portfolio.positions[context.GLD].amount * data.current(
        context.GLD, 'price')
    IEF = context.portfolio.positions[context.IEF].amount * data.current(
        context.IEF, 'price')
    TLT = context.portfolio.positions[context.TLT].amount * data.current(
        context.TLT, 'price')

    log.info(
        "Market Value: DBC %.5f, SPY %.5f, IEI %.5f, GLD %.5f, IEF %.5f, TLT %.5f"
        % (DBC, SPY, IEI, GLD, IEF, TLT))
    log.info("Portfolio Value: %.5f" % (context.portfolio.portfolio_value))
Exemplo n.º 13
0
def set_fixed_stop(context, data):
    #Only call this once when the stock is bought
    if data.can_trade(context.XIV):
        price = data.current(context.XIV, 'price')
        context.BuyPrice = price
        context.SellLossPrice= price - (context.StopLossPct * price)
def my_rebalance(context,data):
    xiv_prices = data.history(context.XIV, "price", 1600, "1m").resample('30T',  closed='right', label='right').last().dropna()

    #convert to 2 hour:
    xiv2hr = []
    if '20:00:00+00:00' in str(xiv_prices.index[-1]) or '21:00:00+00:00' in str(xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-28], xiv_prices[-28]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]])        
        xiv2hr.append([xiv_prices.index[-23], xiv_prices[-23]])
        xiv2hr.append([xiv_prices.index[-19], xiv_prices[-19]])
        xiv2hr.append([xiv_prices.index[-15], xiv_prices[-15]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])
        xiv2hr.append([xiv_prices.index[-10], xiv_prices[-10]])
        xiv2hr.append([xiv_prices.index[-6], xiv_prices[-6]])
        xiv2hr.append([xiv_prices.index[-2], xiv_prices[-2]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = True
    elif '19:30:00+00:00' in str(xiv_prices.index[-1]) or '20:30:00+00:00' in str(xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-31], xiv_prices[-31]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]]) 
        xiv2hr.append([xiv_prices.index[-26], xiv_prices[-26]])
        xiv2hr.append([xiv_prices.index[-22], xiv_prices[-22]])
        xiv2hr.append([xiv_prices.index[-18], xiv_prices[-18]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]]) 
        xiv2hr.append([xiv_prices.index[-13], xiv_prices[-13]])
        xiv2hr.append([xiv_prices.index[-9], xiv_prices[-9]])
        xiv2hr.append([xiv_prices.index[-5], xiv_prices[-5]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = False
    elif '17:30:00+00:00' in str(xiv_prices.index[-1]) or '18:30:00+00:00' in str(xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-31], xiv_prices[-31]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]]) 
        xiv2hr.append([xiv_prices.index[-23], xiv_prices[-23]])
        xiv2hr.append([xiv_prices.index[-22], xiv_prices[-22]])
        xiv2hr.append([xiv_prices.index[-18], xiv_prices[-18]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])
        xiv2hr.append([xiv_prices.index[-10], xiv_prices[-10]])
        xiv2hr.append([xiv_prices.index[-9], xiv_prices[-9]])
        xiv2hr.append([xiv_prices.index[-5], xiv_prices[-5]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = False
    elif '15:30:00+00:00' in str(xiv_prices.index[-1]) or '16:30:00+00:00' in str(xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-31], xiv_prices[-31]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]]) 
        xiv2hr.append([xiv_prices.index[-23], xiv_prices[-23]])
        xiv2hr.append([xiv_prices.index[-19], xiv_prices[-19]])
        xiv2hr.append([xiv_prices.index[-18], xiv_prices[-18]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])        
        xiv2hr.append([xiv_prices.index[-10], xiv_prices[-10]])
        xiv2hr.append([xiv_prices.index[-6], xiv_prices[-6]])
        xiv2hr.append([xiv_prices.index[-5], xiv_prices[-5]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = False
    else:
        log.error("2 HOUR CONVERSION FAILURE")
        return
    dates, vals = zip(*xiv2hr)
    s = pd.Series(vals, index=dates)
    
    rsi = talib.RSI(s,2)
    RSI5 = talib.RSI(s, 5) 

    # XIV
    # BUY RULE
    if context.xiv_buy is False and rsi[-2] < 70 and rsi[-1] >= 70 and context.portfolio.positions[context.XIV].amount == 0:
        context.xiv_buy = True

    # SELL RULE
    if rsi[-2] > 85 and rsi[-1] <= 85 and context.portfolio.positions[context.XIV].amount > 0:
        order_target_percent(context.XIV, 0)
        context.xiv_buy = False
        
    # VXX     
    if RSI5[-1] < 70:
        if rsi[-2] > 85 and rsi[-1] <= 85:
            if context.portfolio.positions[context.VXX].amount == 0:
            #if len(get_open_orders()) == 0 and context.portfolio.positions[context.VXX].amount == 0:
                context.vxx_buy = True

    if context.portfolio.positions[context.VXX].amount > 0 and len(get_open_orders()) == 0:
        if rsi[-2] < 70 and rsi[-1] >= 70:
            order_target_percent(context.VXX, 0)
        
    # panic button
    high = (data.history(context.XIV, "high", 119, "1m")).max()
    if context.last_bar:
        high = (data.history(context.XIV, "high", 29, "1m")).max()
    price = data.current(context.XIV, 'price')
    if ((high/price) - 1) > .1 and len(get_open_orders()) == 0:
        order_target_percent(context.XIV, 0)
        context.xiv_sell = False
        context.xiv_buy = False 
def set_fixed_stop_xiv(context, data):
    #Only call this once when the stock is bought
    price = data.current(context.XIV, 'price')
    context.XIV_BuyPrice = price
    context.XIV_SellLossPrice= max(context.StopPrice, price - (context.XIV_StopLossPct * price)) 
def my_rebalance(context, data):
    xiv_prices = data.history(context.XIV, "price", 1600,
                              "1m").resample('30T',
                                             closed='right',
                                             label='right').last().dropna()

    #convert to 2 hour:
    xiv2hr = []
    if '20:00:00+00:00' in str(
            xiv_prices.index[-1]) or '21:00:00+00:00' in str(
                xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-28], xiv_prices[-28]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]])
        xiv2hr.append([xiv_prices.index[-23], xiv_prices[-23]])
        xiv2hr.append([xiv_prices.index[-19], xiv_prices[-19]])
        xiv2hr.append([xiv_prices.index[-15], xiv_prices[-15]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])
        xiv2hr.append([xiv_prices.index[-10], xiv_prices[-10]])
        xiv2hr.append([xiv_prices.index[-6], xiv_prices[-6]])
        xiv2hr.append([xiv_prices.index[-2], xiv_prices[-2]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = True
    elif '19:30:00+00:00' in str(
            xiv_prices.index[-1]) or '20:30:00+00:00' in str(
                xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-31], xiv_prices[-31]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]])
        xiv2hr.append([xiv_prices.index[-26], xiv_prices[-26]])
        xiv2hr.append([xiv_prices.index[-22], xiv_prices[-22]])
        xiv2hr.append([xiv_prices.index[-18], xiv_prices[-18]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])
        xiv2hr.append([xiv_prices.index[-13], xiv_prices[-13]])
        xiv2hr.append([xiv_prices.index[-9], xiv_prices[-9]])
        xiv2hr.append([xiv_prices.index[-5], xiv_prices[-5]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = False
    elif '17:30:00+00:00' in str(
            xiv_prices.index[-1]) or '18:30:00+00:00' in str(
                xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-31], xiv_prices[-31]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]])
        xiv2hr.append([xiv_prices.index[-23], xiv_prices[-23]])
        xiv2hr.append([xiv_prices.index[-22], xiv_prices[-22]])
        xiv2hr.append([xiv_prices.index[-18], xiv_prices[-18]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])
        xiv2hr.append([xiv_prices.index[-10], xiv_prices[-10]])
        xiv2hr.append([xiv_prices.index[-9], xiv_prices[-9]])
        xiv2hr.append([xiv_prices.index[-5], xiv_prices[-5]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = False
    elif '15:30:00+00:00' in str(
            xiv_prices.index[-1]) or '16:30:00+00:00' in str(
                xiv_prices.index[-1]):
        xiv2hr.append([xiv_prices.index[-31], xiv_prices[-31]])
        xiv2hr.append([xiv_prices.index[-27], xiv_prices[-27]])
        xiv2hr.append([xiv_prices.index[-23], xiv_prices[-23]])
        xiv2hr.append([xiv_prices.index[-19], xiv_prices[-19]])
        xiv2hr.append([xiv_prices.index[-18], xiv_prices[-18]])
        xiv2hr.append([xiv_prices.index[-14], xiv_prices[-14]])
        xiv2hr.append([xiv_prices.index[-10], xiv_prices[-10]])
        xiv2hr.append([xiv_prices.index[-6], xiv_prices[-6]])
        xiv2hr.append([xiv_prices.index[-5], xiv_prices[-5]])
        xiv2hr.append([xiv_prices.index[-1], xiv_prices[-1]])
        context.last_bar = False
    else:
        log.error("2 HOUR CONVERSION FAILURE")
        return
    dates, vals = zip(*xiv2hr)
    s = pd.Series(vals, index=dates)

    rsi = talib.RSI(s, 2)
    #rsi7 = talib.RSI(s,7)
    record(RSI_2=rsi[-1])

    #FR#4: Improve buy/sell condition
    if context.rsi_last == -1:
        context.rsi_last = rsi[-2]

    #Using Agent conditions
    if context.buy is False and context.rsi_last < TraderAgent_RSIBASED_RGQF.RsiUpperLimit and rsi[
            -1] >= TraderAgent_RSIBASED_RGQF.RsiUpperLimit and context.portfolio.positions[
                context.XIV].amount == 0:
        context.buy = True

    if context.rsi_last > TraderAgent_RSIBASED_RGQF.RsiUpperLimitTrend and rsi[
            -1] <= TraderAgent_RSIBASED_RGQF.RsiUpperLimitTrend and context.portfolio.positions[
                context.XIV].amount > 0 and len(get_open_orders()) == 0:
        order_target_percent(context.XIV, 0)
        context.buy = False

        if context.buyVXX is False and rsi[
                -1] <= TraderAgent_RSIBASED_RGQF.RsiLowerLimit and context.portfolio.positions[
                    context.VXX].amount == 0:
            context.buyVXX = True

    # Buy rule
    if context.buyVXX is False and context.rsi_last > TraderAgent_RSIBASED_RGQF.RsiLowerLimit and rsi[
            -1] <= TraderAgent_RSIBASED_RGQF.RsiLowerLimit and context.portfolio.positions[
                context.XIV].amount == 0 and context.portfolio.positions[
                    context.VXX].amount == 0:
        context.buyVXX = True

    # Sell rule
    if context.rsi_last < TraderAgent_RSIBASED_RGQF.RsiLowerLimitTrend and rsi[
            -1] >= TraderAgent_RSIBASED_RGQF.RsiLowerLimitTrend and context.portfolio.positions[
                context.VXX].amount > 0:
        order_target_percent(context.VXX, 0)
        context.buyVXX = False

    #End of agent usage
    # panic button hard for safety:
    high = (data.history(context.XIV, "high", 119, "1m")).max()
    if context.last_bar:
        high = (data.history(context.XIV, "high", 29, "1m")).max()
    price = data.current(context.XIV, 'price')
    if ((high / price) - 1) > .1:
        order_target_percent(context.XIV, 0)
        context.sell = False
        context.buy = False

    #Update for next cicle
    context.rsi_last = rsi[-1]