Announcement

Collapse
No announcement yet.

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.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    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
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        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.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            42 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            25 views
            0 likes
            Last Post PaulMohn  
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            162 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            98 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            157 views
            2 likes
            Last Post CaptainJack  
            Working...
            X