Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Order not triggering

I'm new to this, and not much of a coder. Having got that out of the way, I have managed to cobble together something using a couple of other tests, plus a little bit of coding knowledge. However, I can't work out why the order on line 29 of this test does not work. Any advice would be greatly appreciated.

8 responses

@MNH, it seems like the order is going through to me. One point of clarification, if you place an order in the current day, the simulation won't fill it until the next trading day, and it doesn't reach your portfolio positions until the day after that. I just re-ran your algorithm, with logging for the current cash and positions at the top of the method. Here is the first few lines of the logs:

2012-01-04handle_data:14INFOcurrent cash: 1000000.0  
2012-01-04handle_data:15INFOcurrent holdings: ndict({8554: ndict({'amount': 0, 'last_sale_price': 127.68, 'cost_basis': 0.0, 'sid': 8554})})  
2012-01-04handle_data:19INFOFirst day price is 127.68  
2012-01-04handle_data:21INFOStarting with 1000000.0  
2012-01-04handle_data:23INFOFirst Order Value: 500000  
2012-01-04handle_data:26INFOFirst Order Size: 3916  
2012-01-04handle_data:28INFOStock is: Security(8554)  
2012-01-04handle_data:36INFONext Buy Price: 126.4032  
2012-01-04handle_data:37INFONext Sell Price: 128.9568  
2012-01-05handle_data:14INFOcurrent cash: 1000000.0  
2012-01-05handle_data:15INFOcurrent holdings: ndict({8554: ndict({'amount': 0, 'last_sale_price': 128.06, 'cost_basis': 0.0, 'sid': 8554})})  
2012-01-06handle_data:14INFOcurrent cash: 499574.359947  
2012-01-06handle_data:15INFOcurrent holdings: ndict({8554: ndict({'amount': 3916, 'last_sale_price': 127.79, 'cost_basis': 127.79000001364061, 'sid': 8554})})  

You can see that you are placing an order for 3916 shares on 1/4, and then when you print the portfolio positions (last line) on 1/06.

That's the current intended behavior, but typing it out here, I think we may be too conservative in our assumptions about placing and filling orders. We are using the same approach with minute bar simulations. Maybe we should consider reporting the order's fill in the same bar as when we calculate the transaction. Today we do that after giving the event to your algorithm. Any thoughts?

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.

After reading your reply I ran it again and got the same result as I did last time... clearly no opening transaction. But then I changed the start date, and it looks okay now. This made me wonder about my opening date which, it turns out, was a Sunday. It looks like because my strategy had to trade on day 1 to "get set", but day 1 was a weekend, it never got another chance to do that opening trade. (I hope that both makes sense and is the reason for the problem. And I now wish I'd checked the opening date instead of spending 3 hours trying to fix my code!)

Oh, and yes, I think reporting the fill as soon as possible would be ideal. I can think of reasons why you would want that, but no reason to want to leave it to the next day. But I am far from an expert trader.

What date did you use originally?

Backtest from 2012-04-15 to 2012-07-30 with $1,000,000 initial capital.

Isn't that what you see when you look at the backtest at the top of this post, or does it default to some other date regardless of what date I used?

@MNH -- hehe, yes that is precisely what I see, sorry for the silly question. When I clone and run, it looks like the dates default to this year, rather than following your shared backtest. I'll just make sure I can reproduce your results.

I think your gut instinct is right. We made the slippage model originally for minute bar trading, so filling an order over three minutes didn't seem overly conservative. Applying the same model to daily trading stretches the order time out way too much. I think the desired behavior is:

  • place a trade on d0
  • trade is filled and posted to your portfolio on d1

I'm still having trouble with this. The opening trade (line 29) of $500k triggers when I run a test starting on some dates, but not others. It always seems to work when I start the test on 3 Jan 2012, but picking some dates in December or February to start the test caused that trade not to trigger. John, can you try the test with a few different dates and see how it works for you?

Hi, it is a bit late here, but I will take a look tomorrow. Thank you for sharing more information, I think it will really help narrow this down and fix the problem.