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 charlesugo_1, 05-26-2026, 05:03 PM
          0 responses
          56 views
          0 likes
          Last Post charlesugo_1  
          Started by DannyP96, 05-18-2026, 02:38 PM
          1 response
          143 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 05-11-2026, 05:56 AM
          0 responses
          160 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 05-10-2026, 08:12 PM
          0 responses
          96 views
          0 likes
          Last Post CarlTrading  
          Started by Hwop38, 05-04-2026, 07:02 PM
          0 responses
          276 views
          0 likes
          Last Post Hwop38
          by Hwop38
           
          Working...
          X