SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Aug 15, 2018 3:50 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Mar 05, 2009 10:39 am 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
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();
    }
  }




Last edited by rdt on Thu Mar 05, 2009 11:54 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 10:33 pm 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group