Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Inistial call GetCurrentAsk() doesn't get correct value until OnMarketData is called

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

    Inistial call GetCurrentAsk() doesn't get correct value until OnMarketData is called

    My Stratefy has both OnBarUpdate() and OnMarketData(). When this Strategy starts, OnBarUpdate() is called first, GetCurrentAsk() doesn't return correct market ask value, instead it returns the most recent market executed price. Same for GetCurrentBid(). Current values can only be obtained after the first time OnBarUpdate() is called. Is there any way this can be corrected?

    But in OnMarketData(), GetCurrentAsk() does get correct value in its first call.

    Get

    #2
    Hello localappleseed,

    Thanks for your post.

    GetCurrentBid/GetCurrentAsk skip ahead of NinjaTrader's data processing methods and get the most recent bid/ask fetched straight from the Level 1 data feed. These prices will be more recent than what is seen in NinjaTrader's data processing methods like OnBarUpdate and OnMarketData. (GetCurrentBid/GetCurrentAsk has newer values than what is currently processing in OnMarketData and OnBarUpdate, and you can see differences between what OnMarketData shows for the current Ask vs. using GetCurrentAsk)

    Please also note that ticks in OnMarketData come after ticks in OnBarUpdate when the script is calculating OnEachTick.

    When processing historical data, Close[0] is substituted since fetching the most recently seen Bid/Ask from realtime Level 1 data is not relevant in a historical context.

    Further detail and discussions can be found in the thread below.

    https://ninjatrader.com/support/foru...=22#post830260

    Let us know if there is anything else we can do to help.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim for showring the info. It seems my question was not asked clearly. here I would like to make my question better.
      The following results are based on the output of Print().

      ...
      OnBarUpdate()
      {
      ...
      GetCurrentAsk(); // this gets WRONG data; It prints the last executed price instead
      }

      OnMarketData()
      {
      ..
      GetCurrentAsk(); // get correct data
      }

      OnBarUpdate()
      {
      ...
      GetCurrentAsk(); // get correct data;
      }

      Comment


        #4
        Hello localappleseed,

        GetCurrentAsk/GetCurrentBid return the Close price when processing historical data.

        When you also print the State in OnBarUpdate, what State are you calling GetCurrentAsk in? State.Historical, or State.Realtime?

        If you add the following:

        if (State == State.Historical) return;

        to the top of your OnBarUpdate method to prevent processing logic on historical data, are you still getting Close[0] with realtime prints?

        We look forward to assisting.

        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks, Jim.
          I am using real time data only, so no historical situation for me.
          But even if I can skip the first time call OnBarUpdate(), it may take long time to get 2nd time to call OnBarUpdate() if I set bar period as 30-minutes for instance.

          My only work around is to do everything in OnMarketData() instead of OnBarUpdate(). But I still hope I can stay with OnBarUpate() if it is possible.

          Comment


            #6
            Hello localappleseed,

            You can use OnBarUpdate and have it update on bar closes, price changes, or on each tick with the Calculate property. Note that historical processing will always use OnBarClose style processing unless Tick Replay is used for historical OnPriceChange/OnEachTick.

            Calculate - https://ninjatrader.com/support/help...?calculate.htm

            We should also have the understanding that GetCurrentAsk/GetCurrentBid would get the bid/ask sooner than it would be processed in OnMarketData. The current bid/ask could be the same as the last executed price as that last tick was processed in OnBarUpdate, and we should be able to illustrate that GetCurrentAsk/GetCurrentBid are always different, but they may be the same as Close[0].

            Could you test the script I have attached which only processes realtime data and prints GetCurrentBid, GetCurrentAsk, and Close[0] and point out what is behaving unexpectedly in it's output? If it is easier, could you modify it to demonstrate?

            I look forward to assisting.
            Attached Files
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by gravdigaz6, Today, 11:40 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by MarianApalaghiei, Today, 10:49 PM
            3 responses
            10 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by XXtrader, Today, 11:30 PM
            0 responses
            4 views
            0 likes
            Last Post XXtrader  
            Started by love2code2trade, Yesterday, 01:45 PM
            4 responses
            28 views
            0 likes
            Last Post love2code2trade  
            Started by funk10101, Today, 09:43 PM
            0 responses
            9 views
            0 likes
            Last Post funk10101  
            Working...
            X