Back to Community
This Week's Concept: Trading on Max and Min

In the Quantopian emails that I send out, I've added a new section that we refer to as the "weekly spark." The intent is to give you a general algorithmic idea, walk you through it, and help spark your imagination for new ideas and variations.

This week's concept is to watch the recent maximum and minimum prices, and then trade when the price hits one of them. You have choices in how to implement this. If you think that the stock is momentum-driven, you can use a short time frame, and then go long when it hits the max and go short when it hits the minimum. That's essentially riding the momentum. Or you can use max and min to work a contrarian play: use a longer window, sell when it hits the high, and buy when it hits the low. Here's what the contrarian version looks like in a bit of code, and a bit of explanation of the code:

@batch_transform(refresh_period=1, window_length=10)  
def minmax(datapanel, sid):  
  prices_df = datapanel['price']  
  min_price = prices_df[sid].min()  
  max_price = prices_df[sid].max()

if data[sid].price <  min_price :  
  order(sid, 100)  
if data[sid].price >  min_price :  
  order(sid, -100)  

The first line creates a datapanel for our stock. That datapanel is the last 10 days of data (defined by window_length). The datapanel is reloaded once per day (defined by refresh_period). The minmax function queries that datapanel to find the highest and lowest prices in those 10 days of data. Then, if we go over that max price, we sell. If we go lower than the min price, we buy. The theory is that the price of the stock is reasonably stable, and you're betting that any movement to an extreme will later return to the mean.

Below is a full implementation of the momentum version of a min/max algorithm. When you dig into this example, you'll see that the order management gets a bit tricky. It's not enough to just buy and sell in this case; you also have to close out your position at the right times, too. You should clone that algorithm and create your own version. You can try it on different stocks and see which ones are momentum and which ones are more stable. You can try different window lengths. You can change the order management.

(This example is the same example that is in the help docs - so if you already used the batch transform example, then you've seen this one before!)

Have any comments or questions?

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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
# Backtest ID: 50b78b767899861f73015de4
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

1 response

Thanks Dan,

I suggest augmenting your code and text discussions with some qualitative/quantitative "cartoons." For example, use flow/state/timing diagrams, logic tables, etc. Also, I gather (from one of your communications) that in-house you can generate custom plots. These could be used to illustrate what's going on "under the hood" of the algorithm versus time (instead of referring the reader to have a look at the text of the algorithm log output).

Initially, there is a bit of work to generate the diagrams, etc., but they can be recycled and edited.

As an example, Google Drive could work for this (see I poked around github--perhaps the wiki function there could be used? Or maybe this forum could have file upload or embedded graphics enabled?