Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop fill quantity problem with partial fills

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Stop fill quantity problem with partial fills

    Hi all, I have a strategy which places the stop "on execution". The problem is that when the order has partial fills, stops are placed for each fill, but each stop order replaces the earlier one so that I end up with a final stop order that is only as much as the last partial fill. This leaves a part of the position unprotected.
    I would love some help with this :-)
    Helen

    #2
    Hi Helen, can you please post your OnExecution() used in your script so we could take a look?

    Comment


      #3
      Hi Bertrand,
      Thanks for taking a look and sorry there are quite a few lines here. The problem is likely to be with the use of execution.Quantity which will only hold the current fill quantity and each successive stop order replaces the last rather than adds to it. My preference is to wait until OrderState.Filled (true) and then place a stop for the whole position. (Note that this is not my code and I am in catch-up mode learning Ninjascript.)
      Helen

      #region Region_OnExecution
      protected override void OnExecution(IExecution execution)
      {
      try
      {
      for(int i = 0 ; i < this.tradeList.Count ; i++)
      {
      // ENTRY / EXIT ORDER
      if(this.tradeList[i].entryOrder != null)
      {
      if(this.tradeList[i].entryOrder == execution.Order)
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
      {
      // Entry Order
      if(execution.Name.Contains(sENTRY))
      {
      // Long Entry
      if(execution.MarketPosition == MarketPosition.Long)
      {
      double sPrice = execution.Price - indAtr[0] * this.iAtrMult;

      sTradeObject to;
      to.bOfOrder = this.tradeList[i].bOfOrder;
      to.entryOrder = this.tradeList[i].entryOrder;
      //helen add print
      Print("OnExecution follows:");
      Print(execution.Name + " LONG Stop Price=" + sPrice + " Qty=" + execution.Quantity + " q=" );
      to.stopOrder = ExitLongStop(sBIP, true, execution.Quantity, sPrice, sSTOP + "L" + this.tradeList[i].bOfOrder.ToString(), execution.Name);

      this.tradeList.RemoveAt(i);
      this.tradeList.Add(to);
      }

      // Short Entry
      if(execution.MarketPosition == MarketPosition.Short)
      {
      double sPrice = execution.Price + indAtr[0] * this.iAtrMult;

      sTradeObject to;
      to.bOfOrder = this.tradeList[i].bOfOrder;
      to.entryOrder = this.tradeList[i].entryOrder;
      //helen add print
      Print("OnExecution follows:");
      Print(execution.Name + " SHORT Stop Price=" + sPrice + " Qty=" + execution.Quantity);
      to.stopOrder = ExitShortStop(sBIP, true, execution.Quantity, sPrice, sSTOP + "S" + this.tradeList[i].bOfOrder.ToString(), execution.Name);

      this.tradeList.RemoveAt(i);
      this.tradeList.Add(to);
      }
      }

      // if(execution.Order.OrderState == OrderState.Filled)
      // this.tradeList[i].entryOrder = null;
      }
      }
      }
      // Stop Order
      if(this.tradeList[i].stopOrder != null)
      {
      if(this.tradeList[i].stopOrder == execution.Order)
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
      {
      if(execution.Order.OrderState == OrderState.Filled)
      this.tradeList.RemoveAt(i);
      }
      }
      }
      }
      }
      catch
      {

      }

      if(Position.MarketPosition == MarketPosition.Flat)
      {
      if(Performance.AllTrades.Count > 0)
      {
      if(Historical)
      tVars.accountAmount = this.iInitialInvest + Performance.AllTrades.TradesPerformance.Currency.C umProfit;
      else
      tVars.accountAmount = GetAccountValue(AccountItem.CashValue);
      }
      }
      }

      Comment


        #4
        Correct Helen, Execution.Quantity like is the issue here - we would advice using execution.Order.Filled, as this would increase as the individual fills are reported in and show your current total filled qty for the order, which would be good to amend the stop order to covering your position as needed.

        Comment


          #5
          Hello Bertrand, thank you for your reply. Can I assume that execution.Order.Filled will provide the total position quantity at the point in time that OrderState.Filled is true? This can then be passed to ExitLongStop in place of execution.Quantity?
          Also, it would be great if you could point me to documentation on this as I could not find any!
          Thanks
          Helen

          Comment


            #6
            That's correct Helen, it would accumulate the incoming fills seen for the IOrder and not represent a single execution seen as in your initial case.

            The docs could be found here (http://www.ninjatrader.com/support/h...iexecution.htm), mostly we refer clients to working alongside this reference sample we have put together -

            The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

            Comment


              #7
              Hi Bertrand,
              Thanks for adding these links - I had found the first one, but not the reference samples. They will be extremely useful, I think. Much appreciated!
              Helen

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              637 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              366 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              571 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X