Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple Pipeline Help

I'm new...and I have been banging my head against the wall trying to create a simple algorithm that does a couple of very simple things that I have backtested in Think or Swim with individual stocks.

  1. I want my base universe to be the NASDAQ 100.
  2. I want to purchase a stock when it hits a 10 week high with.
  3. I want to sell a stock if it falls below the 75 week low.
  4. Each position should be around 1% of the whole.

Someone please tell me this is a simple problem and I am just over complicating it. :)

7 responses
  1. I want my base universe to be the NASDAQ 100.

You have to download all the historical NASDAQ 100 entries, use self-serve data and then upload them. After that, create you're own filter that filters out companies not in that file.

2,3,4:

I don't think pipeline is a good fit for this strategy. Pipeline allows you to very easily create market neutral long/short portfolios. For example, you want each position to be 1%. But using the NASDAQ 100, how is that possible? Not all stocks in the NASDAQ 100 are hitting 10 week highs and falling below 75 week lows.

You need to reformulate your strategy in terms of holding a large number (500+) securities which are held both long and short. To choose these 500, you need to be able to assign a score to each security in your universe and then use that score to figure out portfolio weights. This is how you develop an alpha factor. Each and every stock in existence should have a numerical number that quantifies its exposure to your factor.

Even though this isn't a good fit for pipeline, you can easily do implement your strategy using the algorithm IDE. Look at some of the examples you you'll be able to figure it out.

Yep. Been trying to fit everything into pipelines. 🤦

Someone said impossible... darn, now I HAVE to do it!
I used a custom filter to approximate the criteria for the Nasdaq 100 and a custom factor to screen for the n-weeks-highs/lows. I didn't know what you mean by selling, exiting longs or going short. It now exits longs.

Btw,

you want each position to be 1%. But using the NASDAQ 100, how is that possible?

that was the easiest part ;)

order_target_percent(symbol, 0.01)  

It just came to me that symbols could drop out of the universe. For that case you should add a logic in my_rebalance or you might get stuck with them forever

    for sym in context.portfolio.positions.keys():  
        if sym not in wts.index:  
            order_target_percent(sym, 0)

Probably best before these lines:

    wts = wts[np.isfinite(wts)]  
    wts = wts[wts != 0]  

that was the easiest part ;)

Yes, but assuming you want to invest all of your cash, how are you going to put 1% in each position with a universe of 100 names? Presumably you want to maintain a leverage ratio of 1, correct?

Yep. Been trying to fit everything into pipelines.

Why do you want to use pipeline? It would be much easier to just not use it. What's the value that pipeline adds when it's not suited for this particular strategy?

I wanted to see how close my universe selection is to nasdaq 100, so I applied the weighting methods used by nasdaq, rebalanced accordingly and calculated the correlation of the porfolio values to QQQ. It's very close, you could almost say identical with a correlation between 0.99 and 1.

you want to maintain a leverage ratio of 1, correct?

That depends on many factors, one being who you are testing for. For Quantopian funding, challenges and the contest it was a requirement - but none of this is an option right now. Not all hedge funds have this requirement and it also can be useful to have a cash reserve, for instance in case of a short squeeze and the related margin calls.
And if you are trading with your own money it's even more advisable not to blow up all your cash with a trading strategy ;)