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

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]);
    Paul H.NinjaTrader 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.
          Paul H.NinjaTrader 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
              Paul H.NinjaTrader 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).
                  Paul H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DJ888, 04-16-2024, 06:09 PM
                  4 responses
                  12 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by terofs, Today, 04:18 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post terofs
                  by terofs
                   
                  Started by nandhumca, Today, 03:41 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post nandhumca  
                  Started by The_Sec, Today, 03:37 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post The_Sec
                  by The_Sec
                   
                  Started by GwFutures1988, Today, 02:48 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Working...
                  X