Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
52 Week High/Low Long/Short

Have seen similar strategies peddled across a few trading sites.
So I figured it was worth attempting to quickly code....

Results were a tad dissapointing

8 responses

I am not seeing how you identify the 52-week high. These commands just return the first high (0 is the first period):

high = (high_history[stock])[0]  
low = (low_history[stock])[0]  

I think you need something like this:

high = max(high_history[stock])  
low = min(low_history[stock])  

Tristan

Considering the history() function also get the data of current tick
you need to exclude the current value.

high_history = history(252,'1d','high')  
        low_history = history(252,'1d','low')  
        high = max(high_history[stock][:251])  
        low = min(low_history[stock][:251])  

Yagnesh: Great catch! I believe this can also be done with:

high = max(high_history[stock][:-2])  
low = min(low_history[stock][:-2])  

This should create a "slice" that starts at the beginning (index=0) but does NOT include the most recent entry (index=-1).

On my third take, I think that this would be the right code:

high = max(high_history[stock][:-1])  
low = min(low_history[stock][:-1])  

The reason is because Python ranges and slices are "half open". This means they include the first value listed, but not the last value. So the "-1" is the end of the slice, but it is not included in the slice. :)

Haha yes it was not 52 week highs at all
Ill blame working at 3am!

Just did a run through and will need to manually exclude leveraged etfs

still needs some work with position sizing

Actually, not ordering leveraged ETFs is super-simple. Just add this to your "for" loop:

for stock in data:  
        if stock in security_lists.leveraged_etf_list:  
            continue  
        else:  

The word "continue" will stop processing that stock and start the "for" loop again with the next stock.

The leverage jumps from 1.0 to 0.5 in case there is nothing to go short or long. Also , the volatility is high and can be controlled by putting some weight restrictions.