Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator with Multiple conditions

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

    Indicator with Multiple conditions

    Hello Ninjatrader Team,

    I want to create an indicator with 5 conditions if 3 or more are true than plot 1 else plot 0. This is what I have so far but It does not work. Please advise
    Code:
     protected override void OnBarUpdate()
            {
    			
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                            int Strat1 = 0;
    			int Strat2 = 0;
    			int Strat3 = 0;
    			int Strat4 = 0;
    			int Strat5 = 0;
    			
                // Condition set 1
                            if (Close[0] >= Open[0])
                {
                    Strat1 = 1;
                }
    			
    			if (Close[0] >= EMA(20)[0])
                {
                    Strat2 = 1;
                }
    			
    			if (Rising(RVI(21)))
    	    {
                    Strat3 = 1;
                }
    			
    			if (Close[0] >= EMA(25)[0])
    	    {
                    Strat4 = 1;
                }
    			
    			if (Stochastics(7, 14, 3).D[0] >= 20)
               {
                    Strat5 = 1;
                }
    			
    		
    			if ((Strat1 + Strat2 + Strat3 + Strat4 + Strat5>=3))
    				Plot0.Set(1);
                                         else
    	 		{
    				Plot0.Set(0);
    	              	}

    #2
    wallstreetking, you would need to have an else part in as well, that would reset the values if the condition's wouldn't hit, i.e. -

    Code:
    if (Close[0] >= Open[0])
           Strat1 = 1;
    else Strat1 = 0;
    			
    if (Close[0] >= EMA(20)[0])
           Strat2 = 1;
    else Strat2 = 0;
    			
    if (Rising(RVI(21)))
           Strat3 = 1;
    else Strat3 = 0;
    			
    if (Close[0] >= EMA(25)[0])
           Strat4 = 1;
    else Strat4 = 0;
    			
    if (Stochastics(7, 14, 3).D[0] >= 20)
           Strat5 = 1;
    else Strat5 = 0;
    You also want to declare your int's globally in class scope so they don't get set to 0 on each bar.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I am sort of baffled why this indicator only works on intraday data (ex 5 min) once i switch to daily bars nothing happens the indicator does not work. ?? Even if i switch conditions to 1 ex:
      Code:
      if ((Strat1 + Strat2 + Strat3 + Strat4 + Strat5>=1))
      				Plot0.Set(1);
                                           else
      				Plot0.Set(0);

      Comment


        #4
        What do you exactly mean by 'does not work? You don't get a plot at all or not the expected ones? Is there any error in the log tab?
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Yes, I get an Error.
          9/19/2014 4:20:31 AM Default Error on calling 'OnBarUpdate' method for indicator 'aa' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          Comment


            #6
            Thanks, that means you try accessing an invalid bar in your aa script - here's a check to include to make sure this could not happen - http://www.ninjatrader.com/support/f...ead.php?t=3170
            BertrandNinjaTrader Customer Service

            Comment


              #7
              flawless work NinjaTrader_Bertrand this works perfect now.
              I do have one simple question to quickly ask. If I would of written the code like this
              Code:
              if (Close[0] >= Open[0])
              [B][COLOR="Red"]{[/COLOR][/B]
                     Strat1 = 1;
              else Strat1 = 0;
              [B][COLOR="red"]}[/COLOR][/B]
              it gives me an error message
              ; expected
              Invalid expression term 'else'

              But If i write it like your example without { } the error message disappears.

              Why is that?

              Thank you

              Comment


                #8
                Glad to hear. Yes I would expect that issue, a one line assignment like mine would not need to be wrapped in { }.

                If you choose to work with the { } structure, then the else goes outside of it.
                BertrandNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Christopher_R, Today, 12:29 AM
                0 responses
                7 views
                0 likes
                Last Post Christopher_R  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                166 responses
                2,235 views
                0 likes
                Last Post sidlercom80  
                Started by thread, Yesterday, 11:58 PM
                0 responses
                3 views
                0 likes
                Last Post thread
                by thread
                 
                Started by jclose, Yesterday, 09:37 PM
                0 responses
                7 views
                0 likes
                Last Post jclose
                by jclose
                 
                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                10 responses
                1,415 views
                0 likes
                Last Post Traderontheroad  
                Working...
                X