Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Gap ups and first 5 mins of day

Hello everyone!

Just wanted to say that you guys rock! Learned a lot from everybody- and hope to learn much more. I'm having trouble nailing down a working algo- I'm new to Python- I know about as much as it takes to get me into trouble lol..

Anyways, I'm trying to setup an algo that will basically scan stocks or sectors (I can set the universe) and check to see if there are any tickers that are opening with at least a 15% or so gap up and higher than normal volume. If the condition is met- to run the rest of the algo, but if not- don't place any orders for that day.. (been playing around with other algos and most will always execute an order every day.)

If the condition is met- I want it to log the high and low of the first 5 mins after market open. On min 6, I want it to execute a short order (maybe 1000 shares or something) and place a stop loss at the high of first 5 min.

Once a position is open- it would have a profit target to unload 1/2-3/4 shares at $1 profit, and the rest for either EOD or $2 profit target. If it reaches the first target ($1)- the stop loss would be canceled- and the stop loss would be reset to break even (without commissions) for the remaining shares.

If there are still open positions at 3:45pm- close all positions.

I realize this may not make sense but just looking for a little help. If you have suggestions or can help with parts of it- I would really appreciate it.

5 responses

James,

This is a little help so you cant start with:-

  • below is the closing of any opening orders at 3:45pm:

 schedule_function(  
        func=close_orders,  
        date_rule=date_rules.every_day(),  
        time_rule=time_rules.market_close(minutes=15),  
        half_days=True  
    )



def close_orders(context,data):  
    """  
    loop through the whole universe and close any open positions  
    """  
    for stock in data:  
        order_share = context.portfolio.positions[stock].amount  

        if order_share > 0:  
            order_target(stock, 0)  
            print ('exited long',stock.symbol)

        elif order_share < 0:  
            order_target(stock,0)  
            print ('exited short', stock.symbol)
  • Below is the gap calculation code:-
 price_history = history(3,'1d','price')[context.stock]  
    # to compute the difference  
    gap = price_history.iloc[[-2, -1]].diff()  
    print(gap[-1])

    # to compute the percent change  
    gap = price_history.iloc[[-2,-1]].pct_change()  
    print (gap[-1])  

Thanks Adham- this is why I enjoy the site. I appreciate your help, and your code looks cleaner than mine.

The parts I'm having the most issue with is the order that gets placed at min 6- and a stop loss is set at the same time. I've dug around to see if anybody else has something I can cut up- but no luck yet. But again, I do appreciate this help, and will play with it.

Why not define a function for placing orders, and schedule it to minute 6, same logic for the closing orders function at 3:45? if condition is not met then

return

Hey Adham- that is a great idea. The only thing that was holding me back was the S/L order being placed at the same moment- or very shortly after. And finding a way to set the S/L based on the high of the first 5 mins. I tried making a separate variable to hold the logs, and trying to pull that reference up with the S/L- but I believe this is where the program finds errors..

high and low of the last 5 minutes from open can be achieved as follows:-

# this function should be scheduled at 5 minutes from open  
schedule_function(  
        func=get_high_low,  
        date_rule=date_rules.every_day(),  
        time_rule=time_rules.market_open(minutes=5),  
        half_days=True  
    )


def get_high_low(context,data):

    high_history = history(5,'1m','high')[context.stock]  
    low_history = history(5,'1m','low')[context.stock]

    context.high5minutes = high_history.max()  
    context.low5minutes = low_history.min()  

context variable should of course be initialized first in the initialize function.

for placing stop loss you can write it right after your market order, (I have not tried this before as I usually check for exit conditions manually), but it should be like this:

order_target(context.stock,100)
order_target(context.stock,-100,style=StopOrder(yourStopPrice))