def bar_open(context,data): if context.xiv_buy is True and data.can_trade(context.XIV) and len(get_open_orders()) == 0: set_fixed_stop_xiv(context, data) order_target_percent(context.XIV, context.VOLorderpct) order_target_percent(context.BOND, context.BONDorderpct) context.xiv_sell = False context.xiv_buy = False if context.xiv_sell is True and context.portfolio.positions[context.XIV].amount > 0: order_target_percent(context.XIV, 0) context.xiv_buy = False context.xiv_sell = False if context.vxx_buy is True and context.vxx_sell is False and len(get_open_orders()) == 0: if data.can_trade(context.VXX): set_fixed_stop_vxx(context, data) order_target_percent(context.VXX, context.VOLorderpct) order_target_percent(context.BOND, context.BONDorderpct) context.vxx_sell = False context.vxx_buy = False if context.vxx_sell is True and context.portfolio.positions[context.VXX].amount > 0: order_target_percent(context.VXX, 0) context.vxx_buy = False context.vxx_sell = False
def bar_open(context, data): if context.buy is True and len(get_open_orders()) == 0 and data.can_trade( context.XIV): set_fixed_stop(context, data) if context.portfolio.positions[context.VXX].amount > 0: order_target_percent(context.VXX, 0) #log.info("Sell VXX at %.2f" %data.current(context.VXX, 'price')) order_target_percent(context.XIV, context.xivTradePercent * 0.35) order_target_percent(context.XIV, context.xivTradePercent * 0.3) order_target_percent(context.XIV, context.xivTradePercent * 0.3) #log.info("Buy XIV at %.2f" %data.current(context.XIV, 'price')) context.buy = False context.sell = False context.buyVXX = False context.newsl = 0 if context.buyVXX is True and context.portfolio.positions[ context.VXX].amount == 0 and len( get_open_orders()) == 0 and data.can_trade(context.VXX): #set_fixed_stop(context, data) order_target_percent(context.VXX, context.vxxTradePercent * 0.35) order_target_percent(context.VXX, context.vxxTradePercent * 0.3) order_target_percent(context.VXX, context.vxxTradePercent * 0.3) #log.info("Buy VXX at %.2f" %data.current(context.VXX, 'price')) context.buyVXX = False if context.sell is True and context.portfolio.positions[ context.XIV].amount > 0: order_target_percent(context.XIV, 0) #log.info("Sell XIV at %.2f" %data.current(context.XIV, 'price')) context.sell = False context.buy = False
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
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 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)