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

Within an indicator, how to retrieve the “IndicatorId” from the NT8 workspace file?

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

    Within an indicator, how to retrieve the “IndicatorId” from the NT8 workspace file?

    Within an indicator, how to retrieve the “IndicatorId” from a NT8 workspace file?

    1. Reason:

    Use IndicatorID to locate the data of public persistent variables [Browsable(false)] that are stored in the workspace. Normally NT8 loads these public variables values from the workspace when NT8 is initiated. However, there are two situations that NT8 would not load the public variables from the workspace as follows:

    a) When NT8 is initiated with no data connection, the data of the persistent public variables [Browsable(false)] are loaded correctly. After NT8 has completely loaded, initiating the data connection will cause the charts to reload but without the correct data for these persistent public variables.

    b) After opening the chart UI input and changing any one of the public input variables, NT8 will reload the chart but without the correct data for these persistent public variables.


    2. Properties
    [NinjaScriptProperty]
    [Display(Name="Input variable", Description = "Enter input variable", Order=1, GroupName="General Parameters")]
    public double dValue_input
    { get; set; }

    [NinjaScriptProperty]
    [Browsable(false)]
    public int ibar_persist
    { get; set;}

    [NinjaScriptProperty]
    [Browsable(false)]
    public double dValue_persist
    { get; set;}


    3. IndicatorID stored in the workspace file.

    <IndicatorId>936</IndicatorId>
    <MaxSerialized>0</MaxSerialized>
    <MinSerialized>0</MinSerialized>
    <ZOrder>10004</ZOrder>
    <dValue_input>101</dValue_input>
    <ibar_persist>300</ibar_persist>
    <dValue_persist>951</dValue_persist>

    4. Once the matching IndicatorId is located inside the workspace file, the public persistent variables [Browsable(false)] data can be retrieved. The question is how to retrieve the IndicatorID from within an indicator?
    Attached Files

    #2
    Hello kkc2015,

    Thanks for your post.

    There isn't anything documented I could share (or undocumented code I may hint at) for getting IndicatorId programmatically.

    I'm curious on the reasoning here, though. [Browsable(false)] just means the value is not visible in the property grid, constructors using the parameter are still made, and this also comes into play for indicator caching.

    I think some the issues you are facing would be due to using the workspace to save data in the indicator that should not be recalculated, rather than storing a value made to reconstruct the indicator (what we would save in the workspace to have the indicator rebuilt.)

    For the purpose of saving and loading data, I may suggest using a StreamReader/StreamWriter, and storing the files with a name that is associated with Indicator name, BarsPeriod properties, and other specific identifiers instead of trying to save in the workspace.

    For the thread's reference examples for using a StreamReader or StreamWriter can be found below.




    If this information does not help, could you clarify on your use case and why the suggestion would not be appropriate? (A small example script could also help get me on the same page.)

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      Thank you for your quick and precise response. And thanks for the reference to the StreamReader / StreamWriter. I do use StreamWriter to store statistics data for the indicators. StreamReader is used to retrieve the data from the workspace for the persistent variables.

      Attached is a sample indicator (test6) to explain what I am trying to achieve.
      The indicator has two buttons as follows:

      “Inc Var” : to increase the values of the persistent variables [Browsable(false)] for testing purpose.

      “GetValue” retrieve the values from the NT8 workspace for the persistent variables.

      Normally when NT8 is initiated, it retrieves the persistent variables from the workspace and loads them into the indicator. All is well – the indicator now has the persistent values saved from the last session. I am using these data to restore the status of the previous menu selection + few other key values.

      The problems are:

      1. After NT8 is loaded, if I connect or disconnect the data feed, NT8 will reload the chart but NOT the persistent variables.

      2. After NT8 is loaded, if I open the UI dialog to change any input data, NT8 will reload the chart but NOT the persistent variables.

      3. If NT8 consistently loads the persistent variables, it would solve my problem.

      The values of these persistent variables are saved in the workspace by NT8. As shown on the Test6 indicator, pressing the “GetValue” button will retrieve the values from the workspace. For testing purpose, using the “Inc Var” button, would increase the variable values. Upon exiting, NT8 is saving these persistent variables correctly.

      As shown on the codes of Test6, I was able to retrieve the persistent variables by using the instrument name + indicator tag to locate the relevant data. However, if there are two chart tabs opened with the same instrument name and indicator, there will be two sets of similar data. Looking at the workspace file, the data location could be identified by the unique “IndicatorId”.

      As per your suggestion, the indicator persistent data could be saved in a file using StreamWriter. However, whether to retrieve the data from a separate file or the NT8 workspace, it is still necessary to have a unique indicator tag (such as IndicatorID) to identify the file or data location. As it is not possible to retrieve the IndicatorID from within an indicator, could please please suggest if there are other retrievable unique tags.

      Thanks again for your help.
      Attached Files

      Comment


        #4
        Hello Jim,
        Further to my request for other retrievable tags, please be informed that I was able to find a unique persistent ID that was saved in the workspace. NT8 saves the persistent variable data in the workspace, with data for each chart tab delimited by a unique persistent tab ID (see excerpt below from workspace for example):

        <Tab-b36b770ceceb460bb3e1a4cb226ac299>
        ... persistent data are stored here ...
        </Tab-b36b770ceceb460bb3e1a4cb226ac299>

        This persistent unique chart tab ID can retrieve from:
        this.ChartControl.ChartTab.PersistenceId

        This ID tag will allow me to locate and retrieve the relevant data for the persistent variables from the NT8 workspace.
        Thanks for your help again.

        Comment


          #5
          Hello kkc2015,

          I see what you are doing, but it is not a path I would recommend.

          The workspace file is intended to hold public variables that are used to construct the NinjaScript object and shouldn't be used to persist variables.

          The approach taken here would not be our supported approach, but I can note that using the [NinjaScriptProperty] attribute will make the property an input for the constructor and will make a different indicator instance.

          Our recommended approach for persisting data would be to have the script save some data to your own file, named by some input parameters of the script and the script's name, and to load that data based on that same "key."

          I hope this helps.
          JimNinjaTrader Customer Service

          Comment


            #6
            Jim,
            Thanks for the advice. After further consideration, I was able to save the initial NT8 loaded persistent variables [Browsable(false)] in a sealed singleton class using the ChartTab ID as a seeking key. Thus, when NT8 reloads subsequently without loading these persistent variables, the data can be retrieved at OnBarUpdate(). Fortunately NT8 does save these variables consistently in the workspace.
            Thanks again for your help.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by RaddiFX, Today, 10:15 AM
            2 responses
            13 views
            0 likes
            Last Post RaddiFX
            by RaddiFX
             
            Started by patrickmlee007, Today, 09:33 AM
            2 responses
            17 views
            0 likes
            Last Post patrickmlee007  
            Started by magnatauren, 08-15-2020, 02:12 PM
            5 responses
            206 views
            0 likes
            Last Post RaddiFX
            by RaddiFX
             
            Started by rene69851, 05-02-2024, 03:25 PM
            1 response
            22 views
            0 likes
            Last Post rene69851  
            Started by ETFVoyageur, Yesterday, 07:05 PM
            5 responses
            45 views
            0 likes
            Last Post ETFVoyageur  
            Working...
            X