Back

How to place three way order?

Hi Guys, it is very common to place an entry order with a related stop and limit order. Can this be done in QuantConnect?

Update Backtest








Yes, it can be done.

Please checkout the docs under the Trading and Orders section.
We support many different order types. In live trading some of these order types may be simulated depending on your brokerage (for more information on this see Live Trading):

// Various order types:
// Fill a market order immediately (before moving to next line of code)
var newTicket = MarketOrder("IBM", 100);

// Place a long limit order with limit price less than current price
var newTicket = LimitOrder("IBM", 100, lastClose * .999m);

// Closing out a short position; long stop above the last close price
var stopPrice = close * 1.0025m;
var newTicket = StopMarketOrder("IBM", 100, stopPrice);

// Closing out a long position on market drop
var stopPrice = close * .9975m;
var newTicket = StopMarketOrder("IBM", -100, stopPrice);

// Limit order trigger on reaching the stop price
var newTicket = StopLimitOrder("IBM", 100, stopPrice, limitPrice);

// Market on Open/Close:
var newTicket = MarketOnCloseOrder("IBM", 100);
var newTicket = MarketOnOpenOrder("IBM", 100);

There are working QuantConnect University examples where the limit/stop orders are updated if not filled after a given period.

0

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.


Hi Alexandre, thank you very much, but I am not sure you answered my question:-). Imagine my algorithm has discovered an opportunity to enter the market "now". I would like to stay in the market until I have either lost 2% or I have "won" 10%. This is a fairly common scenario.

I would then like to do something like this:

currentPrice=... whatever;
stopPrice=currentPrice*0.98;
takeProfitPrice=currentPrice*1.10;

var orders {

  entryOrder:new MarketOrder("IBM",1000),

  relatedOrders {

    stopLossOrder:new StopMarketOrder("IBM",-1000,stopPrice),

    takeProfitOrder:new LimitOrder("IBM",-1000,limitPrice)

  }

}

PlaceOrders(orders);

The idea is that:

a) The stop and Limit orders are only created if the market order was successfully executed and resulted in a position.

b) The stop and Limit orders are explicitly linked to the position created.

c) If later I hit the stop order, the limit order is cancled, and visa versa, so I don't end up suddenly being short, if the limit price and then the top price are hit in close succession..

I know that I can probably code this as part of the algorithm, but most brokers support these type of three way orders out of the box, and it is quite nice to have the stopLoss and takeProfit orders rest at the broker (should the QuantConnect server be down).

 

 

0

Currently, we need to manage the two exit orders (cancel one when the other is filled). We can easily do that in the OnOrderEvent event handler.

I wrote a simple algorithm where I propose a solution to a three way order. In this solution, we use the Tag member of the OrderTicket class to save the entry order id number:

// In OnData method:
var quantity = 100;
var currentPrice = data[_symbol].Price;
var stopPrice = currentPrice * 0.98m;
var takeProfitPrice = currentPrice * 1.10m;

var entryOrder = MarketOrder(_symbol, quantity);

// Using the Entry order Id number to relate orders
_entryOrderId = entryOrder.OrderId;
StopMarketOrder(_symbol, -quantity, stopPrice,
_entryOrderId.ToString());
LimitOrder(_symbol, -quantity, takeProfitPrice,
_entryOrderId.ToString());

Once one of the exit orders are filled, we look for the orders that were tagged with that number and cancel the order that is still open:

// In OnOrderEvent method:
// Get orders that we tagged with the entry order number
var tickets = Transactions
.GetOrderTickets(x => x.Tag == _entryOrderId.ToString());

foreach (var ticket in tickets)
{
// Cancel the open orders (should be just one)
if (ticket.Status.IsOpen())
{
ticket.Cancel();
}
}

The stop loss and take profit orders rest on the brokerage. For that reason, if the brokerage does not support one of our order types, the algorithm will not place the order and warns the user. 
 

0

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.


OK. Thanks a lot for the fast reply.
0

Update Backtest





0

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.


Loading...

This discussion is closed