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

Request for Guidance - Indicator Behavior When Hosted by Strategy in NinjaTrader

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

    Request for Guidance - Indicator Behavior When Hosted by Strategy in NinjaTrader

    NT Community,

    Please assist with a seemingly straightforward issue. I've attached two scripts for your reference: a Strategy ("TestStrategyRealtimePrint") and an Indicator ("TestRealTimePrint"). These scripts serve as learning tools for me, helping to refine my understanding of how Indicators function when hosted within a Strategy.

    The Indicator prints the time of bars after it enters the State.Realtime, hence excluding the time of historical bars. This script performs as intended when applied directly to a chart.

    However, I'm encountering some unexpected behavior when the Indicator is hosted by the Strategy. When applied to a chart, the Strategy prints the time of bars after it enters the State.Realtime, However, it also (unexpectedly) prints the time of historical bars.

    Any insight or guidance is appreciated. I'm keen to learn why the Indicator behaves differently when used within the context of the Strategy.

    Thank you in advance for your time and assistance.

    Shannon​

    Attached Files
    Last edited by Shansen; 07-05-2023, 05:30 AM.

    #2
    Hello Shansen,

    Thank you for your post.

    I suggest adding print statements to understand when your bool _isRealtime is set to true, as the behavior seems to be related to your condition:
    Code:
    if (!_isRealtime) return;
    You can add a debugging print, such as the following suggestions, into State.Realtime when you set this bool to true:
    Code:
    else if (State == State.Realtime)
    {
    _isRealtime = true;
    Print("indicator State.Realtime. setting _isRealtime to true.  _isRealtime: " + _isRealtime);
    // or, if this is the strategy, try the following print:
    Print("strategy State.Realtime. setting _isRealtime to true.  _isRealtime: " + _isRealtime);
    }
    By adding prints to your logic, you can understand the values of your variables and when they are changed to better understand unexpected behavior. For more information on using prints to debug your scripts:


    Otherwise, you could try a different approach inside of OnBarUpdate. Rather than relying on a bool that is set inside of OnStateChange(), you could consider adding the following:
    Code:
    if (State == State.Historical)
    return;
    This logic should ensure that your OnBarUpdate() in your script, whether it is the indicator or the strategy, is not processed during historical data.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your prompt reply.

      By applying your suggested approach, it seems that the CurrentBar of the Indicator remains at -1 through all states until the first execution of OnBarUpdate(). At this point, it appears the Indicator processes all bars when hosted by the Strategy.

      Here's the output from the print statements:
      Code:
      Print(string.Format("Ind - {0} {1} {2}", State, CurrentBar, _isRealtime));
      // Output:
      Ind - SetDefaults -1 False
      Ind - Configure -1 False
      Ind - DataLoaded -1 False
      Ind - Historical -1 False
      Enabling NinjaScript strategy ...
      Str - Realtime 2063 True
      Ind - RealTime -1 True
      When I implemented your other suggestion, to insert "if (State == State.Historical) return;" into OnBarUpdate(), it still produced the same issue: the first OnBarUpdate() processes all Bars. This makes sense as at the time of the first OnBarUpdate() call, the State is already State.Realtime.

      From my observations, it seems that a potential solution would require the Strategy's call to the Indicator to precede the statement "if (!_isRealtime) return;" or alternatively, that statement could be removed altogether.

      However, I am not certain if this is how the scripts should be structured. It seems somewhat counter-intuitive to exclude _isRealtime from the Strategy, while still including _isRealtime in the Indicator to produce the intended behavior.

      Could you please clarify whether my understanding is correct? Alternatively, could you advise on how to structure the scripts so that they can process efficiently whether applied directly to a chart or hosted by the Strategy?

      Last edited by Shansen; 07-05-2023, 11:34 PM.

      Comment


        #4
        Hello Shansen,

        Thank you for your reply.

        You mentioned, "at the time of the first OnBarUpdate() call, the State is already State.Realtime." Which is not the case. You may test this by adding the following print statement as the first line of OnBarUpdate():
        Code:
        Print("Current state is: " + State);
        What you are seeing could be related to how you are calculating _isRealtime. This really is not necessary as NinjaScripts keep track of the State already, so adding this additional variable truly is not necessary. I have created the attached example of an indicator as well as a strategy that hosts the indicator which prints only in real-time. Please download the attached PrintTimeInRealTime_NT8.zip and import it via the Control Center > Tools > Import > NinjaScript AddOn menu. You may then test out both the indicator and the strategy and review the prints in the NinjaScript Output window. I hope this helps you to better understand the way the host script processes the hosted script.

        I also suggest referring to the sample "Exposing indicator values that are not plots" in the case that you'd like to use indicator values that are not plots; in this case you are using a DateTime series, though if you were to use a value that is not a series you would have to use Update() so that the value is updated to the current bar index when it is called in the host script:For more details on OnStateChange() and State:Please let us know if we may be of further assistance.
        Attached Files
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank Emily & Team.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jjopd2001, Today, 05:26 AM
          0 responses
          0 views
          0 likes
          Last Post jjopd2001  
          Started by JaniceContreras, Today, 05:16 AM
          0 responses
          3 views
          0 likes
          Last Post JaniceContreras  
          Started by Margi2021, Yesterday, 01:04 PM
          2 responses
          22 views
          0 likes
          Last Post Margi2021  
          Started by SteveReiza, Yesterday, 11:34 PM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by SteveReiza, Yesterday, 11:20 PM
          2 responses
          12 views
          0 likes
          Last Post NinjaTrader_Jason  
          Working...
          X