SmartQuant Discussion
http://www.smartquant.com/forums/

Problem with deleting limitorder at partial fills.How to do?
http://www.smartquant.com/forums/viewtopic.php?f=42&t=7331
Page 1 of 1

Author:  rdt [ Thu Mar 05, 2009 10:39 am ]
Post subject:  Problem with deleting limitorder at partial fills.How to do?

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();
    }
  }



Author:  rdt [ Mon Mar 09, 2009 10:33 pm ]
Post subject: 

I found a solution but leave it as an example in case somebody wants to study.

1. Order were too unique - BuyLimit -> BuyLimit1, BuyLimit2, etc. Otherwise it would not be possible to distinguish orders.

2. Flags were in my case not necessary. If (MyOrder != null) without any other flags. Otherwise there are more conditions which must be checked (New, PartiallyFilled, IsDone, etc).

3. When a stop-order is filled then OnOrderFilled is also started (no position - if (HasPosition) should be used). Please keep this in mind, event is also triggered without a position (in case of StopLoss).

4. When a new order is placed in OnPartiallyFilled then it has less quantity. When this new order is triggered (with less quantity) OnOrderFilled become active. So always be careful by using OnPartiallyFilled and on OnOrderFilled with limit-orders where computation of size is required.

Regarding 4.: The best thing is not to mix up OnOrderPartiallyFilled and OnPositionChanged/OnOrderFilled. When there is a partiall fill and not fully filled you can be sure that you make a profit.

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/