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'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
    Brandon H.NinjaTrader Customer Service

    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.
        Brandon H.NinjaTrader Customer Service

        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 wzgy0920, Yesterday, 09:53 PM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by Rapine Heihei, Yesterday, 07:51 PM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by kaywai, Today, 06:26 AM
          1 response
          6 views
          0 likes
          Last Post kaywai
          by kaywai
           
          Started by ct, 05-07-2023, 12:31 PM
          6 responses
          206 views
          0 likes
          Last Post wisconsinpat  
          Started by kevinenergy, 02-17-2023, 12:42 PM
          118 responses
          2,780 views
          1 like
          Last Post kevinenergy  
          Working...
          X