• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Shared array in addon class is not reliably accessible

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

    Shared array in addon class is not reliably accessible

    Hello,

    I am hoping you can help me understand why this is not working reliably.

    I wish to share some values between different indicators and charts as efficiently as possible, so without having to install the indicators again, so, I created an addon class with a public array of doubles. Now my indicators can reference that public double array and access the values. It works, mostly.

    The issue is as follows.

    I have the indicators on my charts and all is working perfectly. I add an indicator to another chart, and dispute it working fine across multiple other charts, this one fails to work. It cannot access the values - They are all returned as zeros. As if a new instance of the class has been loaded, or the variable was undeclared/unset.

    I am accessing the array values as per the following example

    NinjaTrader.NinjaScript.AddOns.myIndicatorScoring. shared_iScore[0,2]

    I am both setting and reading the values in different indicators.

    The array is simply declared in the addon as follows

    public static double[,] shared_iScore = new double[20,20];

    Most of the time, this is fine. Multiple writers and multiple readers are all getting the values fine. Sometimes, maybe just on a single chart as per today, it does not work, but a few refreshes of the chart, or charts (including the one setting the values perhaps), and maybe it starts working!

    I'm a little confused as to why this might be. Could it be some sort of threading issue?

    Any insight would be appreciated.

    This has happened across multiple NT8 versions BTW - It's not a new thing. I am currently testing on latest released version. I suspect my method or understanding of how this should work in NT8 is flawed. Hoping you can steer me in the right direction.

    Thanks.

    #2
    Hello pjsmith,

    It looks like you are using static to share variables, while this is a concept of C# it is generally not suggested in NinjaScript for the reasons you mentioned. In most cases a static class/property would be best used for a constant value rather than sharing data. In NinjaScript this is more difficult to use static in a reliable way due to the timing of when your logic may get/set the value from each script. We generally suggest avoiding this type of design pattern to go with a more reliable solution.

    What is the purpose of the shared data in this context? Would it be possible to use the standard mechanisms for passing data such as one indicator calling another and supplying a series as input? Your scripts are not going to be directly in sync when sharing the value through a static property so there is always the chance one script will read the value before the other sets it etc. If the value is price data or is being used for signals, i really would not suggest this type of pattern.

    I can suggest to use Prints or Output.Process to further identify when each script is getting/setting the value, this could help highlight why you are seeing the discrepancy so that you can try to mitigate the problem.

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

    Comment


      #3
      Hi,

      Thanks for your response. I would not like to use the method of calling instances of the indicators, because it is very inefficient vs simply accessing the value I want which an instance of the indicator has readily available.

      In summary, and simplified. I have a set of indicators. Mostly, they output a score, mostly simply 1, 0, -1. Those indicators however can be fairly resource intensive as they analyse tick data. Another indicator(s) need to access those scores. I would not want to run multiple indicators on different charts, just to access the values they calculate. But, other indicators on other charts do need the values. Sync is not an issue at all for me as such. If I get the value now, on the next tick, or the next tick, it's fine. I do not need the values to be in sync with the tick data/bars.

      Prints I have tried. For instance. I added the indicator to a chart this AM, 2 charts in fact. 1 was printing the correct 1's and 0's etc., one just printed 0's. Same indicator, same settings, different charts. That was hence my original post. After refreshes of both charts and the indicators that were setting the values, both started working.

      I hope that adds some colour as to what I am trying to do. If you have a suggestion to make this work, or a suggestion as to how to better go about it, I would very much be interested to know. Thanks.

      Comment


        #4
        Hello pjsmith,

        From the added details I couldn't really say what may be happening, this will be something you will need to continue to debug to find a solution.

        I generally suggest to make the most simplistic example of the problem that you can, if you don't solve the problem in doing so it makes for a much more clear picture that we can view together. The property you provided really doesn't tell me much other than your using a static variable to share a value. If I can see a more complete example of what is being done, I may be able to provide other insight. From these details I can only assume one of the scripts is doing something incorrectly or at an invalid time. This could also relate to other factors such as did you complete a compile during that period of time? If so, its likely the addon was reset which may also reset the variable based on your logic.

        There are no examples or suggested patterns surrounding using static for this concept in NinjaScript so really whatever you make here will be custom for your use case. If syncing is not an issue, you can continue to try this approach however all I can really suggest would be to make a host script which adds the noted indicators and collects values from each as you normally would.

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

        Comment


          #5
          "did you complete a compile during that period of time? If so, its likely the addon was reset which may also reset the variable based on your logic. "

          You might have hit the nail on the head there. I quite possibly was compiling code once in a while. That would mess up the existing charts and prevent them communicating unless I refreshed them all? I did not think about this...

          Comment


            #6
            Hello pjsmith,

            If you are inheriting from AddOnBase with the class being used (default for any new addon class) then yes, it would run its OnStateChange and terminate/restart. If you are doing logic such as initializing a variable in an addon, thats likely it.

            I cant tell from the sample you provided to know if this applies or not, so this is mainly just a guess but also an observation of how addons OnStateChange works. If the purpose is simply to share data, you can delete the whole addon class and create your own namespace/class which does not inherit from anything:

            Code:
            namespace NinjaTrader.NinjaScript.AddOns
            {
                public static class SharedData {
                    public static double[,] shared_iScore = new double[20,20];
                }
            }



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

            Comment


              #7
              Sounds great! Thank you. I will experiment further....

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by BrandonT1292, 06-17-2019, 03:03 PM
              2 responses
              22 views
              0 likes
              Last Post thinarthrill  
              Started by cbentrikin, Yesterday, 07:46 PM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_Marco  
              Started by dr0832, 01-04-2019, 10:34 AM
              7 responses
              143 views
              0 likes
              Last Post Lancer
              by Lancer
               
              Started by RedDuke, Yesterday, 06:14 PM
              0 responses
              5 views
              0 likes
              Last Post RedDuke
              by RedDuke
               
              Started by DariusDeck, Yesterday, 04:22 PM
              1 response
              15 views
              0 likes
              Last Post NinjaTrader_Marco  
              Working...
              X