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

How to Properly Use OnMarketData()?????

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

    How to Properly Use OnMarketData()?????

    I have a simple indicator that plots the 1-second Ask-Bid spread on the NQ. I would like to use OnMarketData() instead of adding a dataseries and using OnBarUpdate like I am currently doing. Could anybody please give some advice on how to use OnMarketData for this? I can not find any good examples of how to use OnMarketData other than how to print to the output window.

    Here is the original indicator:


    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;
    Add("WBS 07-11",PeriodType.Second, 1, MarketDataType.Ask);
    Add("WBS 07-11",PeriodType.Second, 1, MarketDataType.Bid);
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return;

    Plot0.Set((Closes[1][0]) - (Closes[2][0]));
    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Plot0
    {
    get { return Values[0]; }
    }
    #endregion

    #2
    Baseheadz, why are you looking to use OnMarketData() for this task - it would not be available on historical data / backtesting.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Ryan had suggested that I use OnMarketData because it would work better for the Real-Time Bid-Ask data. The issue is that when I run this particular indicator in real time, the results are totally different than if I were to load an additional chart with the same indicator using historical data. Basically the historical

      I've also noticed that my strategies which use this indicator are not getting the same values from the indicator as, even when everything is running in real-time.

      Comment


        #4
        It would depend on the event timing that you're trying to access the data with, if you work in OnMarketData() the timing you receive the update is the event coming in itself, so for example a change in Level 1 bid data or volume. If you work in OnBarUpdate() you're limited to updates being driven by the bar update, so either on bar close or each tick. Recreating the exact sequence historically is not easy and not always feasible, as the granularity on historical data is down to 1 second where in real time it's updating whenever the update comes in (pending any CalculateOnBarClose setting).
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thanks Bertrand, I have thought about this alot in terms of the granularity. When we think in terms of milliseconds and nanoseconds and microseconds, there could be over a hundred different values for the Ask-Bid spread on the level-1 inside of a 1-second bar.

          You're saying that the OnBarUpdate of my main symbol on the chart is determining what the Ask-Bid spread of the 1-sec indicator is returning. Correct?

          Using the indicator that I posted here, is it possible to Plot the highest value for the Ask-Bid spread during that 1-second bar?

          Comment


            #6
            Also, would you need OnMarketData() for returning the highest Ask-Bid spread during the duration of the 1-second bar?

            Comment


              #7
              Correct if you work in OnBarUpdate(), it's event timing / firing will limit your to the incoming tick (if COBC false) level in real time processing. You can certainly track the intrabar developing max spread in your code - however recreating this historically will be giving different results, closest should be replaying on MarketReplay data.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Bertrand. How could I start with coding this using OnMarketData()?
                I can not find an example anywhere and I would eagerly like to learn how to code this.

                Comment


                  #9
                  Baseheadz, I'm unfortunately not aware of another example besides the snippet mentioned in the docs, that would be the primary operation of OnMarketData(), from there onward it's really custom coding what you need.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Baseheadz,

                    I have written about a hundred indicators that use OnMarketData as well as OnMarketDepth (level2 data).

                    I have posted several indicators that you can look at to get some ideas.

                    They are available here.




                    You will have to register with a donation to gain access.

                    There are hundreds of indicators there, well worth the investment.

                    RJay
                    RJay
                    NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Perr0Grande, Yesterday, 08:16 PM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by f.saeidi, Yesterday, 08:12 AM
                    3 responses
                    24 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by algospoke, Yesterday, 06:40 PM
                    1 response
                    14 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by quantismo, Yesterday, 05:13 PM
                    1 response
                    14 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by The_Sec, 04-16-2024, 02:29 PM
                    3 responses
                    16 views
                    0 likes
                    Last Post NinjaTrader_ChristopherS  
                    Working...
                    X