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 cmoran13, 04-16-2026, 01:02 PM
        0 responses
        43 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        25 views
        0 likes
        Last Post PaulMohn  
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        162 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        98 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        158 views
        2 likes
        Last Post CaptainJack  
        Working...
        X