Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Historical bid/ask/last data series in OBU

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

    Historical bid/ask/last data series in OBU

    Hi, I would like to calculate historical buy/sell volume in the OBU method. For this I have added a data series:
    Code:
    AddDataSeries(Instrument.FullName, BarsPeriodType.Tick, 1, MarketDataType.Last);
    In OnBarUpdate:
    Code:
    if (BarsInProgress == 1 && State == State.Historical)
    {
        Print(" lastVolume=" + Volume[1]);
    }
    how do I find out if the volume is a buy or sell volume?
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thanks for your post.

    You would need to compare the close price of the bar to the ask or bid.

    I've attached an example of this (created by NinjaTrader_Jim) in the BuySellVolume indicator modified to work with an added 1 tick series. Lines 125 - 128.

    BuySellVolumeOneTick.zip



    Comment


      #3
      Hello _PaulH, thank you very much, this is exactly what I was looking for, very helpful!
      sidlercom80
      NinjaTrader Ecosystem Vendor - Sidi Trading

      Comment


        #4
        Hi _PaulH, maybe you can help me
        In your very good example, I have noticed a small problem. I'm racking my brain about how to solve it, but unfortunately I don't have an answer yet.
        Whenever the indicator is reloaded, there is a data leak at the first candle, determined with calculation method OnEachTick.
        From Status.Historical to State.Realtime. The candle actually has total volume of 615(buy+sell), that's perfect, but with the first tick a reset is made, and the values start again with the counting, although the candle has not yet been formed. I was able to determine where the problem lies, but not how to solve it, maybe you can help me?

        here on the first picture the first tick after loading the indicator is NOT yet executed, so still historical. All fine here.
        Click image for larger version

Name:	Screenshot_2.jpg
Views:	369
Size:	40.9 KB
ID:	1169477

        here the values were reset after the first tick
        Click image for larger version

Name:	Screenshot_3.jpg
Views:	314
Size:	43.8 KB
ID:	1169478

        and here seems to be the problem in the code:

        Click image for larger version

Name:	Screenshot_1.jpg
Views:	333
Size:	84.8 KB
ID:	1169479

        Code:
        // If we are Calculate.OnBarClose or we are in Historical processing mode, we are already update to date on the 1 tick series so we reset here
        if (Calculate == Calculate.OnBarClose || (lastBar != CurrentBars[0] && ([U][I][B][COLOR=#e74c3c]State == State.Historical[/COLOR][/B][/I][/U] || State == State.Realtime && indexOffset > 0)))
            ResetValues(false);
        sidlercom80
        NinjaTrader Ecosystem Vendor - Sidi Trading

        Comment


          #5
          Hello sidlercom80,

          Thanks for your reply.

          You would need to create some coding that would recognize the "transition" bar by comparing the Bars.Count to the CurrentBar counter, in addition, the the "State". A transition bar is usually a part historical and part realtime bar

          For example:

          protected override void OnBarUpdate()
          {
          Print ("State: "+State+" CB: "+CurrentBar+" Bars.Count: "+Bars.Count);
          }


          You would see something like:

          State: Historical CB: 4970 Bars.Count: 4974 // only 1 bar per tick historically
          State: Historical CB: 4971 Bars.Count: 4974
          State: Historical CB: 4972 Bars.Count: 4974
          State: Historical CB: 4973 Bars.Count: 4974
          State: Realtime CB: 4973 Bars.Count: 4974

          State: Realtime CB: 4973 Bars.Count: 4974
          State: Realtime CB: 4973 Bars.Count: 4974
          State: Realtime CB: 4973 Bars.Count: 4974
          State: Realtime CB: 4973 Bars.Count: 4974
          State: Realtime CB: 4973 Bars.Count: 4974
          State: Realtime CB: 4974 Bars.Count: 4975

          In this example (Run with Calculate.OnEachTick), the historical bars are only printed once. When connecting to real-time it prints on each tick and you can see the Current Bar (which is zero based) is 1 behind real-time. When the CurrentBar+1 equals Bars.Count and State equal State.Historical, then you have the last historical bar and can save the data from that last historical bar and then process the real-time part of that bar.

          Comment


            #6
            Hi _PaulH, thank you very much for your answer, I appreciate very much!

            the historical bars are only printed once
            that was a real help to me ;-)

            I'm still testing it, but it looks good
            sidlercom80
            NinjaTrader Ecosystem Vendor - Sidi Trading

            Comment


              #7
              Hi, somehow I have problems with it, maybe I'm too stupid for that ;-) I have been trying for quite a while now to calculate the transition bar correctly, unfortunately without success.
              The historical bars and all real-time bars are calculated correctly, the only problem is the transition from historical to real-time.
              Maybe someone can give me a tip on what I'm doing wrong? This is what I have tried so far:

              Click image for larger version

Name:	Screenshot_1.jpg
Views:	336
Size:	117.1 KB
ID:	1177053

              Code:
              protected override void OnBarUpdate()
              {[INDENT]if (BarsInProgress == 0)[/INDENT][INDENT=2]{[/INDENT][INDENT=3]if (CurrentBar + 1 == Bars.Count && State == State.Historical)[/INDENT][INDENT=3]{[/INDENT][INDENT=4]transBuys = buys;
              transSells = sells;
              isLastHistoricalBarProcessed = true;[/INDENT][INDENT=3]}[/INDENT][INDENT]......[/INDENT]
              Code:
              private void ResetValues(bool isNewSession)
              {[INDENT]if (!isLastHistoricalBarProcessed)
              {[/INDENT][INDENT=2]buys = sells = 0;[/INDENT][INDENT]}
              else
              {[/INDENT][INDENT=2]buys = transBuys;
              sells = transSells;
              isLastHistoricalBarProcessed = false;[/INDENT][INDENT]}
              if (isNewSession)
              {[/INDENT][INDENT=2]// Cumulative values (per session) would reset here
              // EMA is not gapless so we ignore in this example[/INDENT][INDENT]}[/INDENT]
               }
              sidlercom80
              NinjaTrader Ecosystem Vendor - Sidi Trading

              Comment


                #8
                Hello sidlercom80,

                Thanks for your reply.

                Can you elaborate on what specific results you are finding with an easily reproducible example? (steps to recreate).

                Comment


                  #9
                  Hi PaulH, thank you very much for your reply! The problem always occurs at the first bar that changes from historical to realtime. I adjust the script accordingly so that it can be compared with the VOL indicator. Only the total bar volume is displayed for testing.
                  What I have noticed:
                  • The historical completely processed bars are correct. (State.Historical)
                  • The first bar that changes from historical to real-time is not calculated quite correctly and remains incorrect even when closing the bar.
                  • All bars that were started and finished in real time are displayed incorrectly at the beginning of the bar by 1-2 volume ticks, but at the end of the closed bar everything is correct. Here it seems that the last tick still takes into account the missing volumes, which should have been calculated appropriately from the beginning.
                  I made a short video for a better understanding: http://www.youtube.com/watch?v=vQKmsBi_wy8
                  Attached Files
                  sidlercom80
                  NinjaTrader Ecosystem Vendor - Sidi Trading

                  Comment


                    #10
                    Hello sidlercom80,

                    Thanks for your reply.

                    My reply in post #5 did not take into consideration how the BuySellVolumeOneTick was coded and I apologize as I should have studied it more closely.

                    From the discussion here we can advise:

                    The first real-time bar cannot be perfect, it is a limitation of the multi time frame approach in the example.

                    "There are deep core limitations surrounding the transition from historical to real-time with Multi Time Frame scripts that prevent the result from being perfect upon transition.

                    The example we have given is designed to address some of these limitations, but not all."








                    Comment


                      #11
                      Hi Paul, thanks for your explanation, I guessed that, see you next time
                      sidlercom80
                      NinjaTrader Ecosystem Vendor - Sidi Trading

                      Comment


                        #12
                        Hello sidlercom80,

                        NinjaTrader_Jim has created a new version of BuySellOneTick that is intended to demonstrate the use of dictionaries and he also improved the transition bar.
                        Please give this a test.

                        BuySellVolumeOneTickAtPriceDictionaryExample.zip

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        596 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        343 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        103 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        556 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        554 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X