• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

firstbarofsession bars back

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

    firstbarofsession bars back

    Hello-

    I'm looking for a chosen time of day--doesn't have to be firstbarofsession--to get this on the chosen bar:
    var = value[0] - value[1];
    then the rest of the bars that day I want this:
    var = var + (value[0] - value[1]);

    This does not work:

    if (firstbarofsession)
    var = value[0] - value[1];
    else
    var = var + (value[0] - value[1]);

    I always get var = value[0];

    How can this be done?

    I'm using intraday tick bars.

    Thank you.

    #2
    Hello imalil,

    Thanks for your post.

    "var" is a C# keyword that means to allow the compiler to determine the type of variable that follows the word "var".

    I would suggest something like: var X = value[0] - value[1]; In this case X would contain the value and var would define X as a double variable.

    However, this statement:

    if (firstbarofsession)
    var = value[0] - value[1];
    else
    var = var + (value[0] - value[1]);


    even if changed to:

    if (firstbarofsession)
    var X = value[0] - value[1];
    else
    X = X+(value[0] - value[1]);


    would not work because you would be defining the variable conditionally.

    If you know the variable will be used as a double it would be better to declare the variable as a double and you may want to declare it at the class level, for example:

    public class imalilTest : Indicator
    {
    private double myAccumulator = 0.0; // declare as double and set to zero value.
    protected override void OnStateChange()
    {


    then in the OnBarUpdate() section:

    if (Bars.IsFirstBarOfSession)
    myAccumulator = value[0] - value[1];
    else
    myAccumulator = myAccumulator + (value[0] - value[1]);
    PaulNinjaTrader Customer Service

    Comment


      #3
      Thanks, I'll try it.

      Incidentally, I typed 'var' as an abbreviation for variable. I'm not actually using 'var' as my variable. But thanks for the info.

      Comment


        #4
        Hello-

        I did this:

        public class imalilTest : Indicator
        {
        private double myAccumulator = 0.0; // declare as double and set to zero value.
        protected override void OnStateChange()
        {

        then in the OnBarUpdate() section:

        if (Bars.IsFirstBarOfSession)
        myAccumulator = value[0] - value[1];
        else
        myAccumulator = myAccumulator + (value[0] - value[1]);
        I'm printing to output in both conditions so I know value[0] & value[1] have values, and I know it stays in the 'else' statement, myAccumulator = myAccumulator + (value[0] - value[1]);, on every bar, but the only value I get is myAccumulator = value[0];.

        I'm using simulated data feed on a test tickbar workspace.

        Is there a function that says something like 'if (current bar is (user-defined time))' instead of if (Bars.IsFirstBarOfSession)?

        I'm looking for anything that will let my variable accumulate the way I want it to.

        Thank you.

        Comment


          #5
          Hello imalil,

          Thanks for your post.

          Please post or provide your exact code used.
          PaulNinjaTrader Customer Service

          Comment


            #6
            public class Imbaltest : Indicator
            {
            private double VolAccum = 0.0;
            private double ImbalAccum = 0.0;
            private double ATSAccum = 0.0;



            if (Bars.IsFirstBarOfSession)
            {
            VolAccum = DeltaModSeries[0] - DeltaModSeries[1];
            ImbalAccum = AISaskbidFactorDiffSeries[0] - AISaskbidFactorDiffSeries[1];
            ATSAccum = SMAdiffSeries[0] - SMAdiffSeries[1];
            }
            else
            {
            Print("VolAccum1 " + VolAccum.ToString("0"));
            Print("ImbalAccum1 " + ImbalAccum.ToString("0"));
            Print("ATSAccum1 " + ATSAccum.ToString("0"));
            VolAccum = VolAccum + (DeltaModSeries[0] - DeltaModSeries[1]);
            ImbalAccum = ImbalAccum + (AISaskbidFactorDiffSeries[0] - AISaskbidFactorDiffSeries[1]);
            ATSAccum = ATSAccum + (SMAdiffSeries[0] - SMAdiffSeries[1]);
            Print("VolAccum2 " + VolAccum.ToString("0"));
            Print("ImbalAccum2 " + ImbalAccum.ToString("0"));
            Print("ATSAccum2 " + ATSAccum.ToString("0"));
            }
            Regarding: if (Bars.IsFirstBarOfSession): does this need to occur at the beginning of OnBarUpdate? Can it occur towards the end after certain variables have been calculated?

            Sorry for the dumb questions, I just want to get this to work.

            Thank you.

            Comment


              #7
              Hello imalil,

              Thanks for your reply and posting some of your code.

              I do not see anything that would prevent the accumulators from accumulating. You may want to create a simple test indicator that only has one accumulator in which to test.

              I've attached an example of adding the code to an SMA indicator code and its output which shows it accumulates as expected and is clearly not the value of Value[0].

              You can place Bars.IsFirstBarOfSession anywhere that you need it, it does not need to be at the top of OnbarUpdate unless you have a need for it to be there.
              Attached Files
              PaulNinjaTrader Customer Service

              Comment


                #8
                Thanks for the code, I understand how it works. Mine is still giving me accum = value[0] and never visits the firstbarofsession curly braces. I think where mine differs from your SMA example is the values I want to accumulate are level 2, non historical. They vanish when I refresh my charts. I'm using OnMarketData, calc on bar close. I don't want to calculate my data this way, but as of right now I can't get my results any other way. Is this what's keeping it from accumulating?

                My [0] and [1] values are correct.

                I read the firstbarofsession help guide and saw nothing about level 2 issues.

                I'll gladly use something other than firstbarofsession, I just don't know what that is.

                Thank you.

                Comment


                  #9
                  Hello imalil,

                  Thanks for your reply.

                  You would need to use FirstBarOfSession in the OnBarUpdate() method.

                  You may want to look at the NinjaTrader indicator "BuySellVolume" as an example of accumulators collecting data in OnMarketData() and then plotting that data in OnBarUpdate() (and is where the accumulators are reset).
                  PaulNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Sadat, Today, 09:09 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post Sadat
                  by Sadat
                   
                  Started by login_dejavu, Today, 07:57 AM
                  3 responses
                  13 views
                  0 likes
                  Last Post NinjaTrader_PatrickG  
                  Started by Lender, Yesterday, 05:15 PM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_Jim  
                  Started by rcmcd, Today, 07:52 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_PatrickG  
                  Started by rcmcd, Today, 07:49 AM
                  2 responses
                  9 views
                  0 likes
                  Last Post rcmcd
                  by rcmcd
                   
                  Working...
                  X