Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
First attempt at a "peak detector" = Broker's Dream

My first go at this (too much fun!). Basically cloned Dan Dunn's "200,000% Return" algo plus some from Jonathan Kaman's "Simple Local Min/Max". Changed a great deal of both, though.

The algorithm attempts to implement a model of a very simple electrical circuit function called a 'peak detector', just to see what would happen. Basically just trying to sell quickly when the price starts falling (to hold on to the cash), and to buy just as quickly when the price goes up (to catch the peaks). Also wanted it to only buy or sell when the volume for this stock (Apple) was greater than the previous increment, to roughly gauge demand. My intention was that it would either buy OR sell each time through, but it seems to do both each time. I know that's a bug in my code, but the results seemed worth sharing, if only to find out what I could be doing wrong that could make things looks so right! I doubt that buying and selling so frequently is really a viable strategy, unless you want to make your broker rich on commissions.

Looking forward to the comments (Guffaws welcome, too).

7 responses

@Ed, glad you had fun making this algo! I wanted to point you to a convenience function that tracks current positions. You can get your current position in a security by using: data.portfolio.positions[sid(24)]. The return value is an object with amount, current price, and cost basis (average price you paid). There is auto-complete that will lead you through the function calls. You could track the actual amount of AAPL in your portfolio with data.portfolio.positions[sid(24)].amount.

What did you mean by "buy and sell each time"? Looking at your code, it seems like the algo will buy OR sell at most every minute, but the algo will very likely buy AND sell each day. Were you looking to limit transactions on a given day?

Disclaimer

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.

Thanks, fawce! That will definitely be a useful function, and I know there are many more of them out there. I really should spend some more time with the API documentation.

What I was trying to say was that, each time through the handle_data function, I expected the algorithm to either buy OR sell, but not both. I'd like to understand why, in fact, it does both.

Regarding the transaction rate, I did try out some of Kaman's code in one version to limit transactions to once per day, and that worked well. But the goal of this algorithm is to capture the peaks, and you miss most of them if you only buy or sell once per day. So I decided to let it fly, and that's when I got this surprise.

P.S. Can't seem to get the Reply button to work when accessing this forum from my iPad. Is it me?

@Ed, on line 21 you could replace the if with elif, which would force only one condition to be selected. That said, I can't see how those two conditions could ever both be True :). Another quick note - you are logging every single minute, which is definitely allowed, but will slow down your simulations. You may want to disable those log lines if they aren't necessary any more.

I just tried the send reply button from my iPhone, and it is not working there either. I've opened a ticket for that.

@fawce - The elif didn't appear to make a difference (I had tried it once before also), but I think it will be good practice to leave it in even though the two conditions should exclude one another. I have probably mis-interpreted the graphical output, and this is the cause for my confusion. The buy and sell data is only presented on the graph as cumulative amounts for the day, correct? The actual transactions are more frequent, and when I look in detail at them, they are a reasonable looking mix of buying and selling.

Thanks for the tip about logging. That helps, and also makes this a good time to ask for a feature: I frequently work with intermediate variables, and it would be useful to be able to plot one or two of them as the backtest proceeds. Can't do that with the log output because it only provides 512 values.

@Ed, yes the transactions on the graph are summarizing a full day of trading, but the details in the transaction tab will be every individual trade.

+1 for plotting intermediate values. I was missing this sorely when working on the OLS window - I'd really like to see the spread, beta, and zscore over time.

@Ed - just wanted to let you know the various buttons on the forums are now iPhone/iPad/android friendly. I submitted this from my iPhone :).

Thanks (from my iPad)!