Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to manually re-run indicator

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

    How to manually re-run indicator


    Hi - I have an indicator that reads data from a text file and draws lines on a chart, using the values in the text file. The lines are static once the user adds them.

    The indicator works as intended when I first add it to a chart, but in order to load new data and draw new lines I have to remove the indicator and add it again.

    Currently, I have the drawing portion of the code (DrawLines) run when state = State.Realtime. I tried to also add the DrawLines function to the State.Configure, thinking that the user could open the parameters, re-enter the filename and then it would run again (I thought that state triggered when the user opened and closed the UI) - but this did not work.

    Initially, I had the DrawLines occurring in OnBarUpdate, and this caused the lines to be re-written over each other for each bar, generating thousands of line objects on top of each other.

    What I would like is for the indicator to run once when added and then update (re-read the input file and execute DrawLines) upon a user command - after they have updated the text file and they want to add the new lines.

    Thanks for any assistance.​

    #2
    Hello john_44573,

    The most simple way would be to jus press F5 to reload NinjaScript, that will reload the indicator.

    Alternatively you could add a custom button and then do your action when the button is pressed. There is a sample of making a button here: https://ninjatrader.com/support/help...collection.htm

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello john_44573,

      The most simple way would be to jus press F5 to reload NinjaScript, that will reload the indicator.

      Alternatively you could add a custom button and then do your action when the button is pressed. There is a sample of making a button here: https://ninjatrader.com/support/help...collection.htm
      Jesse - thank you for the response, I'll look at the manual button option and, yes, F5 is an option.

      The other question in my post was regarding the placement of my read file/drawing code, which is currently in the State.Realtime section. When I placed the code in State.Historical, it was occasionally re-executing and was trying to re-read the file, which is not desired behavior. I only want the file read when the lines are originally drawn. Is State.Realtime the correct state to achieve this?

      Comment


        #4
        Hello john_44573,

        Yes you can use State.Realtime for that purpose however that requires a live connection, you could also use DataLoaded.

        Comment


          #5
          Thanks for that additional information.

          I'm still learning NT scripting and states, etc. DataLoaded completes much earlier. Is there a specific reason you suggested this state instead of Historical or Transition?

          Comment


            #6
            Hello john_44573,

            State.DataLoaded is generally where you would put any one time action that the script may need for setup, that just happens after the data has loaded which the script will be using and is called one time when the script starts.

            Comment


              #7
              Ok, great, thank you. I'm going to move my execution to DataLoaded and test. I appreciate the replies.

              Comment


                #8
                Hi Jesse - I'm having a problem with using DataLoaded, I think this is why I changed to a later state as I was encountering this issue previously. There may be a better way to implement the feature which is causing this issue.
                Last edited by john_44573; 09-10-2024, 11:29 AM. Reason: Removed unnecessary details.

                Comment


                  #9
                  Hello john_44573,

                  If you are using OnStateChange for anything bar related that would not be the correct location for that code. Your data loading code should be run 1 time from DataLoaded and populate a variable with that data. Later from OnBarUpdate you could reference that data and do things like draw on the chart.

                  If you plan to use BarsAgo its best to just draw the items as they happen from OnBarUpdate, for example check the variable with the data if some datapoint exists for this bar, if so draw the object and place your text.

                  If you are instead using a loop and wanted to do that all at once you can just wait until the bars are processed by making a condition like the following in OnBarUpdate which processes on the last historical bar.

                  if(CurrentBar == Count - 2)
                  {

                  }

                  Comment


                    #10
                    Hi Jesse,

                    I'm editing the response here, since I finally was able to interpret your response above. I split the function into read file (in DataLoaded) and drawing (in OnBarUpdate).

                    For other new users:
                    • CurrentBar: This refers to the current bar number being processed, starting from 0 for the oldest bar to N-1 for the most recent bar, where N is the total number of bars on the chart.
                    • Count: This refers to the total number of bars on the chart, which is equal to the total number of historical bars plus any real-time bars that have formed. Count - 1 refers to the most recent bar, while Count - 2 refers to the second-to-last bar (the last historical bar or the bar just before the current, in-progress bar).
                    ​ Why does if (CurrentBar == Count - 2) work?
                    • When CurrentBar == Count - 2, you're effectively checking for the second-to-last bar on the chart, which is the last fully closed historical bar.
                    • By triggering the draw routine when CurrentBar reaches Count - 2, you're ensuring that all historical bars have been processed (including the current bar), and now you're drawing the rectangles based on fully loaded data.
                    Last edited by john_44573; 09-10-2024, 11:26 AM. Reason: The prior response was the answer

                    Comment


                      #11
                      Hello john_44573,

                      Only the code that reads the file should be in OnStateChange, you would end up with a variable that holds your data.

                      Any code that references bars like DrawingObjects would need to go in OnBarUpdate when bars are actually being processed.

                      Because you are using times instead of BarsAgo you can just loop over your data for a single bar in OnBarUpdate to process the data and draw the objects. That would be the if condition that I provided in my last post, that waits to process until the last historical bar.




                      Comment


                        #12
                        Removing unnecessary additional post.
                        Last edited by john_44573; 09-10-2024, 11:27 AM.

                        Comment


                          #13
                          Hello john_44573,

                          You can do an action one time by using a condition like the one I previously provided. If you wanted to an action only on new bars you could additionally use a condition checking if the state is realtime so only realtime bars cause the logic to be called.

                          Because you are using times you don't need to reference any bars, if you do need to reference specific bar data you could use the time with the GetBar method to get the number of bars ago.



                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          607 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          353 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          105 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          560 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          561 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X