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

Processing the last 20 bars that have been completed

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

    Processing the last 20 bars that have been completed

    NinjaTrader Support,

    I am trying to look at the last 20 bars on the chart and assign a score to each bar, for example:

    Red bar with lower low = -2
    Green bar with higher higher = +2

    How can I access the characteristics of previous bars?

    Thanks,

    -Omer

    #2
    Hello Omer,

    Data is presented with a series collection. A Series<double> contains one slot for every bar on the chart. The index of each bar is found with a BarsAgo value.

    0 is zero bars ago, or the most recently updated bar. 1 would be the previous bar.


    To loop through the last 20 bars and print the time and the value of the SMA on that bar:

    Code:
    if (CurrentBar > 20)
    {
    for (int index = 0; index < 20; index++)
    {
    Print(string.Format("{0} | index: {1}, SMA(7)[{1}]: {2}", Time[0], index, SMA(7)[index]));
    }
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks I'll give it a try - should this go inside of OnBarUpdate() ?

      Comment


        #4
        Hello Omer,

        Yes, this should be placed in OnBarUpdate().

        I am also including a link to a forum post with helpful resources on getting started with NinjaScript and C#.


        Kind regards.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Can you help me out a bit here - I tried the code but it's still reading every bar and not the last 20 (screenshot atatched)

          if (CurrentBar > 20)


          {

          for (int i = 0; i < 20 + 1; i++)
          {


          if (Close[i] < Open[i]) //Red Bar
          {
          trend_score=trend_score-1;

          }



          if ((Close[i] < Open[i]) && (Low[i] < Low[i+1])) //Red Bar with lower low
          {
          trend_score-=2;

          }



          if (Close[i] > Open[i]) //Green bar
          {
          trend_score=trend_score+1;

          }




          if ((Close[i] > Open[i]) && (High[i] > High[i+1])) //Green bar with higher high
          {
          trend_score=trend_score+2;

          }

          }

          }
          Attached Files

          Comment


            #6
            Hello omermirza,

            On each new bar it should be looping through the last 20 bars. Is this not what you are trying to acheive?

            Are you specifically referring to triggering an action on the last historical bar on the chart before realtime data starts?

            if (State == State.Historical && CurrentBar == Count - 2)

            This would be the last historical bar if you wanted to run the loop then.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello omermirza,

              On each new bar it should be looping through the last 20 bars. Is this not what you are trying to acheive?

              .
              That is what I'm trying to achieve but that's not happening. If you look at the screenshot, the TrendScore shows 7647 - there's no way it should be that high based upon the code and the last 20 bars processed.

              Comment


                #8
                Hello omermirza,

                I wouldn't be able to comment on your custom logic or variable or what that is suppose to mean.

                If you want to know what bars are being looped through, then print the time of the bar.

                Code:
                if (CurrentBar > 20)
                {
                for (int i = 0; i < 20 + 1; i++)
                {
                Print(string.Format("{0} | {1}", i, Time[i]));
                }
                }
                Below is a link to a forum post that demonstrates using Print() to understand behavior.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I am still stuck on this - can you please show me how to declare a series that holds the last 20 historical bars?

                  I think that the problem is that the trend_score keeps counting even after 20 bars have passed.

                  Comment


                    #10
                    Hello omermirza,

                    "declare a series that holds the last 20 historical bars"

                    I am not clear on what this means. A Series holds one value per bar slot.. It doesn't hold multiple values for every bar slot. You can have multiple custom series..

                    However, is this related to your initial inquiry?

                    Previously you wanted to:
                    "I am trying to look at the last 20 bars on the chart and assign a score to each bar"

                    Look at the last 20 bars to me means loop through the last 20 bars.

                    To loop through the last 20 bars, that would be:
                    for (int index = 0; index < 20; index++)
                    This would loop the last 20 bars.

                    When you loop the last 20 bars is up to you.
                    On every bar you could loop through the last 20 bars.
                    Or on the last historical bar you could loop through the last 20 bars..
                    Or at 9:00 am you could loop the last 20 bars..
                    Or after 20 historical bars you could loop the last 20 bars..
                    Or after 20 real-time bars you could loop the last 20 bars..
                    Or you could loop through current bar, through all the available bars, for the first 20 historical bars..

                    What do you want to do?

                    Do you have a new inquiry where you are wanting to store the sum of the last 20 bars to a double value in a series?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I think what I really need to do is loop through the last 20 bars starting from the last historical bar.

                      Comment


                        #12
                        Hello omermirza,

                        Code:
                        if (CurrentBar < 20)
                        return;
                        
                        if (State == State.Realtime || (State == State.Historical && CurrentBar == Count - 2))
                        {
                        for (int index = 0; index < 20; index++)
                        {
                        Print(string.Format("{0} | Close[0]: {1}", Time[index], Close[index]));
                        }
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello omermirza,

                          Doing a quick read through this thread. Are you resetting trend_score = 0 before the code runs through the loop? Just a guess, but I don't believe that is happening. That might be why you're seeing trend_score continuing to go up, especially, on a significant up trend.

                          Hope that helps!
                          Matt

                          Comment


                            #14
                            Chelsea and Steatlh,

                            Thanks for the help, I finally got it working now!

                            Comment


                              #15
                              If the indicator is updating on each tick instead of on bar close is it still
                              Code:
                              CurrentBar == Count -2
                              to determine the last historical bar?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by lightsun47, Today, 03:51 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post lightsun47  
                              Started by 00nevest, Today, 02:27 PM
                              1 response
                              8 views
                              0 likes
                              Last Post 00nevest  
                              Started by futtrader, 04-21-2024, 01:50 AM
                              4 responses
                              44 views
                              0 likes
                              Last Post futtrader  
                              Started by Option Whisperer, Today, 09:55 AM
                              1 response
                              13 views
                              0 likes
                              Last Post bltdavid  
                              Started by port119, Today, 02:43 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post port119
                              by port119
                               
                              Working...
                              X