Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Where did delay in placing order come from?

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

    Where did delay in placing order come from?

    I have a strategy that exited a position using ExitShort(), and in the next block of code executed (after SetStopLoss and SetProfitTarget) a EnterLong() is executed. Here is what the executions looked like on the chart:

    Click image for larger version

Name:	executions.png
Views:	1
Size:	48.2 KB
ID:	898427

    Here is what the code looks like:

    Code:
    #region IfFlat
    					// If no position is on follow these rules...(enterOkay bool variable is to prevent same bar re-entry)
    					if (Position.MarketPosition == MarketPosition.Flat && enterOkay == true) 
    					{
    						// reset bool control variable for dynamic stop resetting
    						stopControl = true;
    						entryOffset = entryOffset1;//setting offset variables based on current setup bar type
    						iniOffset = iniOffset1;
    						mdOffset = mdOffset1;
    						oneDone = oneDone1;
    						
    					
    						
    						
    						 if (( kciLong ) == true && ToTime(Time[0])>=startTime && ToTime(Time[0])< endTime && Math.Abs(Close[2]-Close[1])<2*BarsPeriod.Value*TickSize && Close[0]>= High[1]+ entryOffset*TickSize && tradeLong == true)
    						
    							{
    							orderName = orderLabel;
    							theoEntry = High[1] + entryOffset*TickSize;//variable to manage stops against theoretical no-slippage entry
    							stopPrice = Low[1] - iniOffset*TickSize;
    							SetStopLoss(CalculationMode.Price,stopPrice);//initial stop set to entry plus wiggle room
    							SetProfitTarget(orderName+"FIRSTTARGET",CalculationMode.Price,High[1] + entryOffset*TickSize + firstTarget*TickSize);//targets set from strategy setup user parameters
    							SetProfitTarget(orderName+"SECONDTARGET",CalculationMode.Price,High[1] + entryOffset*TickSize + secondTarget*TickSize);
    							SetProfitTarget(orderName+"THIRDTARGET",CalculationMode.Price,High[1] + entryOffset*TickSize + thirdTarget*TickSize);
    							// 4 unique orders and order names to facillatate scaling out
    							EnterLong(lotSize, orderName+"FIRSTTARGET");
    							EnterLong(lotSize, orderName+"SECONDTARGET");
    							EnterLong(lotSize, orderName+"THIRDTARGET");
    							EnterLong(lotSize, orderName+"RUNNER");
    							enterOkay = false;
    							}
    						
    					}
    			#endregion
    The EnterLong() was executed 1 minute and 15 seconds after the ExitShort() was confirmed as filled (according to the Ninjatrader log file). The strategy runs as COBC = false, so as soon as the short position was closed, on the next tick the block of code above would be executed. All the bool variables in the "if" statement would have already been true, as they are set on a "firstTickofBar" and are known to be true because they also effected the closing of the short position.

    From the log file times, it really appears as though the EnterLong was not even submitted to the brokers until 1 minute 15 seconds later. So far, this type of problem has been very rare, but still needs to be understood.

    Thanks much for your help on a difficult problem.

    #2
    I found something that may or may not be of help in sorting this out. In the trace, the last line before the 1 min 15 sec delay was this:

    2012-04-20 10:20:18:475 (IB) Cbi.PositionUpdateEventArgs.Process: Instrument='SHLD' Account='U1055468' Avg price=0 Quantity=0 Market position=Long Operation=Remove Currency=UsDollar

    Why would it show a quantity of 0 and yet still show position=Long?

    If this message was telling my strategy it was long, that could explain why the delay was there. However, there was no other message updating this position. The next trace line was over a minute later and was the submission of the next entry for this instrument.

    Can you please clarify if indeed the line above from the trace log was a message from IB stating the position was long?

    Comment


      #3
      coolmoss,

      This message means a long position was closed if tied to a short execution.

      How are you calling ExitShort? Please note an ExitShort() and EnterLong() at the same time can cause some issues if not handled appropriately.

      Here are some order handling rules you may want to review : http://www.ninjatrader.com/support/h...d_approach.htm
      Adam P.NinjaTrader Customer Service

      Comment


        #4
        Hi Adam,

        Yes, I'm familiar with that link. I'm using ExitShort() inside an "if":

        if (Position.MarketPosition == MarketPosition.Short)

        Then, I don't use EnterLong() until checked to be flat:

        if (Position.MarketPosition == MarketPosition.Flat && enterOkay == true)

        I ran this strategy on playback at 100X and it didn't produce any problem at all.

        So, to further clarify on the trace file line from above post: are you saying that even though this line says position=Long, that Ninjatrader strategy would correctly know the position was actually flat?

        Thanks.

        Comment


          #5
          coolmoss,

          That is just IB indicating that a position was closed. NinjaTrader strategies have their own internal position tracking.



          I am wondering if you are experiencing an out of sync strategy vs. account position. You can check order status in OnOrderUpdate() and OnExecution(). Its also possible your condition wasn't true right after the ExitShort(), can you verify this?





          Here is also a forum post on "race conditions" that may be relevant.



          Do you notice any other behavior when this issue occurs? Was your condition before the enter long immediately true after the ExitShort was called?
          Adam P.NinjaTrader Customer Service

          Comment


            #6
            Hi Adam,

            I don't think it could have been out of sync as no positions are held overnight and each morning all instruments are verified that the strategy is in sync and the strategy tab is not yellow.

            The condition had to be true because the condition block is checked inside if(FirstTickOfBar) and is not recalculated until the next bar's first tick. Also, I've been doing a dozen or so trades a day and nothing like this has happened on any of the other trades.

            All code is contained within OnBarUpdate and it's not terribly tricky code. I'm only good enough to do simple stuff

            To clarify the very last question you asked: the condition which effected the closing of the prior short position (and this was done correctly and timely) is the same condition which would have triggered the new long position.

            FWIW- in the log file, I thought it was very weird that not a single line of text was printed for the entire 1 min 15 seconds between the two fills. Is it possible that doing something else in Ninjatrade (starting/stopping another strategy, downloading data, opening a new chart, etc) could effect such a delay?

            Thanks much for your assistance.

            Comment


              #7
              coolmoss,

              Without seeing more of the code its difficulty to ascertain why this would occur. For example, I would need to see the short entry along with this long entry code. Generally, if your conditions are true it will execute immediately unless one of the order entry rules for the managed approach occurs so perhaps you would need to make some print statements in the code to track what part is being executed at each start of the tick, etc.

              Print("string here"+Variable+" other string"); is how you can do it.

              You can also put the print into OnExecution and OnOrderUpdate to track how the orders are being processed, etc. and check to see what occurred right after that exit short.
              Adam P.NinjaTrader Customer Service

              Comment


                #8
                Adam,

                Thank you for your help. I've continued troubleshooting as best I can, and in the process came across similarities between problem trades: it appears in the trace/log file that immediately prior to my problem trades there is an unrelated script which was disabled/enabled. I suspect this caused a near maxing out of system resources which could have been the cause of the delay. My apologies for not catching this earlier, I'm remotely troubleshooting this for another user.

                Comment


                  #9
                  Coolmoss,

                  We are here to help so theres no problem there. I am happy you have found the cause. Try/Catch and Print statements are great tools for debugging. You can find links / examples in my previous posts.
                  Adam P.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  656 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  371 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  109 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  574 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  579 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X