Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Iterate variable for in OnBarUpdate

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

    Iterate variable for in OnBarUpdate

    Hello, I'm very new to ninjatrader and C#. Learned to code in MATLAB.

    With my code, I'm trying to identify and draw a cypher pattern.

    Here are the pieces of my code's structure that are relevant to my question,
    "
    OnStateChange()
    {
    int PatternID = 0;
    }

    OnBarUpdate()
    {
    if (*potential cypher conditions met*) //use new bar to identify potential new patterns
    {
    PatternID = PatternID+1;
    *Draw pattern with tag = "potentialcypher"+PatternID*
    }

    for (int i=1;PatternID;++i) //iterate through old potential patterns to update completion status
    {
    if (*cypher pattern broken*)
    {
    *remove drawings with tag = "potentialcypher"+i*
    }
    }
    }
    "

    I identify potential cypher patterns as they get close to completion. I want to remove old patterns that never complete; this is why I have the for loop to iterate through old patterns.

    I'm getting an error in the line with "PatternID = PatternID+1;" saying that PatternID does not exist in current context. Any idea why? Do I have to re-declare the integer in OnBarUpdate?

    Thank you!

    #2
    Hello jrconsole,

    Thanks for your post and welcome to the NinjaTrader forums.

    When you declare a "local" variable like:

    OnStateChange()
    {
    int PatternID = 0;
    }

    it is only valid with the confines of the { }.

    It would be better to delcare the variable at the class level so you can easily use it in all methods. For example:

    public class atr1 : Indicator
    {
    private int PatternID = 0; // declares PatternID as an integer type and assigns the default value to 0.
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    648 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    369 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    108 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    572 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X