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

How to get indicator value based on another chart timeframe post

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

    How to get indicator value based on another chart timeframe post

    Hello,

    I have a custom indicator applied to a 1m, 5m, and 10m chart. This indicator's value is calculated based on the timeframe it lives on, so the value will be different on these three charts at any given time. I am wanting to check if the indicator is pointing up or down on all three charts at the 1m post. This is easy to check on the 1m chart by comparing the value of the current bar to the previous bar. Ex:
    Code:
    CustomIndicator(<Primary> (1 minute))[0] > CustomIndicator(<Primary> (1 minute))[1]
    However, how do I check what the indicator direction or value is on the 5m and 10m charts at every 1m post?

    #2
    Hello DawnTreader,

    Thank you for your post.

    You can add a 1 minute series to your script using AddDataSeries().



    Use BarsInProgress to check the value of the indicator within the context of the added 1 minute series.



    Code:
    protected override void OnStateChange()
    {
    if (State == State.Configure)
    {
    // Add a 1 minute Bars object: BarsInProgress index = 1
    AddDataSeries(BarsPeriodType.Minute, 1);
    }
    }
    
    protected override void OnBarUpdate()
    {
    // Check which Bars object is calling the OnBarUpdate() method
    if (BarsInProgress == 0)
    {
    // A value of zero represents the primary Bars which is the
    // 5 minute chart.
    // Do something within the context of the 5 minute Bars here
    }
    else if (BarsInProgress == 1)
    {
    // A value of 1 represents the secondary 1 minute bars added in OnStateChange() State.Configure
    // Do something within the context of the 1 minute Bars
    }
    }
    You may also want to read through this Help Guide page on considerations for multi time frame scripts.



    Please let us know if you have any further questions.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hey, thanks for the reply! This is helpful, however, I'm still having a hard time figuring out how to check what the indicator direction or value is on the 5m and 10m charts at every 1m post? My main chart is the 1m. Everything I try is getting the Custom Indicator values of the 5m and 10m at the post of those timeframes, instead of their values at the 1m post. Any other insights?

      Comment


        #4
        Hello DawnTreader,

        Thank you for clarifying, I misunderstood your original post.

        If your main chart is the 1 minute series, then you would add a 5 minute and a 10 minute series to your script using AddDataSeries().

        Then you could have to use their respective price series to calculate the value. When you add a data series to your script, you can uses the Closes, Opens, and Lows, and Highs series to do calculations using a specific bars object.

        Code:
        protected override void OnStateChange()
        {
            if (State == State.Configure)
            {
                // Adds a 5-minute Bars object to the strategy and is automatically assigned
                // a Bars object index of 1 since the primary data the strategy is run against
                // set by the UI takes the index of 0.
                AddDataSeries("AAPL", BarsPeriodType.Minute, 5);
            }
        }
         
        protected override void OnBarUpdate()
        {
            // Compares the primary bar's close price to the 5-minute bar's close price
            if (Closes[0][0] > Closes[1][0])
                Print("The primary bar's close price is greater");
        
        }​​



        What is the calculation your script is doing for the value? Perhaps I can provide a more concrete example related to the calculation you are doing in your script.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thanks. Here is some more context with an abbreviated setup with my custom indicator (LittleGreen). I'm stuck at the `// HELP!!!` comment. I don't know how to get the value of LittleGreen5m on the previous 1m bar/candle close.

          Code:
          private NinjaTrader.NinjaScript.Indicators.LittleGreen LittleGreen1m;
          private NinjaTrader.NinjaScript.Indicators.LittleGreen LittleGreen5m;
          ​
          protected override void OnStateChange()
          {
            if (State == State.SetDefaults)
            {
              ...
              Calculate = Calculate.OnEachTick;
              ...
            }
            else if (State == State.Configure)
            {
              AddDataSeries(Data.BarsPeriodType.Minute, 5); // index = 1
            }
            else if (State == State.DataLoaded)
            {
              LittleGreen1m = LittleGreen(Closes[0]);
              LittleGreen5m = LittleGreen(Closes[1]);
            }
          }
          
          protected override void OnBarUpdate()
          {
            // Ignore bar update events for the 5m Bars object added above
            if (BarsInProgress != 0)
              return;
          
            if (CurrentBars[1] < 1 || CurrentBars[2] < 0)
              return;
            
            // Check which Bars object is calling the OnBarUpdate() method
            // 1m Bars
            if (BarsInProgress == 0)
            {
              // HELP!!!
              // How do I check if the LittleGreen5m value is greater on this current bar than its value on the previous 1m bar close? The second conditional statement below doesn't work, it just gets the values at the 5m posts.
              if ((LittleGreen1m[0] < LittleGreen1m[1])
                && (LittleGreen5m[0] < LittleGreen5m[1]))
              {
                // TODO
              }
            }
            else if (BarsInProgress == 1)
            {
              // A value of 1 represents the secondary 5 minute bars added in OnStateChange() State.Configure
              // Do something within the context of the 5 minute Bars
            }
          }​
          Last edited by DawnTreader; 05-09-2024, 12:29 PM.

          Comment


            #6
            Hello DawnTreader,

            Thanks for sending that over.

            Before I create a sample script for you, I just want to make sure I'm understanding correctly:
            • You are running this script on a 1 minute chart.
            • On every update of the 1 minute chart, you want to get the value of the LittleGreen indicator calculated on 3 minute bars and 5 minute bars.
            Is that correct? If not, please clarify.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Yes, I'm enabling this Strategy on the 1m chart. On every 1m update (bar close), I want to get the value of the LittleGreen indicator that's attached to, or calculated on, a 5m chart and a 10m chart. The indicator plots a line, so I'm just wanting to know if it's pointing up or down (on the 5m and 10m charts/bars) on every 1m update/bar close. Does that make sense?

              Comment


                #8
                Hello,

                Please see the attached example which demonstrates using the SMA.

                If you have any further questions, please let us know.
                Attached Files
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for that example! That seems to work fine for the SMA and my custom indicator, however, this SMA 5m and 10m are only updated on each post. My LittleGreen indicator is being updated on every tick across all timeframes. So, is there a way to get those tick values at the 1m post, like we're doing now? Does that make sense?

                  Thanks so much, I really appreciate your help!
                  Last edited by DawnTreader; 05-10-2024, 09:58 AM.

                  Comment


                    #10
                    Hello DawnTreader,

                    Sorry, I'm not clear on what you're asking. You stated:

                    On every 1m update (bar close), I want to get the value of the LittleGreen indicator that's attached to, or calculated on, a 5m chart and a 10m chart
                    Are you instead wanting to get the updated value every tick instead of every 1 minute update?
                    Gaby V.NinjaTrader Customer Service

                    Comment


                      #11
                      The LittleGreen indicators are updated on every tick (their values change every tick). I wan to get the values of LittleGreen (1m, 5m, 10m) on every 1m update/post.

                      The main goal is to determine if LittleGreen is pointing up or down (on all timeframes—1m, 5m, and 10m) on every 1m update/post. Does that make sense?

                      Comment


                        #12
                        Hello DawnTreader,

                        That is what the sample script is doing. You can still call it in the same way.

                        Below is a revised version of the script. This time it runs Calculate.OnEachTick (since the code you posted uses OnEachTick) and also calls a modified SMA that calculates OnEachTick (the modified SMA is also included in the .zip).

                        I encourage you to review the output of the script and see how the values of the indicator update on every call.
                        Attached Files
                        Gaby V.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you, that makes sense. I'm still not quite connecting the dots on how to check the SMA tick values when the 1m bars post. Since `OnBarUpdate()` fires on every tick because of `Calculate.OnEachTick`, how can I check the SMA tick values on each 1m post?

                          Also, how do I compare, or check, if the SMA is pointing up or down on each 1m post (probably the same question)? If the SMA tick value on each 1m post is greater than the previous 1m post, does that mean it's pointing up?

                          Thanks for the help

                          Comment


                            #14
                            Since `OnBarUpdate()` fires on every tick because of `Calculate.OnEachTick`, how can I check the SMA tick values on each 1m post?
                            If your script is running on a 1minute series, then you can check anytime BarsInProgress == 0. If you only want to check once per bar when running Calculate.OnEachTick (on every close of the 1 minute bar), include a check for IsFirstTickOfBar.



                            how do I compare, or check, if the SMA is pointing up or down on each 1m post (probably the same question)?
                            What exactly would you consider "pointing up or down"? If you want to compare the previous value of the SMA to current value of the SMA, you can check as below:

                            if (SMA1[0] < SMA1[1])
                            //current value of the sma is less than its value 1 bar ago


                            if (SMA1[0] > SMA1[1])
                            //current value of the sma is greater than its value 1 bar ago

                            Please let me know if you have any further questions.
                            Gaby V.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks, IsFirstTickOfBar is helpful! When I mean up or down, I literally mean, is the line pointing up or down in direction.

                              Yes, `(SMA1[0] < SMA1[1])` works great for the 1m, because I want to check all SMA values on the 1m post. However, checking the SMA values of the 5m and 10m bars on the 1m post will vary. For example, at 7:31 the SMA on the 5m may be pointing up, but at 7:32 it may be pointing down. So, comparing the 5m SMA like the 1m—`if (SMA2[0] < SMA2[1])`—won't work because that checks the value at the current bar and at the previous 5m bar post. Right? I want to determine if the 5m and 10m SMA's are pointing up or down at every 1m post. Is that more clear? I can provide screenshots if that will help.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by lcgamarra_pinnacle, Today, 09:33 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post lcgamarra_pinnacle  
                              Started by MokhtarVerdi, Today, 07:21 AM
                              1 response
                              8 views
                              0 likes
                              Last Post MokhtarVerdi  
                              Started by chartchart, 05-19-2021, 04:14 PM
                              6 responses
                              659 views
                              1 like
                              Last Post mmenigma  
                              Started by gabberdim, Yesterday, 09:14 AM
                              4 responses
                              31 views
                              0 likes
                              Last Post sidlercom80  
                              Started by cmtjoancolmenero, Today, 07:22 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post cmtjoancolmenero  
                              Working...
                              X