Announcement

Collapse
No announcement yet.

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 -



    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?

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        332 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
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X