In this example, we show how to place a combine three orders to emulate the bracket order bahavior.
Please also take a look at Levitikon's Support for one-cancels-the-other order (OCO) thread.
In this technique, we define a variable that will refer to the entry ticket. We check whether it is None to make sure only one "bracket" order is active:
# In Initialize:
self.entry_ticket = None
# In OnData
if self.entry_ticket is None:
# Contract selection logic
# Place limit order below market price. Wait for rebound to buy
self.entry_ticket = self.LimitOrder(front.Symbol, 3, price - d.Decimal(0.01))
In OnOrderEvent event handler, we wait for the entry order to be filled to place the take profit and stop loss order:
def OnOrderEvent(self, orderEvent):
#self.Log(str(orderEvent))
if orderEvent.Status != OrderStatus.Filled:
return
if self.entry_ticket is not None:
# When entry order is filled, place TP and SL orders
if orderEvent.OrderId == self.entry_ticket.OrderId:
price = orderEvent.FillPrice
self.LimitOrder(orderEvent.Symbol, -3, price + d.Decimal(0.04))
self.StopMarketOrder(orderEvent.Symbol, -3, price - d.Decimal(0.03))
# Otherwise, one of the exit orders was filled, so cancel the open orders
else:
self.Transactions.CancelOpenOrders(orderEvent.Symbol)
self.entry_ticket = None
When one of the exit orders is filled, the other other is cancelled and entry_ticket is set to None so that we can place a new entry order.
QC Community, please feel free to suggest other approaches!
Houman Dehghan
Hello,
I have two questions.
1. Can the OCA order (One cancels all) be applied to options backtest as well?
2. Is there a way to get weekly/monthly option prices with minute bar on the day that option expires? I think the system only returns prices for the days before expiration. I really need Friday 2017/12/1 prices, for example, for the option that expires on that Friday,
Thanks,
Halldor Andersen
Hi all.
It is possible to use the OCA (One cancels all) order "hack" for options as well.
I have attached two backtests; one where I demonstrate how to retrieve options prices for weekly options, including on the day of expiration and another backtest that shows how to use OCA (One cancels all) for options.
We are hoping to implement the bracket-order type soon.
Halldor Andersen
Hi all.
It is possible to use the OCA (One cancels all) order "hack" for options as well.
I have attached two backtests; one where I demonstrate how to retrieve options prices for weekly options, including on the day of expiration and another backtest that shows how to use OCA (One cancels all) for options.
We are hoping to implement the bracket-order type soon.
Halldor Andersen
The OCA (One cancels all) example:
Alexandre Catarino
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!