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

Times[] problem

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

    Times[] problem

    I have this code:

    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    // if(Times[0][0].Day < 15) return;
    // if(Times[1][0].Day < 15) return;
    Print("CurrentBars[0] " + CurrentBars[0]);
    Print("CurrentBars[1] " + CurrentBars[1]);
    if(CurrentBars[0] < 40) return;
    if(CurrentBars[1] < 40) return;
    Print("times 0 count " + Times[0].Count);
    Print("times 1 count " + Times[1].Count);
    Print("T10 " + Times[1][0] + " T00 " + Times[0][0]);
    Print("T11 " + Times[1][1] + " T01 " + Times[0][1]); //fails

    }


    also in the configure section

    else if (State == State.Configure)
    {
    AddDataSeries("TSLA",Data.BarsPeriodType.Minute, 5);
    }


    I bacame stuck because Times[0][0] prints as the time of the first bar on my chart, which means I am getting an error when I try to access index[1], since it is before the 1st bar on the chart.

    I've tried several ways to block any execution on the 1st bar but it keeps doing it.
    Though uncommenting the first 2 lines will definitely stop it, however that makes nothing print at all! even though my chart has day 15 & 16 visible! so something should be printing then at least.

    also when I add this indicator to a chart and then go back into indicator settings, ninja complains about:

    Indicator 'multiseries': Error on calling 'OnStateChange' method: 'AddDataSeries' cannot be called from this state.

    but it's already in state configure where the guides and even the ninja wizard puts it.

    *forgot to add that the error message says an error on bar 200+ so it's not actually failing on bar 1 at all. really baffling one. haven't been able to figure out how this is even happening.

    *I managed to get it to print by setting the bars to load in the Ui to 400, rather than 5 days. 5 days should have no problem working so I don't understand.

    there is a new problem though. the output prints

    T10 08/04/2019 8:35:00 PM T00 11/04/2019 9:00:00 PM
    T11 08/04/2019 7:56:00 PM T01 11/04/2019 8:57:00 PM

    the values for the 1st data series are somehow 39 minutes apart even though the chart period is 5 minutes. set bars to load to 100 just to check it's not a data gap problem, still 39 minutes. very odd. why 39?

    T10 12/04/2019 7:35:00 PM T00 15/04/2019 6:00:00 PM
    T11 12/04/2019 6:36:00 PM T01 15/04/2019 5:45:00 PM

    **I just noticed the times in the 2nd half of those strings still correspond to the first bar visible on my chart, so somehow it's still going back and doing stuff on bar 1. (by second half I mean comparing T00 & T01 in the outputs above)

    another thing noticed is the time differences of the times in the 2nd half. first one has a 3min time difference, 2nd one 15min.
    and I ran another test with UI setting bars to load 200, and now the gap is 7 minutes.

    T10 11/04/2019 5:45:00 PM T00 15/04/2019 2:40:00 PM
    T11 11/04/2019 5:16:00 PM T01 15/04/2019 2:33:00 PM

    the first bar visible on my chart is 2:40PM. don't know how it accessed 2:33PM without failing.

    the chart period is 2 minutes.. so I'd expect any time difference related to the main series to be 2 minutes.
    Last edited by anon84; 04-16-2019, 08:44 AM.

    #2
    Hello anon84,
    Thanks for your post.

    What are you trying to accomplish specifically with these prints?


    Code:
    also when I add this indicator to a chart and then go back into indicator settings, ninja complains about:
    
    Indicator 'multiseries': Error on calling 'OnStateChange' method: 'AddDataSeries' cannot be called from this state.
    Is the name of this indicator 'multiseries' or is that the name of another indicator on your system?

    Can you show us everything inside this scripts OnStateChange()?
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      HI, was going to say I have no 'multiseries' indicator, but then I noticed it in the ninjascript editor list! it indeed has a data series added in the wrong place. looked at the code and not sure where that came from. file says made in september last year. maybe one of my old tests.

      so 1 problem solved, thanks.

      what am I trying to achieve: the prints are just for debugging this 1 issue that I've isolated. Today I ported one of my NT7 indicators to NT8 but it won't work because of this. (accessing time value x bars back. actually I only require 1 bar back)

      Turned the first 2 date blocks on and added a print to confirm the method has been entered. I had thought the issue could be that the render was happening before the day 15+ bars had been loaded.

      protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
      {
      Print("rendered");
      if(Times[0][0].Day < 15) return;
      if(Times[1][0].Day < 15) return;

      -----------------

      Current chart is 2min 500 bars. It shows dates from 11th april to some bars on 16th april. After the indicator is loaded I click in the chart area to force another render. A print of 'rendered' confirms that, but then nothing else prints. I expect Times[0][0].Day to be 16 now, but I must be misunderstanding how it works in NT8. It's probably still stuck trying to calculate on bar 1 where the Day is lower than 15. In NT7 Times[0][0] would return the time of the latest bar on the chart (primary series), how is this done in NT8? I checked the help but it seems as though it works the same as NT7.

      If it works the same way, could it be some kind of threading issue? like when in OnRender it doesn't have access to all the bars or something.

      Any clues?

      Comment


        #4
        anon84,

        Why are you using OnRender()? Generally OnRender() should only be used for rendering and not for any other type of logic.

        Why not put your debugging prints inside OnBarUpdate()? What do you see if you do this?
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          It works in OnBarupdate as I'd expect, otherwise ninja would be a totally broken program.

          I have seen in forum posts and in the help that you should avoid too much logic in paint/render, but since any logic I've used is quite simple it's never been a problem in NT7. I've literally had 80 of this indicator on screen at the same time in NT7 and everything runs fine.

          I already thought of a way to rework the whole thing so that Times[] is accessed outside of OnRender, but I'd still end up doing a very similar thing, looping through an array to draw lines based on my pre-calculated variables from OnBarUpdate. The looping is done through Closes[], Times[][1] & [0] is just something checked in each loop.

          I just checked if the problem exists for Closes[], and it does. It also gets stuck referencing bar 1. I can duplicate close values in my own array inside OnBarUpdate, then loop throught that in OnRender, but it just seems a bit convoluted to be creating arrays just to copy data from publically accessible arrays that already exist.

          It's almost as if OnRender is working like it were a historically calculated indicator, in that when it draws bar 1 on the chart it sees any reference to Times[0][0] as the time for bar 1, then breaks when trying to access the index 1 bar back from that.

          ** New test to compare. primary series.

          --OnBarUpdate //prints from inside OnBarUpdate (shows the REAL correct values)
          times[0] count 400
          current bar 398
          Close[0] 270.15
          T00 16/04/2019 2:47:00 PM T01 16/04/2019 2:46:00 PM

          --OnRender
          times[0] count 400
          current bar 398
          Close[0] 263.67
          T00 15/04/2019 2:39:00 PM T01 15/04/2019 2:32:00 PM //wrong, plus a strange 7 minute gap?

          What's the real technical reason for why this happens Josh? really interested
          Last edited by anon84; 04-17-2019, 06:22 AM.

          Comment


            #6
            anon84,

            OnRender simply was not designed to be in sync with the current bars in progress. This would be true for any Series<t> object using a barsAgo indexer inside OnRender.

            If you need data about a bar while inside OnRender you would want to use an absolute index method to look up values, such as Bars.GetTime(): https://ninjatrader.com/support/help...s/?gettime.htm

            Josh G.NinjaTrader Customer Service

            Comment


              #7
              Ah... strange that it simply breaks when using an index. (sometimes works & prints wrong data, but sometimes breaks with 'index out of range') The series count is confirmed to be 400 items, so index 1 does exist.. weird how it acts as if the count is only 1 sometimes. Not very intuitive to the end user.

              Thanks anyway, I checked your link and it seems to be just what I need. Didn't think of trying GetBar stuff because I never had reason to use those methods in NT7. Relieved I will not have to rework the whole thing.

              *Thinking about it more, BarsAgo is just CurrentBar - Idx, and it's already proven that OnRender gets the CurrentBar value correct, and it gets the series count correct, so it should be able to figure out the correct index. This is definitely a bug then. Hope it gets fixed.
              Last edited by anon84; 04-17-2019, 12:26 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Max238, Today, 01:28 AM
              1 response
              22 views
              0 likes
              Last Post CactusMan  
              Started by giulyko00, Yesterday, 12:03 PM
              2 responses
              10 views
              0 likes
              Last Post giulyko00  
              Started by r68cervera, Today, 05:29 AM
              0 responses
              4 views
              0 likes
              Last Post r68cervera  
              Started by geddyisodin, Today, 05:20 AM
              0 responses
              7 views
              0 likes
              Last Post geddyisodin  
              Started by JonesJoker, 04-22-2024, 12:23 PM
              6 responses
              38 views
              0 likes
              Last Post JonesJoker  
              Working...
              X