Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trading right before stocks go down, and selling right before they start to go up doesn't work as I hoped

I attended the Quantopian hackathon last week hoping to come out learning something as I know little to nothing about algorithmic trading. I turns out I did. I tried a strategy out an algorithm that had been in the back of my mind for a while. The idea is to sell a stock whenever it starts going down, and buy it whenever it starts going up. Unfortunately it didn't work, and the stocks ended up in the negative. I initially thought that even if it didn't work, it would at least not end up in the negative under any circumstance, but it did. That was because making a trade cost money, which I didn't know, or just forgot.

3 responses

Hi Gregory,

You're right that commissions are costing you a lot here. You can see this if you run a backtest with the line set_commission(commission.PerShare(cost=0, min_trade_cost=0)) at the top, which sets commissions to 0.

But we have another issue here. Your algorithm is able to make a lot of money really quickly, and it also ends up losing more than 50x the money it started with. This indicates that your leverage is extremely high. One best practice that is almost always important is to record your leverage. This will give you a plot of your leverage so that you can make sure it stays near 1.

So why is the leverage so high? It's because you're making new orders every minute without checking for orders that are already open. The order_target_percent() function isn't very smart, and it only looks at your current holdings. It doesn't take into account the new position you will have after your orders resolve. In general, whenever you use multiple order_target_percent()s on the same equity on the same day, you should cancel the first one when you do the second order.

However, if you only do one order_target_percent() per equity per day, you don't need to worry about canceling orders. This is because, at the end of every day, all incomplete orders are canceled automatically.

This leads into one possible solution to your problems. Instead of running your algorithm logic every minute, why not do it every day instead? This kills two birds with one stone: your incomplete orders will automatically be canceled, which will keep your leverage at 1 as desired. And since you only do orders every day instead of every minute, your losses to commissions will be greatly reduced.

I think if you convert this algorithm to run just once a day you'll see it's a lot more successful! After that, maybe think about trying to apply this idea to Pipeline.

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.

Oh, one more best practice: Instead of maintaining an array with previous days' prices, use data.history() instead. When getting data from data.history(), historical prices are automatically adjusted to account for when a stock gets split, for example. If you store past prices in an array, your algorithm might be confused by the sudden price change when a stock splits.

Thanks! That helped a lot.