Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Relationship between schedule function and handle_data

It seems if I use schedule_function in initialize it makes it so my handle_data code does not get run. All I'm doing is buying at start of the day, selling at the end of the day, and having some logic run minutely during market hours with handle_data. Can you not have schedule functions defined as well as handle_data?

5 responses

Should work. One can schedule functions as well as have code in the 'handle_data' method which runs every minute.

Maybe attach a backtest if you would like the community to help.

I'm just curious why the logic in handle_data is not working it seems. I checked the transactions and only the buying and selling functions are actively running it seems. :( I must be doing something basic very wrong

The problem is how you keep track of the current price. You keep adding them together, so every day the value gets higher and higher than the actual stock price. Also consider using >= and '<=' instead of == in your handle_data, since it's likely the price will jump past your target.

Oh darn, yeah that += should be just a '=', but even with these changes the returns are still quite horrible which is unintuitive to me. Obviously, the strategy is trivial and just a toy example but it seems like at least buying at open and then selling at close in an up market would generally have positive returns unlike the vastly negative returns this algo gets :(

If you've managed to fix all the logical mistakes in the code, then the high turnover might be your enemy here. Buying and selling $10m of stocks every day, you're going to lose money to slippage and fees.