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

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)
    {
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Perr0Grande, Today, 08:16 PM
    0 responses
    2 views
    0 likes
    Last Post Perr0Grande  
    Started by elderan, Today, 08:03 PM
    0 responses
    5 views
    0 likes
    Last Post elderan
    by elderan
     
    Started by algospoke, Today, 06:40 PM
    0 responses
    10 views
    0 likes
    Last Post algospoke  
    Started by maybeimnotrader, Today, 05:46 PM
    0 responses
    12 views
    0 likes
    Last Post maybeimnotrader  
    Started by quantismo, Today, 05:13 PM
    0 responses
    7 views
    0 likes
    Last Post quantismo  
    Working...
    X