Ensuring all positions are closed (no holding overnight) for intraday strategy?

Hello,

Is there any way I can ensure my algorithm exits all open positions before the market closes that does not involve putting in the order to exit out all positions an hour or more ahead of market close? Preferably in a way that does not prove problematic in the hypothetical event that I trade this strategy with a large amount of capital, say $10 million. I appreciate any and all help; thanks in advance! 4 Loading... Total Returns -- Alpha -- Beta -- Sharpe -- Sortino -- Max Drawdown -- Benchmark Returns -- Volatility --  Returns 1 Month 3 Month 6 Month 12 Month  Alpha 1 Month 3 Month 6 Month 12 Month  Beta 1 Month 3 Month 6 Month 12 Month  Sharpe 1 Month 3 Month 6 Month 12 Month  Sortino 1 Month 3 Month 6 Month 12 Month  Volatility 1 Month 3 Month 6 Month 12 Month  Max Drawdown 1 Month 3 Month 6 Month 12 Month """ This is a template algorithm on Quantopian for you to adapt and fill in. """ from quantopian.algorithm import attach_pipeline, pipeline_output from quantopian.pipeline import Pipeline from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import AverageDollarVolume from quantopian.pipeline.filters.morningstar import Q1500US import talib def initialize(context):#--------------------------------------------------------------------------------------------------------- context.spylong = sid(38533) context.trade_switch = 0 total_minutes = (6 * 60) #+ 30 for i in range(1,total_minutes): if i % 5 == 0: schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=i), True) schedule_function(abandon_ship, date_rules.every_day(), time_rules.market_close(minutes=30), True) schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) def make_pipeline():#------------------------------------------------------------------------------------------------------------- pass def before_trading_start(context, data):#----------------------------------------------------------------------------------------- context.trade_switch = 0 def target_stop_exit(context,data):#---------------------------------------------------------------------------------------------- security = context.spylong maximum_profit = 0.001 maximum_loss = 0.0005 verdict = "" if context.portfolio.positions[security].amount > 0: amount = context.portfolio.positions[security].amount price = data.current(security,'price') current_value = amount * price paid_value = context.portfolio.positions[security].amount * context.portfolio.positions[security].cost_basis if current_value >= paid_value * (1 + maximum_profit): verdict = "target" if current_value <= paid_value * (1 - maximum_loss): verdict = "stop" return verdict def my_assign_weights(context, data):#-------------------------------------------------------------------------------------------- weight = 0 closes_5m = data.history(context.spylong,'close',780,'1m').resample('5T', label='right', closed='right').last() ema_8 = talib.EMA(closes_5m.dropna(), timeperiod=8) if closes_5m[-1] >= ema_8[-1] and context.trade_switch == 0: weight = 0.95 return weight def my_rebalance(context,data):#-------------------------------------------------------------------------------------------------- weight = my_assign_weights(context,data) if weight > 0 and context.portfolio.positions[context.spylong].amount == 0: print "Entering Position" order_target_percent(context.spylong,weight) if context.portfolio.positions[context.spylong].amount > 0 and len(get_open_orders()) == 0: if target_stop_exit(context,data) == 'target': print "Exiting at target" order_target_percent(context.spylong,0.0) if target_stop_exit(context,data) == 'stop': print "Exiting at stop" order_target_percent(context.spylong,0.0) def abandon_ship(context,data): context.trade_switch = 1 print "Sweet mother of God, the market is closing! Abandon ship!" oo = get_open_orders() for sec in oo: for order in oo[sec]: cancel_order(order.id) order_target_percent(context.spylong,0.0) def my_record_vars(context, data):#----------------------------------------------------------------------------------------------- pass def handle_data(context,data):#--------------------------------------------------------------------------------------------------- pass  There was a runtime error. 3 responses You could use fixed slippage, so that your order only impacts the price. The good news is I tested it and all position closed by EOD, but the bad news is that since they all filled instantly, the pricing was terrible. It was -2% for the same time period that your backtest shows, and I ran it from the earliest date that UPRO traded, and with fixed slippage, it was at -90% within a year or two. I think modifying the slippage model in order to get the performance you want is going to make your back test results less accurate. I don't know where I saw it but I once read about a way to place aggressive orders that will fill at worse prices but are more likely to fill. Not sure I'd possible on quantopian. What you'll likely have to do is analyze average volume in order to predict how early before market close you need to place your sell. Yeah. The issue is, with$10 million and default slippage, it takes an hour to put his order in, so it wouldn't be reasonable to expect it all to sell at the end of the day without a major impact on the price.