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

Bars Since condition

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

    Bars Since condition

    I used the indicator wizard and inserted the code below to calculate the bars since CCI>100 but I get error messages indicating I should have semi colons ";" where I have "-" minus signs to subtaract one value from another. The editor also flagged the leading left bracket after "If". Can you suggest how I can correct this and get the code to compile?
    protectedoverridevoid OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    {
    double BarsSince = 0;
    double TriggerBar = 0;
    }
    If ((WoodiesCCI(14,6) > 100) = true & BarsSince =0);
    { TriggerBar = CurrentBar 1;
    BarsSince = CurrentBar – TriggerBar;
    }
    Else If (WoodiesCCI(14,6) > 100 = true & BarsSince != 0);
    {
    BarsSince = CurrentBar – TriggerBar;
    }
    Else If (WoodiesCCI(14,6) > 100 != true);
    {
    BarsSince = 0;
    TriggerBar = 0;
    }
    Plot0.Set(BarsSince [0]);

    #2
    BurtOD, please give this snippet a try -

    Code:
     
    int BarsSince = 0;
    int TriggerBar = 0;
    
    if (CCI(14)[0] > 100 && BarsSince == 0)
    { 
    TriggerBar = CurrentBar - 1;
    BarsSince = CurrentBar - TriggerBar;
    }
    
    else if (CCI(14)[0] > 100 && BarsSince != 0)
    BarsSince = CurrentBar - TriggerBar;
    
    else if (CCI(14)[0] > 100 != true)
    {
    BarsSince = 0;
    TriggerBar = 0;
    }
    In addition I would suggest you review those tutorials for getting more into the basic NinjaScript syntax and notation -



    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bars Since

      The code compiles now and works fine but something is wrong with my design/logic. The term TriggerBar, which is supposed to stay constant after meeting the condition, continues to update so I get a constant value for BarsSince instead of an increasing value. How do I get the TriggerBar to stay constant once the condition is met until the condition changes?
      if (CCI(14)[0] > 100 && BarsSince == 0)

      Comment


        #4
        BurtOD, if your condition is still true, the 'trigger bar' would still update of course. Have you tried a CrossOver one instead of checking for the CCI being greater than 100?
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alifarahani, Today, 09:40 AM
        4 responses
        20 views
        0 likes
        Last Post alifarahani  
        Started by gentlebenthebear, Today, 01:30 AM
        3 responses
        16 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by PhillT, Today, 02:16 PM
        2 responses
        7 views
        0 likes
        Last Post PhillT
        by PhillT
         
        Started by Kaledus, Today, 01:29 PM
        3 responses
        11 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by frankthearm, Yesterday, 09:08 AM
        14 responses
        47 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Working...
        X