I'm working on a strategy using limit orders, and I need to manage an order on each side, so the strategy can go long or short. If the buy/long entry is triggered, then the sell/short order needs to be canceled, and vice versa. And, when a position opens, I need to use some type of order to set my profit target. I've been having some difficulty with all this, and I have the following questions.
1) Should/can I use Portfolio[symbol].IsLong and Portfolio[symbol].IsShort inside of OnOrderEvent() after a limit order has filled/partially filled, to check my direction, or do I need to check some property of the order event object instead?
2) Should/can I use Liquidate() to close any long and any short position ASAP, regardless of profit or loss?
3) Am I able to use a limit order to close my position at my profit target? When I try this, it seems that my exit order fills before reaching the target. Here's what the code looks like for buy side:
// Set entry limit order in OnData method:
loLower = LimitOrder(assetName, positionSize, priceLevel, "enter long")
// Set profit target in OnOrderEvent method:
loClose = LimitOrder(assetName, -Portfolio[symbol].Quantity, priceLevel, "exit long")
I've been using StopMarketOrder() instead of LimitOrder() to make this work, but I don't think this is what I want, I think this creates more slippage.
4) When do the Inactive and None order statuses occur?
5) I keep getting this error if I change one of my input parameters to certain values, how do I find the cause of it? No line number is given.
Runtime Error: Object reference not set to an instance of an object (Open Stacktrace)
Derek Melchin
Hi Robert,
(1) Either option is valid. Using `IsLong` and `IsShort` may be more convenient though.
(2) Yes
(3) Yes, LimitOrders should work. See this related thread for an example.
(4) Inactive order status is on for IB. Refer to their docs for more info. The None status is provided when no other order state has been declared yet.
(5) This typically occurs when an alpha model's `Update` method lacks a `return` statement.
For further assistance, please attach a backtest.
Best,
Derek Melchin
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.
Robert Christian
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!