Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bid Ask at Tick level for Historical

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

    Bid Ask at Tick level for Historical

    I"ve read all the documentation on tick replay and have been testing code, but I'm not sure if I have it right....still confused...

    Basically I want to accumulate Historical Bid and Ask Volume at the tick level and roll it up to the Primary Series Ten Second Bar

    I'm able to do this in real time with OnMarketData, filtering with e.MarketDataType

    in the help document it mentions setting up two new series "Using Historical Bid/Ask Series", one for Bid and one for Ask, and accessing the Bid/Ask during State.Historical (I assume by filtering

    I did this and it seems to work...I have this quesion

    If the primary series is Second.10 and Calculate.OnEachTick, how should I set up the Bid and Ask Series to provide me with each tick of volume data?

    with Second.10 or Tick.1?

    (Tick.1 seems to cause problems....)

    And then use BarsInProgress to determine if I had a Bid or Ask tick?


    -------------------------------------------------------------------------------------------------------------------

    This then would not use the Tick Replay engine?

    (from the documentation Developing for Tick Replay)


    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    // TickReplay events only occur on the "Last" market data type
    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
    if (marketDataUpdate.Price >= marketDataUpdate.Ask)
    {
    Print(marketDataUpdate.Volume + " contracts traded at asking price " + marketDataUpdate.Ask);
    }

    else if (marketDataUpdate.Price <= marketDataUpdate.Bid)
    {
    Print(marketDataUpdate.Volume + " Contracts Traded at bidding price " + marketDataUpdate.Bid);
    }
    }
    }

    ---------------------------------------------------------------------

    probably the first above is the correct way?

    are there two ways?


    #2
    clarification I accumulate the volume on e.MarketDataType == MarketDataType.Last

    Comment


      #3
      Hello llanqui,

      Thank you for your post.

      If your data provider offers historical bid/ask data, you could call AddDataSeries() with MarketDataType.Bid and MarketDataType.Ask as described on this page:If you go with this approach, the historical bid/ask data will be processed in OnBarUpdate() and OnMarketData() would only be triggered in real-time. If you use Tick Replay, then you can not use this approach of using a MarketDataType.Bid and/or MarketDataType.Ask data series.

      To see if your data provider offers historical bid/ask data, please see the "Data by provider" grid here:


      If you use Tick Replay, you can get the bid and ask at the time of a trade, since OnMarketData will replay the Last data historically. That said, you won't be able to get accurate bid/ask volume with Tick Replay enabled. The OnMarketData() page in the help guide has the following note that explains this:
      If used with TickReplay, please keep in mind Tick Replay ONLY replays the Last market data event, and only stores the best inside bid/ask price at the time of the last trade event. You can think of this as the equivalent of the bid/ask price at the time a trade was reported. As such, historical bid/ask market data events (i..e, bid/ask volume) DO NOT work with Tick Replay. To obtain those values, you need to use a historical bid/ask series separately from TickReplay through OnBarUpdate(). More information can be found under Developing for Tick Replay.​


      You mentioned, "Basically I want to accumulate Historical Bid and Ask Volume at the tick level and roll it up to the Primary Series Ten Second Bar" which would require you to use the first approach of adding bid/ask data series without Tick Replay enabled.

      I hope this helps to clarify. Please let me know if I may be of further assistance.

      Comment


        #4
        ok, thanks....

        then this part of the question...



        If the primary series is Second.10 and Calculate.OnEachTick, how should I set up the Bid and Ask Series to provide me with each tick of volume data?

        with Second.10 or Tick.1?

        (Tick.1 seems to cause problems....)

        And then use BarsInProgress to determine if I had a Bid or Ask tick?


        ---------------------------

        If the primary series is set to Calculate.OnEachTick, do the second and third series also (when set to Second.10) trigger on each tick?

        Comment


          #5
          Hello llanqui,

          Thank you for your reply.

          What kinds of problems are you experiencing? I don't quite understand what you are expressing as Second.10 or Tick.1. The syntax to add each would be as follows:
          Code:
          // add a 10 second AAPL data series using the ask
          AddDataSeries("AAPL", BarsPeriodType.Second, 10, MarketDataType.Ask);
          
          // add a 1 tick AAPL data series using the bid
          AddDataSeries("AAPL", BarsPeriodType.Tick, 1, MarketDataType.Bid);
          Then you could certainly use BarsInProgress to see when OnBarUpdate() is called for an added data series, just as you would for any multi-series script:


          The calculate property applies to OnBarUpdate() in general, so yes whatever calculate property is selected (on each tick, for example) would apply not only to the primary series, but to all added series as well:


          You could get the volume of each bar using the Volumes array (or using Volume with a BarsInProgress filter):


          Please feel free to reach out with any additional questions or concerns.

          Comment


            #6
            thanks, yes I was using short hand... (Second 10)

            also, with the two new DataSeries it seems I am accumulating three times the volume expected in Realtime....could this be because the second and third data series are also triggering OnMarketData?

            How to only get the events from the Primary Series? (in OnMarketData) ??

            Comment


              #7
              in relation to the last question....is it that when I add two new DataSeries that two new datastreams are created and that each triggers OnMarketData()?

              I only need the two additional DataSeries during State.Historical.....immediately when it transitions to State.Realtime I have no more need for these DataSeries

              1) Is there a way to drop the DataSeries when the State transitions .... if they are causing more overhead that needed, and also,

              2) if they are triggering additional OnMarketData events (above the Primary Series), how do I filter them out in OnMarketData?

              thx

              Comment


                #8
                Originally posted by llanqui View Post
                in relation to the last question....is it that when I add two new DataSeries that two new datastreams are created and that each triggers OnMarketData()?

                I only need the two additional DataSeries during State.Historical.....immediately when it transitions to State.Realtime I have no more need for these DataSeries

                1) Is there a way to drop the DataSeries when the State transitions .... if they are causing more overhead that needed, and also,

                2) if they are triggering additional OnMarketData events (above the Primary Series), how do I filter them out in OnMarketData?

                thx
                Please see the following note from the OnMarketData() page in the help guide:
                With multi-time frame and instrument strategies, a subscription will be created on all bars series added in your indicator or strategy strategy (even if the instrument is the same). The market data subscription behavior occurs both in real-time and during TickReplay historical​


                Another note on that page is that OnMarketData() is expected to be called after OnBarUpdate(). This means that the BarsInProgress that called OnBarUpdate() should also allow you to filter the OnMarketData() updates with BarsInProgress filters. Take the following snippet, for example:
                Code:
                        protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
                        {
                //            if (BarsInProgress != 0)
                //                return;
                            Print("OMD BarsInProgress: " + BarsInProgress);
                            // Print some data to the Output window
                            if (marketDataUpdate.MarketDataType == MarketDataType.Last)
                                Print(string.Format("Last = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume));
                            else if (marketDataUpdate.MarketDataType == MarketDataType.Ask)
                                Print(string.Format("Ask = {0} {1} ", marketDataUpdate.Price, marketDataUpdate.Volume));
                            else if (marketDataUpdate.MarketDataType == MarketDataType.Bid)
                                Print(string.Format("Bid = {0} {1}", marketDataUpdate.Price, marketDataUpdate.Volume));
                        }
                ​
                If you run this on a 1-minute ES 09-23 chart, for example, and call AddDataSeries() for 1-minute ES 09-23 ask series and a 1-minute ES 09-23 bid series, try it out with the BarsInProgress check commented and uncommented to see the difference in its behavior and which prints are output to the NinjaScript Output window.

                If you plan to work with OnMarketData() in your script, it is important to understand how it functions and I recommend reading all of the notes from the help guide page to help deepen that understanding. If you have additional questions about the info in the help guide or something that is not addressed in the help guide, you may certainly reach out to support for assistance, though the resources available could help to answer your questions or clarify information more immediately than waiting for a response from our team. I highly suggest taking advantage of these resources when available.

                To address your first question, "1) Is there a way to drop the DataSeries when the State transitions .... if they are causing more overhead that needed, and also," the answer is that no, you may not remove a data series once it has been added. This has been addressed in other forum threads, such as the following:


                As mentioned on that page, you can prevent calculations in OnBarUpdate() from occurring for a specific BarsInProgress. For your situation, if you only need the added data series in State.Historical and they are no longer needed in State.Realtime, you could add the following logic to filter them out in Realtime:
                Code:
                if ((BarsInProgress == 1 || BarsInProgress == 2) && State == State.Realtime)
                return;
                You may also verify the way this works by adding prints into your script to check what BarsInProgress is called and what the State is to ensure it is behaving how you would like it to.

                Please feel free to reach out with any additional questions or concerns.

                Comment


                  #9
                  Very good...thank you...


                  I wasn't sure if BarsInProgress would work in OnMarketData........ your note indicates it does...

                  but if I remember correctly Close[0], Volume [0] and others do not work

                  I assume because it runs in a different thread than OnBarUpdate...??

                  Comment


                    #10
                    Originally posted by llanqui View Post
                    Very good...thank you...


                    I wasn't sure if BarsInProgress would work in OnMarketData........ your note indicates it does...

                    but if I remember correctly Close[0], Volume [0] and others do not work

                    I assume because it runs in a different thread than OnBarUpdate...??
                    There is a note about what you are referring to on the following page:

                    Note: In most cases, you will access the historical price series using a core event handler such as OnBarUpdate. For more advance developers, you may find situations where you wish to access historical price series outside of the core event methods, such as your own custom mouse click. In these advanced scenarios, you may run into situations where the barsAgo pointer is not in sync with the current bar, which may cause errors when trying to obtain this information. In those cases, please use the Bars.Get...() methods with the absolute bar index, e.g., Bars.GetClose(), Bars.GetOpen(), etc.​
                    In some methods outside of OnBarUpdate, the barsAgo pointer is not in sync with the current bar and could result in errors when you try to get info like Close[0] or Volume[0]. In those cases, you could use the Bars.Get() method with the bar index rather than a barsAgo index. For example, Bars.GetClose() or Bars.GetVolume(). There is a list of methods and properties related to bars objects here:


                    Thank you for your time and patience.

                    Comment

                    Latest Posts

                    Collapse

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