Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"orderId = string.Empty"....too late

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

    "orderId = string.Empty"....too late

    Hi, when a trade gets stopped out, i am not able to enter at the same bar's close

    I think the reason for that is orderId is being reset at the bar close in the OnBarUpdate() method and the strategy is not able to enter simultaneously. There is discrepancy between the backtesting and live results (see the attachment). I would like to enter at the close, even when a trade is stopped out at the close. how can i do this? any suggestions?


    thanks for your time
    Attached Files

    #2
    Hi Dawdler,

    Sorry, I'm not following this request. If there's a code snippet you would like for us to shed light on, can you please share the snippet and steps needed to see what you're seeing.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ryan, i am using the code from the sample ATM strategy with slight modification. the problem i am having is that the orderid and atm strategyid are being reset at bar update (which i am guessing is too late).

      so the strategy is not able to enter at the close of the bar. the attachment in my previous post shows this: in backtest there was a short entry at 2307.25 after i was stopped out at 2307.50 seconds before close, but in live trading the strategy did not submit a short order (something isn't working)


      Code:
        [COLOR=blue][FONT=&quot]if[/FONT][/COLOR][COLOR=black][FONT=&quot] (LongSetUp == [/FONT][/COLOR][COLOR=blue][FONT=&quot]true[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        &&    orderId.Length == [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot] && atmStrategyId.Length == [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        {[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              atmStrategyId = GetAtmStrategyUniqueId();[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              orderId = GetAtmStrategyUniqueId();[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              AtmStrategyCreate(OrderAction.Buy, OrderType.Limit, Close[[/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot]], [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot], TimeInForce.Gtc, orderId, [/FONT][/COLOR][COLOR=maroon][FONT=&quot]"NQ Live"[/FONT][/COLOR][COLOR=black][FONT=&quot],atmStrategyId);[/FONT][/COLOR]
         
         [COLOR=black][FONT=&quot]                        }[/FONT][/COLOR]
         
         
         [COLOR=blue][FONT=&quot]if[/FONT][/COLOR][COLOR=black][FONT=&quot] (ShortSetUp == [/FONT][/COLOR][COLOR=blue][FONT=&quot]true[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        &&    orderId.Length == [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot] && atmStrategyId.Length == [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        {[/FONT][/COLOR]
         
         [COLOR=black][FONT=&quot]                              atmStrategyId = GetAtmStrategyUniqueId();[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              orderId = GetAtmStrategyUniqueId();                               [/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              AtmStrategyCreate(OrderAction.Sell, OrderType.Limit, Close[[/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot]], [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot], TimeInForce.Gtc, orderId, [/FONT][/COLOR][COLOR=maroon][FONT=&quot]"NQ Live"[/FONT][/COLOR][COLOR=black][FONT=&quot],atmStrategyId);[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        }[/FONT][/COLOR]
         
         
         
         
         [COLOR=green][FONT=&quot]// Check for a pending entry order[/FONT][/COLOR]
         [COLOR=blue][FONT=&quot]if[/FONT][/COLOR][COLOR=black][FONT=&quot] (orderId.Length > [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                  {[/FONT][/COLOR]
         [COLOR=blue][FONT=&quot]string[/FONT][/COLOR][COLOR=black][FONT=&quot][] status = GetAtmStrategyEntryOrderStatus(orderId);[/FONT][/COLOR]
         
         [COLOR=black][FONT=&quot]                AtmStrategyChangeEntryOrder(Close[[/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot]], [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot], orderId);      [/FONT][/COLOR]
         
         
         [COLOR=green][FONT=&quot]// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements[/FONT][/COLOR]
         [COLOR=blue][FONT=&quot]if[/FONT][/COLOR][COLOR=black][FONT=&quot] (status.GetLength([/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot]) > [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        {[/FONT][/COLOR]
         
         [COLOR=blue][FONT=&quot]if[/FONT][/COLOR][COLOR=black][FONT=&quot] (status[[/FONT][/COLOR][COLOR=purple][FONT=&quot]2[/FONT][/COLOR][COLOR=black][FONT=&quot]] == [/FONT][/COLOR][COLOR=maroon][FONT=&quot]"Filled"[/FONT][/COLOR][COLOR=black][FONT=&quot] || status[[/FONT][/COLOR][COLOR=purple][FONT=&quot]2[/FONT][/COLOR][COLOR=black][FONT=&quot]] == [/FONT][/COLOR][COLOR=maroon][FONT=&quot]"Cancelled"[/FONT][/COLOR][COLOR=black][FONT=&quot] || status[[/FONT][/COLOR][COLOR=purple][FONT=&quot]2[/FONT][/COLOR][COLOR=black][FONT=&quot]] == [/FONT][/COLOR][COLOR=maroon][FONT=&quot]"Rejected"[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                                    orderId = [/FONT][/COLOR][COLOR=blue][FONT=&quot]string[/FONT][/COLOR][COLOR=black][FONT=&quot].Empty;[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                                    ShortSetUp = [/FONT][/COLOR][COLOR=blue][FONT=&quot]false[/FONT][/COLOR][COLOR=black][FONT=&quot];[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                                    LongSetUp = [/FONT][/COLOR][COLOR=blue][FONT=&quot]false[/FONT][/COLOR][COLOR=black][FONT=&quot];[/FONT][/COLOR]
         
         [COLOR=black][FONT=&quot]                        }[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                  } [/FONT][/COLOR]
         [COLOR=blue][FONT=&quot]else[/FONT][/COLOR][COLOR=blue][FONT=&quot]if[/FONT][/COLOR][COLOR=black][FONT=&quot] (atmStrategyId.Length > [/FONT][/COLOR][COLOR=purple][FONT=&quot]0[/FONT][/COLOR][COLOR=black][FONT=&quot] && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                  {[/FONT][/COLOR]
         
         [COLOR=black][FONT=&quot]                        RealizedPNL = GetAtmStrategyRealizedProfitLoss(atmStrategyId) + RealizedPNL;[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                        atmStrategyId = [/FONT][/COLOR][COLOR=blue][FONT=&quot]string[/FONT][/COLOR][COLOR=black][FONT=&quot].Empty;[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                  }[/FONT][/COLOR]
         
         
         [COLOR=blue][FONT=&quot]else[/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                  {                 [/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              LongSetUp = [/FONT][/COLOR][COLOR=blue][FONT=&quot]false[/FONT][/COLOR][COLOR=black][FONT=&quot]; [/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                              ShortSetUp = [/FONT][/COLOR][COLOR=blue][FONT=&quot]false[/FONT][/COLOR][COLOR=black][FONT=&quot]; [/FONT][/COLOR]
         [COLOR=black][FONT=&quot]                  }[/FONT][/COLOR]
         
         
         [COLOR=black][FONT=&quot]                  }[/FONT][/COLOR]

      Comment


        #4
        Ryan, never mind. ignore this thread. i think i will try setting the calculateonbarclose to false.

        Comment


          #5
          Thanks for the update, Dawdler. I see what you mean now and COBC = false seems like a good approach so you're not waiting a whole bar to start the next atm order cycle.
          Ryan M.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          666 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          377 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          110 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          575 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          580 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X