Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Running code only once

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

    Running code only once

    Hello, I created a basic indicator which draws fixed text on the chart. (Attached)

    The text is static and simply pulls the Instrument Name and allows the user to adjust some spacing/colors etc..

    My question is. Obviously I don't want the code to continually run on the close of every bar. But which is the best way to make it only run once.

    I have used an IF statement to contain the entire code and a simple variable to make sure it just runs once but is that the best way to do it?

    Code:
                if (
                    MyVar == 0
                    )
                {
    ​                // My Indicator code
    
                   MyVar =1;
                }
    I also tried to put the entire code inside State.Configured but the code contains a FOR loop and this didn't work inside there.
    Attached Files

    #2
    Hello several,

    For actions you want to trigger only once that doesn't reference bar data can be done in OnStateChange() when State is State.DataLoaded.

    Running a loop within State.DataLoaded is fine to do.

    If the code references bar data, this should be done in OnBarUpdate(). You can use a bool to trigger the action once.

    private bool actionTriggered;

    In OnBarUpdate():

    if (actionTriggered == false)
    {
    // trigger the actions
    actionTriggered = true;
    }
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    557 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X