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

How to call a function only one time ... ?

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

    How to call a function only one time ... ?

    How to call a function only one time when the indicator is setted in the chart but avoiding chart updates?

    I need a function will be called one time when the indicator is showed in the chart the first time.

    The functions of OnStateChange() are launched every F5 or update chart.


    #2
    Hello ninjo,

    Thanks for your post.

    That is correct, a custom NinjaScript indicator's OnStateChange() method would be fired whenever the indicator is added to the chart or the NinjaScript is reloaded.

    See this help guide page about Understanding the Lifecycle of your NinjaScript Objects: https://ninjatrader.com/support/help...fecycle_of.htm

    So we may accurately assist, could you please provide us with a brief description of what exactly you are trying to accomplish?

    We look forward to assisting further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      I want to call a function one time when the indicator is added to the chart the first time but not called in the chart update (F5)

      Comment


        #4
        Hello ninjo,

        Thanks for your notes.

        Hitting F5 to reload a NinjaScript will clone the script which makes it always a new instance.

        NinjaScript Lifecycle Help Guide: https://ninjatrader.com/support/help...fecycle_of.htm

        To accomplish your goal you could create a public bool, check if the bool is false to indicate the script is newly created, and set the bool to true in OnStateChange() when the State == State.Configure. You would then set the clones value to the parents value in the script within public override object Clone().

        Attached is an example script demonstrating this concept that you could view.

        When adding the indicator to the chart for the first time we check if the 'Alive' bool is false and print out "newly created" indicating the script was just created and we set the bool to true.

        Then, if you click F5 to reload NinjaScripts you will notice that the print does not occur again because the indicator was already created.

        Clone() Help Guide: https://ninjatrader.com/support/help.../nt8/clone.htm
        Attached Files
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Great BrandonH

          Exists a method to trigger when the indicator will be removed definitively from chart ?

          Thanks

          Comment


            #6
            Hello ninjo,

            Thanks for your notes.

            There are no supported/documented methods or properties to detect when the indicator is removed definitively from the chart.

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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, Today, 10:57 PM
            0 responses
            6 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            158 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Today, 09:29 PM
            0 responses
            7 views
            0 likes
            Last Post Belfortbucks  
            Started by zstheorist, Today, 07:52 PM
            0 responses
            7 views
            0 likes
            Last Post zstheorist  
            Started by pmachiraju, 11-01-2023, 04:46 AM
            8 responses
            151 views
            0 likes
            Last Post rehmans
            by rehmans
             
            Working...
            X