Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    BertrandNinjaTrader Customer Service

    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.
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by llanqui, Today, 03:53 AM
        0 responses
        4 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        10 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        14 views
        0 likes
        Last Post AaronKoRn  
        Started by carnitron, Yesterday, 08:42 PM
        0 responses
        11 views
        0 likes
        Last Post carnitron  
        Started by strategist007, Yesterday, 07:51 PM
        0 responses
        14 views
        0 likes
        Last Post strategist007  
        Working...
        X