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

Accessing List from State.Transition / State.Realtime

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

    Accessing List from State.Transition / State.Realtime

    Hello,

    During the State.Historical I'm collecting data and storing it in a C# List.

    I want to access and process this list in either OnStateChange State.Transion or State.Realtime

    But the list apparently is not available there...perhaps it is a different thread?

    I can see this because List.Count returns 0 in OnStateChange, even though it is not empty (has been filled by State.Historical OnBarUpdate)

    As soon as the first State.Realtime OnBarUpdate is entered, THEN the List.Count is correct.

    Seems to be a thread issue?


    Thanks


    #2
    Hello llanqui,

    Thanks for your post.

    We are looking into this matter regarding NinjaScript states and threads to provide you with the best information possible. Once we are finished investigating this matter we will update this forum thread.

    Note that using C# Lists in a custom NinjaScript goes outside the scope of support we would be able to provide you with in the Support Department at NinjaTrader.

    This forum thread will be open for other community members to share their insights on the topic.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Ok, can you tell me how to invoke the thread that OnBarUpdate operates in?

      Comment


        #4
        Hello llanqui,

        Thanks for your notes.

        After researching this matter further, I am seeing that a List Count can be printed in OnStateChange() when the State == State.Transition or when the State == State.Realtime.

        For example, in the attached example script 3 items were added to the list and when we print List.Count in State.Transition and State.Realtime in OnStateChange(), I see the count of 3 printing to the NinjaScript Output window.

        Attached is the example script used to test this.

        As for your OnBarUpdate question, there are no means to programmatically force OnBarUpdate to fire. OnBarUpdate will trigger based on the Calculate mode used. When Calculate.OnBarClose is used, OnBarUpdate will trigger at the close of each bar. When Calculate.OnPriceChange is used, OnBarUpdate will trigger for each price change. When Calculate.OnEachTick is used, OnBarUpdate will trigger for each incoming tick.

        Calculate: https://ninjatrader.com/support/help.../calculate.htm
        Attached Files
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          ok, thanks, will check this against my code....

          perhaps there is a delay somewhere....I"m loading the list inside OnBarUpdate during State=Historical

          Comment


            #6
            just a thought....in your example, if OnStateChange is a different thread than OnBarUpdate then your example would still work...the Print statements are in the same thread

            but I'll check it out...thanks

            Comment


              #7
              I don't want to trigger OnBarUpdate...just want to execute a method in the same thread...perhaps with Invoke Asynch

              Comment


                #8
                Ok, did a little more testing. The List.Count can be printed in State.Transition....if the List is being populated by the same Indicator.


                In my case the List is (global / public) and is being populated by another indicator on another chart in OnBarUpdate in State.Historical.

                When I try to access that global/public List in State.Transition on the second indicator it shows it is empty.

                Possibly this is because the two charts are operating asynchrously during the States prior to Realtime???

                And that the (global) list is actually empty (when accessed by the second chart) because the other (first) Chart/Indicator has not finished processing the OnBarUpdate State.Historical?

                Probably the Ninja architecture operates this way? Each chart is asynch until they all reach State.Realtime??? I would expect then that all the charts would need to be synched to the realtime tick data...??

                Comment


                  #9
                  Hello llanqui,

                  Thanks for your notes.

                  Two charts have no association so your indicator won't process at the same time. Using a variable from one chart to another wouldn't be a good approach in this case.

                  We recommend you use a StreamWriter in indicator A to write data to a file.

                  A StreamReader could be used in indicator B to read that data from the file and then that data could be used later in the script.

                  Note that you may have to reload the second indicator depending on when each chart loads. If you load a workspace and all charts load at once then it would be determined by whichever indicator loaded first. If the indicator that writes data takes some time to write the data then the second indicator would fail and have to be f5 reloaded once the other indicator is done.

                  Could you clarify exactly what your end goal is so we may accurately assist?

                  You could consider calling the indicator A that you want data from in indicator B's code instead of trying to share a list.

                  For example, if the indicator that makes the list is on a 5-minute series but your other chart is a 10 minute series the solution would be to use a secondary series and just call the indicator that makes the list in code so it populates the list based on the secondary series, then the primary indicator can just access that list with its populated data.

                  See the help guide documentation below for more information and sample code.

                  StreamWriter: https://ninjatrader.com/support/help...o_write_to.htm
                  StreamReader: https://ninjatrader.com/support/help...o_read_fro.htm
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok, this is the problem....I have some solutions in mind...but there remains this question....

                    it seems that all charts should enter OnBarUpdate State.Realtime perhaps only milliseconds apart....and that from then on they will be closely, if not exactly, each synchronized to the incoming data stream...can you say this is a valid assumption?


                    Comment


                      #11
                      another (related question)..... when does the State Change to Realtime.....at the moment the real time data stream starts feeding, or.... after the first OnBarUpdate Realtime Calculate.OnBarClose?

                      Comment


                        #12
                        Hello llanqui,

                        Thanks for your notes.

                        The charts would open at similar times depending on historical data request and chart settings. The indicators will not process at the same time, those all process individually so there is no expectation of two charts indicators being in sync. Each indicator gets loaded and each indicator takes a different amount of time to process and enter realtime.

                        While we cannot assist with trying to make scripts communicate between two charts we can try to provide a solution if you can give a clear description of what goal you have.​
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          I found a solution, appears to be working well..instead of accessing this data in State.Transition, I will set OnBarUpdate to Calculate.OnPriceChange and sense when the List data is available

                          Then I don't have to wait for a Bar Close event to get the data

                          In a way it is a Server/Client model

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by fx.practic, 10-15-2013, 12:53 AM
                          5 responses
                          5,404 views
                          0 likes
                          Last Post Bidder
                          by Bidder
                           
                          Started by Shai Samuel, 07-02-2022, 02:46 PM
                          4 responses
                          95 views
                          0 likes
                          Last Post Bidder
                          by Bidder
                           
                          Started by DJ888, Yesterday, 10:57 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by MacDad, 02-25-2024, 11:48 PM
                          7 responses
                          159 views
                          0 likes
                          Last Post loganjarosz123  
                          Started by Belfortbucks, Yesterday, 09:29 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post Belfortbucks  
                          Working...
                          X