Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarUdate and OnMarketData same time

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

    OnBarUdate and OnMarketData same time

    Hi,

    Is it possible run a strategy using the OnBarUpdate method as well as the OnMarketData method at the same time. I'd like to have my strategy conditions run in the OnBarUpdate section and then once an order is placed I will have a condition in the OnMarketData section checking to see if an order is working and modify IOrders based on Market changes. I'm able get this to compile but running it in market replay it produces the log error. "Object reference not set to an instance of an object. It looks something like this:

    protected override void OnBarUpdate()


    {
    // Condition set 1
    if ( myOrderShort == null && Close[0] > Open[0] )

    {
    DrawArrowDown(CurrentBar.ToString(), true, 0, Close[0], Color.Red);
    myOrderShort = EnterShortLimit(0, true, ent,GetCurrentAsk(),"SHORT LIMIT 1");
    }
    }

    protected override void OnMarketData (MarketDataEventArgs e)
    {
    {
    if(myOrderShort == null && myOrderShort.OrderState == OrderState.Working && e.marketDataType == MarketDataType.Ask)

    EnterShortLimit(0, true, ent,GetCurrentAsk(),"SHORT LIMIT 1");

    }
    }

    #2
    Hi wahooj,

    Thank you for your note, someone will respond shortly.
    TimNinjaTrader Customer Service

    Comment


      #3
      Hi wahooj,

      You are still trying to access a null value at...

      if(myOrderShort == null && myOrderShort.OrderState == OrderState.Working...

      You will first need to ensure that the object is != to null

      More info at - http://www.ninjatrader-support2.com/...ead.php?t=4226
      TimNinjaTrader Customer Service

      Comment


        #4
        Ahh, yeah that makes more sense. I'll try that now.
        So, it is possible to run both those methods simultaneously?

        Comment


          #5
          Hi wahooj,

          Yes, you will be able to use both, so long as the correct information is used in each. For example, plots should not be set from OnMarketData

          More info at - http://www.ninjatrader-support.com/H...l?OnMarketData
          TimNinjaTrader Customer Service

          Comment


            #6
            Thanks Tim, this is working well.
            However it's constantly placing new orders, which is fine because they just get ignored due to the same price, but this may be a little cpu intensive. I only want to place a new order if the Ask or Bid price changes. It seems to be placing new orders every time there is any market update. Is there a check i can put in to satify my requirments? I thought by specifing (e.marketDataType == MarketDataType.Ask) in my condition it would take care of it.
            thx

            Comment


              #7
              Hi wahooj,

              You can submit and manage orders in one of two ways:

              1. Orders that automatically expire (cancel) at the end of a bar unless re-submitted
              2. Orders that are live until you purposely cancel them

              If you want to avoid the redundant orders, you can use the second method.


              Please see the "Order Submission for Entry and Exit Methods - Basic Operation" section at - http://www.ninjatrader-support.com/H...tml?Overview36
              TimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              647 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              369 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              108 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              572 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X