Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Overfill question

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

    Overfill question

    hi all,

    I have a strategy that is producing over fills.

    The scenario is the following:

    1. I have an open position
    2. The strategy has several conditions to exit
    3. If any of the conditions are met, the strategy flattens the position using market orders using ExitLong() or ExitShort()

    I get an overfill error. Looking at the log, I can see that it results in getting two exit orders like one after the other - the first one is ok, the second raises the over fill error.

    This happens when two exit conditions turn true almost at the same time.

    One thing I don't know, is whether the OnBarUpdate method is re-entrent from a threading perspective. (I.e., can it be called at the same time from two different threads and context switch in the middle.) If not, I can guard against multiple exit calls using a state variable so I wouldn't need to use the unmanaged approach - right?

    Thoughts?

    Hope this makes sense.
    Onn

    #2
    Hi Onn, this isn't really a threading concern here but a true and common scenario for seeing overfills, like in manual trading, you work with competing orders close to the current market - this could be resolved by reviewing and optimizing the strategy order generation process achieve the same outcome with as less orders as possible.

    Comment


      #3
      Hi again, thanks for the response. Let me be more specific.

      Say I have the following code:

      Code:
      if (condA ||  condB)
      {
               if (Position.MarketPosition == MarketPosition.Long)
               {
                       ExitLong();
               }
                      
               if (Position.MarketPosition == MarketPosition.Short)
               {
                       ExitShort();    
               }
      }
      This is just the exit code and is executed from OnBarUpdate.
      As you can see, CondA and condB could be true at the same time resulting in an over fill.

      In general, I want only one order to be submitted and once that order is filled or canceled, I would need to allow new orders to be submitted.

      Set a state variable, such as an IOrder variable. Set that variable when the ExitLong or ExitShort is called. In the above code, test for that variable in the if external if state. Reset that variable to NULL when the order gets filled or canceled.

      the resulting code would look something like this:


      Code:
      private IOrder myExitOrder;
      
      protected override  void OnExecution(IExecution execution)
      {
           myExitOrder = null;
      }
        
      
      if (myExitOrder == null && (condA ||  condB))
      {
               if (Position.MarketPosition == MarketPosition.Long)
               {
                       myExitOrder = ExitLong();
               }
                      
               if (Position.MarketPosition == MarketPosition.Short)
               {
                       myExitOrder = ExitShort();    
                       }
      }
      Does this approach make sense? Can you see any problems with it?

      Onn

      Comment


        #4
        Onn, your approach looks good, as with the IOrder null check only one order at a time could be issued.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        639 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
        572 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X