def check_stop_loss(context, data): px = data.current(context.securities, 'close') for security in context.securities: if context.entry_side[security] == 0: continue loss = px[security] / context.entry_price[security] - 1 if context.entry_side[security] == 1 and\ loss < -context.stoploss: # we were long and hit the stoploss order_target_percent(security, 0) # reset data context.entry_price[security] = 0 context.entry_side[security] = 0 context.target_position[security] = 0 return True elif context.entry_side[security] == -1 and\ loss > context.stoploss: # we were short and hit the stoploss order_target_percent(security, 0) # reset data context.entry_price[security] = 0 context.entry_side[security] = 0 context.target_position[security] = 0 return True return False
def handle_entry(context,data): """ apply the signal and position functions. """ prices = data.current(context.universe, 'close') # apply the signal function for stock in context.universe: # if we already have position, ignore if stock in context.entry_levels: continue # just in case stock data is missing if stock not in context.opening_ranges: continue # get today's opening range high, low, mood = context.opening_ranges[stock] if prices[stock] > high and mood=='bullish': context.signals[stock] = 1 elif prices[stock] < low and mood=='bearish': context.signals[stock] = -1 # apply the position function if len(context.signals) == 0: # nothing to trade here return # else equal position in each of the stocks weight = context.leverage/len(context.signals) for stock in context.signals: # if we already have position, ignore if stock in context.entry_levels: continue context.entry_levels[stock] = prices[stock] context.weights[stock] = context.signals[stock]*weight order_target_percent(stock, context.weights[stock])
def square_off(context): """ cancel all open orders. """ cancel_all_open_orders(context) positions = context.portfolio.positions for asset in positions: order_target_percent(asset, 0)
def rebalance(context, data): """ A function to rebalance the portfolio, passed on to the call of schedule_function above. """ for security in context.universe: order_target_percent(security, 1.0 / 10)
def rebalance(context,data): # square off old positions if any for asset in context.portfolio.positions: if asset not in context.weights: order_target_percent(asset, 0) # Place orders for the new portfolio for asset in context.weights: order_target_percent(asset, context.weights[asset])
def rebalance(context,data): """ A function to rebalance the portfolio, passed on to the call of schedule_function above. """ # Position 50% of portfolio to be long in each security for security in context.long_portfolio: order_target_percent(security, 1.0/10)
def rebalance(context, data): """ A function to rebalance the portfolio, passed on to the call of schedule_function above. """ # Position equally for security in context.short_dollar_basket: w = round(context.short_dollar_basket[security] / 7, 2) order_target_percent(security, w)
def handle_exit(context,data): """ exit if we hit our take profit target. """ prices = data.current(context.universe, 'close') for stock in context.entry_levels: high, low, mood, size = context.opening_ranges[stock] current = prices[stock] entry = context.entry_levels[stock] vol = context.volatilities[stock] move = (current - entry)/vol if context.signals[stock] == 1 and move > context.profit_target: order_target_percent(stock, 0.0) elif context.signals[stock] == -1 and move < -context.profit_target: order_target_percent(stock, 0.0)
def place_order(context): """ A function to place order. """ # no change in positioning if context.signal == 999: return weight = context.signal * context.leverage / 2 # cancel all outstanding orders cancel_all_open_orders(context) # send fresh orders order_target_percent(context.x, -weight * context.hedge_ratio) order_target_percent(context.y, weight)
def rebalance(context, data): if not context.universe: for asset in context.portfolio.positions: order_target_percent(asset, 0) order_target_percent(context.hedge, 0) return for asset in context.portfolio.positions: if asset not in context.universe and asset != context.hedge: order_target_percent(asset, 0) for asset in context.universe: order_target_percent(asset, context.weight) hedge_weight = len(context.universe) * context.weight order_target_percent(context.hedge, hedge_weight)
def rebalance(context, data): compute_signal(context, data) if not context.candidates: for asset in context.portfolio.positions: order_target_percent(asset, 0) return for asset in context.universe: if asset not in context.candidates: order_target_percent(asset, 0) else: order_target_percent(asset, context.weight[asset])
def rebalance(context, data): # weighing function n = len(context.long_securities) if n < 1: return weight = 0.5 / n # square off old positions if any for security in context.portfolio.positions: if security not in context.long_securities and \ security not in context.short_securities: order_target_percent(security, 0) # Place orders for the new portfolio for security in context.long_securities: order_target_percent(security, weight) for security in context.short_securities: order_target_percent(security, -weight)
def rebalance(context, data): """ A function to rebalance - all execution logic goes here """ for security in context.securities: order_target_percent(security, context.target_position[security])
def rebalance(context, data): ''' A function to rebalance - all execution logic goes here ''' for security in context.universe: order_target_percent(security, context.weights[security])
def handle_data(context, data): order_target_percent(context.asset, 0.5) pass