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

Chart to Chart Communication

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

    Chart to Chart Communication

    can I allow several charts to run a script calling getUniqueID()

    Code:
    [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] NinjaTrader.Indicator[/COLOR]
     {
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]partial[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Indicator
     {
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] id = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];
     
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] getUniqueID()
     {
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] id++; 
     }
    [/SIZE][/FONT][/SIZE][/FONT]


    to receive a unique ID?

    are there potential issues I need to consider (racing, thread etc.)?


    Last edited by futurenets; 03-14-2015, 08:17 AM.

    #2
    Originally posted by futurenets View Post
    can I allow several charts to run a script calling getUniqueID()

    Code:
    [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] NinjaTrader.Indicator[/COLOR]
     {
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]partial[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Indicator
     {
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] id = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];
     
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] getUniqueID()
     {
     [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] id++; 
     }
    [/SIZE][/FONT][/SIZE][/FONT]


    to receive a unique ID?

    are there potential issues I need to consider (racing, thread etc.)?


    ints are passed by value, so that only works if your id variable is static, with all the caveats that being static entails. Further, you probably want to update the id before reporting it, so you might want to use "++id" instead of "id++".

    Comment


      #3
      would a Mutex help?

      Comment


        #4
        Originally posted by futurenets View Post
        would a Mutex help?
        Probably an unnecessary complication. You are only trying to set and retrieve an ID after all.

        Also, regardless, in c#, the only simple way for different classes to access the same unique variable is if the variable is declared static.
        Last edited by koganam; 03-14-2015, 07:01 PM.

        Comment


          #5
          ok thanks.

          I don't know much about the internals of Ninjatrader but does this fall into the category of shared resources accessed simultaneously by multiple threads?

          if so is there a recommended approach to handling this?
          Last edited by futurenets; 03-15-2015, 05:19 AM.

          Comment


            #6
            Originally posted by futurenets View Post
            ok thanks.

            I don't know much about the internals of Ninjatrader but does this fall into the category of shared resources accessed simultaneously by multiple threads?

            if so is there a recommended approach to handling this?
            It has nothing to do with NinjaTrader: it is c#. It is not about threading: it is about how the language works.

            You stay aware of the issues, and you use the constructs as you see fit. There are times when there is no choice but to use a static variable, so you use one, and stay aware of the caveats.

            Comment


              #7
              sorry I don't understand. are you saying that multiple threads don't exist in Ninjatrader?

              "It has nothing to do with NinjaTrader" ... what is "it"?

              if resources are shared between charts via "partial class" then surely those resources become critical and thread synchronization may be required regardless of the use of static - hence my post?
              Last edited by futurenets; 03-15-2015, 11:50 AM.

              Comment


                #8
                Originally posted by futurenets View Post
                sorry I don't understand. are you saying that multiple threads don't exist in Ninjatrader?
                I do not see where I said that. I said that the issue you are raising is not a question that is related to threading.
                "It has nothing to do with NinjaTrader" ... what is "it"?
                You asked a specific question: "I don't know much about the internals of Ninjatrader but does this fall into the category of shared resources accessed simultaneously by multiple threads?" The pronoun refers to that question, and the fact that what you are asking has nothing to do with the internals of NinjaTrader: it is a matter directly of the c# language and how it was designed to work.
                if resources are shared between charts via "partial class" then surely those resources become critical and thread synchronization may be required regardless of the use of static - hence my post?
                "Partial classes" have no implication about "criticality". "Partial classes" are merely a facility in the language that allows us to split a class into different files, a situation which is of particular use in creating libraries among other things. There is nothing critical in them, in and of themselves. They are more a convenience to the programmer, allowing fine isolation of related code, than anything else.

                I am not sure where you get the impression that: 'resources are shared between charts via "partial class"'. Charts are merely objects. Whether or not they can access other objects, and how, is a matter of how those other objects are coded. "Partial classes" do not make anything critical.

                Any partial classes that NinjaTrader uses internally as assemblies have no bearing on the matter, unless you are proposing to extend those base classes. Even then, that would depend on what you are writing.

                Comment


                  #9
                  ok thanks. All help appreciated.

                  this helped me from NinjaTrader Help:

                  Multi-Threading Consideration for NinjaScript

                  With the introduction of multi-threading in NinjaTrader when doing things like optimizations special considerations should be made when programming your NinjaScript indicators and strategies. Multi-threading basically allows NinjaTrader to take advantage of multi-core CPUs commonplace in modern computing to do multiple tasks at the same time. What this means for an optimization run is that multiple iterations can be tested by utilizing the various cores available to the computer.

                  Should you be using custom resources like text files, static members, etc. it is important to protect your resources from concurrent access. If NinjaTrader tried to use the resource at the same time you would run into errors similar to this one:

                  8/20/2010 12:14:29 PM|3|128|Error on calling 'OnBarUpdate' method for strategy 'SampleStrategy/1740b50bfe5d4bd896b0533725622400': The process cannot access the file 'c:\sample.txt' because it is being used by another process.
                  Last edited by futurenets; 03-16-2015, 03:45 AM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by EB Worx, 04-04-2023, 02:34 AM
                  7 responses
                  161 views
                  0 likes
                  Last Post VFI26
                  by VFI26
                   
                  Started by Mizzouman1, Today, 07:35 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by Radano, 06-10-2021, 01:40 AM
                  20 responses
                  616 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by i019945nj, 12-14-2023, 06:41 AM
                  6 responses
                  68 views
                  0 likes
                  Last Post i019945nj  
                  Started by aa731, Today, 02:54 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Working...
                  X