Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get indicator's last val in the prev. sesh on the 1st bar of today's sesh?

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

    How to get indicator's last val in the prev. sesh on the 1st bar of today's sesh?

    Example scenario:
    Indicator plots an SMA over several days - added via ninjascript.

    How to, when the 1st bar of "today" prints (only once) get the value of the SMA at yesterday's last bar?

    I am trying to use code similar to this but it is not working as expected, it seems top print more than once and the values don't quite match. Any tips on getting this right?

    if ( CurrentBar == Bars.BarsSinceNewTradingDay) {
    double val = SMA(20)[Bars.BarsSinceNewTradingDay + 1]; // get the value of "last" bar before the new session
    Print(val);
    }

    Why does CurrentBar == Bars.BarsSinceNewTradingDay evaluate to true so many times? I'd expect this to eval true once a day.

    Thank you kindly!
    Last edited by focus333; 10-19-2021, 11:45 PM.

    #2
    Hello focus333,

    Thanks for your post.

    You could use Bars.IsFirstBarOfSession to determine if the current bar processing is the first bar updated in a trading session. Then, you would print out the SMA indicator with a barsAgo value of 1 (SMA(20)[1]) to get the SMA value 1 bar previous to the current session.

    See the attached example script demonstrating this.

    Also, see the help guide page below for more information about IsFirstBarOfSession.

    IsFirstBarOfSession: https://ninjatrader.com/support/help...rofsession.htm

    Let us know if we may assist further.
    Attached Files
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello focus333,

      Thanks for your post.

      You could use Bars.IsFirstBarOfSession to determine if the current bar processing is the first bar updated in a trading session. Then, you would print out the SMA indicator with a barsAgo value of 1 (SMA(20)[1]) to get the SMA value 1 bar previous to the current session.

      See the attached example script demonstrating this.

      Also, see the help guide page below for more information about IsFirstBarOfSession.

      IsFirstBarOfSession: https://ninjatrader.com/support/help...rofsession.htm

      Let us know if we may assist further.
      I'm diving in this one..as I'm figuring out on how to read the 3rd party indicator values inside the indicator.

      From your sample file:

      MySMA[0] = SMA(20)[0];

      if (Bars.IsFirstBarOfSession)
      {
      Print("First bar of current session: " + CurrentBar);
      Print("SMA last bar of prev session: " + MySMA[1] + " Bar of prev session: " + (CurrentBar-1));
      }

      I'm trying to understand about that [0] part there.

      I thought it would be private SMA MySMA first, then go from there.

      There are other indicators such as MACD that need to be read inside the custom indicator.

      Maybe you can clarify on this one, please?

      Thanks!

      Comment


        #4
        Hello nothingbutprofits,

        Thanks for your note.

        MySMA is set up as a plot using a public Series<double> variable in this script instead of setting up MySMA as a private variable by calling private SMA MySMA. This is seen in the Properties section of the previously attached script.

        Since MySMA is a public Series<double> variable, we must specify the barsAgo value for MySMA (MySMA[0]) when assigning a value to it.

        You could also set this up as you described by creating a class-level variable using private SMA MySMA, then assigning a value to the variable with MySMA = SMA(20). See the attached script which is a modified version of the previous script demonstrating this.

        Let us know if we may assist further.

        Attached Files
        Last edited by NinjaTrader_BrandonH; 10-26-2021, 07:38 AM.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Thanks for the updated info. I posted the similar issue..but with more depth detail today.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          574 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          333 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X