I'm a long time C# programmer, but I'm quite new to QantConnect and the stock market in general.
I have a bit of a problem with my algorithm. Occationally there are sell orders fulfilled that I never requested. Take the Debug log below, posted from the attached test algorithm. The ESV purchase goes through fine (Order #010), but instead of selling at or above the requested price (Order #060) another order ID that I never generated (Order #063) sells it at some seemingly random value below that. Compare that to the sell order for, say, ABX (Order #077). For these "working" sell orders their Order # matches in both the request ("Order ### to sell") and the OrderEvent ("Order ### sold").
There are several of these "ghost sell orders" such as ETE and CHK - all of them selling below my purchase price.
Thanks in advance for the help.
(Note that the "after 1 days" is just logging that waited to request the sell order until at least a day after the original order.)
2016-01-02 00:00:00 Launching analysis for 2d45937e5e2e734184bad0cf764ba706 with LEAN Engine v2.2.0.2.689 2015-10-06 00:00:00 Algorithm warming up... 2016-01-02 00:00:00 Algorithm finished warming up. 2016-01-05 00:00:00 Order 001 to buy 52 ABX at $7.71 for $400.77. 2016-01-05 00:00:00 Order 002 to buy 22 AMAT at $18.23 for $401.01. 2016-01-05 00:00:00 Order 003 to buy 36 AMLP at $11.28 for $406.06. 2016-01-05 00:00:00 Order 004 to buy 25 BAC at $16.25 for $406.28. 2016-01-05 00:00:00 Order 005 to buy 21 BKD at $19.29 for $405.09. 2016-01-05 00:00:00 Order 006 to buy 23 BSX at $18.00 for $414.00. 2016-01-05 00:00:00 Order 007 to buy 81 CHK at $4.97 for $402.57. 2016-01-05 00:00:00 Order 008 to buy 23 COG at $17.55 for $403.72. 2016-01-05 00:00:00 Order 009 to buy 21 EPI at $19.35 for $406.41. 2016-01-05 00:00:00 Order 010 to buy 26 ESV at $15.88 for $412.97. 2016-01-05 00:00:00 Order 011 to buy 31 ETE at $13.01 for $403.17. 2016-01-05 00:00:00 Order 012 to buy 34 EWJ at $11.84 for $402.51. 2016-01-05 00:00:00 Order 013 to buy 33 EWT at $12.38 for $408.54. 2016-01-05 00:00:00 Order 014 to buy 21 EWZ at $19.80 for $415.74. 2016-01-05 00:00:00 Order 015 to buy 31 F at $13.23 for $410.17. 2016-01-05 00:00:00 Order 016 to buy 62 FCX at $6.55 for $406.10. 2016-01-05 00:00:00 Order 017 to buy 21 FITB at $19.25 for $404.17. 2016-01-05 00:00:00 Order 018 to buy 29 GDX at $14.09 for $408.61. 2016-01-05 00:00:00 Order 019 to buy 21 GDXJ at $19.79 for $415.59. 2016-01-05 00:00:00 Order 020 to buy 23 GLW at $17.56 for $403.80. 2016-01-05 00:00:00 Order 021 to buy 22 GPRO at $18.69 for $411.18. 2016-01-05 00:00:00 Order 022 to buy 39 HBAN at $10.47 for $408.28. 2016-01-05 00:00:00 Order 023 to buy 27 HPE at $15.03 for $405.72. 2016-01-05 00:00:00 Order 024 to buy 36 HPQ at $11.28 for $405.98. 2016-01-05 00:00:00 Order 025 to buy 28 HST at $14.78 for $413.75. 2016-01-05 00:00:00 Order 026 to buy 67 ITUB at $5.97 for $400.22. 2016-01-05 00:00:00 Order 027 to buy 58 JCP at $6.97 for $404.26. 2016-01-05 00:00:00 Order 028 to buy 32 KEY at $12.69 for $406.09. 2016-01-05 00:00:00 Order 029 to buy 27 KMI at $14.94 for $403.27. 2016-01-05 00:00:00 Order 030 to buy 32 MRO at $12.64 for $404.62. 2016-01-05 00:00:00 Order 031 to buy 28 MU at $14.33 for $401.24. 2016-01-05 00:00:00 Order 032 to buy 22 NEM at $18.39 for $404.65. 2016-01-05 00:00:00 Order 033 to buy 33 P at $12.22 for $403.26. 2016-01-05 00:00:00 Order 034 to buy 28 PGX at $14.33 for $401.27. 2016-01-05 00:00:00 Order 035 to buy 24 PHM at $16.90 for $405.49. 2016-01-05 00:00:00 Order 036 to buy 25 POT at $16.40 for $409.97. 2016-01-05 00:00:00 Order 037 to buy 44 RF at $9.23 for $406.00. 2016-01-05 00:00:00 Order 038 to buy 32 RIG at $12.56 for $401.92. 2016-01-05 00:00:00 Order 039 to buy 29 RSX at $14.23 for $412.67. 2016-01-05 00:00:00 Order 040 to buy 102 SIRI at $3.95 for $402.90. 2016-01-05 00:00:00 Order 041 to buy 21 SQQQ at $19.82 for $416.22. 2016-01-05 00:00:00 Order 042 to buy 70 SUNE at $5.76 for $403.20. 2016-01-05 00:00:00 Order 043 to buy 52 SWN at $7.70 for $400.40. 2016-01-05 00:00:00 Order 044 to buy 3 TVIX at $176.25 for $528.75. 2016-01-05 00:00:00 Order 045 to buy 37 USO at $10.97 for $405.89. 2016-01-05 00:00:00 Order 046 to buy 11 UWTI at $39.30 for $432.30. 2016-01-05 00:00:00 Order 047 to buy 29 VIPS at $14.06 for $407.60. 2016-01-05 00:00:00 Order 048 to buy 6 VIXY at $70.90 for $425.40. 2016-01-05 00:00:00 Order 049 to buy 43 WLL at $9.52 for $409.36. 2016-01-05 09:31:00 Order 001 bought 52 ABX at $7.71 for $400.77. 2016-01-05 09:31:00 Order 002 bought 22 AMAT at $18.23 for $401.01. 2016-01-05 09:31:00 Order 003 bought 36 AMLP at $11.28 for $406.06. 2016-01-05 09:31:00 Order 004 bought 25 BAC at $16.25 for $406.28. 2016-01-05 09:31:00 Order 005 bought 21 BKD at $19.29 for $405.09. 2016-01-05 09:31:00 Order 006 bought 23 BSX at $18.00 for $414.00. 2016-01-05 09:31:00 Order 007 bought 81 CHK at $4.97 for $402.57. 2016-01-05 09:31:00 Order 008 bought 23 COG at $17.55 for $403.72. 2016-01-05 09:31:00 Order 009 bought 21 EPI at $19.35 for $406.41. 2016-01-05 09:31:00 Order 010 bought 26 ESV at $15.88 for $412.97. 2016-01-05 09:31:00 Order 011 bought 31 ETE at $13.01 for $403.17. 2016-01-05 09:31:00 Order 012 bought 34 EWJ at $11.84 for $402.51. 2016-01-07 00:00:00 Order 050 to sell 36 AMLP at $11.62 after 1 days. 2016-01-07 00:00:00 Order 051 to sell 21 EPI at $19.93 after 1 days. 2016-01-07 00:00:00 Order 052 to sell 31 ETE at $13.40 after 1 days. 2016-01-07 00:00:00 Order 053 to sell 21 BKD at $19.87 after 1 days. 2016-01-07 00:00:00 Order 054 to sell 22 AMAT at $18.77 after 1 days. 2016-01-07 00:00:00 Order 055 to sell 25 BAC at $16.74 after 1 days. 2016-01-07 00:00:00 Order 056 to sell 23 COG at $18.08 after 1 days. 2016-01-07 00:00:00 Order 057 to sell 23 BSX at $18.54 after 1 days. 2016-01-07 00:00:00 Order 058 to sell 52 ABX at $7.94 after 1 days. 2016-01-07 00:00:00 Order 059 to sell 34 EWJ at $12.19 after 1 days. 2016-01-07 00:00:00 Order 060 to sell 26 ESV at $16.36 after 1 days. 2016-01-07 00:00:00 Order 061 to sell 81 CHK at $5.12 after 1 days. 2016-01-08 00:00:00 Order 058 sold 52 ABX at $8.18 for $425.35. Net: $24.58 2016-01-12 00:00:00 Order 062 sold 31 ETE at $9.50 for $294.39. Net: $-108.78 2016-01-12 00:00:00 Order 063 sold 26 ESV at $11.76 for $305.70. Net: $-107.27 2016-01-22 00:00:00 Order 056 sold 23 COG at $18.08 for $415.84. Net: $12.11 2016-02-12 00:00:00 Order 064 sold 81 CHK at $1.77 for $142.97. Net: $-259.61 2016-02-26 00:00:00 Order 054 sold 22 AMAT at $18.77 for $413.04. Net: $12.03 2016-03-30 00:00:00 Order 057 sold 23 BSX at $18.54 for $426.42. Net: $12.42 2016-04-20 00:00:00 Order 051 sold 21 EPI at $19.93 for $418.61. Net: $12.19 ...
Alexandre Catarino
On January 12th, the algorithm started to issue margin call orders.
In the IDE, at the Trades tab, column Tag of the trades table, we can see which trades were issued due to a margin call. We can also add the following snippet to print out these orders in the terminal:
public override void OnMarginCall(List<SubmitOrderRequest> requests) { foreach (var order in requests.ToList()) Â Â { Â Â Debug("OnMarginCall: " + order); Â Â Â Â Â Â Â Â Â Â Â } }
This example showcases two margin related event handlers..
Denny Caldwell (Grinn)
Thanks Alexandre, that's very helpful. They were in fact Margin Call orders.
For the time being I put some code in place to not sell based on Portfolio.Cash but my actual cash in hand - decrementing a variable each time I have a Buy order, and incrementing it each time a stock sells.
Denny Caldwell (Grinn)
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!