Here is a simple backtest that uses an if statement to execute a limit order, in essence a stop limit. In this case the stop and limits are set at absurd points, 0.9% (stop) and 50% (sell limit) of the previous close price. The goal was to gaurantee a trigger in extended hours but everything waits till the next morning and gets submitted at either 9:31 or 9:32 (another oddity - note execution times are in the logs 1 minute after they're submitted) Extended hours = True and resolution.Minute.
If I set up a scheduled event to fire in extended hours I can get that to work, but not a limit order submitted during normal hours.
Any thoughts?
Logs of OnOrderEvent.Status = Filled
2017-01-17 15:59:00 : Buy 101.0 @ 9.81
2017-01-18 09:32:00 : Sell -101.0 @ 9.46 Loss: -0.03567787971457692
2017-01-18 15:59:00 : Buy 97.0 @ 9.895
2017-01-19 14:21:00 : Sell -97.0 @ 9.77 Loss: -0.012632642748863063
2017-01-19 15:59:00 : Buy 96.0 @ 9.8
2017-01-20 12:34:00 : Sell -96.0 @ 9.675 Loss: -0.012755102040816325
2017-01-20 15:59:00 : Buy 96.0 @ 9.74
2017-01-25 10:11:00 : Sell -96.0 @ 10.29 Loss: -0.014367816091954057
2017-01-25 15:59:00 : Buy 95.0 @ 10.35
2017-01-30 09:33:00 : Sell -95.0 @ 10.45 Loss: -0.020618556701030986
2017-01-30 15:59:00 : Buy 94.0 @ 10.605
2017-01-31 09:43:00 : Sell -94.0 @ 10.44 Loss: -0.015558698727015645
2017-01-31 15:59:00 : Buy 94.0 @ 10.388
Jared Broad
Hey Christopher, unfortunately, the python implementation doesn't support named arguments yet (coming soon!) so the extended market hours property you set is being ignored and no extended data is piped in. To make it pass in the extended hours you need to fully pass in the parameters.
self.AddEquity("AMD", Resolution.Minute, Market.USA, true, 1, True)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.
Christopher Juncosa
First, thanks Jared, that did seem to solve that issue. however, simply making that change brings back another odd one where the algo goes negative on positions and cant recover and sort of loses its marbles. The logic should prevent that but somehow it shorts the position and can't re-buy. Im scratching my head on this one. The only change was correcting the syntax on the extended hours.
Any thoughts? You can see from the logs that (I guess) the algo sells 196 shares instead of 98 on 1/23/17, and then has a port value of -98 which shouldnt happen. The scheduled event is supposed to fire every day and report portfolio holdings and even that stops functioning on the 23rd. The script runs till 2/2 but nothing happens after the issue on 1/23.
2017-01-16 00:00:00 : Launching analysis 2017-01-17 04:01:00 : Portfolio Quantity: 0.0 2017-01-17 15:59:00 : Buy 101.0 @ 9.81 2017-01-17 19:40:00 : Sell -101.0 @ 9.71 Loss: -0.010193679918450525 2017-01-18 15:59:00 : Portfolio Quantity: 0.0 2017-01-18 15:59:00 : Buy 99.0 @ 9.895 2017-01-19 14:21:00 : Sell -99.0 @ 9.77 Loss: -0.012632642748863063 2017-01-19 15:59:00 : Portfolio Quantity: 0.0 2017-01-19 15:59:00 : Buy 99.0 @ 9.8 2017-01-20 12:34:00 : Sell -99.0 @ 9.675 Loss: -0.012755102040816325 2017-01-20 15:59:00 : Portfolio Quantity: 0.0 2017-01-20 15:59:00 : Buy 98.0 @ 9.74 2017-01-23 04:01:00 : Portfolio Quantity: 98.0 2017-01-23 06:21:00 : Sell -98.0 @ 9.79 Loss: 0.003073770491803213 2017-01-23 15:59:00 : Portfolio Quantity: -98.0 2017-02-02 20:00:00 : Algorithm Id:completed
Rahul Chowdhury
Hey Christopher,
The culprit is this line:
if self.Portfolio.Invested and self.Securities[self.tickerOne].Price <= self.sellPrice:
self.LimitOrder(self.tickerOne, -self.Portfolio[self.tickerOne].Quantity, self.Securities[self.tickerOne].Price*.5)
If let's say, a limit order was submitted and was not filled, another sell limit order could be placed. Your algorithm assumes those limit orders will always be immediately filled, which may not be true in after hours when markets are not liquid.
This would mean that duplicate limit sell orders can been placed, both of which can be filled, putting you net short. This would create a loop where you keep creating sell limit orders because you are always invested short. If you look at the order book in the backtest, you will see countless invalid limit orders.
You can avoid this by keeping track of the order ticket for your limit order and only selling when there is not already a limit order in place.
Here is a modified version of your algorithm where I added in the order ticket functionality.
Christopher Juncosa
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!