Close an order inmediately

Back

Which is the right way to close inmediately an order which is waiting for its limit or stoporder event?

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.


Hey Mario,  you can use SetHoldings(symbol,0) or Liquidate.  You could also create the order yourself if you need more control.

0

But using SetHoldings or Liquidate,as you say, I would be closing all the Symbol pending orders, and, for instance, I could be interested to close only the Long orders, but not the Shorts ones, due to the algo have just received a short Insight.

So, the write answer should be How can I close inmediately all the long or short (but not both) orders which are waiting fot its limit or stoporders? I know the orderEvent.OrderId of these orders I want to close. 

I'll appreciate your new feedback.

0

Yes, you can close them by calling Cancel on the OrderTicket.  Below is a class I use to cancel the opposite side of the StopLoss or TakeProfit.  It stores the order tickets and then will cancel the other tickets once one of them is filled.  I know this is not exactly what you are looking for, but hopefully this shows you how you can cancel orders as needed.

public class OneCancelsOtherTicketSet
{
public OneCancelsOtherTicketSet(params OrderTicket[] orderTickets)
{
this.OrderTickets = new List<OrderTicket>(orderTickets);
}

private List<OrderTicket> OrderTickets { get; set; }

public bool HasTicketId(int id)
{
return OrderTickets.Exists(x => x.OrderId == id);
}

public void CancelOthers()
{
// Cancel all the outstanding tickets.
foreach (var orderTicket in this.OrderTickets)
{
if (orderTicket.Status == OrderStatus.Submitted)
{
orderTicket.Cancel();
}
}
}
}

 

1

Thank you, but I believe that using Cancel OrderId, I will just cancelling the orders, but not closing the position as I want. How can I be sure of this?

0

It sounds like you want to:
1. Close the position
2. Cancel some associated orders

I would typically use Liquidate(symbol) for this, but you say you may have other open orders on the symbol you do not want to cancel, so we//If we sold, remove stop loss or profit taking
if (orderEvent.

need to do it a different way.

You can close the position by creating the new order and submitting it.  You can then subscribe to the OnOrderEvent and ensure your associated orders are closed.  Here is some code that is used in conjunction with OneCancelsOther code above.  In this code you can see that we are checking if an Order was filled or canceled and then taking some action.  Notice the "order.Tag == ENTRY_ORDER", that is a constant that I set on my entry orders so I would know if the order closed was my Entry or Exit order.  I setup the OCO on Entry and then on Exit I canceled the associated open orders.  

Hopefully this helps.

public override void OnOrderEvent(OrderEvent orderEvent)
{

//If we sold, remove stop loss or profit taking
if (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled)
{
var order = Transactions.GetOrderById(orderEvent.OrderId);

// I added a tag to the order so I would know it was an entry order
if (order.Tag == ENTRY_ORDER)
{
// Setup our TakeProfit and StopLoss orders
Symbol s = null;
SymbolData symData = null;
OrderTicket entryTicket;

// Get the order ticket
_entryOrders.TryRemove(orderEvent.OrderId, out entryTicket);
if (entryTicket == null)
throw new Exception("Could not find entry ticket");

s = orderEvent.Symbol;
symData = _symbolData;

var percentMarginRemaining = Portfolio.MarginRemaining / Portfolio.TotalPortfolioValue;

// Calculate my TakeProfit and StopLoss and create the orders
bool isLong = orderEvent.Direction == OrderDirection.Buy;
int sign = isLong ? 1 : -1;
var ps = GetTakeProfitAndStopLoss(symData.ATR14, orderEvent.FillPrice, isLong);
var ticket = LimitOrder(s, -orderEvent.Quantity, ps.Item1, EXIT_ORDER);
var ticket2 = StopLimitOrder(s, -orderEvent.Quantity, ps.Item2, ps.Item2, EXIT_ORDER);
var marketStop = ps.Item2 - sign * symData.ATR14;
var ticket3 = StopMarketOrder(s, -orderEvent.Quantity, marketStop, EXIT_ORDER);
var oco = new OneCancelsOtherTicketSet(ticket, ticket2, ticket3);

// Store the OneCancelsOther so I can get it in the future
_ocoList.Add(oco);

}

// Exit Order
else if (order.Tag == EXIT_ORDER)
{
Symbol s = null;
SymbolData symData = null;
OrderTicket exitTicket = null;

exitTicket = Transactions.GetOrderTicket(orderEvent.OrderId);

s = exitTicket.Symbol;
symData = _symbolData;

var oco = _ocoList.Find(x => x.HasTicketId(exitTicket.OrderId));
if (oco != null)
{
oco.CancelOthers();
}
}
}
else if (orderEvent.Status == OrderStatus.Canceled)
{
if (_entryOrders.ContainsKey(orderEvent.OrderId))
{
OrderTicket discard;
_entryOrders.TryRemove(orderEvent.OrderId, out discard);
}
}
}

 

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