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

Replace values of plot in history

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

    Replace values of plot in history

    I am working on an indicator which changes the period of indicators in response to some market action. Is there a way to change the values of the plot of an indicator on previous bars? For example, if i were plotting an indicator that is the equivalent of the RSI(14) on the chart and I wanted to change the period to 24 because of some market data that just came into NT8. Thanks

    #2
    Hello,

    Thank you for the question.

    You would have to account for this change with your own logic.

    You can set previous plot values using a BarsAgo index, if you were to re-iterate through the bars you could likely reset the values as needed. A For loop could be used to iterate the bars and from there you could get data as needed from the indicators.

    Code:
    protected override void OnStateChange()
    {
    	if (State == State.SetDefaults)
    	{
    		AddPlot(Brushes.Red, "Plot0");
    	}
    }
    
    protected override void OnBarUpdate()
    {
    	if(State == State.Historical)
    	{
    		Value[0] = Close[0];	
    	} 
            else if (State == State.Realtime)
    	{
    		for(int i = 0; i < Count -2; i++)
    		{
    			Value[i] = 120;
    		}
    	}
    }
    There are many ways to go back over the data, this simply goes over all the data. You could limit this to a certain amount of bars or time if necessary as well.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,
      Thanks for this information. I was able to make the data replacement work for the realtime situation. However, when I try to insert the logic into the historical calculation, the calculation works, but takes forever. Is there a way to ignore going through the change of RSI(n) in the Value array until the last historical bar?

      Thanks.

      Comment


        #4
        Hello,

        Thank you for the reply.

        Yes, you could either wait for Realtime, or if you want to do this in historical you would need to check the CurrentBar against the Count.

        Code:
        if(Historical) return;
        OR
        Code:
        if(CurrentBar < Count - 2) return;
        Depending on the CalculateOnBarClose setting the 2 would vary, but the Count can be used to know the bar Count.



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse,
          Thanks for that information. I was able to make that work. Last thing, I hope. I'm trying to provide a displayName that mimics a standard RSI or MACD or whatever. Because the periods are changing underneath, I would like the displayname to update when the periods change. I tried overriding the display name as follows:

          Code:
          public override string DisplayName
          {
                      get { return Name + "( " + this.Instrument.FullName + " (" + Bars.BarsPeriod + "), " + Period +  ")"   ; }
           }
          This code works if the indicator is already on the chart, but makes it impossible to add the indicator to a new chart with an unhandled exception. I'm assuming that this.Instrument.Fullname is null during one of the executions. Is there a property that I can access to get the instrument name during the addition of the indicator to a chart?

          Thanks again!

          Comment


            #6
            Hello,

            In this case because the Instrument is not yet know, the object would be null. Instrument would be null until the indicator is actually added to the chart, so you would likely need to modify the logic to use a variable. Other items may also cause this, so I would suggest for any NinjaScript specific property, to use a variable instead to prevent nulls.

            Here is one example:


            Code:
            private string myInstrument = "Some Placeholder Text";
            
            protected override void OnStateChange()
            {
            	if (State == State.Historical)
            	{
            	      myInstrument = Instrument.FullName;
            	}
            }
            
            public override string DisplayName
            {
                  get { return Name + "( " + myInstrument + " , " + Period +  ")"   ; }
            }
            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Unhandled Exception

              Hello Jesse,
              Thanks for your help so far. I've the code "mostly" working. It plots correctly. However, for a reason that I can't figure out, sometimes when I return to the ninjatrader chart window from another application, the indicator's plot is empty. If I hit F5 to refresh, I get an unhandled exception indicating that an object was expected. It doesn't say what object was expected. I suspect I'm not reinitializing something when the window becomes active again. The code for bar generation is below. It relies on RSI and a custom indicator. I have not experienced this behavior with either RSI or ACPDominantCycle on their own.

              TIA

              Code:
              if((State == State.Historical && CurrentBar == Count - 2) || State == State.Realtime )
                          {
                              double temp = ACPDominantCycle(100,true)[0];
                              int NewPeriod = Convert.ToInt32(temp);
                              int theEnd = (Period == NewPeriod)?1:Count-2;
                              
                              Period = NewPeriod;
                              myName = Name + "( " + this.Instrument.FullName + " (" + Bars.BarsPeriod + "), " + Period + ", "+ Convert.ToInt32(temp*SmoothingRatio) + ")" ;
                              for(int i = 0; i < theEnd; i++)
                              {
                                  Value[i] = RSI(Convert.ToInt32(temp),Convert.ToInt32(temp*SmoothingRatio))[i];
                                  Values[1][i] = RSI(Convert.ToInt32(temp),Convert.ToInt32(temp*SmoothingRatio)).Avg[i];
                              }
                          }

              Comment


                #8
                Hello SkiMajor,

                Thanks for your post.

                In the code of OnStateChange(), in the area of State = State.SetDefaults, do you have IsSuspendedWhileInactive set to true? If so change to false and advise if the issue reoccurs.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  I made the change the and the indicator ran without a problem subsequently. Is there a reason this flag would need set for this indicator even though the three indicators on which it relies don't? Thanks for your help.

                  Comment


                    #10
                    Hello SkiMajor,

                    Thanks for your reply.

                    To clarify, the indicator that has IsSuspendedWhileInactive set false (now), calls 3 other indicators that do not, is that correct?
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      That's correct. One customer indicator (ACPDominantCycle) and two standard ones (RSI and SMA).

                      Comment


                        #12
                        Hello SkiMajor,

                        Thanks for your reply.

                        The called indicators will continue to operate as/when called by the calling indicator.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Paul,
                          That's what I assumed. What I don't understand is, what is it about the code I have that is causing this flag to be necessary? Is there something I should do differently?

                          Comment


                            #14
                            Hello SkiMajor,

                            Thanks for your reply.

                            IsSuspendedWhileInactive was added in Ninjatrader8 as a performance improvement option that is set true by default. From the helpguide: "Prevents real-time market data events from being raised while the indicator's hosting feature is in a state that would be considered suspended and not in immediate use by a user. Enabling this property in your indicator helps save CPU cycles while the hosting feature is suspended and not in use by a user. Once the hosting feature is in a state that would no longer be considered suspended, the historical OnBarUpdate() events will be replayed allowing the indicator to catch up to current real-time values."

                            Full Link: http://ninjatrader.com/support/helpG...leinactive.htm
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              IsSuspendedWhileInactive

                              Hi Paul,
                              Thanks for your reply. IsSuspendedWhileInactive seems like a very useful feature in NT8. However, can you help me understand what it is about my code that is causing an unhandled exception as described in the OP when IsSuspendedWhileInactive = true?

                              Thanks.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,602 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              12 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X