Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnExecution

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

    OnExecution

    Can the Onexecution method use variables from the onbarupdate method? I cant seem to get a price i am storing inside a variable of onbarupdate to transfer to the onexecution method. I am just getting a 0.

    I cant run the same logic to create this value in the onexecution since it does not update per bar or per tick.

    #2
    Well i was able to fix this by using a custom return method, but i am going to need to know how to as i add the multitime frame elements to my strategy.

    Thanks,

    Comment


      #3
      Hello neb1998,

      Yes, it depend on the scope of the variables. If you declare in the Variables region, then you can use anywhere in the strategy.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        I have done so, and the variable is available in each method but when i am in the onexecution method the variable is only updating on execution even though the logic is within the onbarupdate. Is there a way for the variable to reflect the price change on each bar yet be specified as a price for an order within onexecution?

        Comment


          #5
          Sorry, I'm not sure what you are trying to do. Can you please share a code snippet and brief description of what you expect it to do?
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            protected override void OnExecution(IExecution execution)
            {
            Print(myBarsSinceEntry.ToString());
            if (myBarsSinceEntry > 0)
            {

            if ( entryOrder != null && entryOrder == execution.Order && Position.MarketPosition == MarketPosition.Long )
            {
            //Print(entryOrder.ToString());
            //Print(execution.Order.ToString());


            //trailing exit limit order
            exitlimit1 = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,ent ryOrder.Filled ,GetCurrentBid()+5,0,"2","Limit1");

            //money management stop order, does not need updated
            exitstop1 = SubmitOrder(0,OrderAction.Sell,OrderType.Stop,entr yOrder.Filled ,0, entryOrder.LimitPrice-mmstp(),"2", "MM");

            //profit target limit order, set below trailing stop
            exitprofit = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,ent ryOrder.Filled,entryOrder.LimitPrice+profit(),0,"2 ", "profit");

            //if (execution != null) { PrintWithTimeStamp("execution ---" + execution.Order.ToString()); }
            }
            }

            There is the Code, The problem at the moment is that ever since i have tried to add the myBarsSinceEntry variable to the onexecution method i am getting object reference not set to an object........within the onbarupdate method.

            I have the myBarsSinceEntry calculation of CurrentBar-Mybar within the onbarupdate as i need it to be monitored in real time and not per execution.

            I want to set a condition so that the exit cannot take place on the same bar the order enters on.

            I have tried using barssinceentry(0,"",0) and it doesnt allow any orders to execute either. Since there are no signal names with unmanaged i am not sure what to put in the quotes although i have tried my IOrder objects and they do not work, with or without quotes.


            The problem in simple terms seems to be that calculating a variable within onbarsupdate and then trying to recover the REAL TIME variable within the onexecution method. I would imagine since the onexecution method only updates per execution. I worked around this by using methods() for my limit, money management, and profit target but this will not work in all cases.

            ps. This is a long entry, long exit only system. No Reversals. Will be handled with a sep strategy.

            Comment


              #7
              neb1998,

              Your best bet then is to try and isolate to find exactly which line in the code the object reference error is being hit on. Please use try-catch blocks throughout your code to try and isolate. For an example, please see here: http://www.ninjatrader.com/support/f...ead.php?t=9825
              Josh P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              633 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              364 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              105 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              567 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              568 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X