Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close[i], Open[i]... outside OnBarUpdate() ???

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

    Close[i], Open[i]... outside OnBarUpdate() ???

    Hi,

    I test this code:

    Code:
            int i = 0; // we will change the value of i to 1, 2, 3 ....
    	if (Close[i] > Open[i]) Print("Bull");
    	else if (Close[i] < Open[i]) Print("Bear");
    Put the code in OnBarUpdate: OK it works.

    However, a problem occurs when I put the code in my button click event:
    Code:
    void ToggleClick(object sender, RoutedEventArgs e) {
    ...
    }
    For i = 0: it works
    For i = 1, 2, 3...: the following error happens, even though we already have hundreds of bars on chart
    Unhandled exception: Object reference not set to an instance of an object.
    This means we cannot access the historical values of Close, Open, Low, High series outside OnBarUpdate ??? This will be a really serious problem!!!

    Thanks.
    Last edited by ninZa; 11-11-2015, 08:23 PM.
    ninZa
    NinjaTrader Ecosystem Vendor - ninZa.co

    #2
    Hello,

    The basic series such as Close[0] can be used throughout the script so long as there is data for the index location being accessed, for example you can use it in OnMarketData.

    Can you post a sample that demonstrates the error? That would better help identify what may be happening, otherwise based on the error you may need to check what is null to see where the error is coming from.

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by ninZa View Post
      Hi,

      I test this code:

      Code:
              int i = 0; // we will change the value of i to 1, 2, 3 ....
      	if (Close[i] > Open[i]) Print("Bull");
      	else if (Close[i] < Open[i]) Print("Bear");
      Put the code in OnBarUpdate: OK it works.

      However, a problem occurs when I put the code in my button click event:
      Code:
      void ToggleClick(object sender, RoutedEventArgs e) {
      ...
      }
      For i = 0: it works
      For i = 1, 2, 3...: the following error happens, even though we already have hundreds of bars on chart


      This means we cannot access the historical values of Close, Open, Low, High series outside OnBarUpdate ??? This will be a really serious problem!!!

      Thanks.
      Have you escaped the initial bars correctly?

      Comment


        #4
        Originally posted by ninZa View Post
        Hi,

        I test this code:

        Code:
                int i = 0; // we will change the value of i to 1, 2, 3 ....
        	if (Close[i] > Open[i]) Print("Bull");
        	else if (Close[i] < Open[i]) Print("Bear");
        Put the code in OnBarUpdate: OK it works.

        However, a problem occurs when I put the code in my button click event:
        Code:
        void ToggleClick(object sender, RoutedEventArgs e) {
        ...
        }
        For i = 0: it works
        For i = 1, 2, 3...: the following error happens, even though we already have hundreds of bars on chart


        This means we cannot access the historical values of Close, Open, Low, High series outside OnBarUpdate ??? This will be a really serious problem!!!

        Thanks.
        I'm not able to reproduce the object reference error, could you provide more details on how to trigger that? Do you know which object is null in your debugging?

        In my testing though, I do see where accessing the price series object outside of the internal handlers like OnBarUpdate do result in the Open[0] providing the first bar on the chart. So if you accessed Open[1], it would be expected to get an index out of range exception.

        In principle, you should not be accessing these series values outside of the internal handlers by an index.

        So I can help provide a solution or at the very least, provide an enhancement request, can you please outline your full usecase for this? Please elaborate on what you intend to do with this data from the click handler and I can see if there is a documented way to achieve that, or we can review your use case and see if there is something we can do in the future to handle as well.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Close[i], Open[i]... outside OnBarUpdate() ???

          Matthew wrote:
          In principle, you should not be accessing these series values outside of the internal handlers by an index.
          Could you clarify, please? Why would these data not be available in all methods, provided the State was valid for accessing them?

          I'm sure that many traders, like myself, have some complex code that requires these data series outside of any pre-defined methods in NinjaScript. Am I missing something obvious?

          Thanks.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Originally posted by jeronymite View Post
            Matthew wrote: Could you clarify, please? Why would these data not be available in all methods, provided the State was valid for accessing them?

            I'm sure that many traders, like myself, have some complex code that requires these data series outside of any pre-defined methods in NinjaScript. Am I missing something obvious?

            Thanks.
            There are pointers in these methods to ensure the current bar is up to date, and accessing these PriceSeries outside without that code is unpredictable.

            If you need to access values outside, you can use an absolute index which would get you the the value.

            Open.GetValueAt(CurrentBar - barsAgo);

            or

            Open.GetValueAt(ChartBars.Count - barsAgo));

            or

            Bars.GetOpen(CurrentBar - barsAgo);

            If there is some reason you would need the PriceSeries array, because you cannot fetch these values using the absolute index, we would be interested in the use case, as there are other documented ways to get these values.
            MatthewNinjaTrader Product Management

            Comment


              #7
              I found out the reason: bypassing TriggerCustomEvent causes the issue.
              Here it is:

              INCORRECT
              Code:
              void ToggleClick(object sender, RoutedEventArgs e) {
              	if (Close[Index] > Open[Index]) Print(Index + ": Bull");
              	else if (Close[Index] < Open[Index]) Print(Index + ": Bear");
              }
              CORRECT
              Code:
              void ToggleClick(object sender, RoutedEventArgs e) {
              	[B]TriggerCustomEvent[/B](o => {
              		if (Close[Index] > Open[Index]) Print(Index + ": Bull");
              		else if (Close[Index] < Open[Index]) Print(Index + ": Bear");
              	}, e);
              }
              I don't bypass TriggerCustomEvent in a serious programming, but previously I just quickly tested out my idea so I forgot it (or lazy). This proves the power of TriggerCustomEvent in guaranteeing a stable operation for indicators.
              ninZa
              NinjaTrader Ecosystem Vendor - ninZa.co

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Irukandji, Today, 04:58 AM
              0 responses
              2 views
              0 likes
              Last Post Irukandji  
              Started by fitspressoburnfat, Today, 04:25 AM
              0 responses
              2 views
              0 likes
              Last Post fitspressoburnfat  
              Started by Skifree, Today, 03:41 AM
              1 response
              4 views
              0 likes
              Last Post Skifree
              by Skifree
               
              Started by usazencort, Today, 01:16 AM
              0 responses
              1 view
              0 likes
              Last Post usazencort  
              Started by kaywai, 09-01-2023, 08:44 PM
              5 responses
              604 views
              0 likes
              Last Post NinjaTrader_Jason  
              Working...
              X