I have a problem with deleting Limitorders with Interactive Papertrader in case of partial fills. Has anybody a suggestion how I can solve it? I don't know if this is a matter of Interactive or (hopefully) I did not really undrestand how to do it.
When I use Event-handler OnOrderFilled without partial fills everything is fine.
The problem occurs when I use OnOrderPartiallyFilled where I really get partial fills. In this case I want to delete older limit-orders which are open and place instead new limitorders. This does with my implementation not happen. I want to delete in OnOrderPartialFilled the open limitorder, but they are still active in TWS. There I sum up limitorders (like first partfill 1 lmt-order, next part fill a second lmt-order additionally and so on - it means that I don't cancel older Limitorder whenever OnPartiallyFilled is triggered). The question now is how I must change my private-functions that also the limit-order in OnOrderPartiallyFilled will be deleted. Otherwise I add up some limit-orders which I can never delete by OpenQuant.
Thanks for the help
Jens
Code:
public override void OnOrderPartiallyFilled(Order order)
{
if(order.Side == OrderSide.Buy)
{
CancelSellLimitPartial();
selllimitOrder = SellLimitOrder(order.CumQty, price1, "ProfitL");
selllimitOrder.Send();
}
if(order.Side == OrderSide.Sell)
{
CancelBuyLimitPartial();
buylimitOrder = BuyLimitOrder(order.CumQty, price2, "ProfitS");
buylimitOrder.Send();
}
}
public override void OnOrderFilled(Order order)
{
if(order.Side == OrderSide.Buy)
{
CancelSellLimit();
selllimitOrder = SellLimitOrder(order.CumQty, price1, "ProfitL");
selllimitOrder.Send();
sellstopOrder = SellStopOrder(Qty, price1-StopLoss, "StopLossL");
sellstopOrder.Send();
}
if(order.Side == OrderSide.Sell)
{
CancelBuyLimit();
buylimitOrder = BuyLimitOrder(order.CumQty, price2, "ProfitS");
buylimitOrder.Send();
buystopOrder = BuyStopOrder(Qty, price2+StopLoss, "StopLossS");
buystopOrder.Send();
}
}
//=============================================================
private void CancelBuyLimit(){
if (buylimitOrder !=null)
{
if (!buylimitOrder.IsPendingNew && !buylimitOrder.IsDone)
buylimitOrder.Cancel();
}
}
private void CancelSellLimit(){
if (selllimitOrder !=null)
{
if (!selllimitOrder.IsPendingNew && !selllimitOrder.IsDone)
selllimitOrder.Cancel();
}
}
private void CancelBuyLimitPartial()
{
if (buylimitOrder !=null)
{
if (!buylimitOrder.IsPartiallyFilled)
buylimitOrder.Cancel();
else if (!buylimitOrder.IsPendingCancel)
buylimitOrder.Cancel();
}
}
private void CancelSellLimitPartial()
{
if (selllimitOrder !=null)
{
if (!selllimitOrder.IsPartiallyFilled)
selllimitOrder.Cancel();
else if (!selllimitOrder.IsPendingCancel)
selllimitOrder.Cancel();
}
}