Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Scanning for top percent gainers once a day

I am new to quantopian and coding in general. I am looking to create a function that once a day at a given time will print the current price, the high and low of the day and the open. I would like to use the pipeline to select current prices in a given range, then sort table by high-open. I would like to then place an order for a certain number off the top of that table, hold on to them for a day then do it again the next day.

Any advice or direction to a tutorial or example I can build off of which be appreciated.

3 responses

Michael

First off, welcome!

A good place to start is the "Getting Started" tutorials (https://www.quantopian.com/tutorials/getting-started)

If you are new to coding, you may find algorithms in Quantopian to be a bit daunting at first. It's all Python so an understanding of the Python syntax and especially the basic data structures (lists, dictionaries, and sets) is a must. Do a web search and you will find a lot of online Python tutorials that can help.

Second, there is a "real time" interactive research part of the Quantopian platform called "notebooks" and then there is the backtest (and ultimately live implementation) platform called "algorithms". Both use Python and (generally) the same methods such as Pipeline etc. Best practice is to develop the security selection logic in an interactive notebook then copy that logic to an algorithm where one adds the actual scheduling and order logic. It's much easier (and faster) to see how ones selection logic works in a notebook. Start there.

You can also get a sense of how well ones selection logic actually performs using notebooks rather than jumping right into backtesting. Take a look at this excellent post and associated notebook https://www.quantopian.com/posts/creating-a-new-strategy-with-pipeline-factors-quantcon-presentation .

So, I made a big assumption and assumed your statement "then sort table by high-open" meant "sort by the difference, or delta, between the previous high and the previous open". To get you started with the "selection" logic of your post I've attached a notebook that does 5 things:
1. gets the high, low, open, and close of every security in the Quantopian database via Pipeline
2. creates a custom "High_Open_Delta" factor to get the latest high minus open value for each security
3. filters the securities to the Q1500US universe (this wasn't in your original requirement but is good practice at least to filter out ETFs)
4. filter securities by close price within a range (I assumed "select current price" would mean close)
5. sort securities by largest high minus open and take the top 20 (I assumed the sort was to be largest first but smallest first may be better?)

One improvement right off you may wish to make is to not look at the absolute gain (high minus open) but rather the relative gain (high minus open divided by open). Just a thought.

Your next step would be copy this into an algorithm, schedule a trading function for once a day, and devise logic to sell after one day (and handle the cases where a position doesn't sell after one day).

Good luck!

It may be helpful to look at how well the "high minus open" or "High_Open_Delta" custom factor performs in general before jumping into backtesting. What is the best holding time 1, 5, or 20 days? Does this factor perform the same across all sectors? Do the highest values or the lowest values perform the best?

Creating a factor tear sheet helps answer these questions. See the attached. The first bunch of cells are the nuts and bolts to plot the results. The second from the last cell is the actual custom factor and the last cell is what actually gets the results.

There's no one indicator that a factor will work or not. It's all about interpretation. A couple of things to note in the tear sheet results however:

  1. "Mean Return by Factor Quantile" shows that big gainers (most positive high minus open) have the biggest jumps
  2. "Mean Return by Factor Quantile" shows that small gainers (most negative high minus open) have big jumps but not as high as in 1 above
  3. big gainers have the biggest one day losses but also the largest 20 day gains
  4. the second and third highest quantiles have respectable 1 day gains
  5. returns are very highly autocorrelated
  6. the factor returns vary quite a bit by sector. Maybe consider limiting this strategy to certain sectors.

I have been playing around with the above notebook. What if i wanted to look at historical data of every stock that has gone up for example 200% or more in a 10 day span but has also come down after that run up more than 40% during a 20 day span after the high of that first 10 day span and then analyze the average market cap, revenue of those companies and other fundamentals. How could I do that?

I also want to look at all equities that are actual companies, not just the Q1500 but I do want to exclude etfs. a lot of the stocks that have big run ups will not be in the Q1500

thanks!