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

C# Newbie - Please Help Me Understand Using Bar Data

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

    #31
    EDIT -- DO NOT USE ADD DATA SERIES if you don't need it!! This was causing OnBarUpdate() to execute more than once, which may trip you up if you're not expecting it. I am a total n00b and was using it, even though there wasn't a good reason to. Thanks, bltdavid!!

    Small update -- got everything working, but still getting OnBarUpdate() loading twice. I've worked around it, but I'd like to fix that eventually.

    For the newbies, I figured out how you deal with switching chart time periods so thought I'd share -- its in the State.Configure section:

    Code:
                else if (State == State.Configure)
                {
                    if(Bars.BarsPeriod.BarsPeriodType == BarsPeriodType.Day)
                    {
                         AddDataSeries(BarsPeriodType.Day, Bars.BarsPeriod.Value); // Get Daily data series
                    }                
    
                    if(Bars.BarsPeriod.BarsPeriodType == BarsPeriodType.Minute)
                    {
                         AddDataSeries(BarsPeriodType.Minute, Bars.BarsPeriod.Value); // Get Minute data series
                    }
                }​
    The veterans probably already know how -- but thought I'd save someone the trouble if they were all happy about making an indicator on one timeframe and wanted to try it out on the Daily bar charts.
    Last edited by TallTim; 12-18-2022, 11:53 AM.

    Comment


      #32
      Originally posted by TallTim View Post
      but still getting OnBarUpdate() loading twice.
      Ok, tough love coming your way. Get ready.

      Why do you say 'loading twice'?

      OnBarUpdate is a method, you write it, NT calls it (according to the frequency
      dictated by the Calculate setting) and when it's called, the code is executed.
      The concept of 'loading' (into RAM) happened a long time ago, my friend.

      And OnBarUpdate is not 'executing twice' to cause two Print statements.

      You have a hard time describing and/or proving to yourself the simplest
      explanation: OnBarUpdate is being called once per bar close, but it's
      generating 2 print statements each time it executes. You need to prove
      this to yourself
      -- so concoct better print statements to do that.

      Also, why are you (still) using Count?
      You are probably confused about what Count does.
      Stop using it.

      [EDIT: Even if you wanted to lookback across the whole chart, you'd use
      CurrentBar for that, not Count.]

      ​Are you trying to distinguish between historical bars and realtime bars? You
      seem to want the 'whole chart' loaded. What do you mean by that?

      Saying you are waiting for the whole chart to be loaded is imprecise. Do you
      mean you're waiting for all historical bars to be drawn onto the chart?

      Try this:
      Rewrite your code without using Count.

      Why do that?
      Because then you'd be forced to rely on CurrentBar, and the State property,
      which is probably what you should be doing. You can always check State
      inside OnBarUpdate, not just OnStateChange ...

      Also, earlier you asked about an interval, a sliding window.
      Are you still using that concept?

      Let's say your interval is '5' -- you need 5 bars to lookback for your sliding
      window to do your analysis. So, you only need 5 bars, not the whole chart.

      If you want your sliding window to be all the bars on the chart, then use
      CurrentBar for your interval -- but if someone loads 2 years of minute data,
      that is an awful lot of lookback, and most of that lookback would probably be
      pointless -- so skip that, CurrentBar is probably a terrible interval value. Maybe
      you should think in terms of a maximum lookback, say 100 bars or so ...

      So, let's get functionally descriptive, what are you trying to do, exactly?

      ​Please explain, precisely and clearly,
      1. What you're trying to do.
      2. Why you're using Count.
      3. Why you need the 'whole chart' loaded.
      4. What to do when 'whole chart loaded' vs 'bar just closed'.

      -=o=-

      Never the less, my friend, do hang in there.
      You're making tons of progress, but it's not easy.
      Let's reboot your thinking and get your brain back up & running.

      Comment


        #33
        I appreciate the reply - and its a lot to digest, so allow me some wiggle room.

        What confuses me is when I stripped everything down to the barest of the bare -- the Print statement still executes two times.

        In my 'Bars with currant glaze' demo indicator -- it doesn't do that, but when I compare the two I don't see any differences that are readily apparent, so I'm not sure why it happens.

        Is there a simple explanation for that?

        I am going to parse and consider your words, I thank you for taking the time to mentor me on this.

        Comment


          #34
          Originally posted by TallTim View Post
          I appreciate the reply - and its a lot to digest, so allow me some wiggle room.

          What confuses me is when I stripped everything down to the barest of the bare -- the Print statement still executes two times.

          In my 'Bars with currant glaze' demo indicator -- it doesn't do that, but when I compare the two I don't see any differences that are readily apparent, so I'm not sure why it happens.

          Is there a simple explanation for that?

          I am going to parse and consider your words, I thank you for taking the time to mentor me on this.
          No problem.
          Yes, there will be a very simple explanation.

          Remove the AddDataSeries and you will remove the 'duplicate' output
          you see. That's the problem.

          Why?
          Because your script is working with multiple data series, and each
          data series calls OnBarUpdate each time a bar closes in its own series.



          Yep, I missed it before, but that AddDataSeries explains everything.

          So, now you need to learn about BarsInProgress.
          [Normally, this depth of learning is postponed, but you're forcing misery
          upon yourself by using a secondary data series without understanding
          these ramifications.]

          -=o=-

          So, let's discuss why you see two outputs.

          Every bar series on every chart is known as the primary bar series,
          but additional bar series can be made available to your code. These
          bar series are added via AddDataSeries, but they are invisible on the
          chart, only available in code.

          So, how do you access an additional data series added via AddDataSeries?

          Easy.
          Use BarsInProgress to differentiate between the two.
          That's exactly why it exists.

          Inside OnBarUpdate,
          When BarsInProgress is 0, you are processing the primary data series.
          When BarsInProgress is 1, you are processing the secondary data series.

          That is, each time a bar closes on the primary data series, OnBarUpdate
          is called with BarsInProgress set to 0 -- and Open/High/Low/Close/etc
          series reflect the primary bars.

          And ... guess what?
          Each time a bar closes on the secondary data series, OnBarUpdate is
          called with BarsInProgress set to 1 -- and Open/High/Low/Close/etc
          series reflect the secondary bars.

          It's sometimes called the 'BarsInProgress context'.

          Yep, if you're gonna use AddDataSeries, there are special considerations
          you must be aware of. If you have 5 calls to AddDataSeries, then you'll
          have a variety of calls into OnBarUpdate each time a bar closes on any
          of the 6 bar series (primary + 5 secondary), BarsInProgress would vary
          in values 0 to 5 -- yep, your OnBarUpdate will become a very busy bee.

          -=o=-

          What to do?

          If you don't really need it (and you probably don't) comment out the
          AddDataSeries.

          Or,
          You could add some guard code at the top of your OnBarUpdate to
          ignore the secondary data series,

          Code:
          protected override void OnBarUpdate()
          {
              if (BarsInProgress != 0)
                  return;
          
              // remainder of your code
          }
          Or,
          just segregate your code with if (BarsInProgress == 0) and with an
          else if (BarsInProgress == 1), like the examples seen here.

          -=o=-

          Using AddDataSeries means you're jumping into the 'Multi-Time Frame',
          (aka MTF) end of the pool -- that end of the pool is the deep end, and
          its easy for newbies to drown there.

          But,
          If you really want to go there, study (and then study again) this.

          Coding an MTF script is relatively advanced stuff.

          Last edited by bltdavid; 12-17-2022, 08:11 PM.

          Comment


            #35
            I have to parse the rest of this, but I want to make it clear that in my bare-bones example I'm only working with one data series, so the -- "Remove the AddDataSeries and you will remove the 'duplicate' output you see. That's the problem.​" doesn't make sense to me.

            In the "Bars with currant glaze" indicator - only one data series.

            In my stripped down windowing indicator - only one data series. I only added the second one today to go between minute/daily charts.

            Thanks for the replies, its a lot to think about!

            Comment


              #36
              Sorry, my above admonishment was misplaced.
              I missed it, I should have focused on your use of AddDataSeries.
              That's what I should have done.

              Hmm, this therapy session will help.

              Imagine, if you will, a proper NinjaScript interrogation.
              You're sitting at a table, with a bright light in your face.
              Your no-nonsense coding Master walks in, wearing black
              Ninja garb head to toe, his face is concealed, his black
              leather gloves are worn tightly.

              Master: Tie his hands.
              You: What? Why? What did I do?
              Master: Who told you about AddDataSeries?
              You: Huh? I don't know.
              (slap slap)
              You: Ouch!
              Master: Why are you using AddDataSeries?
              You: I thought I had to.
              (slap slap)
              Master: Says who?
              You: Ouch! How do I get data without it?
              (slap slap)
              Master: You fool! The primary bars on the chart, they're free!
              You: What?
              Master: Everybody gets BarsInProgress zero for free!
              (slap slap)
              You: Oh? Oh ... I didn't know ... thank you, master.
              Master: You don't need AddDataSeries to get your chart data.
              (slap slap)
              Master: Untie his hands. You have much to learn, grasshopper.
              You: Ok, I won't use AddDataSeries until I am ready, master.
              Master: Much progress you have made, your own master you shall soon become.

              lol ... all in good fun

              Comment


                #37
                Hmm.

                Why do you think I asked you to attach your exact script?

                I actually took the time to copy & paste the TTBullBear
                indicator in your post no. 30 above.

                Yep, I did that. You could have made it easy by attaching
                your exact script, but, nope, you took the chance that I
                would have the time to do all the extra work to copy &
                paste it into a file, in order to duplicate your problem.

                I don't normally do that, it takes too much extra time.
                But, it's Saturday, it's slow, I made an exception.

                Well, this code, as seen in your post #30,

                Code:
                else if (State == State.Configure)
                {
                AddDataSeries(BarsPeriodType.Minute, Bars.BarsPeriod.Value); // Get data series
                }​
                was the problem. Do you not agree?

                Without an exact script to discuss, helping someone to
                understand their problem can be very hit or miss.

                It's like driving your car to the mechanic and complaining
                about a nasty sound in your pickup -- the mechanic wants
                to see the pickup.
                Last edited by bltdavid; 12-17-2022, 08:53 PM.

                Comment


                  #38
                  Hahahah nice. I promise to examine this further, but I'm brain fried after a few hours of doing this. I don't normally code in C#, so going back and forth between languages taxes me.

                  Sorry about the code paste -- I should've made it a file attachment, or done both so people could see the code AND get the file quickly. So that point is well taken!!

                  Appreciate the replies, I understand it can be frustrating to get someone to "see", but I can tell you when I finally do -- I give you full credit for being patient and wise.

                  Enjoy the weekend!

                  Comment


                    #39
                    Originally posted by TallTim View Post
                    I promise to examine this further, but I'm brain fried after a few hours of doing this.
                    Ok, no prob.

                    This may help.
                    You were closer to being right than I gave you credit for.

                    How so?
                    You said your OnBarUpdate was being called twice.
                    That the output was being duplicated.
                    You couldn't understand why.

                    In fact, it was probably being duplicated exactly.
                    AddDataSeries is the reason.

                    If you created a 1-Min chart, and then loaded the TTBullBear
                    indicator from post #30, AddDataSeries added a secondary
                    1-Min data series.

                    In fact, whether the chart itself was 1-min or 5-min, or
                    any number of minutes, the AddDataSeries created an
                    exact duplicate secondary data series.

                    Your OnBarUpdate ignored all historical bars until the last
                    one, at which time it would loop through all historical bars
                    and print the output. Yep, all your output was generated
                    one time, from the last call to OnBarUpdate.

                    But this happened twice.
                    Once for BIP=0 and then again for BIP=1.

                    Understanding AddDataSeries and BarsInProgress are key.
                    Commenting out the AddDataSeries solves the duplicate
                    output problem that you could not understand.

                    Study the output from the attached script.
                    Note how all BIP=0 output is printed first,
                    then BIP=1 output is printed.

                    Q.E.D.
                    Problem solved.
                    Do you agree?

                    Attached Files
                    Last edited by bltdavid; 12-18-2022, 11:40 AM.

                    Comment


                      #40
                      bltdavid hahaha oh man, is my face red.

                      yes -- you are exactly right (as if there was any doubt) that using "AddDataSeries" causes the reload.

                      It is funny - I appreciated your humor about that - "WHO TOLD YOU ABOUT ADD DATA SERIES" in your previous post. I suppose in my mad dash to get my indicator working I saw a reference to it somewhere and falsely thought "Oh man, I gotta put that in there."

                      Well, I commented out the AddDataSeries entries and everything worked just fine.

                      I know its a small step in my journey, but thank you -- I'll examine the rest of your advice more thoroughly this coming week.

                      Comment


                        #41
                        So you'll be happy to hear that I stripped out any reference to "Count", and I'm now processing my indicator using CurrentBar when there's been enough loaded to start.

                        I still think CurrentBar should be renamed to "ElapsedBar" or "BuiltBars" but that is water under the bridge for now.

                        Having trouble reference an indicator from another though, so that's a whole other world of hurt.

                        (I'll just post a new topic since this thread is pretty long.)
                        Last edited by TallTim; 12-19-2022, 10:40 AM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by kevinenergy, 02-17-2023, 12:42 PM
                        118 responses
                        2,778 views
                        1 like
                        Last Post kevinenergy  
                        Started by briansaul, Today, 05:31 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post briansaul  
                        Started by fwendolynlpxz, Today, 05:19 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post fwendolynlpxz  
                        Started by traderqz, Yesterday, 12:06 AM
                        11 responses
                        28 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by PaulMohn, Today, 03:49 AM
                        0 responses
                        8 views
                        0 likes
                        Last Post PaulMohn  
                        Working...
                        X