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

Getting past int value

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

    Getting past int value

    I have
    Code:
    int = myCount
    myCount is a value that is assigned to a bar depending on different factors.

    I want to access and print out random past values of myCount. Lets say I want to see the value from bar #19 (assuming current bar is 0). How do I access it?

    #2
    At the top of your code in the variable declarations, add something like: private Series<int> MyCountSeries = null;

    In State.Configure or State.DataLoaded you'll do something like: MyCountSeries = new Series<int>(this);

    In OnBarUpdate (or wherever) you're setting myCount, right after that, do: MyCountSeries[0] = myCount;

    Then, later, you can use MyCountSeries[1] to get 1 bar back etc.

    A few other notes - you shouldn't (or can't) name your variable int like that because int is a keyword. I'm assuming instead of "int = myCount" you mean "int myCount" or "int myCount = something" or "int myCount; myCount = something".

    Also, if, as you say, current bar is 0, you can't get bar #19 because that is in the future. If what you mean is current bar is 0 BACK then bar 19 BACK is MyCountSeries[19].

    If you are using more than one data series, but this series is about the chart bars, you should check "if (CurrentBars[0] >= 0)" before you access MyCountSeries[0] because there might not be any chart bars yet.

    If you are exporting this series e.g. it's a vector output from an indicator that you need in a strategy, you should initialize it in State.Configure - otherwise, it should be in State.DataLoaded.

    If you need more than 256 bars of history, you can use the overload with MaximumBarsLookback.Infinite but this uses more resources so you should not do it unless you need more than 256 bars of history (an example would be if you are using it in OnRender, or need to look all the way back to the start of the series for some reason).
    Last edited by QuantKey_Bruce; 03-31-2023, 04:43 AM.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello, thanks for writing in. You can create a class level Series object. Series objects are arrays that have a slot index for every bar on the chart. Please see the example here:



      Kind regards,
      -ChrisL​
      Last edited by NinjaTrader_ChrisL; 03-31-2023, 07:48 AM. Reason: Fixed the link
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChrisL View Post
        Hello, thanks for writing in. You can create a class level Series object. Series objects are arrays that have a slot index for every bar on the chart. Please see the example here:

        https://ninjatrader.com/support/help...a_series_or_da taseries_o.htm

        Kind regards,
        -ChrisL​
        That link does not link.

        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Originally posted by QuantKey_Bruce View Post
          At the top of your code in the variable declarations, add something like: private Series<int> MyCountSeries = null;

          In State.Configure or State.DataLoaded you'll do something like: MyCountSeries = new Series<int>(this);

          In OnBarUpdate (or wherever) you're setting myCount, right after that, do: MyCountSeries[0] = myCount;

          Then, later, you can use MyCountSeries[1] to get 1 bar back etc.

          A few other notes - you shouldn't (or can't) name your variable int like that because int is a keyword. I'm assuming instead of "int = myCount" you mean "int myCount" or "int myCount = something" or "int myCount; myCount = something".

          Also, if, as you say, current bar is 0, you can't get bar #19 because that is in the future. If what you mean is current bar is 0 BACK then bar 19 BACK is MyCountSeries[19].

          If you are using more than one data series, but this series is about the chart bars, you should check "if (CurrentBars[0] >= 0)" before you access MyCountSeries[0] because there might not be any chart bars yet.

          If you are exporting this series e.g. it's a vector output from an indicator that you need in a strategy, you should initialize it in State.Configure - otherwise, it should be in State.DataLoaded.

          If you need more than 256 bars of history, you can use the overload with MaximumBarsLookback.Infinite but this uses more resources so you should not do it unless you need more than 256 bars of history (an example would be if you are using it in OnRender, or need to look all the way back to the start of the series for some reason).
          Works. Thanks!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cre8able, 02-11-2023, 05:43 PM
          3 responses
          236 views
          0 likes
          Last Post rhubear
          by rhubear
           
          Started by frslvr, 04-11-2024, 07:26 AM
          8 responses
          113 views
          1 like
          Last Post NinjaTrader_BrandonH  
          Started by stafe, 04-15-2024, 08:34 PM
          10 responses
          46 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by rocketman7, Today, 09:41 AM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by traderqz, Today, 09:44 AM
          2 responses
          10 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X